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 */ 10286898fa6STejun Heo WORK_CANCEL_DISABLE = 1 << 1, /* canceling to disable */ 103c5f5b942STejun Heo }; 104c5f5b942STejun Heo 105e563d0a7STejun Heo enum wq_internal_consts { 106e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 1074ce62e9eSTejun Heo 10829c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ 109c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 110db7bccf4STejun Heo 111e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 112e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 113e22bee78STejun Heo 1143233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 1153233cdbdSTejun Heo /* call for help after 10ms 1163233cdbdSTejun Heo (min two ticks) */ 117e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 118e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds /* 121e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 1228698a745SDongsheng Yang * all cpus. Give MIN_NICE. 123e22bee78STejun Heo */ 1248698a745SDongsheng Yang RESCUER_NICE_LEVEL = MIN_NICE, 1258698a745SDongsheng Yang HIGHPRI_NICE_LEVEL = MIN_NICE, 126ecf6881fSTejun Heo 12731c89007SAudra Mitchell WQ_NAME_LEN = 32, 128c8e55f36STejun Heo }; 129c8e55f36STejun Heo 1301da177e4SLinus Torvalds /* 1314cb1ef64STejun Heo * We don't want to trap softirq for too long. See MAX_SOFTIRQ_TIME and 1324cb1ef64STejun Heo * MAX_SOFTIRQ_RESTART in kernel/softirq.c. These are macros because 1334cb1ef64STejun Heo * msecs_to_jiffies() can't be an initializer. 1344cb1ef64STejun Heo */ 1354cb1ef64STejun Heo #define BH_WORKER_JIFFIES msecs_to_jiffies(2) 1364cb1ef64STejun Heo #define BH_WORKER_RESTARTS 10 1374cb1ef64STejun Heo 1384cb1ef64STejun Heo /* 1394690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1404690c4abSTejun Heo * 141e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 142e41e704bSTejun Heo * everyone else. 1434690c4abSTejun Heo * 144e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 145e22bee78STejun Heo * only be modified and accessed from the local cpu. 146e22bee78STejun Heo * 147d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1484690c4abSTejun Heo * 1495797b1c1STejun Heo * LN: pool->lock and wq_node_nr_active->lock protected for writes. Either for 1505797b1c1STejun Heo * reads. 1515797b1c1STejun Heo * 152bdf8b9bfSTejun Heo * K: Only modified by worker while holding pool->lock. Can be safely read by 153bdf8b9bfSTejun Heo * self, while holding pool->lock or from IRQ context if %current is the 154bdf8b9bfSTejun Heo * kworker. 155bdf8b9bfSTejun Heo * 156bdf8b9bfSTejun Heo * S: Only modified by worker self. 157bdf8b9bfSTejun Heo * 1581258fae7STejun Heo * A: wq_pool_attach_mutex protected. 159822d8405STejun Heo * 16068e13a67SLai Jiangshan * PL: wq_pool_mutex protected. 16176af4d93STejun Heo * 16224acfb71SThomas Gleixner * PR: wq_pool_mutex protected for writes. RCU protected for reads. 1635bcab335STejun Heo * 1645b95e1afSLai Jiangshan * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads. 1655b95e1afSLai Jiangshan * 1665b95e1afSLai Jiangshan * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or 16724acfb71SThomas Gleixner * RCU for reads. 1685b95e1afSLai Jiangshan * 1693c25a55dSLai Jiangshan * WQ: wq->mutex protected. 1703c25a55dSLai Jiangshan * 17124acfb71SThomas Gleixner * WR: wq->mutex protected for writes. RCU protected for reads. 1722e109a28STejun Heo * 173a045a272STejun Heo * WO: wq->mutex protected for writes. Updated with WRITE_ONCE() and can be read 174a045a272STejun Heo * with READ_ONCE() without locking. 175a045a272STejun Heo * 1762e109a28STejun Heo * MD: wq_mayday_lock protected. 177cd2440d6SPetr Mladek * 178cd2440d6SPetr Mladek * WD: Used internally by the watchdog. 1794690c4abSTejun Heo */ 1804690c4abSTejun Heo 1812eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 182c34056a3STejun Heo 183bd7bdd43STejun Heo struct worker_pool { 184a9b8a985SSebastian Andrzej Siewior raw_spinlock_t lock; /* the pool lock */ 185d84ff051STejun Heo int cpu; /* I: the associated cpu */ 186f3f90ad4STejun Heo int node; /* I: the associated node ID */ 1879daf9e67STejun Heo int id; /* I: pool ID */ 188bc8b50c2STejun Heo unsigned int flags; /* L: flags */ 189bd7bdd43STejun Heo 19082607adcSTejun Heo unsigned long watchdog_ts; /* L: watchdog timestamp */ 191cd2440d6SPetr Mladek bool cpu_stall; /* WD: stalled cpu bound pool */ 19282607adcSTejun Heo 193bc35f7efSLai Jiangshan /* 194bc35f7efSLai Jiangshan * The counter is incremented in a process context on the associated CPU 195bc35f7efSLai Jiangshan * w/ preemption disabled, and decremented or reset in the same context 196bc35f7efSLai Jiangshan * but w/ pool->lock held. The readers grab pool->lock and are 197bc35f7efSLai Jiangshan * guaranteed to see if the counter reached zero. 198bc35f7efSLai Jiangshan */ 199bc35f7efSLai Jiangshan int nr_running; 20084f91c62SLai Jiangshan 201bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 202ea1abd61SLai Jiangshan 2035826cc8fSLai Jiangshan int nr_workers; /* L: total number of workers */ 2045826cc8fSLai Jiangshan int nr_idle; /* L: currently idle workers */ 205bd7bdd43STejun Heo 2062c1f1a91SLai Jiangshan struct list_head idle_list; /* L: list of idle workers */ 207bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 2083f959aa3SValentin Schneider struct work_struct idle_cull_work; /* L: worker idle cleanup */ 2093f959aa3SValentin Schneider 210bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 211bd7bdd43STejun Heo 212c5aa87bbSTejun Heo /* a workers is either on busy_hash or idle_list, or the manager */ 213c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 214c9e7cf27STejun Heo /* L: hash of busy workers */ 215c9e7cf27STejun Heo 2162607d7a6STejun Heo struct worker *manager; /* L: purely informational */ 21792f9c5c4SLai Jiangshan struct list_head workers; /* A: attached workers */ 218e02b9312SValentin Schneider struct list_head dying_workers; /* A: workers about to die */ 21960f5a4bcSLai Jiangshan struct completion *detach_completion; /* all workers detached */ 220e19e397aSTejun Heo 2217cda9aaeSLai Jiangshan struct ida worker_ida; /* worker IDs for task name */ 222e19e397aSTejun Heo 2237a4e344cSTejun Heo struct workqueue_attrs *attrs; /* I: worker attributes */ 22468e13a67SLai Jiangshan struct hlist_node hash_node; /* PL: unbound_pool_hash node */ 22568e13a67SLai Jiangshan int refcnt; /* PL: refcnt for unbound pools */ 2267a4e344cSTejun Heo 227e19e397aSTejun Heo /* 22824acfb71SThomas Gleixner * Destruction of pool is RCU protected to allow dereferences 22929c91e99STejun Heo * from get_work_pool(). 23029c91e99STejun Heo */ 23129c91e99STejun Heo struct rcu_head rcu; 23284f91c62SLai Jiangshan }; 2338b03ae3cSTejun Heo 2348b03ae3cSTejun Heo /* 235725e8ec5STejun Heo * Per-pool_workqueue statistics. These can be monitored using 236725e8ec5STejun Heo * tools/workqueue/wq_monitor.py. 237725e8ec5STejun Heo */ 238725e8ec5STejun Heo enum pool_workqueue_stats { 239725e8ec5STejun Heo PWQ_STAT_STARTED, /* work items started execution */ 240725e8ec5STejun Heo PWQ_STAT_COMPLETED, /* work items completed execution */ 2418a1dd1e5STejun Heo PWQ_STAT_CPU_TIME, /* total CPU time consumed */ 242616db877STejun Heo PWQ_STAT_CPU_INTENSIVE, /* wq_cpu_intensive_thresh_us violations */ 243725e8ec5STejun Heo PWQ_STAT_CM_WAKEUP, /* concurrency-management worker wakeups */ 2448639ecebSTejun Heo PWQ_STAT_REPATRIATED, /* unbound workers brought back into scope */ 245725e8ec5STejun Heo PWQ_STAT_MAYDAY, /* maydays to rescuer */ 246725e8ec5STejun Heo PWQ_STAT_RESCUED, /* linked work items executed by rescuer */ 247725e8ec5STejun Heo 248725e8ec5STejun Heo PWQ_NR_STATS, 249725e8ec5STejun Heo }; 250725e8ec5STejun Heo 251725e8ec5STejun Heo /* 252e9a8e01fSTejun Heo * The per-pool workqueue. While queued, bits below WORK_PWQ_SHIFT 253112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits 254112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the 255112202d9STejun Heo * number of flag bits. 2561da177e4SLinus Torvalds */ 257112202d9STejun Heo struct pool_workqueue { 258bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 2594690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 26073f53c4aSTejun Heo int work_color; /* L: current color */ 26173f53c4aSTejun Heo int flush_color; /* L: flushing color */ 2628864b4e5STejun Heo int refcnt; /* L: reference count */ 26373f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 26473f53c4aSTejun Heo /* L: nr of in_flight works */ 2654c065dbcSWaiman Long bool plugged; /* L: execution suspended */ 266018f3a13SLai Jiangshan 267018f3a13SLai Jiangshan /* 268018f3a13SLai Jiangshan * nr_active management and WORK_STRUCT_INACTIVE: 269018f3a13SLai Jiangshan * 270018f3a13SLai Jiangshan * When pwq->nr_active >= max_active, new work item is queued to 271018f3a13SLai Jiangshan * pwq->inactive_works instead of pool->worklist and marked with 272018f3a13SLai Jiangshan * WORK_STRUCT_INACTIVE. 273018f3a13SLai Jiangshan * 2745797b1c1STejun Heo * All work items marked with WORK_STRUCT_INACTIVE do not participate in 2755797b1c1STejun Heo * nr_active and all work items in pwq->inactive_works are marked with 2765797b1c1STejun Heo * WORK_STRUCT_INACTIVE. But not all WORK_STRUCT_INACTIVE work items are 2775797b1c1STejun Heo * in pwq->inactive_works. Some of them are ready to run in 2785797b1c1STejun Heo * pool->worklist or worker->scheduled. Those work itmes are only struct 2795797b1c1STejun Heo * wq_barrier which is used for flush_work() and should not participate 2805797b1c1STejun Heo * in nr_active. For non-barrier work item, it is marked with 2815797b1c1STejun Heo * WORK_STRUCT_INACTIVE iff it is in pwq->inactive_works. 282018f3a13SLai Jiangshan */ 2831e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 284f97a4a1aSLai Jiangshan struct list_head inactive_works; /* L: inactive works */ 2855797b1c1STejun Heo struct list_head pending_node; /* LN: node on wq_node_nr_active->pending_pwqs */ 2863c25a55dSLai Jiangshan struct list_head pwqs_node; /* WR: node on wq->pwqs */ 2872e109a28STejun Heo struct list_head mayday_node; /* MD: node on wq->maydays */ 2888864b4e5STejun Heo 289725e8ec5STejun Heo u64 stats[PWQ_NR_STATS]; 290725e8ec5STejun Heo 2918864b4e5STejun Heo /* 292967b494eSTejun Heo * Release of unbound pwq is punted to a kthread_worker. See put_pwq() 293687a9aa5STejun Heo * and pwq_release_workfn() for details. pool_workqueue itself is also 294687a9aa5STejun Heo * RCU protected so that the first pwq can be determined without 295967b494eSTejun Heo * grabbing wq->mutex. 2968864b4e5STejun Heo */ 297687a9aa5STejun Heo struct kthread_work release_work; 2988864b4e5STejun Heo struct rcu_head rcu; 299e9a8e01fSTejun Heo } __aligned(1 << WORK_STRUCT_PWQ_SHIFT); 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds /* 30273f53c4aSTejun Heo * Structure used to wait for workqueue flush. 30373f53c4aSTejun Heo */ 30473f53c4aSTejun Heo struct wq_flusher { 3053c25a55dSLai Jiangshan struct list_head list; /* WQ: list of flushers */ 3063c25a55dSLai Jiangshan int flush_color; /* WQ: flush color waiting for */ 30773f53c4aSTejun Heo struct completion done; /* flush completion */ 30873f53c4aSTejun Heo }; 3091da177e4SLinus Torvalds 310226223abSTejun Heo struct wq_device; 311226223abSTejun Heo 31273f53c4aSTejun Heo /* 31391ccc6e7STejun Heo * Unlike in a per-cpu workqueue where max_active limits its concurrency level 31491ccc6e7STejun Heo * on each CPU, in an unbound workqueue, max_active applies to the whole system. 31591ccc6e7STejun Heo * As sharing a single nr_active across multiple sockets can be very expensive, 31691ccc6e7STejun Heo * the counting and enforcement is per NUMA node. 3175797b1c1STejun Heo * 3185797b1c1STejun Heo * The following struct is used to enforce per-node max_active. When a pwq wants 3195797b1c1STejun Heo * to start executing a work item, it should increment ->nr using 3205797b1c1STejun Heo * tryinc_node_nr_active(). If acquisition fails due to ->nr already being over 3215797b1c1STejun Heo * ->max, the pwq is queued on ->pending_pwqs. As in-flight work items finish 3225797b1c1STejun Heo * and decrement ->nr, node_activate_pending_pwq() activates the pending pwqs in 3235797b1c1STejun Heo * round-robin order. 32491ccc6e7STejun Heo */ 32591ccc6e7STejun Heo struct wq_node_nr_active { 3265797b1c1STejun Heo int max; /* per-node max_active */ 3275797b1c1STejun Heo atomic_t nr; /* per-node nr_active */ 3285797b1c1STejun Heo raw_spinlock_t lock; /* nests inside pool locks */ 3295797b1c1STejun Heo struct list_head pending_pwqs; /* LN: pwqs with inactive works */ 33091ccc6e7STejun Heo }; 33191ccc6e7STejun Heo 33291ccc6e7STejun Heo /* 333c5aa87bbSTejun Heo * The externally visible workqueue. It relays the issued work items to 334c5aa87bbSTejun Heo * the appropriate worker_pool through its pool_workqueues. 3351da177e4SLinus Torvalds */ 3361da177e4SLinus Torvalds struct workqueue_struct { 3373c25a55dSLai Jiangshan struct list_head pwqs; /* WR: all pwqs of this wq */ 338e2dca7adSTejun Heo struct list_head list; /* PR: list of all workqueues */ 33973f53c4aSTejun Heo 3403c25a55dSLai Jiangshan struct mutex mutex; /* protects this wq */ 3413c25a55dSLai Jiangshan int work_color; /* WQ: current work color */ 3423c25a55dSLai Jiangshan int flush_color; /* WQ: current flush color */ 343112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 3443c25a55dSLai Jiangshan struct wq_flusher *first_flusher; /* WQ: first flusher */ 3453c25a55dSLai Jiangshan struct list_head flusher_queue; /* WQ: flush waiters */ 3463c25a55dSLai Jiangshan struct list_head flusher_overflow; /* WQ: flush overflow list */ 34773f53c4aSTejun Heo 3482e109a28STejun Heo struct list_head maydays; /* MD: pwqs requesting rescue */ 34930ae2fc0STejun Heo struct worker *rescuer; /* MD: rescue worker */ 350e22bee78STejun Heo 35187fc741eSLai Jiangshan int nr_drainers; /* WQ: drain in progress */ 3525797b1c1STejun Heo 3535797b1c1STejun Heo /* See alloc_workqueue() function comment for info on min/max_active */ 354a045a272STejun Heo int max_active; /* WO: max active works */ 3555797b1c1STejun Heo int min_active; /* WO: min active works */ 356a045a272STejun Heo int saved_max_active; /* WQ: saved max_active */ 3575797b1c1STejun Heo int saved_min_active; /* WQ: saved min_active */ 358226223abSTejun Heo 3595b95e1afSLai Jiangshan struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */ 3609f66cff2STejun Heo struct pool_workqueue __rcu *dfl_pwq; /* PW: only for unbound wqs */ 3616029a918STejun Heo 362226223abSTejun Heo #ifdef CONFIG_SYSFS 363226223abSTejun Heo struct wq_device *wq_dev; /* I: for sysfs interface */ 364226223abSTejun Heo #endif 3654e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 366669de8bdSBart Van Assche char *lock_name; 367669de8bdSBart Van Assche struct lock_class_key key; 3684e6045f1SJohannes Berg struct lockdep_map lockdep_map; 3694e6045f1SJohannes Berg #endif 370ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */ 3712728fd2fSTejun Heo 372e2dca7adSTejun Heo /* 37324acfb71SThomas Gleixner * Destruction of workqueue_struct is RCU protected to allow walking 37424acfb71SThomas Gleixner * the workqueues list without grabbing wq_pool_mutex. 375e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq. 376e2dca7adSTejun Heo */ 377e2dca7adSTejun Heo struct rcu_head rcu; 378e2dca7adSTejun Heo 3792728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */ 3802728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ 381636b927eSTejun Heo struct pool_workqueue __percpu __rcu **cpu_pwq; /* I: per-cpu pwqs */ 38291ccc6e7STejun Heo struct wq_node_nr_active *node_nr_active[]; /* I: per-node nr_active */ 3831da177e4SLinus Torvalds }; 3841da177e4SLinus Torvalds 38584193c07STejun Heo /* 38684193c07STejun Heo * Each pod type describes how CPUs should be grouped for unbound workqueues. 38784193c07STejun Heo * See the comment above workqueue_attrs->affn_scope. 38884193c07STejun Heo */ 38984193c07STejun Heo struct wq_pod_type { 39084193c07STejun Heo int nr_pods; /* number of pods */ 39184193c07STejun Heo cpumask_var_t *pod_cpus; /* pod -> cpus */ 39284193c07STejun Heo int *pod_node; /* pod -> node */ 39384193c07STejun Heo int *cpu_pod; /* cpu -> pod */ 39484193c07STejun Heo }; 39584193c07STejun Heo 3961211f3b2STejun Heo struct work_offq_data { 3971211f3b2STejun Heo u32 pool_id; 39886898fa6STejun Heo u32 disable; 3991211f3b2STejun Heo u32 flags; 4001211f3b2STejun Heo }; 4011211f3b2STejun Heo 40263c5484eSTejun Heo static const char *wq_affn_names[WQ_AFFN_NR_TYPES] = { 403523a301eSTejun Heo [WQ_AFFN_DFL] = "default", 40463c5484eSTejun Heo [WQ_AFFN_CPU] = "cpu", 40563c5484eSTejun Heo [WQ_AFFN_SMT] = "smt", 40663c5484eSTejun Heo [WQ_AFFN_CACHE] = "cache", 40763c5484eSTejun Heo [WQ_AFFN_NUMA] = "numa", 40863c5484eSTejun Heo [WQ_AFFN_SYSTEM] = "system", 40963c5484eSTejun Heo }; 410bce90380STejun Heo 411616db877STejun Heo /* 412616db877STejun Heo * Per-cpu work items which run for longer than the following threshold are 413616db877STejun Heo * automatically considered CPU intensive and excluded from concurrency 414616db877STejun Heo * management to prevent them from noticeably delaying other per-cpu work items. 415aa6fde93STejun Heo * ULONG_MAX indicates that the user hasn't overridden it with a boot parameter. 416aa6fde93STejun Heo * The actual value is initialized in wq_cpu_intensive_thresh_init(). 417616db877STejun Heo */ 418aa6fde93STejun Heo static unsigned long wq_cpu_intensive_thresh_us = ULONG_MAX; 419616db877STejun Heo module_param_named(cpu_intensive_thresh_us, wq_cpu_intensive_thresh_us, ulong, 0644); 420ccdec921SXuewen Yan #ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT 421ccdec921SXuewen Yan static unsigned int wq_cpu_intensive_warning_thresh = 4; 422ccdec921SXuewen Yan module_param_named(cpu_intensive_warning_thresh, wq_cpu_intensive_warning_thresh, uint, 0644); 423ccdec921SXuewen Yan #endif 424616db877STejun Heo 425cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 426552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 427cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 428cee22a15SViresh Kumar 429863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */ 430c7a40c49STejun Heo static bool wq_topo_initialized __read_mostly = false; 431c7a40c49STejun Heo 432c7a40c49STejun Heo static struct kmem_cache *pwq_cache; 433c7a40c49STejun Heo 434c7a40c49STejun Heo static struct wq_pod_type wq_pod_types[WQ_AFFN_NR_TYPES]; 435c7a40c49STejun Heo static enum wq_affn_scope wq_affn_dfl = WQ_AFFN_CACHE; 4363347fa09STejun Heo 437fef59c9cSTejun Heo /* buf for wq_update_unbound_pod_attrs(), protected by CPU hotplug exclusion */ 438fef59c9cSTejun Heo static struct workqueue_attrs *wq_update_pod_attrs_buf; 4394c16bd32STejun Heo 44068e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 4411258fae7STejun Heo static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ 442a9b8a985SSebastian Andrzej Siewior static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 443d8bb65abSSebastian Andrzej Siewior /* wait for manager to go away */ 444d8bb65abSSebastian Andrzej Siewior static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait); 4455bcab335STejun Heo 446e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 44768e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 4487d19c5ceSTejun Heo 44999c621efSLai Jiangshan /* PL&A: allowable cpus for unbound wqs and work items */ 450ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 451ef557180SMike Galbraith 452fe28f631SWaiman Long /* PL: user requested unbound cpumask via sysfs */ 453fe28f631SWaiman Long static cpumask_var_t wq_requested_unbound_cpumask; 454fe28f631SWaiman Long 455fe28f631SWaiman Long /* PL: isolated cpumask to be excluded from unbound cpumask */ 456fe28f631SWaiman Long static cpumask_var_t wq_isolated_cpumask; 457fe28f631SWaiman Long 458ace3c549Stiozhang /* for further constrain wq_unbound_cpumask by cmdline parameter*/ 459ace3c549Stiozhang static struct cpumask wq_cmdline_cpumask __initdata; 460ace3c549Stiozhang 461ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 462ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 463b05a7928SFrederic Weisbecker 464f303fccbSTejun Heo /* 465f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 466f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 467f303fccbSTejun Heo * to uncover usages which depend on it. 468f303fccbSTejun Heo */ 469f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 470f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 471f303fccbSTejun Heo #else 472f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 473f303fccbSTejun Heo #endif 474f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 475f303fccbSTejun Heo 4762f34d733STejun Heo /* to raise softirq for the BH worker pools on other CPUs */ 4772f34d733STejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct irq_work [NR_STD_WORKER_POOLS], 4782f34d733STejun Heo bh_pool_irq_works); 4792f34d733STejun Heo 4804cb1ef64STejun Heo /* the BH worker pools */ 4814cb1ef64STejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], 4824cb1ef64STejun Heo bh_worker_pools); 4834cb1ef64STejun Heo 4847d19c5ceSTejun Heo /* the per-cpu worker pools */ 4854cb1ef64STejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], 4864cb1ef64STejun Heo cpu_worker_pools); 4877d19c5ceSTejun Heo 48868e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 4897d19c5ceSTejun Heo 49068e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 49129c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 49229c91e99STejun Heo 493c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 49429c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 49529c91e99STejun Heo 4968a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 4978a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 4988a2b7538STejun Heo 499967b494eSTejun Heo /* 500967b494eSTejun Heo * I: kthread_worker to release pwq's. pwq release needs to be bounced to a 501967b494eSTejun Heo * process context while holding a pool lock. Bounce to a dedicated kthread 502967b494eSTejun Heo * worker to avoid A-A deadlocks. 503967b494eSTejun Heo */ 50468279f9cSAlexey Dobriyan static struct kthread_worker *pwq_release_worker __ro_after_init; 505967b494eSTejun Heo 50668279f9cSAlexey Dobriyan struct workqueue_struct *system_wq __ro_after_init; 507ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 50868279f9cSAlexey Dobriyan struct workqueue_struct *system_highpri_wq __ro_after_init; 5091aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 51068279f9cSAlexey Dobriyan struct workqueue_struct *system_long_wq __ro_after_init; 511d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 51268279f9cSAlexey Dobriyan struct workqueue_struct *system_unbound_wq __ro_after_init; 513f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 51468279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_wq __ro_after_init; 51524d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 51668279f9cSAlexey Dobriyan struct workqueue_struct *system_power_efficient_wq __ro_after_init; 5170668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 51868279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_power_efficient_wq __ro_after_init; 5190668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 5204cb1ef64STejun Heo struct workqueue_struct *system_bh_wq; 5214cb1ef64STejun Heo EXPORT_SYMBOL_GPL(system_bh_wq); 5224cb1ef64STejun Heo struct workqueue_struct *system_bh_highpri_wq; 5234cb1ef64STejun Heo EXPORT_SYMBOL_GPL(system_bh_highpri_wq); 524d320c038STejun Heo 5257d19c5ceSTejun Heo static int worker_thread(void *__worker); 5266ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 527c29eb853STejun Heo static void show_pwq(struct pool_workqueue *pwq); 52855df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool); 5297d19c5ceSTejun Heo 53097bd2347STejun Heo #define CREATE_TRACE_POINTS 53197bd2347STejun Heo #include <trace/events/workqueue.h> 53297bd2347STejun Heo 53368e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 534d355001fSTejun Heo RCU_LOCKDEP_WARN(!rcu_read_lock_any_held() && \ 535f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 53624acfb71SThomas Gleixner "RCU or wq_pool_mutex should be held") 5375bcab335STejun Heo 5385b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 539d355001fSTejun Heo RCU_LOCKDEP_WARN(!rcu_read_lock_any_held() && \ 540f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 541f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 54224acfb71SThomas Gleixner "RCU, wq->mutex or wq_pool_mutex should be held") 5435b95e1afSLai Jiangshan 5444cb1ef64STejun Heo #define for_each_bh_worker_pool(pool, cpu) \ 5454cb1ef64STejun Heo for ((pool) = &per_cpu(bh_worker_pools, cpu)[0]; \ 5464cb1ef64STejun Heo (pool) < &per_cpu(bh_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 5474cb1ef64STejun Heo (pool)++) 5484cb1ef64STejun Heo 549f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 550f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 551f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 5527a62c2c8STejun Heo (pool)++) 5534ce62e9eSTejun Heo 55449e3cf44STejun Heo /** 55517116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 55617116969STejun Heo * @pool: iteration cursor 557611c92a0STejun Heo * @pi: integer used for iteration 558fa1b54e6STejun Heo * 55924acfb71SThomas Gleixner * This must be called either with wq_pool_mutex held or RCU read 56068e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 56168e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 562fa1b54e6STejun Heo * 563fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 564fa1b54e6STejun Heo * ignored. 56517116969STejun Heo */ 566611c92a0STejun Heo #define for_each_pool(pool, pi) \ 567611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 56868e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 569fa1b54e6STejun Heo else 57017116969STejun Heo 57117116969STejun Heo /** 572822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 573822d8405STejun Heo * @worker: iteration cursor 574822d8405STejun Heo * @pool: worker_pool to iterate workers of 575822d8405STejun Heo * 5761258fae7STejun Heo * This must be called with wq_pool_attach_mutex. 577822d8405STejun Heo * 578822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 579822d8405STejun Heo * ignored. 580822d8405STejun Heo */ 581da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 582da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 5831258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 584822d8405STejun Heo else 585822d8405STejun Heo 586822d8405STejun Heo /** 58749e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 58849e3cf44STejun Heo * @pwq: iteration cursor 58949e3cf44STejun Heo * @wq: the target workqueue 59076af4d93STejun Heo * 59124acfb71SThomas Gleixner * This must be called either with wq->mutex held or RCU read locked. 592794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 593794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 59476af4d93STejun Heo * 59576af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 59676af4d93STejun Heo * ignored. 59749e3cf44STejun Heo */ 59849e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 59949e9d1a9SSebastian Andrzej Siewior list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ 6005a644662SJoel Fernandes (Google) lockdep_is_held(&(wq->mutex))) 601f3421797STejun Heo 602dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 603dc186ad7SThomas Gleixner 604f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr; 605dc186ad7SThomas Gleixner 60699777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 60799777288SStanislaw Gruszka { 60899777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 60999777288SStanislaw Gruszka } 61099777288SStanislaw Gruszka 611b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 612b9fdac7fSDu, Changbin { 613b9fdac7fSDu, Changbin struct work_struct *work = addr; 614b9fdac7fSDu, Changbin 615b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 616b9fdac7fSDu, Changbin } 617b9fdac7fSDu, Changbin 618dc186ad7SThomas Gleixner /* 619dc186ad7SThomas Gleixner * fixup_init is called when: 620dc186ad7SThomas Gleixner * - an active object is initialized 621dc186ad7SThomas Gleixner */ 62202a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 623dc186ad7SThomas Gleixner { 624dc186ad7SThomas Gleixner struct work_struct *work = addr; 625dc186ad7SThomas Gleixner 626dc186ad7SThomas Gleixner switch (state) { 627dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 628dc186ad7SThomas Gleixner cancel_work_sync(work); 629dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 63002a982a6SDu, Changbin return true; 631dc186ad7SThomas Gleixner default: 63202a982a6SDu, Changbin return false; 633dc186ad7SThomas Gleixner } 634dc186ad7SThomas Gleixner } 635dc186ad7SThomas Gleixner 636dc186ad7SThomas Gleixner /* 637dc186ad7SThomas Gleixner * fixup_free is called when: 638dc186ad7SThomas Gleixner * - an active object is freed 639dc186ad7SThomas Gleixner */ 64002a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 641dc186ad7SThomas Gleixner { 642dc186ad7SThomas Gleixner struct work_struct *work = addr; 643dc186ad7SThomas Gleixner 644dc186ad7SThomas Gleixner switch (state) { 645dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 646dc186ad7SThomas Gleixner cancel_work_sync(work); 647dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 64802a982a6SDu, Changbin return true; 649dc186ad7SThomas Gleixner default: 65002a982a6SDu, Changbin return false; 651dc186ad7SThomas Gleixner } 652dc186ad7SThomas Gleixner } 653dc186ad7SThomas Gleixner 654f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr = { 655dc186ad7SThomas Gleixner .name = "work_struct", 65699777288SStanislaw Gruszka .debug_hint = work_debug_hint, 657b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 658dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 659dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 660dc186ad7SThomas Gleixner }; 661dc186ad7SThomas Gleixner 662dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 663dc186ad7SThomas Gleixner { 664dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 665dc186ad7SThomas Gleixner } 666dc186ad7SThomas Gleixner 667dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 668dc186ad7SThomas Gleixner { 669dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 670dc186ad7SThomas Gleixner } 671dc186ad7SThomas Gleixner 672dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 673dc186ad7SThomas Gleixner { 674dc186ad7SThomas Gleixner if (onstack) 675dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 676dc186ad7SThomas Gleixner else 677dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 678dc186ad7SThomas Gleixner } 679dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 680dc186ad7SThomas Gleixner 681dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 682dc186ad7SThomas Gleixner { 683dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 684dc186ad7SThomas Gleixner } 685dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 686dc186ad7SThomas Gleixner 687ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 688ea2e64f2SThomas Gleixner { 689ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 690ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 691ea2e64f2SThomas Gleixner } 692ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 693ea2e64f2SThomas Gleixner 694dc186ad7SThomas Gleixner #else 695dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 696dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 697dc186ad7SThomas Gleixner #endif 698dc186ad7SThomas Gleixner 6994e8b22bdSLi Bin /** 70067dc8325SCai Huoqing * worker_pool_assign_id - allocate ID and assign it to @pool 7014e8b22bdSLi Bin * @pool: the pool pointer of interest 7024e8b22bdSLi Bin * 7034e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 7044e8b22bdSLi Bin * successfully, -errno on failure. 7054e8b22bdSLi Bin */ 7069daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 7079daf9e67STejun Heo { 7089daf9e67STejun Heo int ret; 7099daf9e67STejun Heo 71068e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 7115bcab335STejun Heo 7124e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 7134e8b22bdSLi Bin GFP_KERNEL); 714229641a6STejun Heo if (ret >= 0) { 715e68035fbSTejun Heo pool->id = ret; 716229641a6STejun Heo return 0; 717229641a6STejun Heo } 7189daf9e67STejun Heo return ret; 7199daf9e67STejun Heo } 7209daf9e67STejun Heo 7219f66cff2STejun Heo static struct pool_workqueue __rcu ** 7229f66cff2STejun Heo unbound_pwq_slot(struct workqueue_struct *wq, int cpu) 7239f66cff2STejun Heo { 7249f66cff2STejun Heo if (cpu >= 0) 7259f66cff2STejun Heo return per_cpu_ptr(wq->cpu_pwq, cpu); 7269f66cff2STejun Heo else 7279f66cff2STejun Heo return &wq->dfl_pwq; 7289f66cff2STejun Heo } 7299f66cff2STejun Heo 7309f66cff2STejun Heo /* @cpu < 0 for dfl_pwq */ 7319f66cff2STejun Heo static struct pool_workqueue *unbound_pwq(struct workqueue_struct *wq, int cpu) 7329f66cff2STejun Heo { 7339f66cff2STejun Heo return rcu_dereference_check(*unbound_pwq_slot(wq, cpu), 7349f66cff2STejun Heo lockdep_is_held(&wq_pool_mutex) || 7359f66cff2STejun Heo lockdep_is_held(&wq->mutex)); 7369f66cff2STejun Heo } 7379f66cff2STejun Heo 7385797b1c1STejun Heo /** 7395797b1c1STejun Heo * unbound_effective_cpumask - effective cpumask of an unbound workqueue 7405797b1c1STejun Heo * @wq: workqueue of interest 7415797b1c1STejun Heo * 7425797b1c1STejun Heo * @wq->unbound_attrs->cpumask contains the cpumask requested by the user which 7435797b1c1STejun Heo * is masked with wq_unbound_cpumask to determine the effective cpumask. The 7445797b1c1STejun Heo * default pwq is always mapped to the pool with the current effective cpumask. 7455797b1c1STejun Heo */ 7465797b1c1STejun Heo static struct cpumask *unbound_effective_cpumask(struct workqueue_struct *wq) 7475797b1c1STejun Heo { 7485797b1c1STejun Heo return unbound_pwq(wq, -1)->pool->attrs->__pod_cpumask; 7495797b1c1STejun Heo } 7505797b1c1STejun Heo 75173f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 75273f53c4aSTejun Heo { 75373f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 75473f53c4aSTejun Heo } 75573f53c4aSTejun Heo 756c4560c2cSLai Jiangshan static int get_work_color(unsigned long work_data) 75773f53c4aSTejun Heo { 758c4560c2cSLai Jiangshan return (work_data >> WORK_STRUCT_COLOR_SHIFT) & 75973f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 76073f53c4aSTejun Heo } 76173f53c4aSTejun Heo 76273f53c4aSTejun Heo static int work_next_color(int color) 76373f53c4aSTejun Heo { 76473f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 765a848e3b6SOleg Nesterov } 766a848e3b6SOleg Nesterov 7674594bf15SDavid Howells /* 768112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 769112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 7707c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 7717a22ad75STejun Heo * 772afe928c1STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending() and mark_work_canceling() 773afe928c1STejun Heo * can be used to set the pwq, pool or clear work->data. These functions should 774afe928c1STejun Heo * only be called while the work is owned - ie. while the PENDING bit is set. 7757a22ad75STejun Heo * 776112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 7777c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 778112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 7797c3eed5cSTejun Heo * available only while the work item is queued. 7804594bf15SDavid Howells */ 781bccdc1faSTejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data) 7827a22ad75STejun Heo { 7836183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 784bccdc1faSTejun Heo atomic_long_set(&work->data, data | work_static(work)); 7857a22ad75STejun Heo } 7867a22ad75STejun Heo 787112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 788bccdc1faSTejun Heo unsigned long flags) 789365970a1SDavid Howells { 790bccdc1faSTejun Heo set_work_data(work, (unsigned long)pwq | WORK_STRUCT_PENDING | 791bccdc1faSTejun Heo WORK_STRUCT_PWQ | flags); 792365970a1SDavid Howells } 793365970a1SDavid Howells 7944468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 795bccdc1faSTejun Heo int pool_id, unsigned long flags) 7964468a00fSLai Jiangshan { 797bccdc1faSTejun Heo set_work_data(work, ((unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT) | 798bccdc1faSTejun Heo WORK_STRUCT_PENDING | flags); 7994468a00fSLai Jiangshan } 8004468a00fSLai Jiangshan 8017c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 802bccdc1faSTejun Heo int pool_id, unsigned long flags) 8034d707b9fSOleg Nesterov { 80423657bb1STejun Heo /* 80523657bb1STejun Heo * The following wmb is paired with the implied mb in 80623657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 80723657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 80823657bb1STejun Heo * owner. 80923657bb1STejun Heo */ 81023657bb1STejun Heo smp_wmb(); 811bccdc1faSTejun Heo set_work_data(work, ((unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT) | 812bccdc1faSTejun Heo flags); 813346c09f8SRoman Pen /* 814346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 815346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 816346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 8178bdc6201SLiu Song * reordering can lead to a missed execution on attempt to queue 818346c09f8SRoman Pen * the same @work. E.g. consider this case: 819346c09f8SRoman Pen * 820346c09f8SRoman Pen * CPU#0 CPU#1 821346c09f8SRoman Pen * ---------------------------- -------------------------------- 822346c09f8SRoman Pen * 823346c09f8SRoman Pen * 1 STORE event_indicated 824346c09f8SRoman Pen * 2 queue_work_on() { 825346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 826346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 827346c09f8SRoman Pen * 5 set_work_data() # clear bit 828346c09f8SRoman Pen * 6 smp_mb() 829346c09f8SRoman Pen * 7 work->current_func() { 830346c09f8SRoman Pen * 8 LOAD event_indicated 831346c09f8SRoman Pen * } 832346c09f8SRoman Pen * 833346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 834346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 835346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 836346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 837346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 838346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 839346c09f8SRoman Pen * before actual STORE. 840346c09f8SRoman Pen */ 841346c09f8SRoman Pen smp_mb(); 8424d707b9fSOleg Nesterov } 8434d707b9fSOleg Nesterov 844afa4bb77SLinus Torvalds static inline struct pool_workqueue *work_struct_pwq(unsigned long data) 845afa4bb77SLinus Torvalds { 846e9a8e01fSTejun Heo return (struct pool_workqueue *)(data & WORK_STRUCT_PWQ_MASK); 847afa4bb77SLinus Torvalds } 848afa4bb77SLinus Torvalds 849112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 8507a22ad75STejun Heo { 851e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 8527a22ad75STejun Heo 853112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 854afa4bb77SLinus Torvalds return work_struct_pwq(data); 855e120153dSTejun Heo else 856e120153dSTejun Heo return NULL; 8577a22ad75STejun Heo } 8587a22ad75STejun Heo 8597c3eed5cSTejun Heo /** 8607c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 8617c3eed5cSTejun Heo * @work: the work item of interest 8627c3eed5cSTejun Heo * 86368e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 86424acfb71SThomas Gleixner * access under RCU read lock. As such, this function should be 86524acfb71SThomas Gleixner * called under wq_pool_mutex or inside of a rcu_read_lock() region. 866fa1b54e6STejun Heo * 867fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 868fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 869fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 870fa1b54e6STejun Heo * returned pool is and stays online. 871d185af30SYacine Belkadi * 872d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 8737c3eed5cSTejun Heo */ 8747c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 8757a22ad75STejun Heo { 876e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 8777c3eed5cSTejun Heo int pool_id; 8787a22ad75STejun Heo 87968e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 880fa1b54e6STejun Heo 881112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 882afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool; 8837a22ad75STejun Heo 8847c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 8857c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 8867a22ad75STejun Heo return NULL; 8877a22ad75STejun Heo 888fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 8897c3eed5cSTejun Heo } 8907c3eed5cSTejun Heo 8911211f3b2STejun Heo static unsigned long shift_and_mask(unsigned long v, u32 shift, u32 bits) 8927c3eed5cSTejun Heo { 8931211f3b2STejun Heo return (v >> shift) & ((1 << bits) - 1); 8947c3eed5cSTejun Heo } 8957c3eed5cSTejun Heo 8961211f3b2STejun Heo static void work_offqd_unpack(struct work_offq_data *offqd, unsigned long data) 897bbb68dfaSTejun Heo { 8981211f3b2STejun Heo WARN_ON_ONCE(data & WORK_STRUCT_PWQ); 899bbb68dfaSTejun Heo 9001211f3b2STejun Heo offqd->pool_id = shift_and_mask(data, WORK_OFFQ_POOL_SHIFT, 9011211f3b2STejun Heo WORK_OFFQ_POOL_BITS); 90286898fa6STejun Heo offqd->disable = shift_and_mask(data, WORK_OFFQ_DISABLE_SHIFT, 90386898fa6STejun Heo WORK_OFFQ_DISABLE_BITS); 9041211f3b2STejun Heo offqd->flags = data & WORK_OFFQ_FLAG_MASK; 9051211f3b2STejun Heo } 9061211f3b2STejun Heo 9071211f3b2STejun Heo static unsigned long work_offqd_pack_flags(struct work_offq_data *offqd) 9081211f3b2STejun Heo { 90986898fa6STejun Heo return ((unsigned long)offqd->disable << WORK_OFFQ_DISABLE_SHIFT) | 91086898fa6STejun Heo ((unsigned long)offqd->flags); 911bbb68dfaSTejun Heo } 912bbb68dfaSTejun Heo 913e22bee78STejun Heo /* 9143270476aSTejun Heo * Policy functions. These define the policies on how the global worker 9153270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 916d565ed63STejun Heo * they're being called with pool->lock held. 917e22bee78STejun Heo */ 918e22bee78STejun Heo 919e22bee78STejun Heo /* 920e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 921e22bee78STejun Heo * running workers. 922974271c4STejun Heo * 923974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 924706026c2STejun Heo * function will always return %true for unbound pools as long as the 925974271c4STejun Heo * worklist isn't empty. 926e22bee78STejun Heo */ 92763d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 928e22bee78STejun Heo { 9290219a352STejun Heo return !list_empty(&pool->worklist) && !pool->nr_running; 930e22bee78STejun Heo } 931e22bee78STejun Heo 932e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 93363d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 934e22bee78STejun Heo { 93563d95a91STejun Heo return pool->nr_idle; 936e22bee78STejun Heo } 937e22bee78STejun Heo 938e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 93963d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 940e22bee78STejun Heo { 941bc35f7efSLai Jiangshan return !list_empty(&pool->worklist) && (pool->nr_running <= 1); 942e22bee78STejun Heo } 943e22bee78STejun Heo 944e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 94563d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 946e22bee78STejun Heo { 94763d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 948e22bee78STejun Heo } 949e22bee78STejun Heo 950e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 95163d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 952e22bee78STejun Heo { 953692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 95463d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 95563d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 956e22bee78STejun Heo 957e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 958e22bee78STejun Heo } 959e22bee78STejun Heo 9604690c4abSTejun Heo /** 961e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 962cb444766STejun Heo * @worker: self 963d302f017STejun Heo * @flags: flags to set 964d302f017STejun Heo * 965228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 966d302f017STejun Heo */ 967228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 968d302f017STejun Heo { 969bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 970e22bee78STejun Heo 971bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 972cb444766STejun Heo 973228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 974e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 975e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 976bc35f7efSLai Jiangshan pool->nr_running--; 977e22bee78STejun Heo } 978e22bee78STejun Heo 979d302f017STejun Heo worker->flags |= flags; 980d302f017STejun Heo } 981d302f017STejun Heo 982d302f017STejun Heo /** 983e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 984cb444766STejun Heo * @worker: self 985d302f017STejun Heo * @flags: flags to clear 986d302f017STejun Heo * 987e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 988d302f017STejun Heo */ 989d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 990d302f017STejun Heo { 99163d95a91STejun Heo struct worker_pool *pool = worker->pool; 992e22bee78STejun Heo unsigned int oflags = worker->flags; 993e22bee78STejun Heo 994bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 995cb444766STejun Heo 996d302f017STejun Heo worker->flags &= ~flags; 997e22bee78STejun Heo 99842c025f3STejun Heo /* 99942c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 100042c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 100142c025f3STejun Heo * of multiple flags, not a single flag. 100242c025f3STejun Heo */ 1003e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 1004e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 1005bc35f7efSLai Jiangshan pool->nr_running++; 1006d302f017STejun Heo } 1007d302f017STejun Heo 1008797e8345STejun Heo /* Return the first idle worker. Called with pool->lock held. */ 1009797e8345STejun Heo static struct worker *first_idle_worker(struct worker_pool *pool) 1010797e8345STejun Heo { 1011797e8345STejun Heo if (unlikely(list_empty(&pool->idle_list))) 1012797e8345STejun Heo return NULL; 1013797e8345STejun Heo 1014797e8345STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 1015797e8345STejun Heo } 1016797e8345STejun Heo 1017797e8345STejun Heo /** 1018797e8345STejun Heo * worker_enter_idle - enter idle state 1019797e8345STejun Heo * @worker: worker which is entering idle state 1020797e8345STejun Heo * 1021797e8345STejun Heo * @worker is entering idle state. Update stats and idle timer if 1022797e8345STejun Heo * necessary. 1023797e8345STejun Heo * 1024797e8345STejun Heo * LOCKING: 1025797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1026797e8345STejun Heo */ 1027797e8345STejun Heo static void worker_enter_idle(struct worker *worker) 1028797e8345STejun Heo { 1029797e8345STejun Heo struct worker_pool *pool = worker->pool; 1030797e8345STejun Heo 1031797e8345STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 1032797e8345STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 1033797e8345STejun Heo (worker->hentry.next || worker->hentry.pprev))) 1034797e8345STejun Heo return; 1035797e8345STejun Heo 1036797e8345STejun Heo /* can't use worker_set_flags(), also called from create_worker() */ 1037797e8345STejun Heo worker->flags |= WORKER_IDLE; 1038797e8345STejun Heo pool->nr_idle++; 1039797e8345STejun Heo worker->last_active = jiffies; 1040797e8345STejun Heo 1041797e8345STejun Heo /* idle_list is LIFO */ 1042797e8345STejun Heo list_add(&worker->entry, &pool->idle_list); 1043797e8345STejun Heo 1044797e8345STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1045797e8345STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1046797e8345STejun Heo 1047797e8345STejun Heo /* Sanity check nr_running. */ 1048797e8345STejun Heo WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); 1049797e8345STejun Heo } 1050797e8345STejun Heo 1051797e8345STejun Heo /** 1052797e8345STejun Heo * worker_leave_idle - leave idle state 1053797e8345STejun Heo * @worker: worker which is leaving idle state 1054797e8345STejun Heo * 1055797e8345STejun Heo * @worker is leaving idle state. Update stats. 1056797e8345STejun Heo * 1057797e8345STejun Heo * LOCKING: 1058797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1059797e8345STejun Heo */ 1060797e8345STejun Heo static void worker_leave_idle(struct worker *worker) 1061797e8345STejun Heo { 1062797e8345STejun Heo struct worker_pool *pool = worker->pool; 1063797e8345STejun Heo 1064797e8345STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 1065797e8345STejun Heo return; 1066797e8345STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1067797e8345STejun Heo pool->nr_idle--; 1068797e8345STejun Heo list_del_init(&worker->entry); 1069797e8345STejun Heo } 1070797e8345STejun Heo 1071797e8345STejun Heo /** 1072797e8345STejun Heo * find_worker_executing_work - find worker which is executing a work 1073797e8345STejun Heo * @pool: pool of interest 1074797e8345STejun Heo * @work: work to find worker for 1075797e8345STejun Heo * 1076797e8345STejun Heo * Find a worker which is executing @work on @pool by searching 1077797e8345STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 1078797e8345STejun Heo * to match, its current execution should match the address of @work and 1079797e8345STejun Heo * its work function. This is to avoid unwanted dependency between 1080797e8345STejun Heo * unrelated work executions through a work item being recycled while still 1081797e8345STejun Heo * being executed. 1082797e8345STejun Heo * 1083797e8345STejun Heo * This is a bit tricky. A work item may be freed once its execution 1084797e8345STejun Heo * starts and nothing prevents the freed area from being recycled for 1085797e8345STejun Heo * another work item. If the same work item address ends up being reused 1086797e8345STejun Heo * before the original execution finishes, workqueue will identify the 1087797e8345STejun Heo * recycled work item as currently executing and make it wait until the 1088797e8345STejun Heo * current execution finishes, introducing an unwanted dependency. 1089797e8345STejun Heo * 1090797e8345STejun Heo * This function checks the work item address and work function to avoid 1091797e8345STejun Heo * false positives. Note that this isn't complete as one may construct a 1092797e8345STejun Heo * work function which can introduce dependency onto itself through a 1093797e8345STejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 1094797e8345STejun Heo * foot that badly, there's only so much we can do, and if such deadlock 1095797e8345STejun Heo * actually occurs, it should be easy to locate the culprit work function. 1096797e8345STejun Heo * 1097797e8345STejun Heo * CONTEXT: 1098797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1099797e8345STejun Heo * 1100797e8345STejun Heo * Return: 1101797e8345STejun Heo * Pointer to worker which is executing @work if found, %NULL 1102797e8345STejun Heo * otherwise. 1103797e8345STejun Heo */ 1104797e8345STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 1105797e8345STejun Heo struct work_struct *work) 1106797e8345STejun Heo { 1107797e8345STejun Heo struct worker *worker; 1108797e8345STejun Heo 1109797e8345STejun Heo hash_for_each_possible(pool->busy_hash, worker, hentry, 1110797e8345STejun Heo (unsigned long)work) 1111797e8345STejun Heo if (worker->current_work == work && 1112797e8345STejun Heo worker->current_func == work->func) 1113797e8345STejun Heo return worker; 1114797e8345STejun Heo 1115797e8345STejun Heo return NULL; 1116797e8345STejun Heo } 1117797e8345STejun Heo 1118797e8345STejun Heo /** 1119797e8345STejun Heo * move_linked_works - move linked works to a list 1120797e8345STejun Heo * @work: start of series of works to be scheduled 1121797e8345STejun Heo * @head: target list to append @work to 1122797e8345STejun Heo * @nextp: out parameter for nested worklist walking 1123797e8345STejun Heo * 1124873eaca6STejun Heo * Schedule linked works starting from @work to @head. Work series to be 1125873eaca6STejun Heo * scheduled starts at @work and includes any consecutive work with 1126873eaca6STejun Heo * WORK_STRUCT_LINKED set in its predecessor. See assign_work() for details on 1127873eaca6STejun Heo * @nextp. 1128797e8345STejun Heo * 1129797e8345STejun Heo * CONTEXT: 1130797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1131797e8345STejun Heo */ 1132797e8345STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1133797e8345STejun Heo struct work_struct **nextp) 1134797e8345STejun Heo { 1135797e8345STejun Heo struct work_struct *n; 1136797e8345STejun Heo 1137797e8345STejun Heo /* 1138797e8345STejun Heo * Linked worklist will always end before the end of the list, 1139797e8345STejun Heo * use NULL for list head. 1140797e8345STejun Heo */ 1141797e8345STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1142797e8345STejun Heo list_move_tail(&work->entry, head); 1143797e8345STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1144797e8345STejun Heo break; 1145797e8345STejun Heo } 1146797e8345STejun Heo 1147797e8345STejun Heo /* 1148797e8345STejun Heo * If we're already inside safe list traversal and have moved 1149797e8345STejun Heo * multiple works to the scheduled queue, the next position 1150797e8345STejun Heo * needs to be updated. 1151797e8345STejun Heo */ 1152797e8345STejun Heo if (nextp) 1153797e8345STejun Heo *nextp = n; 1154797e8345STejun Heo } 1155797e8345STejun Heo 1156797e8345STejun Heo /** 1157873eaca6STejun Heo * assign_work - assign a work item and its linked work items to a worker 1158873eaca6STejun Heo * @work: work to assign 1159873eaca6STejun Heo * @worker: worker to assign to 1160873eaca6STejun Heo * @nextp: out parameter for nested worklist walking 1161873eaca6STejun Heo * 1162873eaca6STejun Heo * Assign @work and its linked work items to @worker. If @work is already being 1163873eaca6STejun Heo * executed by another worker in the same pool, it'll be punted there. 1164873eaca6STejun Heo * 1165873eaca6STejun Heo * If @nextp is not NULL, it's updated to point to the next work of the last 1166873eaca6STejun Heo * scheduled work. This allows assign_work() to be nested inside 1167873eaca6STejun Heo * list_for_each_entry_safe(). 1168873eaca6STejun Heo * 1169873eaca6STejun Heo * Returns %true if @work was successfully assigned to @worker. %false if @work 1170873eaca6STejun Heo * was punted to another worker already executing it. 1171873eaca6STejun Heo */ 1172873eaca6STejun Heo static bool assign_work(struct work_struct *work, struct worker *worker, 1173873eaca6STejun Heo struct work_struct **nextp) 1174873eaca6STejun Heo { 1175873eaca6STejun Heo struct worker_pool *pool = worker->pool; 1176873eaca6STejun Heo struct worker *collision; 1177873eaca6STejun Heo 1178873eaca6STejun Heo lockdep_assert_held(&pool->lock); 1179873eaca6STejun Heo 1180873eaca6STejun Heo /* 1181873eaca6STejun Heo * A single work shouldn't be executed concurrently by multiple workers. 1182873eaca6STejun Heo * __queue_work() ensures that @work doesn't jump to a different pool 1183873eaca6STejun Heo * while still running in the previous pool. Here, we should ensure that 1184873eaca6STejun Heo * @work is not executed concurrently by multiple workers from the same 1185873eaca6STejun Heo * pool. Check whether anyone is already processing the work. If so, 1186873eaca6STejun Heo * defer the work to the currently executing one. 1187873eaca6STejun Heo */ 1188873eaca6STejun Heo collision = find_worker_executing_work(pool, work); 1189873eaca6STejun Heo if (unlikely(collision)) { 1190873eaca6STejun Heo move_linked_works(work, &collision->scheduled, nextp); 1191873eaca6STejun Heo return false; 1192873eaca6STejun Heo } 1193873eaca6STejun Heo 1194873eaca6STejun Heo move_linked_works(work, &worker->scheduled, nextp); 1195873eaca6STejun Heo return true; 1196873eaca6STejun Heo } 1197873eaca6STejun Heo 11982f34d733STejun Heo static struct irq_work *bh_pool_irq_work(struct worker_pool *pool) 11992f34d733STejun Heo { 12002f34d733STejun Heo int high = pool->attrs->nice == HIGHPRI_NICE_LEVEL ? 1 : 0; 12012f34d733STejun Heo 12022f34d733STejun Heo return &per_cpu(bh_pool_irq_works, pool->cpu)[high]; 12032f34d733STejun Heo } 12042f34d733STejun Heo 1205fd0a68a2STejun Heo static void kick_bh_pool(struct worker_pool *pool) 1206fd0a68a2STejun Heo { 1207fd0a68a2STejun Heo #ifdef CONFIG_SMP 12081acd92d9STejun Heo /* see drain_dead_softirq_workfn() for BH_DRAINING */ 12091acd92d9STejun Heo if (unlikely(pool->cpu != smp_processor_id() && 12101acd92d9STejun Heo !(pool->flags & POOL_BH_DRAINING))) { 1211fd0a68a2STejun Heo irq_work_queue_on(bh_pool_irq_work(pool), pool->cpu); 1212fd0a68a2STejun Heo return; 1213fd0a68a2STejun Heo } 1214fd0a68a2STejun Heo #endif 1215fd0a68a2STejun Heo if (pool->attrs->nice == HIGHPRI_NICE_LEVEL) 1216fd0a68a2STejun Heo raise_softirq_irqoff(HI_SOFTIRQ); 1217fd0a68a2STejun Heo else 1218fd0a68a2STejun Heo raise_softirq_irqoff(TASKLET_SOFTIRQ); 1219fd0a68a2STejun Heo } 1220fd0a68a2STejun Heo 1221873eaca6STejun Heo /** 12220219a352STejun Heo * kick_pool - wake up an idle worker if necessary 12230219a352STejun Heo * @pool: pool to kick 1224797e8345STejun Heo * 12250219a352STejun Heo * @pool may have pending work items. Wake up worker if necessary. Returns 12260219a352STejun Heo * whether a worker was woken up. 1227797e8345STejun Heo */ 12280219a352STejun Heo static bool kick_pool(struct worker_pool *pool) 1229797e8345STejun Heo { 1230797e8345STejun Heo struct worker *worker = first_idle_worker(pool); 12318639ecebSTejun Heo struct task_struct *p; 1232797e8345STejun Heo 12330219a352STejun Heo lockdep_assert_held(&pool->lock); 12340219a352STejun Heo 12350219a352STejun Heo if (!need_more_worker(pool) || !worker) 12360219a352STejun Heo return false; 12370219a352STejun Heo 12384cb1ef64STejun Heo if (pool->flags & POOL_BH) { 1239fd0a68a2STejun Heo kick_bh_pool(pool); 12404cb1ef64STejun Heo return true; 12414cb1ef64STejun Heo } 12424cb1ef64STejun Heo 12438639ecebSTejun Heo p = worker->task; 12448639ecebSTejun Heo 12458639ecebSTejun Heo #ifdef CONFIG_SMP 12468639ecebSTejun Heo /* 12478639ecebSTejun Heo * Idle @worker is about to execute @work and waking up provides an 12488639ecebSTejun Heo * opportunity to migrate @worker at a lower cost by setting the task's 12498639ecebSTejun Heo * wake_cpu field. Let's see if we want to move @worker to improve 12508639ecebSTejun Heo * execution locality. 12518639ecebSTejun Heo * 12528639ecebSTejun Heo * We're waking the worker that went idle the latest and there's some 12538639ecebSTejun Heo * chance that @worker is marked idle but hasn't gone off CPU yet. If 12548639ecebSTejun Heo * so, setting the wake_cpu won't do anything. As this is a best-effort 12558639ecebSTejun Heo * optimization and the race window is narrow, let's leave as-is for 12568639ecebSTejun Heo * now. If this becomes pronounced, we can skip over workers which are 12578639ecebSTejun Heo * still on cpu when picking an idle worker. 12588639ecebSTejun Heo * 12598639ecebSTejun Heo * If @pool has non-strict affinity, @worker might have ended up outside 12608639ecebSTejun Heo * its affinity scope. Repatriate. 12618639ecebSTejun Heo */ 12628639ecebSTejun Heo if (!pool->attrs->affn_strict && 12638639ecebSTejun Heo !cpumask_test_cpu(p->wake_cpu, pool->attrs->__pod_cpumask)) { 12648639ecebSTejun Heo struct work_struct *work = list_first_entry(&pool->worklist, 12658639ecebSTejun Heo struct work_struct, entry); 12668639ecebSTejun Heo p->wake_cpu = cpumask_any_distribute(pool->attrs->__pod_cpumask); 12678639ecebSTejun Heo get_work_pwq(work)->stats[PWQ_STAT_REPATRIATED]++; 12688639ecebSTejun Heo } 12698639ecebSTejun Heo #endif 12708639ecebSTejun Heo wake_up_process(p); 12710219a352STejun Heo return true; 1272797e8345STejun Heo } 1273797e8345STejun Heo 127463638450STejun Heo #ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT 127563638450STejun Heo 127663638450STejun Heo /* 127763638450STejun Heo * Concurrency-managed per-cpu work items that hog CPU for longer than 127863638450STejun Heo * wq_cpu_intensive_thresh_us trigger the automatic CPU_INTENSIVE mechanism, 127963638450STejun Heo * which prevents them from stalling other concurrency-managed work items. If a 128063638450STejun Heo * work function keeps triggering this mechanism, it's likely that the work item 128163638450STejun Heo * should be using an unbound workqueue instead. 128263638450STejun Heo * 128363638450STejun Heo * wq_cpu_intensive_report() tracks work functions which trigger such conditions 128463638450STejun Heo * and report them so that they can be examined and converted to use unbound 128563638450STejun Heo * workqueues as appropriate. To avoid flooding the console, each violating work 128663638450STejun Heo * function is tracked and reported with exponential backoff. 128763638450STejun Heo */ 128863638450STejun Heo #define WCI_MAX_ENTS 128 128963638450STejun Heo 129063638450STejun Heo struct wci_ent { 129163638450STejun Heo work_func_t func; 129263638450STejun Heo atomic64_t cnt; 129363638450STejun Heo struct hlist_node hash_node; 129463638450STejun Heo }; 129563638450STejun Heo 129663638450STejun Heo static struct wci_ent wci_ents[WCI_MAX_ENTS]; 129763638450STejun Heo static int wci_nr_ents; 129863638450STejun Heo static DEFINE_RAW_SPINLOCK(wci_lock); 129963638450STejun Heo static DEFINE_HASHTABLE(wci_hash, ilog2(WCI_MAX_ENTS)); 130063638450STejun Heo 130163638450STejun Heo static struct wci_ent *wci_find_ent(work_func_t func) 130263638450STejun Heo { 130363638450STejun Heo struct wci_ent *ent; 130463638450STejun Heo 130563638450STejun Heo hash_for_each_possible_rcu(wci_hash, ent, hash_node, 130663638450STejun Heo (unsigned long)func) { 130763638450STejun Heo if (ent->func == func) 130863638450STejun Heo return ent; 130963638450STejun Heo } 131063638450STejun Heo return NULL; 131163638450STejun Heo } 131263638450STejun Heo 131363638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) 131463638450STejun Heo { 131563638450STejun Heo struct wci_ent *ent; 131663638450STejun Heo 131763638450STejun Heo restart: 131863638450STejun Heo ent = wci_find_ent(func); 131963638450STejun Heo if (ent) { 132063638450STejun Heo u64 cnt; 132163638450STejun Heo 132263638450STejun Heo /* 1323ccdec921SXuewen Yan * Start reporting from the warning_thresh and back off 132463638450STejun Heo * exponentially. 132563638450STejun Heo */ 132663638450STejun Heo cnt = atomic64_inc_return_relaxed(&ent->cnt); 1327ccdec921SXuewen Yan if (wq_cpu_intensive_warning_thresh && 1328ccdec921SXuewen Yan cnt >= wq_cpu_intensive_warning_thresh && 1329ccdec921SXuewen Yan is_power_of_2(cnt + 1 - wq_cpu_intensive_warning_thresh)) 133063638450STejun Heo printk_deferred(KERN_WARNING "workqueue: %ps hogged CPU for >%luus %llu times, consider switching to WQ_UNBOUND\n", 133163638450STejun Heo ent->func, wq_cpu_intensive_thresh_us, 133263638450STejun Heo atomic64_read(&ent->cnt)); 133363638450STejun Heo return; 133463638450STejun Heo } 133563638450STejun Heo 133663638450STejun Heo /* 133763638450STejun Heo * @func is a new violation. Allocate a new entry for it. If wcn_ents[] 133863638450STejun Heo * is exhausted, something went really wrong and we probably made enough 133963638450STejun Heo * noise already. 134063638450STejun Heo */ 134163638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) 134263638450STejun Heo return; 134363638450STejun Heo 134463638450STejun Heo raw_spin_lock(&wci_lock); 134563638450STejun Heo 134663638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) { 134763638450STejun Heo raw_spin_unlock(&wci_lock); 134863638450STejun Heo return; 134963638450STejun Heo } 135063638450STejun Heo 135163638450STejun Heo if (wci_find_ent(func)) { 135263638450STejun Heo raw_spin_unlock(&wci_lock); 135363638450STejun Heo goto restart; 135463638450STejun Heo } 135563638450STejun Heo 135663638450STejun Heo ent = &wci_ents[wci_nr_ents++]; 135763638450STejun Heo ent->func = func; 1358ccdec921SXuewen Yan atomic64_set(&ent->cnt, 0); 135963638450STejun Heo hash_add_rcu(wci_hash, &ent->hash_node, (unsigned long)func); 136063638450STejun Heo 136163638450STejun Heo raw_spin_unlock(&wci_lock); 1362ccdec921SXuewen Yan 1363ccdec921SXuewen Yan goto restart; 136463638450STejun Heo } 136563638450STejun Heo 136663638450STejun Heo #else /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 136763638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) {} 136863638450STejun Heo #endif /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 136963638450STejun Heo 1370c54d5046STejun Heo /** 13711da177e4SLinus Torvalds * wq_worker_running - a worker is running again 13721da177e4SLinus Torvalds * @task: task waking up 13731da177e4SLinus Torvalds * 13741da177e4SLinus Torvalds * This function is called when a worker returns from schedule() 13751da177e4SLinus Torvalds */ 13761da177e4SLinus Torvalds void wq_worker_running(struct task_struct *task) 13771da177e4SLinus Torvalds { 13781da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 13791da177e4SLinus Torvalds 1380c8f6219bSZqiang if (!READ_ONCE(worker->sleeping)) 13811da177e4SLinus Torvalds return; 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds /* 13841da177e4SLinus Torvalds * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check 13851da177e4SLinus Torvalds * and the nr_running increment below, we may ruin the nr_running reset 13861da177e4SLinus Torvalds * and leave with an unexpected pool->nr_running == 1 on the newly unbound 13871da177e4SLinus Torvalds * pool. Protect against such race. 13881da177e4SLinus Torvalds */ 13891da177e4SLinus Torvalds preempt_disable(); 13901da177e4SLinus Torvalds if (!(worker->flags & WORKER_NOT_RUNNING)) 13911da177e4SLinus Torvalds worker->pool->nr_running++; 13921da177e4SLinus Torvalds preempt_enable(); 1393616db877STejun Heo 1394616db877STejun Heo /* 1395616db877STejun Heo * CPU intensive auto-detection cares about how long a work item hogged 1396616db877STejun Heo * CPU without sleeping. Reset the starting timestamp on wakeup. 1397616db877STejun Heo */ 1398616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 1399616db877STejun Heo 1400c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 0); 14011da177e4SLinus Torvalds } 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds /** 14041da177e4SLinus Torvalds * wq_worker_sleeping - a worker is going to sleep 14051da177e4SLinus Torvalds * @task: task going to sleep 14061da177e4SLinus Torvalds * 14071da177e4SLinus Torvalds * This function is called from schedule() when a busy worker is 14081da177e4SLinus Torvalds * going to sleep. 14091da177e4SLinus Torvalds */ 14101da177e4SLinus Torvalds void wq_worker_sleeping(struct task_struct *task) 14111da177e4SLinus Torvalds { 14121da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 14131da177e4SLinus Torvalds struct worker_pool *pool; 14141da177e4SLinus Torvalds 14151da177e4SLinus Torvalds /* 14161da177e4SLinus Torvalds * Rescuers, which may not have all the fields set up like normal 14171da177e4SLinus Torvalds * workers, also reach here, let's not access anything before 14181da177e4SLinus Torvalds * checking NOT_RUNNING. 14191da177e4SLinus Torvalds */ 14201da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) 14211da177e4SLinus Torvalds return; 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds pool = worker->pool; 14241da177e4SLinus Torvalds 14251da177e4SLinus Torvalds /* Return if preempted before wq_worker_running() was reached */ 1426c8f6219bSZqiang if (READ_ONCE(worker->sleeping)) 14271da177e4SLinus Torvalds return; 14281da177e4SLinus Torvalds 1429c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 1); 14301da177e4SLinus Torvalds raw_spin_lock_irq(&pool->lock); 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds /* 14331da177e4SLinus Torvalds * Recheck in case unbind_workers() preempted us. We don't 14341da177e4SLinus Torvalds * want to decrement nr_running after the worker is unbound 14351da177e4SLinus Torvalds * and nr_running has been reset. 14361da177e4SLinus Torvalds */ 14371da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) { 14381da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 14391da177e4SLinus Torvalds return; 14401da177e4SLinus Torvalds } 14411da177e4SLinus Torvalds 14421da177e4SLinus Torvalds pool->nr_running--; 14430219a352STejun Heo if (kick_pool(pool)) 1444725e8ec5STejun Heo worker->current_pwq->stats[PWQ_STAT_CM_WAKEUP]++; 14450219a352STejun Heo 14461da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds /** 1450616db877STejun Heo * wq_worker_tick - a scheduler tick occurred while a kworker is running 1451616db877STejun Heo * @task: task currently running 1452616db877STejun Heo * 1453616db877STejun Heo * Called from scheduler_tick(). We're in the IRQ context and the current 1454616db877STejun Heo * worker's fields which follow the 'K' locking rule can be accessed safely. 1455616db877STejun Heo */ 1456616db877STejun Heo void wq_worker_tick(struct task_struct *task) 1457616db877STejun Heo { 1458616db877STejun Heo struct worker *worker = kthread_data(task); 1459616db877STejun Heo struct pool_workqueue *pwq = worker->current_pwq; 1460616db877STejun Heo struct worker_pool *pool = worker->pool; 1461616db877STejun Heo 1462616db877STejun Heo if (!pwq) 1463616db877STejun Heo return; 1464616db877STejun Heo 14658a1dd1e5STejun Heo pwq->stats[PWQ_STAT_CPU_TIME] += TICK_USEC; 14668a1dd1e5STejun Heo 146718c8ae81SZqiang if (!wq_cpu_intensive_thresh_us) 146818c8ae81SZqiang return; 146918c8ae81SZqiang 1470616db877STejun Heo /* 1471616db877STejun Heo * If the current worker is concurrency managed and hogged the CPU for 1472616db877STejun Heo * longer than wq_cpu_intensive_thresh_us, it's automatically marked 1473616db877STejun Heo * CPU_INTENSIVE to avoid stalling other concurrency-managed work items. 1474c8f6219bSZqiang * 1475c8f6219bSZqiang * Set @worker->sleeping means that @worker is in the process of 1476c8f6219bSZqiang * switching out voluntarily and won't be contributing to 1477c8f6219bSZqiang * @pool->nr_running until it wakes up. As wq_worker_sleeping() also 1478c8f6219bSZqiang * decrements ->nr_running, setting CPU_INTENSIVE here can lead to 1479c8f6219bSZqiang * double decrements. The task is releasing the CPU anyway. Let's skip. 1480c8f6219bSZqiang * We probably want to make this prettier in the future. 1481616db877STejun Heo */ 1482c8f6219bSZqiang if ((worker->flags & WORKER_NOT_RUNNING) || READ_ONCE(worker->sleeping) || 1483616db877STejun Heo worker->task->se.sum_exec_runtime - worker->current_at < 1484616db877STejun Heo wq_cpu_intensive_thresh_us * NSEC_PER_USEC) 1485616db877STejun Heo return; 1486616db877STejun Heo 1487616db877STejun Heo raw_spin_lock(&pool->lock); 1488616db877STejun Heo 1489616db877STejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE); 149063638450STejun Heo wq_cpu_intensive_report(worker->current_func); 1491616db877STejun Heo pwq->stats[PWQ_STAT_CPU_INTENSIVE]++; 1492616db877STejun Heo 14930219a352STejun Heo if (kick_pool(pool)) 1494616db877STejun Heo pwq->stats[PWQ_STAT_CM_WAKEUP]++; 1495616db877STejun Heo 1496616db877STejun Heo raw_spin_unlock(&pool->lock); 1497616db877STejun Heo } 1498616db877STejun Heo 1499616db877STejun Heo /** 15001da177e4SLinus Torvalds * wq_worker_last_func - retrieve worker's last work function 15011da177e4SLinus Torvalds * @task: Task to retrieve last work function of. 15021da177e4SLinus Torvalds * 15031da177e4SLinus Torvalds * Determine the last function a worker executed. This is called from 15041da177e4SLinus Torvalds * the scheduler to get a worker's last known identity. 15051da177e4SLinus Torvalds * 15069b41ea72SAndrew Morton * CONTEXT: 15071da177e4SLinus Torvalds * raw_spin_lock_irq(rq->lock) 15081da177e4SLinus Torvalds * 1509f756d5e2SNathan Lynch * This function is called during schedule() when a kworker is going 1510f756d5e2SNathan Lynch * to sleep. It's used by psi to identify aggregation workers during 15111da177e4SLinus Torvalds * dequeuing, to allow periodic aggregation to shut-off when that 15121da177e4SLinus Torvalds * worker is the last task in the system or cgroup to go to sleep. 15131da177e4SLinus Torvalds * 15141da177e4SLinus Torvalds * As this function doesn't involve any workqueue-related locking, it 15151da177e4SLinus Torvalds * only returns stable values when called from inside the scheduler's 15161da177e4SLinus Torvalds * queuing and dequeuing paths, when @task, which must be a kworker, 1517365970a1SDavid Howells * is guaranteed to not be processing any works. 1518365970a1SDavid Howells * 1519365970a1SDavid Howells * Return: 1520365970a1SDavid Howells * The last work function %current executed as a worker, NULL if it 1521365970a1SDavid Howells * hasn't executed any work yet. 1522365970a1SDavid Howells */ 1523365970a1SDavid Howells work_func_t wq_worker_last_func(struct task_struct *task) 1524365970a1SDavid Howells { 1525365970a1SDavid Howells struct worker *worker = kthread_data(task); 1526365970a1SDavid Howells 1527365970a1SDavid Howells return worker->last_func; 1528365970a1SDavid Howells } 1529365970a1SDavid Howells 1530d302f017STejun Heo /** 153191ccc6e7STejun Heo * wq_node_nr_active - Determine wq_node_nr_active to use 153291ccc6e7STejun Heo * @wq: workqueue of interest 153391ccc6e7STejun Heo * @node: NUMA node, can be %NUMA_NO_NODE 153491ccc6e7STejun Heo * 153591ccc6e7STejun Heo * Determine wq_node_nr_active to use for @wq on @node. Returns: 153691ccc6e7STejun Heo * 153791ccc6e7STejun Heo * - %NULL for per-cpu workqueues as they don't need to use shared nr_active. 153891ccc6e7STejun Heo * 153991ccc6e7STejun Heo * - node_nr_active[nr_node_ids] if @node is %NUMA_NO_NODE. 154091ccc6e7STejun Heo * 154191ccc6e7STejun Heo * - Otherwise, node_nr_active[@node]. 154291ccc6e7STejun Heo */ 154391ccc6e7STejun Heo static struct wq_node_nr_active *wq_node_nr_active(struct workqueue_struct *wq, 154491ccc6e7STejun Heo int node) 154591ccc6e7STejun Heo { 154691ccc6e7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 154791ccc6e7STejun Heo return NULL; 154891ccc6e7STejun Heo 154991ccc6e7STejun Heo if (node == NUMA_NO_NODE) 155091ccc6e7STejun Heo node = nr_node_ids; 155191ccc6e7STejun Heo 155291ccc6e7STejun Heo return wq->node_nr_active[node]; 155391ccc6e7STejun Heo } 155491ccc6e7STejun Heo 155591ccc6e7STejun Heo /** 15565797b1c1STejun Heo * wq_update_node_max_active - Update per-node max_actives to use 15575797b1c1STejun Heo * @wq: workqueue to update 15585797b1c1STejun Heo * @off_cpu: CPU that's going down, -1 if a CPU is not going down 15595797b1c1STejun Heo * 15605797b1c1STejun Heo * Update @wq->node_nr_active[]->max. @wq must be unbound. max_active is 15615797b1c1STejun Heo * distributed among nodes according to the proportions of numbers of online 15625797b1c1STejun Heo * cpus. The result is always between @wq->min_active and max_active. 15635797b1c1STejun Heo */ 15645797b1c1STejun Heo static void wq_update_node_max_active(struct workqueue_struct *wq, int off_cpu) 15655797b1c1STejun Heo { 15665797b1c1STejun Heo struct cpumask *effective = unbound_effective_cpumask(wq); 15675797b1c1STejun Heo int min_active = READ_ONCE(wq->min_active); 15685797b1c1STejun Heo int max_active = READ_ONCE(wq->max_active); 15695797b1c1STejun Heo int total_cpus, node; 15705797b1c1STejun Heo 15715797b1c1STejun Heo lockdep_assert_held(&wq->mutex); 15725797b1c1STejun Heo 1573c5f8cd6cSTejun Heo if (!wq_topo_initialized) 1574c5f8cd6cSTejun Heo return; 1575c5f8cd6cSTejun Heo 157615930da4STejun Heo if (off_cpu >= 0 && !cpumask_test_cpu(off_cpu, effective)) 15775797b1c1STejun Heo off_cpu = -1; 15785797b1c1STejun Heo 15795797b1c1STejun Heo total_cpus = cpumask_weight_and(effective, cpu_online_mask); 15805797b1c1STejun Heo if (off_cpu >= 0) 15815797b1c1STejun Heo total_cpus--; 15825797b1c1STejun Heo 15835797b1c1STejun Heo for_each_node(node) { 15845797b1c1STejun Heo int node_cpus; 15855797b1c1STejun Heo 15865797b1c1STejun Heo node_cpus = cpumask_weight_and(effective, cpumask_of_node(node)); 15875797b1c1STejun Heo if (off_cpu >= 0 && cpu_to_node(off_cpu) == node) 15885797b1c1STejun Heo node_cpus--; 15895797b1c1STejun Heo 15905797b1c1STejun Heo wq_node_nr_active(wq, node)->max = 15915797b1c1STejun Heo clamp(DIV_ROUND_UP(max_active * node_cpus, total_cpus), 15925797b1c1STejun Heo min_active, max_active); 15935797b1c1STejun Heo } 15945797b1c1STejun Heo 15955797b1c1STejun Heo wq_node_nr_active(wq, NUMA_NO_NODE)->max = min_active; 15965797b1c1STejun Heo } 15975797b1c1STejun Heo 15985797b1c1STejun Heo /** 15998864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 16008864b4e5STejun Heo * @pwq: pool_workqueue to get 16018864b4e5STejun Heo * 16028864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 16038864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 16048864b4e5STejun Heo */ 16058864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 16068864b4e5STejun Heo { 16078864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 16088864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 16098864b4e5STejun Heo pwq->refcnt++; 16108864b4e5STejun Heo } 16118864b4e5STejun Heo 16128864b4e5STejun Heo /** 16138864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 16148864b4e5STejun Heo * @pwq: pool_workqueue to put 16158864b4e5STejun Heo * 16168864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 16178864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 16188864b4e5STejun Heo */ 16198864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 16208864b4e5STejun Heo { 16218864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 16228864b4e5STejun Heo if (likely(--pwq->refcnt)) 16238864b4e5STejun Heo return; 16248864b4e5STejun Heo /* 1625967b494eSTejun Heo * @pwq can't be released under pool->lock, bounce to a dedicated 1626967b494eSTejun Heo * kthread_worker to avoid A-A deadlocks. 16278864b4e5STejun Heo */ 1628687a9aa5STejun Heo kthread_queue_work(pwq_release_worker, &pwq->release_work); 16298864b4e5STejun Heo } 16308864b4e5STejun Heo 1631dce90d47STejun Heo /** 1632dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1633dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1634dce90d47STejun Heo * 1635dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1636dce90d47STejun Heo */ 1637dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1638dce90d47STejun Heo { 1639dce90d47STejun Heo if (pwq) { 1640dce90d47STejun Heo /* 164124acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the 1642dce90d47STejun Heo * following lock operations are safe. 1643dce90d47STejun Heo */ 1644a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 1645dce90d47STejun Heo put_pwq(pwq); 1646a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 1647dce90d47STejun Heo } 1648dce90d47STejun Heo } 1649dce90d47STejun Heo 1650afa87ce8STejun Heo static bool pwq_is_empty(struct pool_workqueue *pwq) 1651afa87ce8STejun Heo { 1652afa87ce8STejun Heo return !pwq->nr_active && list_empty(&pwq->inactive_works); 1653afa87ce8STejun Heo } 1654afa87ce8STejun Heo 16554c638030STejun Heo static void __pwq_activate_work(struct pool_workqueue *pwq, 16564c638030STejun Heo struct work_struct *work) 1657bf4ede01STejun Heo { 16581c270b79STejun Heo unsigned long *wdb = work_data_bits(work); 16591c270b79STejun Heo 16601c270b79STejun Heo WARN_ON_ONCE(!(*wdb & WORK_STRUCT_INACTIVE)); 1661bf4ede01STejun Heo trace_workqueue_activate_work(work); 166282607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 166382607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1664112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 16651c270b79STejun Heo __clear_bit(WORK_STRUCT_INACTIVE_BIT, wdb); 16664c638030STejun Heo } 16674c638030STejun Heo 16684c638030STejun Heo /** 16694c638030STejun Heo * pwq_activate_work - Activate a work item if inactive 16704c638030STejun Heo * @pwq: pool_workqueue @work belongs to 16714c638030STejun Heo * @work: work item to activate 16724c638030STejun Heo * 16734c638030STejun Heo * Returns %true if activated. %false if already active. 16744c638030STejun Heo */ 16754c638030STejun Heo static bool pwq_activate_work(struct pool_workqueue *pwq, 16764c638030STejun Heo struct work_struct *work) 16774c638030STejun Heo { 16784c638030STejun Heo struct worker_pool *pool = pwq->pool; 167991ccc6e7STejun Heo struct wq_node_nr_active *nna; 16804c638030STejun Heo 16814c638030STejun Heo lockdep_assert_held(&pool->lock); 16824c638030STejun Heo 16834c638030STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_INACTIVE)) 16844c638030STejun Heo return false; 16854c638030STejun Heo 168691ccc6e7STejun Heo nna = wq_node_nr_active(pwq->wq, pool->node); 168791ccc6e7STejun Heo if (nna) 168891ccc6e7STejun Heo atomic_inc(&nna->nr); 168991ccc6e7STejun Heo 1690112202d9STejun Heo pwq->nr_active++; 16914c638030STejun Heo __pwq_activate_work(pwq, work); 16924c638030STejun Heo return true; 1693bf4ede01STejun Heo } 1694bf4ede01STejun Heo 16955797b1c1STejun Heo static bool tryinc_node_nr_active(struct wq_node_nr_active *nna) 16965797b1c1STejun Heo { 16975797b1c1STejun Heo int max = READ_ONCE(nna->max); 16985797b1c1STejun Heo 16995797b1c1STejun Heo while (true) { 17005797b1c1STejun Heo int old, tmp; 17015797b1c1STejun Heo 17025797b1c1STejun Heo old = atomic_read(&nna->nr); 17035797b1c1STejun Heo if (old >= max) 17045797b1c1STejun Heo return false; 17055797b1c1STejun Heo tmp = atomic_cmpxchg_relaxed(&nna->nr, old, old + 1); 17065797b1c1STejun Heo if (tmp == old) 17075797b1c1STejun Heo return true; 17085797b1c1STejun Heo } 17095797b1c1STejun Heo } 17105797b1c1STejun Heo 17111c270b79STejun Heo /** 17121c270b79STejun Heo * pwq_tryinc_nr_active - Try to increment nr_active for a pwq 17131c270b79STejun Heo * @pwq: pool_workqueue of interest 17145797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level 17151c270b79STejun Heo * 17161c270b79STejun Heo * Try to increment nr_active for @pwq. Returns %true if an nr_active count is 17171c270b79STejun Heo * successfully obtained. %false otherwise. 17181c270b79STejun Heo */ 17195797b1c1STejun Heo static bool pwq_tryinc_nr_active(struct pool_workqueue *pwq, bool fill) 17203aa62497SLai Jiangshan { 17211c270b79STejun Heo struct workqueue_struct *wq = pwq->wq; 17221c270b79STejun Heo struct worker_pool *pool = pwq->pool; 172391ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(wq, pool->node); 17245797b1c1STejun Heo bool obtained = false; 17251c270b79STejun Heo 17261c270b79STejun Heo lockdep_assert_held(&pool->lock); 17271c270b79STejun Heo 17285797b1c1STejun Heo if (!nna) { 17294cb1ef64STejun Heo /* BH or per-cpu workqueue, pwq->nr_active is sufficient */ 17301c270b79STejun Heo obtained = pwq->nr_active < READ_ONCE(wq->max_active); 17315797b1c1STejun Heo goto out; 173291ccc6e7STejun Heo } 17335797b1c1STejun Heo 17344c065dbcSWaiman Long if (unlikely(pwq->plugged)) 17354c065dbcSWaiman Long return false; 17364c065dbcSWaiman Long 17375797b1c1STejun Heo /* 17385797b1c1STejun Heo * Unbound workqueue uses per-node shared nr_active $nna. If @pwq is 17395797b1c1STejun Heo * already waiting on $nna, pwq_dec_nr_active() will maintain the 17405797b1c1STejun Heo * concurrency level. Don't jump the line. 17415797b1c1STejun Heo * 17425797b1c1STejun Heo * We need to ignore the pending test after max_active has increased as 17435797b1c1STejun Heo * pwq_dec_nr_active() can only maintain the concurrency level but not 17445797b1c1STejun Heo * increase it. This is indicated by @fill. 17455797b1c1STejun Heo */ 17465797b1c1STejun Heo if (!list_empty(&pwq->pending_node) && likely(!fill)) 17475797b1c1STejun Heo goto out; 17485797b1c1STejun Heo 17495797b1c1STejun Heo obtained = tryinc_node_nr_active(nna); 17505797b1c1STejun Heo if (obtained) 17515797b1c1STejun Heo goto out; 17525797b1c1STejun Heo 17535797b1c1STejun Heo /* 17545797b1c1STejun Heo * Lockless acquisition failed. Lock, add ourself to $nna->pending_pwqs 17555797b1c1STejun Heo * and try again. The smp_mb() is paired with the implied memory barrier 17565797b1c1STejun Heo * of atomic_dec_return() in pwq_dec_nr_active() to ensure that either 17575797b1c1STejun Heo * we see the decremented $nna->nr or they see non-empty 17585797b1c1STejun Heo * $nna->pending_pwqs. 17595797b1c1STejun Heo */ 17605797b1c1STejun Heo raw_spin_lock(&nna->lock); 17615797b1c1STejun Heo 17625797b1c1STejun Heo if (list_empty(&pwq->pending_node)) 17635797b1c1STejun Heo list_add_tail(&pwq->pending_node, &nna->pending_pwqs); 17645797b1c1STejun Heo else if (likely(!fill)) 17655797b1c1STejun Heo goto out_unlock; 17665797b1c1STejun Heo 17675797b1c1STejun Heo smp_mb(); 17685797b1c1STejun Heo 17695797b1c1STejun Heo obtained = tryinc_node_nr_active(nna); 17705797b1c1STejun Heo 17715797b1c1STejun Heo /* 17725797b1c1STejun Heo * If @fill, @pwq might have already been pending. Being spuriously 17735797b1c1STejun Heo * pending in cold paths doesn't affect anything. Let's leave it be. 17745797b1c1STejun Heo */ 17755797b1c1STejun Heo if (obtained && likely(!fill)) 17765797b1c1STejun Heo list_del_init(&pwq->pending_node); 17775797b1c1STejun Heo 17785797b1c1STejun Heo out_unlock: 17795797b1c1STejun Heo raw_spin_unlock(&nna->lock); 17805797b1c1STejun Heo out: 17815797b1c1STejun Heo if (obtained) 17825797b1c1STejun Heo pwq->nr_active++; 17831c270b79STejun Heo return obtained; 17841c270b79STejun Heo } 17851c270b79STejun Heo 17861c270b79STejun Heo /** 17871c270b79STejun Heo * pwq_activate_first_inactive - Activate the first inactive work item on a pwq 17881c270b79STejun Heo * @pwq: pool_workqueue of interest 17895797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level 17901c270b79STejun Heo * 17911c270b79STejun Heo * Activate the first inactive work item of @pwq if available and allowed by 17921c270b79STejun Heo * max_active limit. 17931c270b79STejun Heo * 17941c270b79STejun Heo * Returns %true if an inactive work item has been activated. %false if no 17951c270b79STejun Heo * inactive work item is found or max_active limit is reached. 17961c270b79STejun Heo */ 17975797b1c1STejun Heo static bool pwq_activate_first_inactive(struct pool_workqueue *pwq, bool fill) 17981c270b79STejun Heo { 17991c270b79STejun Heo struct work_struct *work = 18001c270b79STejun Heo list_first_entry_or_null(&pwq->inactive_works, 18013aa62497SLai Jiangshan struct work_struct, entry); 18023aa62497SLai Jiangshan 18035797b1c1STejun Heo if (work && pwq_tryinc_nr_active(pwq, fill)) { 18041c270b79STejun Heo __pwq_activate_work(pwq, work); 18051c270b79STejun Heo return true; 18061c270b79STejun Heo } else { 18071c270b79STejun Heo return false; 18081c270b79STejun Heo } 18091c270b79STejun Heo } 18101c270b79STejun Heo 18111c270b79STejun Heo /** 1812516d3dc9SWaiman Long * unplug_oldest_pwq - unplug the oldest pool_workqueue 1813516d3dc9SWaiman Long * @wq: workqueue_struct where its oldest pwq is to be unplugged 18144c065dbcSWaiman Long * 1815516d3dc9SWaiman Long * This function should only be called for ordered workqueues where only the 1816516d3dc9SWaiman Long * oldest pwq is unplugged, the others are plugged to suspend execution to 1817516d3dc9SWaiman Long * ensure proper work item ordering:: 18184c065dbcSWaiman Long * 18194c065dbcSWaiman Long * dfl_pwq --------------+ [P] - plugged 18204c065dbcSWaiman Long * | 18214c065dbcSWaiman Long * v 18224c065dbcSWaiman Long * pwqs -> A -> B [P] -> C [P] (newest) 18234c065dbcSWaiman Long * | | | 18244c065dbcSWaiman Long * 1 3 5 18254c065dbcSWaiman Long * | | | 18264c065dbcSWaiman Long * 2 4 6 1827516d3dc9SWaiman Long * 1828516d3dc9SWaiman Long * When the oldest pwq is drained and removed, this function should be called 1829516d3dc9SWaiman Long * to unplug the next oldest one to start its work item execution. Note that 1830516d3dc9SWaiman Long * pwq's are linked into wq->pwqs with the oldest first, so the first one in 1831516d3dc9SWaiman Long * the list is the oldest. 18324c065dbcSWaiman Long */ 18334c065dbcSWaiman Long static void unplug_oldest_pwq(struct workqueue_struct *wq) 18344c065dbcSWaiman Long { 18354c065dbcSWaiman Long struct pool_workqueue *pwq; 18364c065dbcSWaiman Long 18374c065dbcSWaiman Long lockdep_assert_held(&wq->mutex); 18384c065dbcSWaiman Long 18394c065dbcSWaiman Long /* Caller should make sure that pwqs isn't empty before calling */ 18404c065dbcSWaiman Long pwq = list_first_entry_or_null(&wq->pwqs, struct pool_workqueue, 18414c065dbcSWaiman Long pwqs_node); 18424c065dbcSWaiman Long raw_spin_lock_irq(&pwq->pool->lock); 18434c065dbcSWaiman Long if (pwq->plugged) { 18444c065dbcSWaiman Long pwq->plugged = false; 18454c065dbcSWaiman Long if (pwq_activate_first_inactive(pwq, true)) 18464c065dbcSWaiman Long kick_pool(pwq->pool); 18474c065dbcSWaiman Long } 18484c065dbcSWaiman Long raw_spin_unlock_irq(&pwq->pool->lock); 18494c065dbcSWaiman Long } 18504c065dbcSWaiman Long 18514c065dbcSWaiman Long /** 18525797b1c1STejun Heo * node_activate_pending_pwq - Activate a pending pwq on a wq_node_nr_active 18535797b1c1STejun Heo * @nna: wq_node_nr_active to activate a pending pwq for 18545797b1c1STejun Heo * @caller_pool: worker_pool the caller is locking 18555797b1c1STejun Heo * 18565797b1c1STejun Heo * Activate a pwq in @nna->pending_pwqs. Called with @caller_pool locked. 18575797b1c1STejun Heo * @caller_pool may be unlocked and relocked to lock other worker_pools. 18585797b1c1STejun Heo */ 18595797b1c1STejun Heo static void node_activate_pending_pwq(struct wq_node_nr_active *nna, 18605797b1c1STejun Heo struct worker_pool *caller_pool) 18615797b1c1STejun Heo { 18625797b1c1STejun Heo struct worker_pool *locked_pool = caller_pool; 18635797b1c1STejun Heo struct pool_workqueue *pwq; 18645797b1c1STejun Heo struct work_struct *work; 18655797b1c1STejun Heo 18665797b1c1STejun Heo lockdep_assert_held(&caller_pool->lock); 18675797b1c1STejun Heo 18685797b1c1STejun Heo raw_spin_lock(&nna->lock); 18695797b1c1STejun Heo retry: 18705797b1c1STejun Heo pwq = list_first_entry_or_null(&nna->pending_pwqs, 18715797b1c1STejun Heo struct pool_workqueue, pending_node); 18725797b1c1STejun Heo if (!pwq) 18735797b1c1STejun Heo goto out_unlock; 18745797b1c1STejun Heo 18755797b1c1STejun Heo /* 18765797b1c1STejun Heo * If @pwq is for a different pool than @locked_pool, we need to lock 18775797b1c1STejun Heo * @pwq->pool->lock. Let's trylock first. If unsuccessful, do the unlock 18785797b1c1STejun Heo * / lock dance. For that, we also need to release @nna->lock as it's 18795797b1c1STejun Heo * nested inside pool locks. 18805797b1c1STejun Heo */ 18815797b1c1STejun Heo if (pwq->pool != locked_pool) { 18825797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock); 18835797b1c1STejun Heo locked_pool = pwq->pool; 18845797b1c1STejun Heo if (!raw_spin_trylock(&locked_pool->lock)) { 18855797b1c1STejun Heo raw_spin_unlock(&nna->lock); 18865797b1c1STejun Heo raw_spin_lock(&locked_pool->lock); 18875797b1c1STejun Heo raw_spin_lock(&nna->lock); 18885797b1c1STejun Heo goto retry; 18895797b1c1STejun Heo } 18905797b1c1STejun Heo } 18915797b1c1STejun Heo 18925797b1c1STejun Heo /* 18935797b1c1STejun Heo * $pwq may not have any inactive work items due to e.g. cancellations. 18945797b1c1STejun Heo * Drop it from pending_pwqs and see if there's another one. 18955797b1c1STejun Heo */ 18965797b1c1STejun Heo work = list_first_entry_or_null(&pwq->inactive_works, 18975797b1c1STejun Heo struct work_struct, entry); 18985797b1c1STejun Heo if (!work) { 18995797b1c1STejun Heo list_del_init(&pwq->pending_node); 19005797b1c1STejun Heo goto retry; 19015797b1c1STejun Heo } 19025797b1c1STejun Heo 19035797b1c1STejun Heo /* 19045797b1c1STejun Heo * Acquire an nr_active count and activate the inactive work item. If 19055797b1c1STejun Heo * $pwq still has inactive work items, rotate it to the end of the 19065797b1c1STejun Heo * pending_pwqs so that we round-robin through them. This means that 19075797b1c1STejun Heo * inactive work items are not activated in queueing order which is fine 19085797b1c1STejun Heo * given that there has never been any ordering across different pwqs. 19095797b1c1STejun Heo */ 19105797b1c1STejun Heo if (likely(tryinc_node_nr_active(nna))) { 19115797b1c1STejun Heo pwq->nr_active++; 19125797b1c1STejun Heo __pwq_activate_work(pwq, work); 19135797b1c1STejun Heo 19145797b1c1STejun Heo if (list_empty(&pwq->inactive_works)) 19155797b1c1STejun Heo list_del_init(&pwq->pending_node); 19165797b1c1STejun Heo else 19175797b1c1STejun Heo list_move_tail(&pwq->pending_node, &nna->pending_pwqs); 19185797b1c1STejun Heo 19195797b1c1STejun Heo /* if activating a foreign pool, make sure it's running */ 19205797b1c1STejun Heo if (pwq->pool != caller_pool) 19215797b1c1STejun Heo kick_pool(pwq->pool); 19225797b1c1STejun Heo } 19235797b1c1STejun Heo 19245797b1c1STejun Heo out_unlock: 19255797b1c1STejun Heo raw_spin_unlock(&nna->lock); 19265797b1c1STejun Heo if (locked_pool != caller_pool) { 19275797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock); 19285797b1c1STejun Heo raw_spin_lock(&caller_pool->lock); 19295797b1c1STejun Heo } 19305797b1c1STejun Heo } 19315797b1c1STejun Heo 19325797b1c1STejun Heo /** 19331c270b79STejun Heo * pwq_dec_nr_active - Retire an active count 19341c270b79STejun Heo * @pwq: pool_workqueue of interest 19351c270b79STejun Heo * 19361c270b79STejun Heo * Decrement @pwq's nr_active and try to activate the first inactive work item. 19375797b1c1STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock. 19381c270b79STejun Heo */ 19391c270b79STejun Heo static void pwq_dec_nr_active(struct pool_workqueue *pwq) 19401c270b79STejun Heo { 19411c270b79STejun Heo struct worker_pool *pool = pwq->pool; 194291ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(pwq->wq, pool->node); 19431c270b79STejun Heo 19441c270b79STejun Heo lockdep_assert_held(&pool->lock); 19451c270b79STejun Heo 194691ccc6e7STejun Heo /* 194791ccc6e7STejun Heo * @pwq->nr_active should be decremented for both percpu and unbound 194891ccc6e7STejun Heo * workqueues. 194991ccc6e7STejun Heo */ 19501c270b79STejun Heo pwq->nr_active--; 195191ccc6e7STejun Heo 195291ccc6e7STejun Heo /* 195391ccc6e7STejun Heo * For a percpu workqueue, it's simple. Just need to kick the first 195491ccc6e7STejun Heo * inactive work item on @pwq itself. 195591ccc6e7STejun Heo */ 195691ccc6e7STejun Heo if (!nna) { 19575797b1c1STejun Heo pwq_activate_first_inactive(pwq, false); 195891ccc6e7STejun Heo return; 195991ccc6e7STejun Heo } 196091ccc6e7STejun Heo 19615797b1c1STejun Heo /* 19625797b1c1STejun Heo * If @pwq is for an unbound workqueue, it's more complicated because 19635797b1c1STejun Heo * multiple pwqs and pools may be sharing the nr_active count. When a 19645797b1c1STejun Heo * pwq needs to wait for an nr_active count, it puts itself on 19655797b1c1STejun Heo * $nna->pending_pwqs. The following atomic_dec_return()'s implied 19665797b1c1STejun Heo * memory barrier is paired with smp_mb() in pwq_tryinc_nr_active() to 19675797b1c1STejun Heo * guarantee that either we see non-empty pending_pwqs or they see 19685797b1c1STejun Heo * decremented $nna->nr. 19695797b1c1STejun Heo * 19705797b1c1STejun Heo * $nna->max may change as CPUs come online/offline and @pwq->wq's 19715797b1c1STejun Heo * max_active gets updated. However, it is guaranteed to be equal to or 19725797b1c1STejun Heo * larger than @pwq->wq->min_active which is above zero unless freezing. 19735797b1c1STejun Heo * This maintains the forward progress guarantee. 19745797b1c1STejun Heo */ 19755797b1c1STejun Heo if (atomic_dec_return(&nna->nr) >= READ_ONCE(nna->max)) 19765797b1c1STejun Heo return; 19775797b1c1STejun Heo 19785797b1c1STejun Heo if (!list_empty(&nna->pending_pwqs)) 19795797b1c1STejun Heo node_activate_pending_pwq(nna, pool); 19803aa62497SLai Jiangshan } 19813aa62497SLai Jiangshan 1982bf4ede01STejun Heo /** 1983112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1984112202d9STejun Heo * @pwq: pwq of interest 1985c4560c2cSLai Jiangshan * @work_data: work_data of work which left the queue 1986bf4ede01STejun Heo * 1987bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1988112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1989bf4ede01STejun Heo * 1990dd6c3c54STejun Heo * NOTE: 1991dd6c3c54STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock 1992dd6c3c54STejun Heo * and thus should be called after all other state updates for the in-flight 1993dd6c3c54STejun Heo * work item is complete. 1994dd6c3c54STejun Heo * 1995bf4ede01STejun Heo * CONTEXT: 1996a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1997bf4ede01STejun Heo */ 1998c4560c2cSLai Jiangshan static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) 1999bf4ede01STejun Heo { 2000c4560c2cSLai Jiangshan int color = get_work_color(work_data); 2001c4560c2cSLai Jiangshan 20021c270b79STejun Heo if (!(work_data & WORK_STRUCT_INACTIVE)) 20031c270b79STejun Heo pwq_dec_nr_active(pwq); 2004018f3a13SLai Jiangshan 2005018f3a13SLai Jiangshan pwq->nr_in_flight[color]--; 2006bf4ede01STejun Heo 2007bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 2008112202d9STejun Heo if (likely(pwq->flush_color != color)) 20098864b4e5STejun Heo goto out_put; 2010bf4ede01STejun Heo 2011bf4ede01STejun Heo /* are there still in-flight works? */ 2012112202d9STejun Heo if (pwq->nr_in_flight[color]) 20138864b4e5STejun Heo goto out_put; 2014bf4ede01STejun Heo 2015112202d9STejun Heo /* this pwq is done, clear flush_color */ 2016112202d9STejun Heo pwq->flush_color = -1; 2017bf4ede01STejun Heo 2018bf4ede01STejun Heo /* 2019112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 2020bf4ede01STejun Heo * will handle the rest. 2021bf4ede01STejun Heo */ 2022112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 2023112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 20248864b4e5STejun Heo out_put: 20258864b4e5STejun Heo put_pwq(pwq); 2026bf4ede01STejun Heo } 2027bf4ede01STejun Heo 202836e227d2STejun Heo /** 2029bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 203036e227d2STejun Heo * @work: work item to steal 2031c5f5b942STejun Heo * @cflags: %WORK_CANCEL_ flags 2032c26e2f2eSTejun Heo * @irq_flags: place to store irq state 203336e227d2STejun Heo * 203436e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 2035d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 203636e227d2STejun Heo * 2037d185af30SYacine Belkadi * Return: 20383eb6b31bSMauro Carvalho Chehab * 20393eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 204036e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 204136e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 204236e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 20433eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 204436e227d2STejun Heo * 2045d185af30SYacine Belkadi * Note: 2046bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 2047e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 2048e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 2049e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 2050bbb68dfaSTejun Heo * 2051bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 2052c26e2f2eSTejun Heo * responsible for releasing it using local_irq_restore(*@irq_flags). 2053bbb68dfaSTejun Heo * 2054e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 2055bf4ede01STejun Heo */ 2056c5f5b942STejun Heo static int try_to_grab_pending(struct work_struct *work, u32 cflags, 2057c26e2f2eSTejun Heo unsigned long *irq_flags) 2058bf4ede01STejun Heo { 2059d565ed63STejun Heo struct worker_pool *pool; 2060112202d9STejun Heo struct pool_workqueue *pwq; 2061bf4ede01STejun Heo 2062c26e2f2eSTejun Heo local_irq_save(*irq_flags); 2063bbb68dfaSTejun Heo 206436e227d2STejun Heo /* try to steal the timer if it exists */ 2065c5f5b942STejun Heo if (cflags & WORK_CANCEL_DELAYED) { 206636e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 206736e227d2STejun Heo 2068e0aecdd8STejun Heo /* 2069e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 2070e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 2071e0aecdd8STejun Heo * running on the local CPU. 2072e0aecdd8STejun Heo */ 207336e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 207436e227d2STejun Heo return 1; 207536e227d2STejun Heo } 207636e227d2STejun Heo 207736e227d2STejun Heo /* try to claim PENDING the normal way */ 2078bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 2079bf4ede01STejun Heo return 0; 2080bf4ede01STejun Heo 208124acfb71SThomas Gleixner rcu_read_lock(); 2082bf4ede01STejun Heo /* 2083bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 2084bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 2085bf4ede01STejun Heo */ 2086d565ed63STejun Heo pool = get_work_pool(work); 2087d565ed63STejun Heo if (!pool) 2088bbb68dfaSTejun Heo goto fail; 2089bf4ede01STejun Heo 2090a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pool->lock); 2091bf4ede01STejun Heo /* 2092112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 2093112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 2094112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 2095112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 2096112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 20970b3dae68SLai Jiangshan * item is currently queued on that pool. 2098bf4ede01STejun Heo */ 2099112202d9STejun Heo pwq = get_work_pwq(work); 2100112202d9STejun Heo if (pwq && pwq->pool == pool) { 2101c70e1779STejun Heo unsigned long work_data; 2102c70e1779STejun Heo 2103bf4ede01STejun Heo debug_work_deactivate(work); 21043aa62497SLai Jiangshan 21053aa62497SLai Jiangshan /* 2106018f3a13SLai Jiangshan * A cancelable inactive work item must be in the 2107018f3a13SLai Jiangshan * pwq->inactive_works since a queued barrier can't be 2108018f3a13SLai Jiangshan * canceled (see the comments in insert_wq_barrier()). 2109018f3a13SLai Jiangshan * 2110f97a4a1aSLai Jiangshan * An inactive work item cannot be grabbed directly because 2111d812796eSLai Jiangshan * it might have linked barrier work items which, if left 2112f97a4a1aSLai Jiangshan * on the inactive_works list, will confuse pwq->nr_active 211316062836STejun Heo * management later on and cause stall. Make sure the work 211416062836STejun Heo * item is activated before grabbing. 21153aa62497SLai Jiangshan */ 21164c638030STejun Heo pwq_activate_work(pwq, work); 21173aa62497SLai Jiangshan 2118bf4ede01STejun Heo list_del_init(&work->entry); 211936e227d2STejun Heo 2120c70e1779STejun Heo /* 2121c70e1779STejun Heo * work->data points to pwq iff queued. Let's point to pool. As 2122c70e1779STejun Heo * this destroys work->data needed by the next step, stash it. 2123c70e1779STejun Heo */ 2124c70e1779STejun Heo work_data = *work_data_bits(work); 2125bccdc1faSTejun Heo set_work_pool_and_keep_pending(work, pool->id, 0); 21264468a00fSLai Jiangshan 2127dd6c3c54STejun Heo /* must be the last step, see the function comment */ 2128c70e1779STejun Heo pwq_dec_nr_in_flight(pwq, work_data); 2129dd6c3c54STejun Heo 2130a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 213124acfb71SThomas Gleixner rcu_read_unlock(); 213236e227d2STejun Heo return 1; 2133bf4ede01STejun Heo } 2134a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 2135bbb68dfaSTejun Heo fail: 213624acfb71SThomas Gleixner rcu_read_unlock(); 2137c26e2f2eSTejun Heo local_irq_restore(*irq_flags); 213836e227d2STejun Heo return -EAGAIN; 2139bf4ede01STejun Heo } 2140bf4ede01STejun Heo 2141978b8409STejun Heo /** 2142978b8409STejun Heo * work_grab_pending - steal work item from worklist and disable irq 2143978b8409STejun Heo * @work: work item to steal 2144978b8409STejun Heo * @cflags: %WORK_CANCEL_ flags 2145978b8409STejun Heo * @irq_flags: place to store IRQ state 2146978b8409STejun Heo * 2147978b8409STejun Heo * Grab PENDING bit of @work. @work can be in any stable state - idle, on timer 2148978b8409STejun Heo * or on worklist. 2149978b8409STejun Heo * 2150*f09b10b6STejun Heo * Can be called from any context. IRQ is disabled on return with IRQ state 2151978b8409STejun Heo * stored in *@irq_flags. The caller is responsible for re-enabling it using 2152978b8409STejun Heo * local_irq_restore(). 2153978b8409STejun Heo * 2154978b8409STejun Heo * Returns %true if @work was pending. %false if idle. 2155978b8409STejun Heo */ 2156978b8409STejun Heo static bool work_grab_pending(struct work_struct *work, u32 cflags, 2157978b8409STejun Heo unsigned long *irq_flags) 2158978b8409STejun Heo { 2159978b8409STejun Heo int ret; 2160978b8409STejun Heo 2161*f09b10b6STejun Heo while (true) { 2162978b8409STejun Heo ret = try_to_grab_pending(work, cflags, irq_flags); 2163*f09b10b6STejun Heo if (ret >= 0) 2164978b8409STejun Heo return ret; 2165*f09b10b6STejun Heo cpu_relax(); 2166*f09b10b6STejun Heo } 2167978b8409STejun Heo } 2168978b8409STejun Heo 2169bf4ede01STejun Heo /** 2170706026c2STejun Heo * insert_work - insert a work into a pool 2171112202d9STejun Heo * @pwq: pwq @work belongs to 21724690c4abSTejun Heo * @work: work to insert 21734690c4abSTejun Heo * @head: insertion point 21744690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 21754690c4abSTejun Heo * 2176112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 2177706026c2STejun Heo * work_struct flags. 21784690c4abSTejun Heo * 21794690c4abSTejun Heo * CONTEXT: 2180a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2181365970a1SDavid Howells */ 2182112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 2183112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 2184b89deed3SOleg Nesterov { 2185fe089f87STejun Heo debug_work_activate(work); 2186e1d8aa9fSFrederic Weisbecker 2187e89a85d6SWalter Wu /* record the work call stack in order to print it in KASAN reports */ 2188f70da745SMarco Elver kasan_record_aux_stack_noalloc(work); 2189e89a85d6SWalter Wu 21904690c4abSTejun Heo /* we own @work, set data and link */ 2191112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 21921a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 21938864b4e5STejun Heo get_pwq(pwq); 2194b89deed3SOleg Nesterov } 2195b89deed3SOleg Nesterov 2196c8efcc25STejun Heo /* 2197c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 21988d03ecfeSTejun Heo * same workqueue. 2199c8efcc25STejun Heo */ 2200c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 2201c8efcc25STejun Heo { 2202c8efcc25STejun Heo struct worker *worker; 2203c8efcc25STejun Heo 22048d03ecfeSTejun Heo worker = current_wq_worker(); 2205c8efcc25STejun Heo /* 2206bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If 22078d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 2208c8efcc25STejun Heo */ 2209112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 2210c8efcc25STejun Heo } 2211c8efcc25STejun Heo 2212ef557180SMike Galbraith /* 2213ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 2214ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 2215ef557180SMike Galbraith * avoid perturbing sensitive tasks. 2216ef557180SMike Galbraith */ 2217ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 2218ef557180SMike Galbraith { 2219ef557180SMike Galbraith int new_cpu; 2220ef557180SMike Galbraith 2221f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 2222ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 2223ef557180SMike Galbraith return cpu; 2224a8ec5880SAmmar Faizi } else { 2225a8ec5880SAmmar Faizi pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); 2226f303fccbSTejun Heo } 2227f303fccbSTejun Heo 2228ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 2229ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 2230ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 2231ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 2232ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 2233ef557180SMike Galbraith return cpu; 2234ef557180SMike Galbraith } 2235ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 2236ef557180SMike Galbraith 2237ef557180SMike Galbraith return new_cpu; 2238ef557180SMike Galbraith } 2239ef557180SMike Galbraith 2240d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 22411da177e4SLinus Torvalds struct work_struct *work) 22421da177e4SLinus Torvalds { 2243112202d9STejun Heo struct pool_workqueue *pwq; 2244fe089f87STejun Heo struct worker_pool *last_pool, *pool; 22458a2e8e5dSTejun Heo unsigned int work_flags; 2246b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 22478930cabaSTejun Heo 22488930cabaSTejun Heo /* 22498930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 22508930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 22518930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 22528930cabaSTejun Heo * happen with IRQ disabled. 22538930cabaSTejun Heo */ 22548e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 22551da177e4SLinus Torvalds 225633e3f0a3SRichard Clark /* 225733e3f0a3SRichard Clark * For a draining wq, only works from the same workqueue are 225833e3f0a3SRichard Clark * allowed. The __WQ_DESTROYING helps to spot the issue that 225933e3f0a3SRichard Clark * queues a new work item to a wq after destroy_workqueue(wq). 226033e3f0a3SRichard Clark */ 226133e3f0a3SRichard Clark if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && 226233e3f0a3SRichard Clark WARN_ON_ONCE(!is_chained_work(wq)))) 2263e41e704bSTejun Heo return; 226424acfb71SThomas Gleixner rcu_read_lock(); 22659e8cd2f5STejun Heo retry: 2266aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */ 2267636b927eSTejun Heo if (req_cpu == WORK_CPU_UNBOUND) { 2268636b927eSTejun Heo if (wq->flags & WQ_UNBOUND) 2269ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 2270636b927eSTejun Heo else 2271aa202f1fSHillf Danton cpu = raw_smp_processor_id(); 2272aa202f1fSHillf Danton } 2273f3421797STejun Heo 2274636b927eSTejun Heo pwq = rcu_dereference(*per_cpu_ptr(wq->cpu_pwq, cpu)); 2275fe089f87STejun Heo pool = pwq->pool; 2276fe089f87STejun Heo 227718aa9effSTejun Heo /* 2278c9178087STejun Heo * If @work was previously on a different pool, it might still be 2279c9178087STejun Heo * running there, in which case the work needs to be queued on that 2280c9178087STejun Heo * pool to guarantee non-reentrancy. 228118aa9effSTejun Heo */ 2282c9e7cf27STejun Heo last_pool = get_work_pool(work); 2283fe089f87STejun Heo if (last_pool && last_pool != pool) { 228418aa9effSTejun Heo struct worker *worker; 228518aa9effSTejun Heo 2286a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&last_pool->lock); 228718aa9effSTejun Heo 2288c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 228918aa9effSTejun Heo 2290112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 2291c9178087STejun Heo pwq = worker->current_pwq; 2292fe089f87STejun Heo pool = pwq->pool; 2293fe089f87STejun Heo WARN_ON_ONCE(pool != last_pool); 22948594fadeSLai Jiangshan } else { 229518aa9effSTejun Heo /* meh... not running there, queue here */ 2296a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&last_pool->lock); 2297fe089f87STejun Heo raw_spin_lock(&pool->lock); 229818aa9effSTejun Heo } 22998930cabaSTejun Heo } else { 2300fe089f87STejun Heo raw_spin_lock(&pool->lock); 23018930cabaSTejun Heo } 2302502ca9d8STejun Heo 23039e8cd2f5STejun Heo /* 2304636b927eSTejun Heo * pwq is determined and locked. For unbound pools, we could have raced 2305636b927eSTejun Heo * with pwq release and it could already be dead. If its refcnt is zero, 2306636b927eSTejun Heo * repeat pwq selection. Note that unbound pwqs never die without 2307636b927eSTejun Heo * another pwq replacing it in cpu_pwq or while work items are executing 2308636b927eSTejun Heo * on it, so the retrying is guaranteed to make forward-progress. 23099e8cd2f5STejun Heo */ 23109e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 23119e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 2312fe089f87STejun Heo raw_spin_unlock(&pool->lock); 23139e8cd2f5STejun Heo cpu_relax(); 23149e8cd2f5STejun Heo goto retry; 23159e8cd2f5STejun Heo } 23169e8cd2f5STejun Heo /* oops */ 23179e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 23189e8cd2f5STejun Heo wq->name, cpu); 23199e8cd2f5STejun Heo } 23209e8cd2f5STejun Heo 2321112202d9STejun Heo /* pwq determined, queue */ 2322112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 2323502ca9d8STejun Heo 232424acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry))) 232524acfb71SThomas Gleixner goto out; 23261e19ffc6STejun Heo 2327112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 2328112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 23291e19ffc6STejun Heo 2330a045a272STejun Heo /* 2331a045a272STejun Heo * Limit the number of concurrently active work items to max_active. 2332a045a272STejun Heo * @work must also queue behind existing inactive work items to maintain 2333a045a272STejun Heo * ordering when max_active changes. See wq_adjust_max_active(). 2334a045a272STejun Heo */ 23355797b1c1STejun Heo if (list_empty(&pwq->inactive_works) && pwq_tryinc_nr_active(pwq, false)) { 2336fe089f87STejun Heo if (list_empty(&pool->worklist)) 2337fe089f87STejun Heo pool->watchdog_ts = jiffies; 2338fe089f87STejun Heo 2339cdadf009STejun Heo trace_workqueue_activate_work(work); 2340fe089f87STejun Heo insert_work(pwq, work, &pool->worklist, work_flags); 23410219a352STejun Heo kick_pool(pool); 23428a2e8e5dSTejun Heo } else { 2343f97a4a1aSLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 2344fe089f87STejun Heo insert_work(pwq, work, &pwq->inactive_works, work_flags); 23458a2e8e5dSTejun Heo } 23461e19ffc6STejun Heo 234724acfb71SThomas Gleixner out: 2348fe089f87STejun Heo raw_spin_unlock(&pool->lock); 234924acfb71SThomas Gleixner rcu_read_unlock(); 23501da177e4SLinus Torvalds } 23511da177e4SLinus Torvalds 235286898fa6STejun Heo static bool clear_pending_if_disabled(struct work_struct *work) 235386898fa6STejun Heo { 235486898fa6STejun Heo unsigned long data = *work_data_bits(work); 235586898fa6STejun Heo struct work_offq_data offqd; 235686898fa6STejun Heo 235786898fa6STejun Heo if (likely((data & WORK_STRUCT_PWQ) || 235886898fa6STejun Heo !(data & WORK_OFFQ_DISABLE_MASK))) 235986898fa6STejun Heo return false; 236086898fa6STejun Heo 236186898fa6STejun Heo work_offqd_unpack(&offqd, data); 236286898fa6STejun Heo set_work_pool_and_clear_pending(work, offqd.pool_id, 236386898fa6STejun Heo work_offqd_pack_flags(&offqd)); 236486898fa6STejun Heo return true; 236586898fa6STejun Heo } 236686898fa6STejun Heo 23670fcb78c2SRolf Eike Beer /** 2368c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 2369c1a220e7SZhang Rui * @cpu: CPU number to execute work on 2370c1a220e7SZhang Rui * @wq: workqueue to use 2371c1a220e7SZhang Rui * @work: work to queue 2372c1a220e7SZhang Rui * 2373c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 2374443378f0SPaul E. McKenney * can't go away. Callers that fail to ensure that the specified 2375443378f0SPaul E. McKenney * CPU cannot go away will execute on a randomly chosen CPU. 2376854f5cc5SPaul E. McKenney * But note well that callers specifying a CPU that never has been 2377854f5cc5SPaul E. McKenney * online will get a splat. 2378d185af30SYacine Belkadi * 2379d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 2380c1a220e7SZhang Rui */ 2381d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 2382d4283e93STejun Heo struct work_struct *work) 2383c1a220e7SZhang Rui { 2384d4283e93STejun Heo bool ret = false; 2385c26e2f2eSTejun Heo unsigned long irq_flags; 23868930cabaSTejun Heo 2387c26e2f2eSTejun Heo local_irq_save(irq_flags); 2388c1a220e7SZhang Rui 238986898fa6STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) && 239086898fa6STejun Heo !clear_pending_if_disabled(work)) { 23914690c4abSTejun Heo __queue_work(cpu, wq, work); 2392d4283e93STejun Heo ret = true; 2393c1a220e7SZhang Rui } 23948930cabaSTejun Heo 2395c26e2f2eSTejun Heo local_irq_restore(irq_flags); 2396c1a220e7SZhang Rui return ret; 2397c1a220e7SZhang Rui } 2398ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 2399c1a220e7SZhang Rui 24008204e0c1SAlexander Duyck /** 2401fef59c9cSTejun Heo * select_numa_node_cpu - Select a CPU based on NUMA node 24028204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from 24038204e0c1SAlexander Duyck * 24048204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given 24058204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return 24068204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any 24078204e0c1SAlexander Duyck * available CPU if we need to schedule this work. 24088204e0c1SAlexander Duyck */ 2409fef59c9cSTejun Heo static int select_numa_node_cpu(int node) 24108204e0c1SAlexander Duyck { 24118204e0c1SAlexander Duyck int cpu; 24128204e0c1SAlexander Duyck 24138204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */ 24148204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) 24158204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 24168204e0c1SAlexander Duyck 24178204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */ 24188204e0c1SAlexander Duyck cpu = raw_smp_processor_id(); 24198204e0c1SAlexander Duyck if (node == cpu_to_node(cpu)) 24208204e0c1SAlexander Duyck return cpu; 24218204e0c1SAlexander Duyck 24228204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */ 24238204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 24248204e0c1SAlexander Duyck 24258204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */ 24268204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; 24278204e0c1SAlexander Duyck } 24288204e0c1SAlexander Duyck 24298204e0c1SAlexander Duyck /** 24308204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node 24318204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for 24328204e0c1SAlexander Duyck * @wq: workqueue to use 24338204e0c1SAlexander Duyck * @work: work to queue 24348204e0c1SAlexander Duyck * 24358204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic 24368204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given 24378204e0c1SAlexander Duyck * NUMA node. 24388204e0c1SAlexander Duyck * 24398204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto 24408204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is 24418204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior. 24428204e0c1SAlexander Duyck * 24438204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the 24448204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we 24458204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU. 24468204e0c1SAlexander Duyck * 24478204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise. 24488204e0c1SAlexander Duyck */ 24498204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq, 24508204e0c1SAlexander Duyck struct work_struct *work) 24518204e0c1SAlexander Duyck { 2452c26e2f2eSTejun Heo unsigned long irq_flags; 24538204e0c1SAlexander Duyck bool ret = false; 24548204e0c1SAlexander Duyck 24558204e0c1SAlexander Duyck /* 24568204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues. 24578204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given 24588204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node. 24598204e0c1SAlexander Duyck * 24608204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in 24618204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for 24628204e0c1SAlexander Duyck * some round robin type logic. 24638204e0c1SAlexander Duyck */ 24648204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 24658204e0c1SAlexander Duyck 2466c26e2f2eSTejun Heo local_irq_save(irq_flags); 24678204e0c1SAlexander Duyck 246886898fa6STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) && 246986898fa6STejun Heo !clear_pending_if_disabled(work)) { 2470fef59c9cSTejun Heo int cpu = select_numa_node_cpu(node); 24718204e0c1SAlexander Duyck 24728204e0c1SAlexander Duyck __queue_work(cpu, wq, work); 24738204e0c1SAlexander Duyck ret = true; 24748204e0c1SAlexander Duyck } 24758204e0c1SAlexander Duyck 2476c26e2f2eSTejun Heo local_irq_restore(irq_flags); 24778204e0c1SAlexander Duyck return ret; 24788204e0c1SAlexander Duyck } 24798204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node); 24808204e0c1SAlexander Duyck 24818c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 24821da177e4SLinus Torvalds { 24838c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 24841da177e4SLinus Torvalds 2485e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 248660c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 24871da177e4SLinus Torvalds } 24881438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 24891da177e4SLinus Torvalds 24907beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 249152bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 24921da177e4SLinus Torvalds { 24937beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 24947beb2edfSTejun Heo struct work_struct *work = &dwork->work; 24951da177e4SLinus Torvalds 2496637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 24974b243563SSami Tolvanen WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 2498fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 2499fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 25007beb2edfSTejun Heo 25018852aac2STejun Heo /* 25028852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 25038852aac2STejun Heo * both optimization and correctness. The earliest @timer can 25048852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 25058852aac2STejun Heo * on that there's no such delay when @delay is 0. 25068852aac2STejun Heo */ 25078852aac2STejun Heo if (!delay) { 25088852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 25098852aac2STejun Heo return; 25108852aac2STejun Heo } 25118852aac2STejun Heo 251260c057bcSLai Jiangshan dwork->wq = wq; 25131265057fSTejun Heo dwork->cpu = cpu; 25147beb2edfSTejun Heo timer->expires = jiffies + delay; 25157beb2edfSTejun Heo 2516aae17ebbSLeonardo Bras if (housekeeping_enabled(HK_TYPE_TIMER)) { 2517aae17ebbSLeonardo Bras /* If the current cpu is a housekeeping cpu, use it. */ 2518aae17ebbSLeonardo Bras cpu = smp_processor_id(); 2519aae17ebbSLeonardo Bras if (!housekeeping_test_cpu(cpu, HK_TYPE_TIMER)) 2520aae17ebbSLeonardo Bras cpu = housekeeping_any_cpu(HK_TYPE_TIMER); 25217beb2edfSTejun Heo add_timer_on(timer, cpu); 2522aae17ebbSLeonardo Bras } else { 2523aae17ebbSLeonardo Bras if (likely(cpu == WORK_CPU_UNBOUND)) 2524c0e8c5b5SAnna-Maria Behnsen add_timer_global(timer); 2525aae17ebbSLeonardo Bras else 2526aae17ebbSLeonardo Bras add_timer_on(timer, cpu); 2527aae17ebbSLeonardo Bras } 25287beb2edfSTejun Heo } 25291da177e4SLinus Torvalds 25300fcb78c2SRolf Eike Beer /** 25310fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 25320fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 25330fcb78c2SRolf Eike Beer * @wq: workqueue to use 2534af9997e4SRandy Dunlap * @dwork: work to queue 25350fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 25360fcb78c2SRolf Eike Beer * 2537d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 2538715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 2539715f1300STejun Heo * execution. 25400fcb78c2SRolf Eike Beer */ 2541d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 254252bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 25437a6bc1cdSVenkatesh Pallipadi { 254452bad64dSDavid Howells struct work_struct *work = &dwork->work; 2545d4283e93STejun Heo bool ret = false; 2546c26e2f2eSTejun Heo unsigned long irq_flags; 25478930cabaSTejun Heo 25488930cabaSTejun Heo /* read the comment in __queue_work() */ 2549c26e2f2eSTejun Heo local_irq_save(irq_flags); 25507a6bc1cdSVenkatesh Pallipadi 255186898fa6STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) && 255286898fa6STejun Heo !clear_pending_if_disabled(work)) { 25537beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 2554d4283e93STejun Heo ret = true; 25557a6bc1cdSVenkatesh Pallipadi } 25568930cabaSTejun Heo 2557c26e2f2eSTejun Heo local_irq_restore(irq_flags); 25587a6bc1cdSVenkatesh Pallipadi return ret; 25597a6bc1cdSVenkatesh Pallipadi } 2560ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 25611da177e4SLinus Torvalds 2562c8e55f36STejun Heo /** 25638376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 25648376fe22STejun Heo * @cpu: CPU number to execute work on 25658376fe22STejun Heo * @wq: workqueue to use 25668376fe22STejun Heo * @dwork: work to queue 25678376fe22STejun Heo * @delay: number of jiffies to wait before queueing 25688376fe22STejun Heo * 25698376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 25708376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 25718376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 25728376fe22STejun Heo * current state. 25738376fe22STejun Heo * 2574d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 25758376fe22STejun Heo * pending and its timer was modified. 25768376fe22STejun Heo * 2577e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 25788376fe22STejun Heo * See try_to_grab_pending() for details. 25798376fe22STejun Heo */ 25808376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 25818376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 25828376fe22STejun Heo { 2583c26e2f2eSTejun Heo unsigned long irq_flags; 2584*f09b10b6STejun Heo bool ret; 25858376fe22STejun Heo 2586*f09b10b6STejun Heo ret = work_grab_pending(&dwork->work, WORK_CANCEL_DELAYED, &irq_flags); 25878376fe22STejun Heo 2588*f09b10b6STejun Heo if (!clear_pending_if_disabled(&dwork->work)) 25898376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 25908376fe22STejun Heo 2591*f09b10b6STejun Heo local_irq_restore(irq_flags); 25928376fe22STejun Heo return ret; 25938376fe22STejun Heo } 25948376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 25958376fe22STejun Heo 259605f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 259705f0fe6bSTejun Heo { 259805f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 259905f0fe6bSTejun Heo 260005f0fe6bSTejun Heo /* read the comment in __queue_work() */ 260105f0fe6bSTejun Heo local_irq_disable(); 260205f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 260305f0fe6bSTejun Heo local_irq_enable(); 260405f0fe6bSTejun Heo } 260505f0fe6bSTejun Heo 260605f0fe6bSTejun Heo /** 260705f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 260805f0fe6bSTejun Heo * @wq: workqueue to use 260905f0fe6bSTejun Heo * @rwork: work to queue 261005f0fe6bSTejun Heo * 261105f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 261205f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 2613bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the 261405f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 261505f0fe6bSTejun Heo */ 261605f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 261705f0fe6bSTejun Heo { 261805f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 261905f0fe6bSTejun Heo 262086898fa6STejun Heo /* 262186898fa6STejun Heo * rcu_work can't be canceled or disabled. Warn if the user reached 262286898fa6STejun Heo * inside @rwork and disabled the inner work. 262386898fa6STejun Heo */ 262486898fa6STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) && 262586898fa6STejun Heo !WARN_ON_ONCE(clear_pending_if_disabled(work))) { 262605f0fe6bSTejun Heo rwork->wq = wq; 2627a7e30c0eSUladzislau Rezki call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); 262805f0fe6bSTejun Heo return true; 262905f0fe6bSTejun Heo } 263005f0fe6bSTejun Heo 263105f0fe6bSTejun Heo return false; 263205f0fe6bSTejun Heo } 263305f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 263405f0fe6bSTejun Heo 2635f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 2636c34056a3STejun Heo { 2637c34056a3STejun Heo struct worker *worker; 2638c34056a3STejun Heo 2639f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 2640c8e55f36STejun Heo if (worker) { 2641c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 2642affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 2643da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 2644e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 2645e22bee78STejun Heo worker->flags = WORKER_PREP; 2646c8e55f36STejun Heo } 2647c34056a3STejun Heo return worker; 2648c34056a3STejun Heo } 2649c34056a3STejun Heo 26509546b29eSTejun Heo static cpumask_t *pool_allowed_cpus(struct worker_pool *pool) 26519546b29eSTejun Heo { 26528639ecebSTejun Heo if (pool->cpu < 0 && pool->attrs->affn_strict) 26539546b29eSTejun Heo return pool->attrs->__pod_cpumask; 26548639ecebSTejun Heo else 26558639ecebSTejun Heo return pool->attrs->cpumask; 26569546b29eSTejun Heo } 26579546b29eSTejun Heo 2658c34056a3STejun Heo /** 26594736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 26604736cbf7SLai Jiangshan * @worker: worker to be attached 26614736cbf7SLai Jiangshan * @pool: the target pool 26624736cbf7SLai Jiangshan * 26634736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 26644736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 26654736cbf7SLai Jiangshan * cpu-[un]hotplugs. 26664736cbf7SLai Jiangshan */ 26674736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 26684736cbf7SLai Jiangshan struct worker_pool *pool) 26694736cbf7SLai Jiangshan { 26701258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 26714736cbf7SLai Jiangshan 26724736cbf7SLai Jiangshan /* 26734cb1ef64STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains stable 26744cb1ef64STejun Heo * across this function. See the comments above the flag definition for 26754cb1ef64STejun Heo * details. BH workers are, while per-CPU, always DISASSOCIATED. 26764736cbf7SLai Jiangshan */ 26774cb1ef64STejun Heo if (pool->flags & POOL_DISASSOCIATED) { 26784736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 26794cb1ef64STejun Heo } else { 26804cb1ef64STejun Heo WARN_ON_ONCE(pool->flags & POOL_BH); 26815c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu); 26824cb1ef64STejun Heo } 26834736cbf7SLai Jiangshan 2684640f17c8SPeter Zijlstra if (worker->rescue_wq) 26859546b29eSTejun Heo set_cpus_allowed_ptr(worker->task, pool_allowed_cpus(pool)); 2686640f17c8SPeter Zijlstra 26874736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 2688a2d812a2STejun Heo worker->pool = pool; 26894736cbf7SLai Jiangshan 26901258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 26914736cbf7SLai Jiangshan } 26924736cbf7SLai Jiangshan 26934736cbf7SLai Jiangshan /** 269460f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 269560f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 269660f5a4bcSLai Jiangshan * 26974736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 26984736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 26994736cbf7SLai Jiangshan * other reference to the pool. 270060f5a4bcSLai Jiangshan */ 2701a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 270260f5a4bcSLai Jiangshan { 2703a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 270460f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 270560f5a4bcSLai Jiangshan 27064cb1ef64STejun Heo /* there is one permanent BH worker per CPU which should never detach */ 27074cb1ef64STejun Heo WARN_ON_ONCE(pool->flags & POOL_BH); 27084cb1ef64STejun Heo 27091258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 2710a2d812a2STejun Heo 27115c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, -1); 2712da028469SLai Jiangshan list_del(&worker->node); 2713a2d812a2STejun Heo worker->pool = NULL; 2714a2d812a2STejun Heo 2715e02b9312SValentin Schneider if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) 271660f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 27171258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 271860f5a4bcSLai Jiangshan 2719b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 2720b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 2721b62c0751SLai Jiangshan 272260f5a4bcSLai Jiangshan if (detach_completion) 272360f5a4bcSLai Jiangshan complete(detach_completion); 272460f5a4bcSLai Jiangshan } 272560f5a4bcSLai Jiangshan 272660f5a4bcSLai Jiangshan /** 2727c34056a3STejun Heo * create_worker - create a new workqueue worker 272863d95a91STejun Heo * @pool: pool the new worker will belong to 2729c34056a3STejun Heo * 2730051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 2731c34056a3STejun Heo * 2732c34056a3STejun Heo * CONTEXT: 2733c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 2734c34056a3STejun Heo * 2735d185af30SYacine Belkadi * Return: 2736c34056a3STejun Heo * Pointer to the newly created worker. 2737c34056a3STejun Heo */ 2738bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 2739c34056a3STejun Heo { 2740e441b56fSZhen Lei struct worker *worker; 2741e441b56fSZhen Lei int id; 27425d9c7a1eSLucy Mielke char id_buf[23]; 2743c34056a3STejun Heo 27447cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 2745e441b56fSZhen Lei id = ida_alloc(&pool->worker_ida, GFP_KERNEL); 27463f0ea0b8SPetr Mladek if (id < 0) { 27473f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", 27483f0ea0b8SPetr Mladek ERR_PTR(id)); 2749e441b56fSZhen Lei return NULL; 27503f0ea0b8SPetr Mladek } 2751c34056a3STejun Heo 2752f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 27533f0ea0b8SPetr Mladek if (!worker) { 27543f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker\n"); 2755c34056a3STejun Heo goto fail; 27563f0ea0b8SPetr Mladek } 2757c34056a3STejun Heo 2758c34056a3STejun Heo worker->id = id; 2759c34056a3STejun Heo 27604cb1ef64STejun Heo if (!(pool->flags & POOL_BH)) { 276129c91e99STejun Heo if (pool->cpu >= 0) 2762e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 2763e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 2764f3421797STejun Heo else 2765e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 2766e3c916a4STejun Heo 27674cb1ef64STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, 27684cb1ef64STejun Heo pool->node, "kworker/%s", id_buf); 27693f0ea0b8SPetr Mladek if (IS_ERR(worker->task)) { 277060f54038SPetr Mladek if (PTR_ERR(worker->task) == -EINTR) { 277160f54038SPetr Mladek pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", 277260f54038SPetr Mladek id_buf); 277360f54038SPetr Mladek } else { 27743f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to create a worker thread: %pe", 27753f0ea0b8SPetr Mladek worker->task); 277660f54038SPetr Mladek } 2777c34056a3STejun Heo goto fail; 27783f0ea0b8SPetr Mladek } 2779c34056a3STejun Heo 278091151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 27819546b29eSTejun Heo kthread_bind_mask(worker->task, pool_allowed_cpus(pool)); 27824cb1ef64STejun Heo } 278391151228SOleg Nesterov 2784da028469SLai Jiangshan /* successful, attach the worker to the pool */ 27854736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 2786822d8405STejun Heo 2787051e1850SLai Jiangshan /* start the newly created worker */ 2788a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 27890219a352STejun Heo 2790051e1850SLai Jiangshan worker->pool->nr_workers++; 2791051e1850SLai Jiangshan worker_enter_idle(worker); 27920219a352STejun Heo 27930219a352STejun Heo /* 27940219a352STejun Heo * @worker is waiting on a completion in kthread() and will trigger hung 27956a229b0eSTejun Heo * check if not woken up soon. As kick_pool() is noop if @pool is empty, 27966a229b0eSTejun Heo * wake it up explicitly. 27970219a352STejun Heo */ 27984cb1ef64STejun Heo if (worker->task) 2799051e1850SLai Jiangshan wake_up_process(worker->task); 28000219a352STejun Heo 2801a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2802051e1850SLai Jiangshan 2803c34056a3STejun Heo return worker; 2804822d8405STejun Heo 2805c34056a3STejun Heo fail: 2806e441b56fSZhen Lei ida_free(&pool->worker_ida, id); 2807c34056a3STejun Heo kfree(worker); 2808c34056a3STejun Heo return NULL; 2809c34056a3STejun Heo } 2810c34056a3STejun Heo 2811793777bcSValentin Schneider static void unbind_worker(struct worker *worker) 2812793777bcSValentin Schneider { 2813793777bcSValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2814793777bcSValentin Schneider 2815793777bcSValentin Schneider kthread_set_per_cpu(worker->task, -1); 2816793777bcSValentin Schneider if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) 2817793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); 2818793777bcSValentin Schneider else 2819793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); 2820793777bcSValentin Schneider } 2821793777bcSValentin Schneider 2822e02b9312SValentin Schneider static void wake_dying_workers(struct list_head *cull_list) 2823e02b9312SValentin Schneider { 2824e02b9312SValentin Schneider struct worker *worker, *tmp; 2825e02b9312SValentin Schneider 2826e02b9312SValentin Schneider list_for_each_entry_safe(worker, tmp, cull_list, entry) { 2827e02b9312SValentin Schneider list_del_init(&worker->entry); 2828e02b9312SValentin Schneider unbind_worker(worker); 2829e02b9312SValentin Schneider /* 2830e02b9312SValentin Schneider * If the worker was somehow already running, then it had to be 2831e02b9312SValentin Schneider * in pool->idle_list when set_worker_dying() happened or we 2832e02b9312SValentin Schneider * wouldn't have gotten here. 2833c34056a3STejun Heo * 2834e02b9312SValentin Schneider * Thus, the worker must either have observed the WORKER_DIE 2835e02b9312SValentin Schneider * flag, or have set its state to TASK_IDLE. Either way, the 2836e02b9312SValentin Schneider * below will be observed by the worker and is safe to do 2837e02b9312SValentin Schneider * outside of pool->lock. 2838e02b9312SValentin Schneider */ 2839e02b9312SValentin Schneider wake_up_process(worker->task); 2840e02b9312SValentin Schneider } 2841e02b9312SValentin Schneider } 2842e02b9312SValentin Schneider 2843e02b9312SValentin Schneider /** 2844e02b9312SValentin Schneider * set_worker_dying - Tag a worker for destruction 2845e02b9312SValentin Schneider * @worker: worker to be destroyed 2846e02b9312SValentin Schneider * @list: transfer worker away from its pool->idle_list and into list 2847e02b9312SValentin Schneider * 2848e02b9312SValentin Schneider * Tag @worker for destruction and adjust @pool stats accordingly. The worker 2849e02b9312SValentin Schneider * should be idle. 2850c8e55f36STejun Heo * 2851c8e55f36STejun Heo * CONTEXT: 2852a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2853c34056a3STejun Heo */ 2854e02b9312SValentin Schneider static void set_worker_dying(struct worker *worker, struct list_head *list) 2855c34056a3STejun Heo { 2856bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2857c34056a3STejun Heo 2858cd549687STejun Heo lockdep_assert_held(&pool->lock); 2859e02b9312SValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2860cd549687STejun Heo 2861c34056a3STejun Heo /* sanity check frenzy */ 28626183c009STejun Heo if (WARN_ON(worker->current_work) || 286373eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 286473eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 28656183c009STejun Heo return; 2866c34056a3STejun Heo 2867bd7bdd43STejun Heo pool->nr_workers--; 2868bd7bdd43STejun Heo pool->nr_idle--; 2869c8e55f36STejun Heo 2870cb444766STejun Heo worker->flags |= WORKER_DIE; 2871e02b9312SValentin Schneider 2872e02b9312SValentin Schneider list_move(&worker->entry, list); 2873e02b9312SValentin Schneider list_move(&worker->node, &pool->dying_workers); 2874c34056a3STejun Heo } 2875c34056a3STejun Heo 28763f959aa3SValentin Schneider /** 28773f959aa3SValentin Schneider * idle_worker_timeout - check if some idle workers can now be deleted. 28783f959aa3SValentin Schneider * @t: The pool's idle_timer that just expired 28793f959aa3SValentin Schneider * 28803f959aa3SValentin Schneider * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in 28813f959aa3SValentin Schneider * worker_leave_idle(), as a worker flicking between idle and active while its 28823f959aa3SValentin Schneider * pool is at the too_many_workers() tipping point would cause too much timer 28833f959aa3SValentin Schneider * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let 28843f959aa3SValentin Schneider * it expire and re-evaluate things from there. 28853f959aa3SValentin Schneider */ 288632a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 2887e22bee78STejun Heo { 288832a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 28893f959aa3SValentin Schneider bool do_cull = false; 28903f959aa3SValentin Schneider 28913f959aa3SValentin Schneider if (work_pending(&pool->idle_cull_work)) 28923f959aa3SValentin Schneider return; 28933f959aa3SValentin Schneider 28943f959aa3SValentin Schneider raw_spin_lock_irq(&pool->lock); 28953f959aa3SValentin Schneider 28963f959aa3SValentin Schneider if (too_many_workers(pool)) { 28973f959aa3SValentin Schneider struct worker *worker; 28983f959aa3SValentin Schneider unsigned long expires; 28993f959aa3SValentin Schneider 29003f959aa3SValentin Schneider /* idle_list is kept in LIFO order, check the last one */ 29013f959aa3SValentin Schneider worker = list_entry(pool->idle_list.prev, struct worker, entry); 29023f959aa3SValentin Schneider expires = worker->last_active + IDLE_WORKER_TIMEOUT; 29033f959aa3SValentin Schneider do_cull = !time_before(jiffies, expires); 29043f959aa3SValentin Schneider 29053f959aa3SValentin Schneider if (!do_cull) 29063f959aa3SValentin Schneider mod_timer(&pool->idle_timer, expires); 29073f959aa3SValentin Schneider } 29083f959aa3SValentin Schneider raw_spin_unlock_irq(&pool->lock); 29093f959aa3SValentin Schneider 29103f959aa3SValentin Schneider if (do_cull) 29113f959aa3SValentin Schneider queue_work(system_unbound_wq, &pool->idle_cull_work); 29123f959aa3SValentin Schneider } 29133f959aa3SValentin Schneider 29143f959aa3SValentin Schneider /** 29153f959aa3SValentin Schneider * idle_cull_fn - cull workers that have been idle for too long. 29163f959aa3SValentin Schneider * @work: the pool's work for handling these idle workers 29173f959aa3SValentin Schneider * 29183f959aa3SValentin Schneider * This goes through a pool's idle workers and gets rid of those that have been 29193f959aa3SValentin Schneider * idle for at least IDLE_WORKER_TIMEOUT seconds. 2920e02b9312SValentin Schneider * 2921e02b9312SValentin Schneider * We don't want to disturb isolated CPUs because of a pcpu kworker being 2922e02b9312SValentin Schneider * culled, so this also resets worker affinity. This requires a sleepable 2923e02b9312SValentin Schneider * context, hence the split between timer callback and work item. 29243f959aa3SValentin Schneider */ 29253f959aa3SValentin Schneider static void idle_cull_fn(struct work_struct *work) 29263f959aa3SValentin Schneider { 29273f959aa3SValentin Schneider struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); 29289680540cSYang Yingliang LIST_HEAD(cull_list); 2929e22bee78STejun Heo 2930e02b9312SValentin Schneider /* 2931e02b9312SValentin Schneider * Grabbing wq_pool_attach_mutex here ensures an already-running worker 2932e02b9312SValentin Schneider * cannot proceed beyong worker_detach_from_pool() in its self-destruct 2933e02b9312SValentin Schneider * path. This is required as a previously-preempted worker could run after 2934e02b9312SValentin Schneider * set_worker_dying() has happened but before wake_dying_workers() did. 2935e02b9312SValentin Schneider */ 2936e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 2937a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2938e22bee78STejun Heo 29393347fc9fSLai Jiangshan while (too_many_workers(pool)) { 2940e22bee78STejun Heo struct worker *worker; 2941e22bee78STejun Heo unsigned long expires; 2942e22bee78STejun Heo 294363d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 2944e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 2945e22bee78STejun Heo 29463347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 294763d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 29483347fc9fSLai Jiangshan break; 2949e22bee78STejun Heo } 29503347fc9fSLai Jiangshan 2951e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 2952e22bee78STejun Heo } 2953e22bee78STejun Heo 2954a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2955e02b9312SValentin Schneider wake_dying_workers(&cull_list); 2956e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 2957e22bee78STejun Heo } 2958e22bee78STejun Heo 2959493a1724STejun Heo static void send_mayday(struct work_struct *work) 2960e22bee78STejun Heo { 2961112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2962112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 2963493a1724STejun Heo 29642e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 2965e22bee78STejun Heo 2966493008a8STejun Heo if (!wq->rescuer) 2967493a1724STejun Heo return; 2968e22bee78STejun Heo 2969e22bee78STejun Heo /* mayday mayday mayday */ 2970493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 297177668c8bSLai Jiangshan /* 297277668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 297377668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 297477668c8bSLai Jiangshan * rescuer is done with it. 297577668c8bSLai Jiangshan */ 297677668c8bSLai Jiangshan get_pwq(pwq); 2977493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2978e22bee78STejun Heo wake_up_process(wq->rescuer->task); 2979725e8ec5STejun Heo pwq->stats[PWQ_STAT_MAYDAY]++; 2980493a1724STejun Heo } 2981e22bee78STejun Heo } 2982e22bee78STejun Heo 298332a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 2984e22bee78STejun Heo { 298532a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 2986e22bee78STejun Heo struct work_struct *work; 2987e22bee78STejun Heo 2988a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2989a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ 2990e22bee78STejun Heo 299163d95a91STejun Heo if (need_to_create_worker(pool)) { 2992e22bee78STejun Heo /* 2993e22bee78STejun Heo * We've been trying to create a new worker but 2994e22bee78STejun Heo * haven't been successful. We might be hitting an 2995e22bee78STejun Heo * allocation deadlock. Send distress signals to 2996e22bee78STejun Heo * rescuers. 2997e22bee78STejun Heo */ 299863d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 2999e22bee78STejun Heo send_mayday(work); 3000e22bee78STejun Heo } 3001e22bee78STejun Heo 3002a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 3003a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3004e22bee78STejun Heo 300563d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 3006e22bee78STejun Heo } 3007e22bee78STejun Heo 3008e22bee78STejun Heo /** 3009e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 301063d95a91STejun Heo * @pool: pool to create a new worker for 3011e22bee78STejun Heo * 301263d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 3013e22bee78STejun Heo * have at least one idle worker on return from this function. If 3014e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 301563d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 3016e22bee78STejun Heo * possible allocation deadlock. 3017e22bee78STejun Heo * 3018c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 3019c5aa87bbSTejun Heo * may_start_working() %true. 3020e22bee78STejun Heo * 3021e22bee78STejun Heo * LOCKING: 3022a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3023e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 3024e22bee78STejun Heo * manager. 3025e22bee78STejun Heo */ 302629187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 3027d565ed63STejun Heo __releases(&pool->lock) 3028d565ed63STejun Heo __acquires(&pool->lock) 3029e22bee78STejun Heo { 3030e22bee78STejun Heo restart: 3031a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 30329f9c2364STejun Heo 3033e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 303463d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 3035e22bee78STejun Heo 3036e22bee78STejun Heo while (true) { 3037051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 3038e22bee78STejun Heo break; 3039e22bee78STejun Heo 3040e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 30419f9c2364STejun Heo 304263d95a91STejun Heo if (!need_to_create_worker(pool)) 3043e22bee78STejun Heo break; 3044e22bee78STejun Heo } 3045e22bee78STejun Heo 304663d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 3047a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3048051e1850SLai Jiangshan /* 3049051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 3050051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 3051051e1850SLai Jiangshan * already become busy. 3052051e1850SLai Jiangshan */ 305363d95a91STejun Heo if (need_to_create_worker(pool)) 3054e22bee78STejun Heo goto restart; 3055e22bee78STejun Heo } 3056e22bee78STejun Heo 3057e22bee78STejun Heo /** 3058e22bee78STejun Heo * manage_workers - manage worker pool 3059e22bee78STejun Heo * @worker: self 3060e22bee78STejun Heo * 3061706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 3062e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 3063706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 3064e22bee78STejun Heo * 3065e22bee78STejun Heo * The caller can safely start processing works on false return. On 3066e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 3067e22bee78STejun Heo * and may_start_working() is true. 3068e22bee78STejun Heo * 3069e22bee78STejun Heo * CONTEXT: 3070a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3071e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 3072e22bee78STejun Heo * 3073d185af30SYacine Belkadi * Return: 307429187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 307529187a9eSTejun Heo * start processing works, %true if management function was performed and 307629187a9eSTejun Heo * the conditions that the caller verified before calling the function may 307729187a9eSTejun Heo * no longer be true. 3078e22bee78STejun Heo */ 3079e22bee78STejun Heo static bool manage_workers(struct worker *worker) 3080e22bee78STejun Heo { 308163d95a91STejun Heo struct worker_pool *pool = worker->pool; 3082e22bee78STejun Heo 3083692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 308429187a9eSTejun Heo return false; 3085692b4825STejun Heo 3086692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 30872607d7a6STejun Heo pool->manager = worker; 3088e22bee78STejun Heo 308929187a9eSTejun Heo maybe_create_worker(pool); 3090e22bee78STejun Heo 30912607d7a6STejun Heo pool->manager = NULL; 3092692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 3093d8bb65abSSebastian Andrzej Siewior rcuwait_wake_up(&manager_wait); 309429187a9eSTejun Heo return true; 3095e22bee78STejun Heo } 3096e22bee78STejun Heo 3097a62428c0STejun Heo /** 3098a62428c0STejun Heo * process_one_work - process single work 3099c34056a3STejun Heo * @worker: self 3100a62428c0STejun Heo * @work: work to process 3101a62428c0STejun Heo * 3102a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 3103a62428c0STejun Heo * process a single work including synchronization against and 3104a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 3105a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 3106a62428c0STejun Heo * call this function to process a work. 3107a62428c0STejun Heo * 3108a62428c0STejun Heo * CONTEXT: 3109a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which is released and regrabbed. 3110a62428c0STejun Heo */ 3111c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 3112d565ed63STejun Heo __releases(&pool->lock) 3113d565ed63STejun Heo __acquires(&pool->lock) 31141da177e4SLinus Torvalds { 3115112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 3116bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 3117c4560c2cSLai Jiangshan unsigned long work_data; 3118c35aea39STejun Heo int lockdep_start_depth, rcu_start_depth; 31191acd92d9STejun Heo bool bh_draining = pool->flags & POOL_BH_DRAINING; 31204e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 31214e6045f1SJohannes Berg /* 3122a62428c0STejun Heo * It is permissible to free the struct work_struct from 3123a62428c0STejun Heo * inside the function that is called from it, this we need to 3124a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 3125a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 3126a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 31274e6045f1SJohannes Berg */ 31284d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 31294d82a1deSPeter Zijlstra 31304d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 31314e6045f1SJohannes Berg #endif 3132807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 313385327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 3134ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 313525511a47STejun Heo 31368930cabaSTejun Heo /* claim and dequeue */ 3137dc186ad7SThomas Gleixner debug_work_deactivate(work); 3138c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 3139c34056a3STejun Heo worker->current_work = work; 3140a2c1c57bSTejun Heo worker->current_func = work->func; 3141112202d9STejun Heo worker->current_pwq = pwq; 31424cb1ef64STejun Heo if (worker->task) 3143616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 3144c4560c2cSLai Jiangshan work_data = *work_data_bits(work); 3145d812796eSLai Jiangshan worker->current_color = get_work_color(work_data); 31467a22ad75STejun Heo 31478bf89593STejun Heo /* 31488bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 31498bf89593STejun Heo * overridden through set_worker_desc(). 31508bf89593STejun Heo */ 31518bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 31528bf89593STejun Heo 3153a62428c0STejun Heo list_del_init(&work->entry); 3154a62428c0STejun Heo 3155649027d7STejun Heo /* 3156228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 3157228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 3158228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 3159228f1d00SLai Jiangshan * execution of the pending work items. 3160fb0e7bebSTejun Heo */ 3161616db877STejun Heo if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE)) 3162228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 3163fb0e7bebSTejun Heo 3164974271c4STejun Heo /* 31650219a352STejun Heo * Kick @pool if necessary. It's always noop for per-cpu worker pools 31660219a352STejun Heo * since nr_running would always be >= 1 at this point. This is used to 31670219a352STejun Heo * chain execution of the pending work items for WORKER_NOT_RUNNING 31680219a352STejun Heo * workers such as the UNBOUND and CPU_INTENSIVE ones. 3169974271c4STejun Heo */ 31700219a352STejun Heo kick_pool(pool); 3171974271c4STejun Heo 31728930cabaSTejun Heo /* 31737c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 3174d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 317523657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 317623657bb1STejun Heo * disabled. 31778930cabaSTejun Heo */ 3178bccdc1faSTejun Heo set_work_pool_and_clear_pending(work, pool->id, 0); 31791da177e4SLinus Torvalds 3180fe48ba7dSMirsad Goran Todorovac pwq->stats[PWQ_STAT_STARTED]++; 3181a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3182365970a1SDavid Howells 3183c35aea39STejun Heo rcu_start_depth = rcu_preempt_depth(); 3184c35aea39STejun Heo lockdep_start_depth = lockdep_depth(current); 31851acd92d9STejun Heo /* see drain_dead_softirq_workfn() */ 31861acd92d9STejun Heo if (!bh_draining) 3187a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 31883295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 3189e6f3faa7SPeter Zijlstra /* 3190f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 3191f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 3192e6f3faa7SPeter Zijlstra * 3193e6f3faa7SPeter Zijlstra * However, that would result in: 3194e6f3faa7SPeter Zijlstra * 3195e6f3faa7SPeter Zijlstra * A(W1) 3196e6f3faa7SPeter Zijlstra * WFC(C) 3197e6f3faa7SPeter Zijlstra * A(W1) 3198e6f3faa7SPeter Zijlstra * C(C) 3199e6f3faa7SPeter Zijlstra * 3200e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 3201e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 3202e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 3203f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 3204e6f3faa7SPeter Zijlstra * these locks. 3205e6f3faa7SPeter Zijlstra * 3206e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 3207e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 3208e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 3209e6f3faa7SPeter Zijlstra */ 3210f52be570SPeter Zijlstra lockdep_invariant_state(true); 3211e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 3212a2c1c57bSTejun Heo worker->current_func(work); 3213e36c886aSArjan van de Ven /* 3214e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 3215e36c886aSArjan van de Ven * point will only record its address. 3216e36c886aSArjan van de Ven */ 32171c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func); 3218725e8ec5STejun Heo pwq->stats[PWQ_STAT_COMPLETED]++; 32193295f0efSIngo Molnar lock_map_release(&lockdep_map); 32201acd92d9STejun Heo if (!bh_draining) 3221112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 32221da177e4SLinus Torvalds 3223c35aea39STejun Heo if (unlikely((worker->task && in_atomic()) || 3224c35aea39STejun Heo lockdep_depth(current) != lockdep_start_depth || 3225c35aea39STejun Heo rcu_preempt_depth() != rcu_start_depth)) { 3226c35aea39STejun Heo pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n" 3227c35aea39STejun Heo " preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n", 3228c35aea39STejun Heo current->comm, task_pid_nr(current), preempt_count(), 3229c35aea39STejun Heo lockdep_start_depth, lockdep_depth(current), 3230c35aea39STejun Heo rcu_start_depth, rcu_preempt_depth(), 3231c35aea39STejun Heo worker->current_func); 3232d5abe669SPeter Zijlstra debug_show_held_locks(current); 3233d5abe669SPeter Zijlstra dump_stack(); 3234d5abe669SPeter Zijlstra } 3235d5abe669SPeter Zijlstra 3236b22ce278STejun Heo /* 3237025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION 3238b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 3239b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 3240b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 3241789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 3242789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 3243b22ce278STejun Heo */ 32444cb1ef64STejun Heo if (worker->task) 3245a7e6425eSPaul E. McKenney cond_resched(); 3246b22ce278STejun Heo 3247a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3248a62428c0STejun Heo 3249616db877STejun Heo /* 3250616db877STejun Heo * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked 3251616db877STejun Heo * CPU intensive by wq_worker_tick() if @work hogged CPU longer than 3252616db877STejun Heo * wq_cpu_intensive_thresh_us. Clear it. 3253616db877STejun Heo */ 3254fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 3255fb0e7bebSTejun Heo 32561b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 32571b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 32581b69ac6bSJohannes Weiner 3259a62428c0STejun Heo /* we're done with it, release */ 326042f8570fSSasha Levin hash_del(&worker->hentry); 3261c34056a3STejun Heo worker->current_work = NULL; 3262a2c1c57bSTejun Heo worker->current_func = NULL; 3263112202d9STejun Heo worker->current_pwq = NULL; 3264d812796eSLai Jiangshan worker->current_color = INT_MAX; 3265dd6c3c54STejun Heo 3266dd6c3c54STejun Heo /* must be the last step, see the function comment */ 3267c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, work_data); 32681da177e4SLinus Torvalds } 32691da177e4SLinus Torvalds 3270affee4b2STejun Heo /** 3271affee4b2STejun Heo * process_scheduled_works - process scheduled works 3272affee4b2STejun Heo * @worker: self 3273affee4b2STejun Heo * 3274affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 3275affee4b2STejun Heo * may change while processing a work, so this function repeatedly 3276affee4b2STejun Heo * fetches a work from the top and executes it. 3277affee4b2STejun Heo * 3278affee4b2STejun Heo * CONTEXT: 3279a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3280affee4b2STejun Heo * multiple times. 3281affee4b2STejun Heo */ 3282affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 32831da177e4SLinus Torvalds { 3284c0ab017dSTejun Heo struct work_struct *work; 3285c0ab017dSTejun Heo bool first = true; 3286c0ab017dSTejun Heo 3287c0ab017dSTejun Heo while ((work = list_first_entry_or_null(&worker->scheduled, 3288c0ab017dSTejun Heo struct work_struct, entry))) { 3289c0ab017dSTejun Heo if (first) { 3290c0ab017dSTejun Heo worker->pool->watchdog_ts = jiffies; 3291c0ab017dSTejun Heo first = false; 3292c0ab017dSTejun Heo } 3293c34056a3STejun Heo process_one_work(worker, work); 3294a62428c0STejun Heo } 32951da177e4SLinus Torvalds } 32961da177e4SLinus Torvalds 3297197f6accSTejun Heo static void set_pf_worker(bool val) 3298197f6accSTejun Heo { 3299197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 3300197f6accSTejun Heo if (val) 3301197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 3302197f6accSTejun Heo else 3303197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 3304197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 3305197f6accSTejun Heo } 3306197f6accSTejun Heo 33074690c4abSTejun Heo /** 33084690c4abSTejun Heo * worker_thread - the worker thread function 3309c34056a3STejun Heo * @__worker: self 33104690c4abSTejun Heo * 3311c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 3312c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 3313c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 3314c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 3315c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 3316d185af30SYacine Belkadi * 3317d185af30SYacine Belkadi * Return: 0 33184690c4abSTejun Heo */ 3319c34056a3STejun Heo static int worker_thread(void *__worker) 33201da177e4SLinus Torvalds { 3321c34056a3STejun Heo struct worker *worker = __worker; 3322bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 33231da177e4SLinus Torvalds 3324e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 3325197f6accSTejun Heo set_pf_worker(true); 3326c8e55f36STejun Heo woke_up: 3327a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3328affee4b2STejun Heo 3329a9ab775bSTejun Heo /* am I supposed to die? */ 3330a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 3331a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3332197f6accSTejun Heo set_pf_worker(false); 333360f5a4bcSLai Jiangshan 333460f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 3335e441b56fSZhen Lei ida_free(&pool->worker_ida, worker->id); 3336a2d812a2STejun Heo worker_detach_from_pool(worker); 3337e02b9312SValentin Schneider WARN_ON_ONCE(!list_empty(&worker->entry)); 333860f5a4bcSLai Jiangshan kfree(worker); 3339c8e55f36STejun Heo return 0; 3340c8e55f36STejun Heo } 3341c8e55f36STejun Heo 3342c8e55f36STejun Heo worker_leave_idle(worker); 3343db7bccf4STejun Heo recheck: 3344e22bee78STejun Heo /* no more worker necessary? */ 334563d95a91STejun Heo if (!need_more_worker(pool)) 3346e22bee78STejun Heo goto sleep; 3347e22bee78STejun Heo 3348e22bee78STejun Heo /* do we need to manage? */ 334963d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 3350e22bee78STejun Heo goto recheck; 3351e22bee78STejun Heo 3352c8e55f36STejun Heo /* 3353c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 3354c8e55f36STejun Heo * preparing to process a work or actually processing it. 3355c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 3356c8e55f36STejun Heo */ 33576183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 3358c8e55f36STejun Heo 3359e22bee78STejun Heo /* 3360a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 3361a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 3362a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 3363a9ab775bSTejun Heo * management if applicable and concurrency management is restored 3364a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 3365e22bee78STejun Heo */ 3366a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 3367e22bee78STejun Heo 3368e22bee78STejun Heo do { 3369affee4b2STejun Heo struct work_struct *work = 3370bd7bdd43STejun Heo list_first_entry(&pool->worklist, 3371affee4b2STejun Heo struct work_struct, entry); 3372affee4b2STejun Heo 3373873eaca6STejun Heo if (assign_work(work, worker, NULL)) 3374affee4b2STejun Heo process_scheduled_works(worker); 337563d95a91STejun Heo } while (keep_working(pool)); 3376affee4b2STejun Heo 3377228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 3378d313dd85STejun Heo sleep: 3379c8e55f36STejun Heo /* 3380d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 3381d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 3382d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 3383d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 3384d565ed63STejun Heo * event. 3385c8e55f36STejun Heo */ 3386c8e55f36STejun Heo worker_enter_idle(worker); 3387c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 3388a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 33891da177e4SLinus Torvalds schedule(); 3390c8e55f36STejun Heo goto woke_up; 33911da177e4SLinus Torvalds } 33921da177e4SLinus Torvalds 3393e22bee78STejun Heo /** 3394e22bee78STejun Heo * rescuer_thread - the rescuer thread function 3395111c225aSTejun Heo * @__rescuer: self 3396e22bee78STejun Heo * 3397e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 3398493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 3399e22bee78STejun Heo * 3400706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 3401e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 3402e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 3403e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 3404e22bee78STejun Heo * the problem rescuer solves. 3405e22bee78STejun Heo * 3406706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 3407706026c2STejun Heo * workqueues which have works queued on the pool and let them process 3408e22bee78STejun Heo * those works so that forward progress can be guaranteed. 3409e22bee78STejun Heo * 3410e22bee78STejun Heo * This should happen rarely. 3411d185af30SYacine Belkadi * 3412d185af30SYacine Belkadi * Return: 0 3413e22bee78STejun Heo */ 3414111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 3415e22bee78STejun Heo { 3416111c225aSTejun Heo struct worker *rescuer = __rescuer; 3417111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 34184d595b86SLai Jiangshan bool should_stop; 3419e22bee78STejun Heo 3420e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 3421111c225aSTejun Heo 3422111c225aSTejun Heo /* 3423111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 3424111c225aSTejun Heo * doesn't participate in concurrency management. 3425111c225aSTejun Heo */ 3426197f6accSTejun Heo set_pf_worker(true); 3427e22bee78STejun Heo repeat: 3428c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 34291da177e4SLinus Torvalds 34304d595b86SLai Jiangshan /* 34314d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 34324d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 34334d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 34344d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 34354d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 34364d595b86SLai Jiangshan * list is always empty on exit. 34374d595b86SLai Jiangshan */ 34384d595b86SLai Jiangshan should_stop = kthread_should_stop(); 34391da177e4SLinus Torvalds 3440493a1724STejun Heo /* see whether any pwq is asking for help */ 3441a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 3442493a1724STejun Heo 3443493a1724STejun Heo while (!list_empty(&wq->maydays)) { 3444493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 3445493a1724STejun Heo struct pool_workqueue, mayday_node); 3446112202d9STejun Heo struct worker_pool *pool = pwq->pool; 3447e22bee78STejun Heo struct work_struct *work, *n; 3448e22bee78STejun Heo 3449e22bee78STejun Heo __set_current_state(TASK_RUNNING); 3450493a1724STejun Heo list_del_init(&pwq->mayday_node); 3451493a1724STejun Heo 3452a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3453e22bee78STejun Heo 345451697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 345551697d39SLai Jiangshan 3456a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3457e22bee78STejun Heo 3458e22bee78STejun Heo /* 3459e22bee78STejun Heo * Slurp in all works issued via this workqueue and 3460e22bee78STejun Heo * process'em. 3461e22bee78STejun Heo */ 3462873eaca6STejun Heo WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); 346382607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 3464873eaca6STejun Heo if (get_work_pwq(work) == pwq && 3465873eaca6STejun Heo assign_work(work, rescuer, &n)) 3466725e8ec5STejun Heo pwq->stats[PWQ_STAT_RESCUED]++; 346782607adcSTejun Heo } 3468e22bee78STejun Heo 3469873eaca6STejun Heo if (!list_empty(&rescuer->scheduled)) { 3470e22bee78STejun Heo process_scheduled_works(rescuer); 34717576958aSTejun Heo 34727576958aSTejun Heo /* 3473008847f6SNeilBrown * The above execution of rescued work items could 3474008847f6SNeilBrown * have created more to rescue through 3475f97a4a1aSLai Jiangshan * pwq_activate_first_inactive() or chained 3476008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 3477008847f6SNeilBrown * that such back-to-back work items, which may be 3478008847f6SNeilBrown * being used to relieve memory pressure, don't 3479008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 3480008847f6SNeilBrown */ 34814f3f4cf3SLai Jiangshan if (pwq->nr_active && need_to_create_worker(pool)) { 3482a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); 3483e66b39afSTejun Heo /* 3484e66b39afSTejun Heo * Queue iff we aren't racing destruction 3485e66b39afSTejun Heo * and somebody else hasn't queued it already. 3486e66b39afSTejun Heo */ 3487e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) { 3488008847f6SNeilBrown get_pwq(pwq); 3489e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 3490e66b39afSTejun Heo } 3491a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 3492008847f6SNeilBrown } 3493008847f6SNeilBrown } 3494008847f6SNeilBrown 3495008847f6SNeilBrown /* 349677668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 349713b1d625SLai Jiangshan * go away while we're still attached to it. 349877668c8bSLai Jiangshan */ 349977668c8bSLai Jiangshan put_pwq(pwq); 350077668c8bSLai Jiangshan 350177668c8bSLai Jiangshan /* 35020219a352STejun Heo * Leave this pool. Notify regular workers; otherwise, we end up 35030219a352STejun Heo * with 0 concurrency and stalling the execution. 35047576958aSTejun Heo */ 35050219a352STejun Heo kick_pool(pool); 35067576958aSTejun Heo 3507a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 350813b1d625SLai Jiangshan 3509a2d812a2STejun Heo worker_detach_from_pool(rescuer); 351013b1d625SLai Jiangshan 3511a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 35121da177e4SLinus Torvalds } 35131da177e4SLinus Torvalds 3514a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3515493a1724STejun Heo 35164d595b86SLai Jiangshan if (should_stop) { 35174d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 3518197f6accSTejun Heo set_pf_worker(false); 35194d595b86SLai Jiangshan return 0; 35204d595b86SLai Jiangshan } 35214d595b86SLai Jiangshan 3522111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 3523111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 3524e22bee78STejun Heo schedule(); 3525e22bee78STejun Heo goto repeat; 35261da177e4SLinus Torvalds } 35271da177e4SLinus Torvalds 35284cb1ef64STejun Heo static void bh_worker(struct worker *worker) 35294cb1ef64STejun Heo { 35304cb1ef64STejun Heo struct worker_pool *pool = worker->pool; 35314cb1ef64STejun Heo int nr_restarts = BH_WORKER_RESTARTS; 35324cb1ef64STejun Heo unsigned long end = jiffies + BH_WORKER_JIFFIES; 35334cb1ef64STejun Heo 35344cb1ef64STejun Heo raw_spin_lock_irq(&pool->lock); 35354cb1ef64STejun Heo worker_leave_idle(worker); 35364cb1ef64STejun Heo 35374cb1ef64STejun Heo /* 35384cb1ef64STejun Heo * This function follows the structure of worker_thread(). See there for 35394cb1ef64STejun Heo * explanations on each step. 35404cb1ef64STejun Heo */ 35414cb1ef64STejun Heo if (!need_more_worker(pool)) 35424cb1ef64STejun Heo goto done; 35434cb1ef64STejun Heo 35444cb1ef64STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 35454cb1ef64STejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 35464cb1ef64STejun Heo 35474cb1ef64STejun Heo do { 35484cb1ef64STejun Heo struct work_struct *work = 35494cb1ef64STejun Heo list_first_entry(&pool->worklist, 35504cb1ef64STejun Heo struct work_struct, entry); 35514cb1ef64STejun Heo 35524cb1ef64STejun Heo if (assign_work(work, worker, NULL)) 35534cb1ef64STejun Heo process_scheduled_works(worker); 35544cb1ef64STejun Heo } while (keep_working(pool) && 35554cb1ef64STejun Heo --nr_restarts && time_before(jiffies, end)); 35564cb1ef64STejun Heo 35574cb1ef64STejun Heo worker_set_flags(worker, WORKER_PREP); 35584cb1ef64STejun Heo done: 35594cb1ef64STejun Heo worker_enter_idle(worker); 35604cb1ef64STejun Heo kick_pool(pool); 35614cb1ef64STejun Heo raw_spin_unlock_irq(&pool->lock); 35624cb1ef64STejun Heo } 35634cb1ef64STejun Heo 35644cb1ef64STejun Heo /* 35654cb1ef64STejun Heo * TODO: Convert all tasklet users to workqueue and use softirq directly. 35664cb1ef64STejun Heo * 35674cb1ef64STejun Heo * This is currently called from tasklet[_hi]action() and thus is also called 35684cb1ef64STejun Heo * whenever there are tasklets to run. Let's do an early exit if there's nothing 35694cb1ef64STejun Heo * queued. Once conversion from tasklet is complete, the need_more_worker() test 35704cb1ef64STejun Heo * can be dropped. 35714cb1ef64STejun Heo * 35724cb1ef64STejun Heo * After full conversion, we'll add worker->softirq_action, directly use the 35734cb1ef64STejun Heo * softirq action and obtain the worker pointer from the softirq_action pointer. 35744cb1ef64STejun Heo */ 35754cb1ef64STejun Heo void workqueue_softirq_action(bool highpri) 35764cb1ef64STejun Heo { 35774cb1ef64STejun Heo struct worker_pool *pool = 35784cb1ef64STejun Heo &per_cpu(bh_worker_pools, smp_processor_id())[highpri]; 35794cb1ef64STejun Heo if (need_more_worker(pool)) 35804cb1ef64STejun Heo bh_worker(list_first_entry(&pool->workers, struct worker, node)); 35814cb1ef64STejun Heo } 35824cb1ef64STejun Heo 35831acd92d9STejun Heo struct wq_drain_dead_softirq_work { 35841acd92d9STejun Heo struct work_struct work; 35851acd92d9STejun Heo struct worker_pool *pool; 35861acd92d9STejun Heo struct completion done; 35871acd92d9STejun Heo }; 35881acd92d9STejun Heo 35891acd92d9STejun Heo static void drain_dead_softirq_workfn(struct work_struct *work) 35901acd92d9STejun Heo { 35911acd92d9STejun Heo struct wq_drain_dead_softirq_work *dead_work = 35921acd92d9STejun Heo container_of(work, struct wq_drain_dead_softirq_work, work); 35931acd92d9STejun Heo struct worker_pool *pool = dead_work->pool; 35941acd92d9STejun Heo bool repeat; 35951acd92d9STejun Heo 35961acd92d9STejun Heo /* 35971acd92d9STejun Heo * @pool's CPU is dead and we want to execute its still pending work 35981acd92d9STejun Heo * items from this BH work item which is running on a different CPU. As 35991acd92d9STejun Heo * its CPU is dead, @pool can't be kicked and, as work execution path 36001acd92d9STejun Heo * will be nested, a lockdep annotation needs to be suppressed. Mark 36011acd92d9STejun Heo * @pool with %POOL_BH_DRAINING for the special treatments. 36021acd92d9STejun Heo */ 36031acd92d9STejun Heo raw_spin_lock_irq(&pool->lock); 36041acd92d9STejun Heo pool->flags |= POOL_BH_DRAINING; 36051acd92d9STejun Heo raw_spin_unlock_irq(&pool->lock); 36061acd92d9STejun Heo 36071acd92d9STejun Heo bh_worker(list_first_entry(&pool->workers, struct worker, node)); 36081acd92d9STejun Heo 36091acd92d9STejun Heo raw_spin_lock_irq(&pool->lock); 36101acd92d9STejun Heo pool->flags &= ~POOL_BH_DRAINING; 36111acd92d9STejun Heo repeat = need_more_worker(pool); 36121acd92d9STejun Heo raw_spin_unlock_irq(&pool->lock); 36131acd92d9STejun Heo 36141acd92d9STejun Heo /* 36151acd92d9STejun Heo * bh_worker() might hit consecutive execution limit and bail. If there 36161acd92d9STejun Heo * still are pending work items, reschedule self and return so that we 36171acd92d9STejun Heo * don't hog this CPU's BH. 36181acd92d9STejun Heo */ 36191acd92d9STejun Heo if (repeat) { 36201acd92d9STejun Heo if (pool->attrs->nice == HIGHPRI_NICE_LEVEL) 36211acd92d9STejun Heo queue_work(system_bh_highpri_wq, work); 36221acd92d9STejun Heo else 36231acd92d9STejun Heo queue_work(system_bh_wq, work); 36241acd92d9STejun Heo } else { 36251acd92d9STejun Heo complete(&dead_work->done); 36261acd92d9STejun Heo } 36271acd92d9STejun Heo } 36281acd92d9STejun Heo 36291acd92d9STejun Heo /* 36301acd92d9STejun Heo * @cpu is dead. Drain the remaining BH work items on the current CPU. It's 36311acd92d9STejun Heo * possible to allocate dead_work per CPU and avoid flushing. However, then we 36321acd92d9STejun Heo * have to worry about draining overlapping with CPU coming back online or 36331acd92d9STejun Heo * nesting (one CPU's dead_work queued on another CPU which is also dead and so 36341acd92d9STejun Heo * on). Let's keep it simple and drain them synchronously. These are BH work 36351acd92d9STejun Heo * items which shouldn't be requeued on the same pool. Shouldn't take long. 36361acd92d9STejun Heo */ 36371acd92d9STejun Heo void workqueue_softirq_dead(unsigned int cpu) 36381acd92d9STejun Heo { 36391acd92d9STejun Heo int i; 36401acd92d9STejun Heo 36411acd92d9STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 36421acd92d9STejun Heo struct worker_pool *pool = &per_cpu(bh_worker_pools, cpu)[i]; 36431acd92d9STejun Heo struct wq_drain_dead_softirq_work dead_work; 36441acd92d9STejun Heo 36451acd92d9STejun Heo if (!need_more_worker(pool)) 36461acd92d9STejun Heo continue; 36471acd92d9STejun Heo 36481acd92d9STejun Heo INIT_WORK(&dead_work.work, drain_dead_softirq_workfn); 36491acd92d9STejun Heo dead_work.pool = pool; 36501acd92d9STejun Heo init_completion(&dead_work.done); 36511acd92d9STejun Heo 36521acd92d9STejun Heo if (pool->attrs->nice == HIGHPRI_NICE_LEVEL) 36531acd92d9STejun Heo queue_work(system_bh_highpri_wq, &dead_work.work); 36541acd92d9STejun Heo else 36551acd92d9STejun Heo queue_work(system_bh_wq, &dead_work.work); 36561acd92d9STejun Heo 36571acd92d9STejun Heo wait_for_completion(&dead_work.done); 36581acd92d9STejun Heo } 36591acd92d9STejun Heo } 36601acd92d9STejun Heo 3661fca839c0STejun Heo /** 3662fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 3663fca839c0STejun Heo * @target_wq: workqueue being flushed 3664fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 3665fca839c0STejun Heo * 3666fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 3667fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 3668fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 3669fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 3670fca839c0STejun Heo * a deadlock. 3671fca839c0STejun Heo */ 3672fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 3673fca839c0STejun Heo struct work_struct *target_work) 3674fca839c0STejun Heo { 3675fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 3676fca839c0STejun Heo struct worker *worker; 3677fca839c0STejun Heo 3678fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 3679fca839c0STejun Heo return; 3680fca839c0STejun Heo 3681fca839c0STejun Heo worker = current_wq_worker(); 3682fca839c0STejun Heo 3683fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 3684d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", 3685fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 368623d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 368723d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 3688d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", 3689fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 3690fca839c0STejun Heo target_wq->name, target_func); 3691fca839c0STejun Heo } 3692fca839c0STejun Heo 3693fc2e4d70SOleg Nesterov struct wq_barrier { 3694fc2e4d70SOleg Nesterov struct work_struct work; 3695fc2e4d70SOleg Nesterov struct completion done; 36962607d7a6STejun Heo struct task_struct *task; /* purely informational */ 3697fc2e4d70SOleg Nesterov }; 3698fc2e4d70SOleg Nesterov 3699fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 3700fc2e4d70SOleg Nesterov { 3701fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 3702fc2e4d70SOleg Nesterov complete(&barr->done); 3703fc2e4d70SOleg Nesterov } 3704fc2e4d70SOleg Nesterov 37054690c4abSTejun Heo /** 37064690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 3707112202d9STejun Heo * @pwq: pwq to insert barrier into 37084690c4abSTejun Heo * @barr: wq_barrier to insert 3709affee4b2STejun Heo * @target: target work to attach @barr to 3710affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 37114690c4abSTejun Heo * 3712affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 3713affee4b2STejun Heo * @target finishes execution. Please note that the ordering 3714affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 3715affee4b2STejun Heo * cpu. 3716affee4b2STejun Heo * 3717affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 3718affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 3719affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 3720affee4b2STejun Heo * flag of the previous work while there must be a valid next work 3721affee4b2STejun Heo * after a work with LINKED flag set. 3722affee4b2STejun Heo * 3723affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 3724112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 37254690c4abSTejun Heo * 37264690c4abSTejun Heo * CONTEXT: 3727a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 37284690c4abSTejun Heo */ 3729112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 3730affee4b2STejun Heo struct wq_barrier *barr, 3731affee4b2STejun Heo struct work_struct *target, struct worker *worker) 3732fc2e4d70SOleg Nesterov { 37334cb1ef64STejun Heo static __maybe_unused struct lock_class_key bh_key, thr_key; 3734d812796eSLai Jiangshan unsigned int work_flags = 0; 3735d812796eSLai Jiangshan unsigned int work_color; 3736affee4b2STejun Heo struct list_head *head; 3737affee4b2STejun Heo 3738dc186ad7SThomas Gleixner /* 3739d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 3740dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 3741dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 3742dc186ad7SThomas Gleixner * might deadlock. 37434cb1ef64STejun Heo * 37444cb1ef64STejun Heo * BH and threaded workqueues need separate lockdep keys to avoid 37454cb1ef64STejun Heo * spuriously triggering "inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} 37464cb1ef64STejun Heo * usage". 3747dc186ad7SThomas Gleixner */ 37484cb1ef64STejun Heo INIT_WORK_ONSTACK_KEY(&barr->work, wq_barrier_func, 37494cb1ef64STejun Heo (pwq->wq->flags & WQ_BH) ? &bh_key : &thr_key); 375022df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 375152fa5bc5SBoqun Feng 3752fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 3753fd1a5b04SByungchul Park 37542607d7a6STejun Heo barr->task = current; 375583c22520SOleg Nesterov 37565797b1c1STejun Heo /* The barrier work item does not participate in nr_active. */ 3757018f3a13SLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 3758018f3a13SLai Jiangshan 3759affee4b2STejun Heo /* 3760affee4b2STejun Heo * If @target is currently being executed, schedule the 3761affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 3762affee4b2STejun Heo */ 3763d812796eSLai Jiangshan if (worker) { 3764affee4b2STejun Heo head = worker->scheduled.next; 3765d812796eSLai Jiangshan work_color = worker->current_color; 3766d812796eSLai Jiangshan } else { 3767affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 3768affee4b2STejun Heo 3769affee4b2STejun Heo head = target->entry.next; 3770affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 3771d21cece0SLai Jiangshan work_flags |= *bits & WORK_STRUCT_LINKED; 3772d812796eSLai Jiangshan work_color = get_work_color(*bits); 3773affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 3774affee4b2STejun Heo } 3775affee4b2STejun Heo 3776d812796eSLai Jiangshan pwq->nr_in_flight[work_color]++; 3777d812796eSLai Jiangshan work_flags |= work_color_to_flags(work_color); 3778d812796eSLai Jiangshan 3779d21cece0SLai Jiangshan insert_work(pwq, &barr->work, head, work_flags); 3780fc2e4d70SOleg Nesterov } 3781fc2e4d70SOleg Nesterov 378273f53c4aSTejun Heo /** 3783112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 378473f53c4aSTejun Heo * @wq: workqueue being flushed 378573f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 378673f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 378773f53c4aSTejun Heo * 3788112202d9STejun Heo * Prepare pwqs for workqueue flushing. 378973f53c4aSTejun Heo * 3790112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 3791112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 3792112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 3793112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 3794112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 379573f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 379673f53c4aSTejun Heo * 379773f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 379873f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 379973f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 380073f53c4aSTejun Heo * is returned. 380173f53c4aSTejun Heo * 3802112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 380373f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 380473f53c4aSTejun Heo * advanced to @work_color. 380573f53c4aSTejun Heo * 380673f53c4aSTejun Heo * CONTEXT: 38073c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 380873f53c4aSTejun Heo * 3809d185af30SYacine Belkadi * Return: 381073f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 381173f53c4aSTejun Heo * otherwise. 381273f53c4aSTejun Heo */ 3813112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 381473f53c4aSTejun Heo int flush_color, int work_color) 38151da177e4SLinus Torvalds { 381673f53c4aSTejun Heo bool wait = false; 381749e3cf44STejun Heo struct pool_workqueue *pwq; 38181da177e4SLinus Torvalds 381973f53c4aSTejun Heo if (flush_color >= 0) { 38206183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 3821112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 3822dc186ad7SThomas Gleixner } 382314441960SOleg Nesterov 382449e3cf44STejun Heo for_each_pwq(pwq, wq) { 3825112202d9STejun Heo struct worker_pool *pool = pwq->pool; 38261da177e4SLinus Torvalds 3827a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 382873f53c4aSTejun Heo 382973f53c4aSTejun Heo if (flush_color >= 0) { 38306183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 383173f53c4aSTejun Heo 3832112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 3833112202d9STejun Heo pwq->flush_color = flush_color; 3834112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 383573f53c4aSTejun Heo wait = true; 38361da177e4SLinus Torvalds } 383773f53c4aSTejun Heo } 383873f53c4aSTejun Heo 383973f53c4aSTejun Heo if (work_color >= 0) { 38406183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 3841112202d9STejun Heo pwq->work_color = work_color; 384273f53c4aSTejun Heo } 384373f53c4aSTejun Heo 3844a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 38451da177e4SLinus Torvalds } 38461da177e4SLinus Torvalds 3847112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 384873f53c4aSTejun Heo complete(&wq->first_flusher->done); 384973f53c4aSTejun Heo 385073f53c4aSTejun Heo return wait; 385183c22520SOleg Nesterov } 38521da177e4SLinus Torvalds 3853c35aea39STejun Heo static void touch_wq_lockdep_map(struct workqueue_struct *wq) 3854c35aea39STejun Heo { 38554cb1ef64STejun Heo #ifdef CONFIG_LOCKDEP 38564cb1ef64STejun Heo if (wq->flags & WQ_BH) 38574cb1ef64STejun Heo local_bh_disable(); 38584cb1ef64STejun Heo 3859c35aea39STejun Heo lock_map_acquire(&wq->lockdep_map); 3860c35aea39STejun Heo lock_map_release(&wq->lockdep_map); 38614cb1ef64STejun Heo 38624cb1ef64STejun Heo if (wq->flags & WQ_BH) 38634cb1ef64STejun Heo local_bh_enable(); 38644cb1ef64STejun Heo #endif 3865c35aea39STejun Heo } 3866c35aea39STejun Heo 3867c35aea39STejun Heo static void touch_work_lockdep_map(struct work_struct *work, 3868c35aea39STejun Heo struct workqueue_struct *wq) 3869c35aea39STejun Heo { 38704cb1ef64STejun Heo #ifdef CONFIG_LOCKDEP 38714cb1ef64STejun Heo if (wq->flags & WQ_BH) 38724cb1ef64STejun Heo local_bh_disable(); 38734cb1ef64STejun Heo 3874c35aea39STejun Heo lock_map_acquire(&work->lockdep_map); 3875c35aea39STejun Heo lock_map_release(&work->lockdep_map); 38764cb1ef64STejun Heo 38774cb1ef64STejun Heo if (wq->flags & WQ_BH) 38784cb1ef64STejun Heo local_bh_enable(); 38794cb1ef64STejun Heo #endif 3880c35aea39STejun Heo } 3881c35aea39STejun Heo 38820fcb78c2SRolf Eike Beer /** 3883c4f135d6STetsuo Handa * __flush_workqueue - ensure that any scheduled work has run to completion. 38840fcb78c2SRolf Eike Beer * @wq: workqueue to flush 38851da177e4SLinus Torvalds * 3886c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 3887c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 38881da177e4SLinus Torvalds */ 3889c4f135d6STetsuo Handa void __flush_workqueue(struct workqueue_struct *wq) 38901da177e4SLinus Torvalds { 389173f53c4aSTejun Heo struct wq_flusher this_flusher = { 389273f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 389373f53c4aSTejun Heo .flush_color = -1, 3894fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 389573f53c4aSTejun Heo }; 389673f53c4aSTejun Heo int next_color; 3897b1f4ec17SOleg Nesterov 38983347fa09STejun Heo if (WARN_ON(!wq_online)) 38993347fa09STejun Heo return; 39003347fa09STejun Heo 3901c35aea39STejun Heo touch_wq_lockdep_map(wq); 390287915adcSJohannes Berg 39033c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 390473f53c4aSTejun Heo 390573f53c4aSTejun Heo /* 390673f53c4aSTejun Heo * Start-to-wait phase 390773f53c4aSTejun Heo */ 390873f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 390973f53c4aSTejun Heo 391073f53c4aSTejun Heo if (next_color != wq->flush_color) { 391173f53c4aSTejun Heo /* 391273f53c4aSTejun Heo * Color space is not full. The current work_color 391373f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 391473f53c4aSTejun Heo * by one. 391573f53c4aSTejun Heo */ 39166183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 391773f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 391873f53c4aSTejun Heo wq->work_color = next_color; 391973f53c4aSTejun Heo 392073f53c4aSTejun Heo if (!wq->first_flusher) { 392173f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 39226183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 392373f53c4aSTejun Heo 392473f53c4aSTejun Heo wq->first_flusher = &this_flusher; 392573f53c4aSTejun Heo 3926112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 392773f53c4aSTejun Heo wq->work_color)) { 392873f53c4aSTejun Heo /* nothing to flush, done */ 392973f53c4aSTejun Heo wq->flush_color = next_color; 393073f53c4aSTejun Heo wq->first_flusher = NULL; 393173f53c4aSTejun Heo goto out_unlock; 393273f53c4aSTejun Heo } 393373f53c4aSTejun Heo } else { 393473f53c4aSTejun Heo /* wait in queue */ 39356183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 393673f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 3937112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 393873f53c4aSTejun Heo } 393973f53c4aSTejun Heo } else { 394073f53c4aSTejun Heo /* 394173f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 394273f53c4aSTejun Heo * The next flush completion will assign us 394373f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 394473f53c4aSTejun Heo */ 394573f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 394673f53c4aSTejun Heo } 394773f53c4aSTejun Heo 3948fca839c0STejun Heo check_flush_dependency(wq, NULL); 3949fca839c0STejun Heo 39503c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 395173f53c4aSTejun Heo 395273f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 395373f53c4aSTejun Heo 395473f53c4aSTejun Heo /* 395573f53c4aSTejun Heo * Wake-up-and-cascade phase 395673f53c4aSTejun Heo * 395773f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 395873f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 395973f53c4aSTejun Heo */ 396000d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher) 396173f53c4aSTejun Heo return; 396273f53c4aSTejun Heo 39633c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 396473f53c4aSTejun Heo 39654ce48b37STejun Heo /* we might have raced, check again with mutex held */ 39664ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 39674ce48b37STejun Heo goto out_unlock; 39684ce48b37STejun Heo 396900d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL); 397073f53c4aSTejun Heo 39716183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 39726183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 397373f53c4aSTejun Heo 397473f53c4aSTejun Heo while (true) { 397573f53c4aSTejun Heo struct wq_flusher *next, *tmp; 397673f53c4aSTejun Heo 397773f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 397873f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 397973f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 398073f53c4aSTejun Heo break; 398173f53c4aSTejun Heo list_del_init(&next->list); 398273f53c4aSTejun Heo complete(&next->done); 398373f53c4aSTejun Heo } 398473f53c4aSTejun Heo 39856183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 398673f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 398773f53c4aSTejun Heo 398873f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 398973f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 399073f53c4aSTejun Heo 399173f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 399273f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 399373f53c4aSTejun Heo /* 399473f53c4aSTejun Heo * Assign the same color to all overflowed 399573f53c4aSTejun Heo * flushers, advance work_color and append to 399673f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 399773f53c4aSTejun Heo * phase for these overflowed flushers. 399873f53c4aSTejun Heo */ 399973f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 400073f53c4aSTejun Heo tmp->flush_color = wq->work_color; 400173f53c4aSTejun Heo 400273f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 400373f53c4aSTejun Heo 400473f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 400573f53c4aSTejun Heo &wq->flusher_queue); 4006112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 400773f53c4aSTejun Heo } 400873f53c4aSTejun Heo 400973f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 40106183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 401173f53c4aSTejun Heo break; 401273f53c4aSTejun Heo } 401373f53c4aSTejun Heo 401473f53c4aSTejun Heo /* 401573f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 4016112202d9STejun Heo * the new first flusher and arm pwqs. 401773f53c4aSTejun Heo */ 40186183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 40196183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 402073f53c4aSTejun Heo 402173f53c4aSTejun Heo list_del_init(&next->list); 402273f53c4aSTejun Heo wq->first_flusher = next; 402373f53c4aSTejun Heo 4024112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 402573f53c4aSTejun Heo break; 402673f53c4aSTejun Heo 402773f53c4aSTejun Heo /* 402873f53c4aSTejun Heo * Meh... this color is already done, clear first 402973f53c4aSTejun Heo * flusher and repeat cascading. 403073f53c4aSTejun Heo */ 403173f53c4aSTejun Heo wq->first_flusher = NULL; 403273f53c4aSTejun Heo } 403373f53c4aSTejun Heo 403473f53c4aSTejun Heo out_unlock: 40353c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 40361da177e4SLinus Torvalds } 4037c4f135d6STetsuo Handa EXPORT_SYMBOL(__flush_workqueue); 40381da177e4SLinus Torvalds 40399c5a2ba7STejun Heo /** 40409c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 40419c5a2ba7STejun Heo * @wq: workqueue to drain 40429c5a2ba7STejun Heo * 40439c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 40449c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 40459c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 4046b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 40479c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 40489c5a2ba7STejun Heo * takes too long. 40499c5a2ba7STejun Heo */ 40509c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 40519c5a2ba7STejun Heo { 40529c5a2ba7STejun Heo unsigned int flush_cnt = 0; 405349e3cf44STejun Heo struct pool_workqueue *pwq; 40549c5a2ba7STejun Heo 40559c5a2ba7STejun Heo /* 40569c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 40579c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 4058618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 40599c5a2ba7STejun Heo */ 406087fc741eSLai Jiangshan mutex_lock(&wq->mutex); 40619c5a2ba7STejun Heo if (!wq->nr_drainers++) 4062618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 406387fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 40649c5a2ba7STejun Heo reflush: 4065c4f135d6STetsuo Handa __flush_workqueue(wq); 40669c5a2ba7STejun Heo 4067b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 406876af4d93STejun Heo 406949e3cf44STejun Heo for_each_pwq(pwq, wq) { 4070fa2563e4SThomas Tuttle bool drained; 40719c5a2ba7STejun Heo 4072a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 4073afa87ce8STejun Heo drained = pwq_is_empty(pwq); 4074a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 4075fa2563e4SThomas Tuttle 4076fa2563e4SThomas Tuttle if (drained) 40779c5a2ba7STejun Heo continue; 40789c5a2ba7STejun Heo 40799c5a2ba7STejun Heo if (++flush_cnt == 10 || 40809c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 4081e9ad2eb3SStephen Zhang pr_warn("workqueue %s: %s() isn't complete after %u tries\n", 4082e9ad2eb3SStephen Zhang wq->name, __func__, flush_cnt); 408376af4d93STejun Heo 4084b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 40859c5a2ba7STejun Heo goto reflush; 40869c5a2ba7STejun Heo } 40879c5a2ba7STejun Heo 40889c5a2ba7STejun Heo if (!--wq->nr_drainers) 4089618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 409087fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 40919c5a2ba7STejun Heo } 40929c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 40939c5a2ba7STejun Heo 4094d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 4095d6e89786SJohannes Berg bool from_cancel) 4096baf59022STejun Heo { 4097baf59022STejun Heo struct worker *worker = NULL; 4098c9e7cf27STejun Heo struct worker_pool *pool; 4099112202d9STejun Heo struct pool_workqueue *pwq; 4100c35aea39STejun Heo struct workqueue_struct *wq; 4101baf59022STejun Heo 4102baf59022STejun Heo might_sleep(); 4103baf59022STejun Heo 410424acfb71SThomas Gleixner rcu_read_lock(); 4105fa1b54e6STejun Heo pool = get_work_pool(work); 4106fa1b54e6STejun Heo if (!pool) { 410724acfb71SThomas Gleixner rcu_read_unlock(); 4108fa1b54e6STejun Heo return false; 4109fa1b54e6STejun Heo } 4110fa1b54e6STejun Heo 4111a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 41120b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 4113112202d9STejun Heo pwq = get_work_pwq(work); 4114112202d9STejun Heo if (pwq) { 4115112202d9STejun Heo if (unlikely(pwq->pool != pool)) 4116baf59022STejun Heo goto already_gone; 4117606a5020STejun Heo } else { 4118c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 4119baf59022STejun Heo if (!worker) 4120baf59022STejun Heo goto already_gone; 4121112202d9STejun Heo pwq = worker->current_pwq; 4122606a5020STejun Heo } 4123baf59022STejun Heo 4124c35aea39STejun Heo wq = pwq->wq; 4125c35aea39STejun Heo check_flush_dependency(wq, work); 4126fca839c0STejun Heo 4127112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 4128a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 4129baf59022STejun Heo 4130c35aea39STejun Heo touch_work_lockdep_map(work, wq); 4131c35aea39STejun Heo 4132e159489bSTejun Heo /* 4133a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 4134a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 4135a1d14934SPeter Zijlstra * 4136a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 4137a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 4138a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 4139a1d14934SPeter Zijlstra * forward progress. 4140e159489bSTejun Heo */ 4141c35aea39STejun Heo if (!from_cancel && (wq->saved_max_active == 1 || wq->rescuer)) 4142c35aea39STejun Heo touch_wq_lockdep_map(wq); 4143c35aea39STejun Heo 414424acfb71SThomas Gleixner rcu_read_unlock(); 4145baf59022STejun Heo return true; 4146baf59022STejun Heo already_gone: 4147a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 414824acfb71SThomas Gleixner rcu_read_unlock(); 4149baf59022STejun Heo return false; 4150baf59022STejun Heo } 4151baf59022STejun Heo 4152d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 4153d6e89786SJohannes Berg { 4154d6e89786SJohannes Berg struct wq_barrier barr; 4155d6e89786SJohannes Berg 4156d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 4157d6e89786SJohannes Berg return false; 4158d6e89786SJohannes Berg 41594d43d395STetsuo Handa if (WARN_ON(!work->func)) 41604d43d395STetsuo Handa return false; 41614d43d395STetsuo Handa 4162d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 4163d6e89786SJohannes Berg wait_for_completion(&barr.done); 4164d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 4165d6e89786SJohannes Berg return true; 4166d6e89786SJohannes Berg } else { 4167d6e89786SJohannes Berg return false; 4168d6e89786SJohannes Berg } 4169d6e89786SJohannes Berg } 4170d6e89786SJohannes Berg 4171db700897SOleg Nesterov /** 4172401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 4173401a8d04STejun Heo * @work: the work to flush 4174db700897SOleg Nesterov * 4175606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 4176606a5020STejun Heo * on return if it hasn't been requeued since flush started. 4177401a8d04STejun Heo * 4178d185af30SYacine Belkadi * Return: 4179401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 4180401a8d04STejun Heo * %false if it was already idle. 4181db700897SOleg Nesterov */ 4182401a8d04STejun Heo bool flush_work(struct work_struct *work) 4183db700897SOleg Nesterov { 4184d6e89786SJohannes Berg return __flush_work(work, false); 4185606a5020STejun Heo } 4186db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 4187db700897SOleg Nesterov 4188cdc6e4b3STejun Heo /** 4189cdc6e4b3STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 4190cdc6e4b3STejun Heo * @dwork: the delayed work to flush 4191cdc6e4b3STejun Heo * 4192cdc6e4b3STejun Heo * Delayed timer is cancelled and the pending work is queued for 4193cdc6e4b3STejun Heo * immediate execution. Like flush_work(), this function only 4194cdc6e4b3STejun Heo * considers the last queueing instance of @dwork. 4195cdc6e4b3STejun Heo * 4196cdc6e4b3STejun Heo * Return: 4197cdc6e4b3STejun Heo * %true if flush_work() waited for the work to finish execution, 4198cdc6e4b3STejun Heo * %false if it was already idle. 4199cdc6e4b3STejun Heo */ 4200cdc6e4b3STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 4201cdc6e4b3STejun Heo { 4202cdc6e4b3STejun Heo local_irq_disable(); 4203cdc6e4b3STejun Heo if (del_timer_sync(&dwork->timer)) 4204cdc6e4b3STejun Heo __queue_work(dwork->cpu, dwork->wq, &dwork->work); 4205cdc6e4b3STejun Heo local_irq_enable(); 4206cdc6e4b3STejun Heo return flush_work(&dwork->work); 4207cdc6e4b3STejun Heo } 4208cdc6e4b3STejun Heo EXPORT_SYMBOL(flush_delayed_work); 4209cdc6e4b3STejun Heo 4210cdc6e4b3STejun Heo /** 4211cdc6e4b3STejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 4212cdc6e4b3STejun Heo * @rwork: the rcu work to flush 4213cdc6e4b3STejun Heo * 4214cdc6e4b3STejun Heo * Return: 4215cdc6e4b3STejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 4216cdc6e4b3STejun Heo * %false if it was already idle. 4217cdc6e4b3STejun Heo */ 4218cdc6e4b3STejun Heo bool flush_rcu_work(struct rcu_work *rwork) 4219cdc6e4b3STejun Heo { 4220cdc6e4b3STejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 4221cdc6e4b3STejun Heo rcu_barrier(); 4222cdc6e4b3STejun Heo flush_work(&rwork->work); 4223cdc6e4b3STejun Heo return true; 4224cdc6e4b3STejun Heo } else { 4225cdc6e4b3STejun Heo return flush_work(&rwork->work); 4226cdc6e4b3STejun Heo } 4227cdc6e4b3STejun Heo } 4228cdc6e4b3STejun Heo EXPORT_SYMBOL(flush_rcu_work); 4229cdc6e4b3STejun Heo 423086898fa6STejun Heo static void work_offqd_disable(struct work_offq_data *offqd) 423186898fa6STejun Heo { 423286898fa6STejun Heo const unsigned long max = (1lu << WORK_OFFQ_DISABLE_BITS) - 1; 423386898fa6STejun Heo 423486898fa6STejun Heo if (likely(offqd->disable < max)) 423586898fa6STejun Heo offqd->disable++; 423686898fa6STejun Heo else 423786898fa6STejun Heo WARN_ONCE(true, "workqueue: work disable count overflowed\n"); 423886898fa6STejun Heo } 423986898fa6STejun Heo 424086898fa6STejun Heo static void work_offqd_enable(struct work_offq_data *offqd) 424186898fa6STejun Heo { 424286898fa6STejun Heo if (likely(offqd->disable > 0)) 424386898fa6STejun Heo offqd->disable--; 424486898fa6STejun Heo else 424586898fa6STejun Heo WARN_ONCE(true, "workqueue: work disable count underflowed\n"); 424686898fa6STejun Heo } 424786898fa6STejun Heo 4248c5f5b942STejun Heo static bool __cancel_work(struct work_struct *work, u32 cflags) 4249cdc6e4b3STejun Heo { 42501211f3b2STejun Heo struct work_offq_data offqd; 4251c26e2f2eSTejun Heo unsigned long irq_flags; 4252cdc6e4b3STejun Heo int ret; 4253cdc6e4b3STejun Heo 425486898fa6STejun Heo ret = work_grab_pending(work, cflags, &irq_flags); 4255cdc6e4b3STejun Heo 42561211f3b2STejun Heo work_offqd_unpack(&offqd, *work_data_bits(work)); 425786898fa6STejun Heo 425886898fa6STejun Heo if (cflags & WORK_CANCEL_DISABLE) 425986898fa6STejun Heo work_offqd_disable(&offqd); 426086898fa6STejun Heo 42611211f3b2STejun Heo set_work_pool_and_clear_pending(work, offqd.pool_id, 42621211f3b2STejun Heo work_offqd_pack_flags(&offqd)); 4263c26e2f2eSTejun Heo local_irq_restore(irq_flags); 4264cdc6e4b3STejun Heo return ret; 4265cdc6e4b3STejun Heo } 4266cdc6e4b3STejun Heo 4267c5f5b942STejun Heo static bool __cancel_work_sync(struct work_struct *work, u32 cflags) 4268401a8d04STejun Heo { 4269978b8409STejun Heo bool ret; 42701f1f642eSOleg Nesterov 4271*f09b10b6STejun Heo ret = __cancel_work(work, cflags | WORK_CANCEL_DISABLE); 4272bbb68dfaSTejun Heo 42733347fa09STejun Heo /* 4274c7a40c49STejun Heo * Skip __flush_work() during early boot when we know that @work isn't 4275c7a40c49STejun Heo * executing. This allows canceling during early boot. 42763347fa09STejun Heo */ 42773347fa09STejun Heo if (wq_online) 4278d6e89786SJohannes Berg __flush_work(work, true); 42793347fa09STejun Heo 4280*f09b10b6STejun Heo if (!(cflags & WORK_CANCEL_DISABLE)) 4281*f09b10b6STejun Heo enable_work(work); 42828603e1b3STejun Heo 42831f1f642eSOleg Nesterov return ret; 42841f1f642eSOleg Nesterov } 42851f1f642eSOleg Nesterov 4286cdc6e4b3STejun Heo /* 4287cdc6e4b3STejun Heo * See cancel_delayed_work() 4288cdc6e4b3STejun Heo */ 4289cdc6e4b3STejun Heo bool cancel_work(struct work_struct *work) 4290cdc6e4b3STejun Heo { 4291c5f5b942STejun Heo return __cancel_work(work, 0); 4292cdc6e4b3STejun Heo } 4293cdc6e4b3STejun Heo EXPORT_SYMBOL(cancel_work); 4294cdc6e4b3STejun Heo 42956e84d644SOleg Nesterov /** 4296401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 4297401a8d04STejun Heo * @work: the work to cancel 42986e84d644SOleg Nesterov * 4299401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 4300401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 4301401a8d04STejun Heo * another workqueue. On return from this function, @work is 4302401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 43031f1f642eSOleg Nesterov * 4304401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 4305401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 43066e84d644SOleg Nesterov * 4307401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 43086e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 4309401a8d04STejun Heo * 4310d185af30SYacine Belkadi * Return: 4311401a8d04STejun Heo * %true if @work was pending, %false otherwise. 43126e84d644SOleg Nesterov */ 4313401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 43146e84d644SOleg Nesterov { 4315c5f5b942STejun Heo return __cancel_work_sync(work, 0); 4316b89deed3SOleg Nesterov } 431728e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 4318b89deed3SOleg Nesterov 43196e84d644SOleg Nesterov /** 432057b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 432157b30ae7STejun Heo * @dwork: delayed_work to cancel 432209383498STejun Heo * 4323d185af30SYacine Belkadi * Kill off a pending delayed_work. 4324d185af30SYacine Belkadi * 4325d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 4326d185af30SYacine Belkadi * pending. 4327d185af30SYacine Belkadi * 4328d185af30SYacine Belkadi * Note: 4329d185af30SYacine Belkadi * The work callback function may still be running on return, unless 4330d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 4331d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 433209383498STejun Heo * 433357b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 433409383498STejun Heo */ 433557b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 433609383498STejun Heo { 4337c5f5b942STejun Heo return __cancel_work(&dwork->work, WORK_CANCEL_DELAYED); 433809383498STejun Heo } 433957b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 434009383498STejun Heo 434109383498STejun Heo /** 4342401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 4343401a8d04STejun Heo * @dwork: the delayed work cancel 4344401a8d04STejun Heo * 4345401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 4346401a8d04STejun Heo * 4347d185af30SYacine Belkadi * Return: 4348401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 4349401a8d04STejun Heo */ 4350401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 43516e84d644SOleg Nesterov { 4352c5f5b942STejun Heo return __cancel_work_sync(&dwork->work, WORK_CANCEL_DELAYED); 43536e84d644SOleg Nesterov } 4354f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 43551da177e4SLinus Torvalds 43560fcb78c2SRolf Eike Beer /** 435786898fa6STejun Heo * disable_work - Disable and cancel a work item 435886898fa6STejun Heo * @work: work item to disable 435986898fa6STejun Heo * 436086898fa6STejun Heo * Disable @work by incrementing its disable count and cancel it if currently 436186898fa6STejun Heo * pending. As long as the disable count is non-zero, any attempt to queue @work 436286898fa6STejun Heo * will fail and return %false. The maximum supported disable depth is 2 to the 436386898fa6STejun Heo * power of %WORK_OFFQ_DISABLE_BITS, currently 65536. 436486898fa6STejun Heo * 4365*f09b10b6STejun Heo * Can be called from any context. Returns %true if @work was pending, %false 4366*f09b10b6STejun Heo * otherwise. 436786898fa6STejun Heo */ 436886898fa6STejun Heo bool disable_work(struct work_struct *work) 436986898fa6STejun Heo { 437086898fa6STejun Heo return __cancel_work(work, WORK_CANCEL_DISABLE); 437186898fa6STejun Heo } 437286898fa6STejun Heo EXPORT_SYMBOL_GPL(disable_work); 437386898fa6STejun Heo 437486898fa6STejun Heo /** 437586898fa6STejun Heo * disable_work_sync - Disable, cancel and drain a work item 437686898fa6STejun Heo * @work: work item to disable 437786898fa6STejun Heo * 437886898fa6STejun Heo * Similar to disable_work() but also wait for @work to finish if currently 437986898fa6STejun Heo * executing. 438086898fa6STejun Heo * 438186898fa6STejun Heo * Must be called from a sleepable context. Returns %true if @work was pending, 438286898fa6STejun Heo * %false otherwise. 438386898fa6STejun Heo */ 438486898fa6STejun Heo bool disable_work_sync(struct work_struct *work) 438586898fa6STejun Heo { 438686898fa6STejun Heo return __cancel_work_sync(work, WORK_CANCEL_DISABLE); 438786898fa6STejun Heo } 438886898fa6STejun Heo EXPORT_SYMBOL_GPL(disable_work_sync); 438986898fa6STejun Heo 439086898fa6STejun Heo /** 439186898fa6STejun Heo * enable_work - Enable a work item 439286898fa6STejun Heo * @work: work item to enable 439386898fa6STejun Heo * 439486898fa6STejun Heo * Undo disable_work[_sync]() by decrementing @work's disable count. @work can 439586898fa6STejun Heo * only be queued if its disable count is 0. 439686898fa6STejun Heo * 4397*f09b10b6STejun Heo * Can be called from any context. Returns %true if the disable count reached 0. 4398*f09b10b6STejun Heo * Otherwise, %false. 439986898fa6STejun Heo */ 440086898fa6STejun Heo bool enable_work(struct work_struct *work) 440186898fa6STejun Heo { 440286898fa6STejun Heo struct work_offq_data offqd; 440386898fa6STejun Heo unsigned long irq_flags; 440486898fa6STejun Heo 440586898fa6STejun Heo work_grab_pending(work, 0, &irq_flags); 440686898fa6STejun Heo 440786898fa6STejun Heo work_offqd_unpack(&offqd, *work_data_bits(work)); 440886898fa6STejun Heo work_offqd_enable(&offqd); 440986898fa6STejun Heo set_work_pool_and_clear_pending(work, offqd.pool_id, 441086898fa6STejun Heo work_offqd_pack_flags(&offqd)); 441186898fa6STejun Heo local_irq_restore(irq_flags); 441286898fa6STejun Heo 441386898fa6STejun Heo return !offqd.disable; 441486898fa6STejun Heo } 441586898fa6STejun Heo EXPORT_SYMBOL_GPL(enable_work); 441686898fa6STejun Heo 441786898fa6STejun Heo /** 441886898fa6STejun Heo * disable_delayed_work - Disable and cancel a delayed work item 441986898fa6STejun Heo * @dwork: delayed work item to disable 442086898fa6STejun Heo * 442186898fa6STejun Heo * disable_work() for delayed work items. 442286898fa6STejun Heo */ 442386898fa6STejun Heo bool disable_delayed_work(struct delayed_work *dwork) 442486898fa6STejun Heo { 442586898fa6STejun Heo return __cancel_work(&dwork->work, 442686898fa6STejun Heo WORK_CANCEL_DELAYED | WORK_CANCEL_DISABLE); 442786898fa6STejun Heo } 442886898fa6STejun Heo EXPORT_SYMBOL_GPL(disable_delayed_work); 442986898fa6STejun Heo 443086898fa6STejun Heo /** 443186898fa6STejun Heo * disable_delayed_work_sync - Disable, cancel and drain a delayed work item 443286898fa6STejun Heo * @dwork: delayed work item to disable 443386898fa6STejun Heo * 443486898fa6STejun Heo * disable_work_sync() for delayed work items. 443586898fa6STejun Heo */ 443686898fa6STejun Heo bool disable_delayed_work_sync(struct delayed_work *dwork) 443786898fa6STejun Heo { 443886898fa6STejun Heo return __cancel_work_sync(&dwork->work, 443986898fa6STejun Heo WORK_CANCEL_DELAYED | WORK_CANCEL_DISABLE); 444086898fa6STejun Heo } 444186898fa6STejun Heo EXPORT_SYMBOL_GPL(disable_delayed_work_sync); 444286898fa6STejun Heo 444386898fa6STejun Heo /** 444486898fa6STejun Heo * enable_delayed_work - Enable a delayed work item 444586898fa6STejun Heo * @dwork: delayed work item to enable 444686898fa6STejun Heo * 444786898fa6STejun Heo * enable_work() for delayed work items. 444886898fa6STejun Heo */ 444986898fa6STejun Heo bool enable_delayed_work(struct delayed_work *dwork) 445086898fa6STejun Heo { 445186898fa6STejun Heo return enable_work(&dwork->work); 445286898fa6STejun Heo } 445386898fa6STejun Heo EXPORT_SYMBOL_GPL(enable_delayed_work); 445486898fa6STejun Heo 445586898fa6STejun Heo /** 445631ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 4457b6136773SAndrew Morton * @func: the function to call 4458b6136773SAndrew Morton * 445931ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 446031ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 4461b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 446231ddd871STejun Heo * 4463d185af30SYacine Belkadi * Return: 446431ddd871STejun Heo * 0 on success, -errno on failure. 4465b6136773SAndrew Morton */ 446665f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 446715316ba8SChristoph Lameter { 446815316ba8SChristoph Lameter int cpu; 446938f51568SNamhyung Kim struct work_struct __percpu *works; 447015316ba8SChristoph Lameter 4471b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 4472b6136773SAndrew Morton if (!works) 447315316ba8SChristoph Lameter return -ENOMEM; 4474b6136773SAndrew Morton 4475ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 447693981800STejun Heo 447715316ba8SChristoph Lameter for_each_online_cpu(cpu) { 44789bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 44799bfb1839SIngo Molnar 44809bfb1839SIngo Molnar INIT_WORK(work, func); 44818de6d308SOleg Nesterov schedule_work_on(cpu, work); 448215316ba8SChristoph Lameter } 448393981800STejun Heo 448493981800STejun Heo for_each_online_cpu(cpu) 44858616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 448693981800STejun Heo 4487ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4488b6136773SAndrew Morton free_percpu(works); 448915316ba8SChristoph Lameter return 0; 449015316ba8SChristoph Lameter } 449115316ba8SChristoph Lameter 4492eef6a7d5SAlan Stern /** 44931fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 44941fa44ecaSJames Bottomley * @fn: the function to execute 44951fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 44961fa44ecaSJames Bottomley * be available when the work executes) 44971fa44ecaSJames Bottomley * 44981fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 44991fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 45001fa44ecaSJames Bottomley * 4501d185af30SYacine Belkadi * Return: 0 - function was executed 45021fa44ecaSJames Bottomley * 1 - function was scheduled for execution 45031fa44ecaSJames Bottomley */ 450465f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 45051fa44ecaSJames Bottomley { 45061fa44ecaSJames Bottomley if (!in_interrupt()) { 450765f27f38SDavid Howells fn(&ew->work); 45081fa44ecaSJames Bottomley return 0; 45091fa44ecaSJames Bottomley } 45101fa44ecaSJames Bottomley 451165f27f38SDavid Howells INIT_WORK(&ew->work, fn); 45121fa44ecaSJames Bottomley schedule_work(&ew->work); 45131fa44ecaSJames Bottomley 45141fa44ecaSJames Bottomley return 1; 45151fa44ecaSJames Bottomley } 45161fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 45171fa44ecaSJames Bottomley 45187a4e344cSTejun Heo /** 45197a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 45207a4e344cSTejun Heo * @attrs: workqueue_attrs to free 45217a4e344cSTejun Heo * 45227a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 45237a4e344cSTejun Heo */ 4524513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs) 45257a4e344cSTejun Heo { 45267a4e344cSTejun Heo if (attrs) { 45277a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 45289546b29eSTejun Heo free_cpumask_var(attrs->__pod_cpumask); 45297a4e344cSTejun Heo kfree(attrs); 45307a4e344cSTejun Heo } 45317a4e344cSTejun Heo } 45327a4e344cSTejun Heo 45337a4e344cSTejun Heo /** 45347a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 45357a4e344cSTejun Heo * 45367a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 4537d185af30SYacine Belkadi * return it. 4538d185af30SYacine Belkadi * 4539d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 45407a4e344cSTejun Heo */ 4541513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void) 45427a4e344cSTejun Heo { 45437a4e344cSTejun Heo struct workqueue_attrs *attrs; 45447a4e344cSTejun Heo 4545be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 45467a4e344cSTejun Heo if (!attrs) 45477a4e344cSTejun Heo goto fail; 4548be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) 45497a4e344cSTejun Heo goto fail; 45509546b29eSTejun Heo if (!alloc_cpumask_var(&attrs->__pod_cpumask, GFP_KERNEL)) 45519546b29eSTejun Heo goto fail; 45527a4e344cSTejun Heo 455313e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 4554523a301eSTejun Heo attrs->affn_scope = WQ_AFFN_DFL; 45557a4e344cSTejun Heo return attrs; 45567a4e344cSTejun Heo fail: 45577a4e344cSTejun Heo free_workqueue_attrs(attrs); 45587a4e344cSTejun Heo return NULL; 45597a4e344cSTejun Heo } 45607a4e344cSTejun Heo 456129c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 456229c91e99STejun Heo const struct workqueue_attrs *from) 456329c91e99STejun Heo { 456429c91e99STejun Heo to->nice = from->nice; 456529c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 45669546b29eSTejun Heo cpumask_copy(to->__pod_cpumask, from->__pod_cpumask); 45678639ecebSTejun Heo to->affn_strict = from->affn_strict; 456884193c07STejun Heo 45692865a8fbSShaohua Li /* 457084193c07STejun Heo * Unlike hash and equality test, copying shouldn't ignore wq-only 457184193c07STejun Heo * fields as copying is used for both pool and wq attrs. Instead, 457284193c07STejun Heo * get_unbound_pool() explicitly clears the fields. 45732865a8fbSShaohua Li */ 457484193c07STejun Heo to->affn_scope = from->affn_scope; 4575af73f5c9STejun Heo to->ordered = from->ordered; 457629c91e99STejun Heo } 457729c91e99STejun Heo 45785de7a03cSTejun Heo /* 45795de7a03cSTejun Heo * Some attrs fields are workqueue-only. Clear them for worker_pool's. See the 45805de7a03cSTejun Heo * comments in 'struct workqueue_attrs' definition. 45815de7a03cSTejun Heo */ 45825de7a03cSTejun Heo static void wqattrs_clear_for_pool(struct workqueue_attrs *attrs) 45835de7a03cSTejun Heo { 458484193c07STejun Heo attrs->affn_scope = WQ_AFFN_NR_TYPES; 45855de7a03cSTejun Heo attrs->ordered = false; 45865de7a03cSTejun Heo } 45875de7a03cSTejun Heo 458829c91e99STejun Heo /* hash value of the content of @attr */ 458929c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 459029c91e99STejun Heo { 459129c91e99STejun Heo u32 hash = 0; 459229c91e99STejun Heo 459329c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 459413e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 459513e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 45969546b29eSTejun Heo hash = jhash(cpumask_bits(attrs->__pod_cpumask), 45979546b29eSTejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 45988639ecebSTejun Heo hash = jhash_1word(attrs->affn_strict, hash); 459929c91e99STejun Heo return hash; 460029c91e99STejun Heo } 460129c91e99STejun Heo 460229c91e99STejun Heo /* content equality test */ 460329c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 460429c91e99STejun Heo const struct workqueue_attrs *b) 460529c91e99STejun Heo { 460629c91e99STejun Heo if (a->nice != b->nice) 460729c91e99STejun Heo return false; 460829c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 460929c91e99STejun Heo return false; 46109546b29eSTejun Heo if (!cpumask_equal(a->__pod_cpumask, b->__pod_cpumask)) 46119546b29eSTejun Heo return false; 46128639ecebSTejun Heo if (a->affn_strict != b->affn_strict) 46138639ecebSTejun Heo return false; 461429c91e99STejun Heo return true; 461529c91e99STejun Heo } 461629c91e99STejun Heo 46170f36ee24STejun Heo /* Update @attrs with actually available CPUs */ 46180f36ee24STejun Heo static void wqattrs_actualize_cpumask(struct workqueue_attrs *attrs, 46190f36ee24STejun Heo const cpumask_t *unbound_cpumask) 46200f36ee24STejun Heo { 46210f36ee24STejun Heo /* 46220f36ee24STejun Heo * Calculate the effective CPU mask of @attrs given @unbound_cpumask. If 46230f36ee24STejun Heo * @attrs->cpumask doesn't overlap with @unbound_cpumask, we fallback to 46240f36ee24STejun Heo * @unbound_cpumask. 46250f36ee24STejun Heo */ 46260f36ee24STejun Heo cpumask_and(attrs->cpumask, attrs->cpumask, unbound_cpumask); 46270f36ee24STejun Heo if (unlikely(cpumask_empty(attrs->cpumask))) 46280f36ee24STejun Heo cpumask_copy(attrs->cpumask, unbound_cpumask); 46290f36ee24STejun Heo } 46300f36ee24STejun Heo 463184193c07STejun Heo /* find wq_pod_type to use for @attrs */ 463284193c07STejun Heo static const struct wq_pod_type * 463384193c07STejun Heo wqattrs_pod_type(const struct workqueue_attrs *attrs) 463484193c07STejun Heo { 4635523a301eSTejun Heo enum wq_affn_scope scope; 4636523a301eSTejun Heo struct wq_pod_type *pt; 4637523a301eSTejun Heo 4638523a301eSTejun Heo /* to synchronize access to wq_affn_dfl */ 4639523a301eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4640523a301eSTejun Heo 4641523a301eSTejun Heo if (attrs->affn_scope == WQ_AFFN_DFL) 4642523a301eSTejun Heo scope = wq_affn_dfl; 4643523a301eSTejun Heo else 4644523a301eSTejun Heo scope = attrs->affn_scope; 4645523a301eSTejun Heo 4646523a301eSTejun Heo pt = &wq_pod_types[scope]; 464784193c07STejun Heo 464884193c07STejun Heo if (!WARN_ON_ONCE(attrs->affn_scope == WQ_AFFN_NR_TYPES) && 464984193c07STejun Heo likely(pt->nr_pods)) 465084193c07STejun Heo return pt; 465184193c07STejun Heo 465284193c07STejun Heo /* 465384193c07STejun Heo * Before workqueue_init_topology(), only SYSTEM is available which is 465484193c07STejun Heo * initialized in workqueue_init_early(). 465584193c07STejun Heo */ 465684193c07STejun Heo pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 465784193c07STejun Heo BUG_ON(!pt->nr_pods); 465884193c07STejun Heo return pt; 465984193c07STejun Heo } 466084193c07STejun Heo 46617a4e344cSTejun Heo /** 46627a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 46637a4e344cSTejun Heo * @pool: worker_pool to initialize 46647a4e344cSTejun Heo * 4665402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 4666d185af30SYacine Belkadi * 4667d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 466829c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 466929c91e99STejun Heo * on @pool safely to release it. 46707a4e344cSTejun Heo */ 46717a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 46724e1a1f9aSTejun Heo { 4673a9b8a985SSebastian Andrzej Siewior raw_spin_lock_init(&pool->lock); 467429c91e99STejun Heo pool->id = -1; 467529c91e99STejun Heo pool->cpu = -1; 4676f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 46774e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 467882607adcSTejun Heo pool->watchdog_ts = jiffies; 46794e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 46804e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 46814e1a1f9aSTejun Heo hash_init(pool->busy_hash); 46824e1a1f9aSTejun Heo 468332a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 46843f959aa3SValentin Schneider INIT_WORK(&pool->idle_cull_work, idle_cull_fn); 46854e1a1f9aSTejun Heo 468632a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 46874e1a1f9aSTejun Heo 4688da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 4689e02b9312SValentin Schneider INIT_LIST_HEAD(&pool->dying_workers); 46907a4e344cSTejun Heo 46917cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 469229c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 469329c91e99STejun Heo pool->refcnt = 1; 469429c91e99STejun Heo 469529c91e99STejun Heo /* shouldn't fail above this point */ 4696be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs(); 46977a4e344cSTejun Heo if (!pool->attrs) 46987a4e344cSTejun Heo return -ENOMEM; 46995de7a03cSTejun Heo 47005de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 47015de7a03cSTejun Heo 47027a4e344cSTejun Heo return 0; 47034e1a1f9aSTejun Heo } 47044e1a1f9aSTejun Heo 4705669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP 4706669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4707669de8bdSBart Van Assche { 4708669de8bdSBart Van Assche char *lock_name; 4709669de8bdSBart Van Assche 4710669de8bdSBart Van Assche lockdep_register_key(&wq->key); 4711669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); 4712669de8bdSBart Van Assche if (!lock_name) 4713669de8bdSBart Van Assche lock_name = wq->name; 471469a106c0SQian Cai 471569a106c0SQian Cai wq->lock_name = lock_name; 4716669de8bdSBart Van Assche lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); 4717669de8bdSBart Van Assche } 4718669de8bdSBart Van Assche 4719669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4720669de8bdSBart Van Assche { 4721669de8bdSBart Van Assche lockdep_unregister_key(&wq->key); 4722669de8bdSBart Van Assche } 4723669de8bdSBart Van Assche 4724669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4725669de8bdSBart Van Assche { 4726669de8bdSBart Van Assche if (wq->lock_name != wq->name) 4727669de8bdSBart Van Assche kfree(wq->lock_name); 4728669de8bdSBart Van Assche } 4729669de8bdSBart Van Assche #else 4730669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4731669de8bdSBart Van Assche { 4732669de8bdSBart Van Assche } 4733669de8bdSBart Van Assche 4734669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4735669de8bdSBart Van Assche { 4736669de8bdSBart Van Assche } 4737669de8bdSBart Van Assche 4738669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4739669de8bdSBart Van Assche { 4740669de8bdSBart Van Assche } 4741669de8bdSBart Van Assche #endif 4742669de8bdSBart Van Assche 474391ccc6e7STejun Heo static void free_node_nr_active(struct wq_node_nr_active **nna_ar) 474491ccc6e7STejun Heo { 474591ccc6e7STejun Heo int node; 474691ccc6e7STejun Heo 474791ccc6e7STejun Heo for_each_node(node) { 474891ccc6e7STejun Heo kfree(nna_ar[node]); 474991ccc6e7STejun Heo nna_ar[node] = NULL; 475091ccc6e7STejun Heo } 475191ccc6e7STejun Heo 475291ccc6e7STejun Heo kfree(nna_ar[nr_node_ids]); 475391ccc6e7STejun Heo nna_ar[nr_node_ids] = NULL; 475491ccc6e7STejun Heo } 475591ccc6e7STejun Heo 475691ccc6e7STejun Heo static void init_node_nr_active(struct wq_node_nr_active *nna) 475791ccc6e7STejun Heo { 4758c5f8cd6cSTejun Heo nna->max = WQ_DFL_MIN_ACTIVE; 475991ccc6e7STejun Heo atomic_set(&nna->nr, 0); 47605797b1c1STejun Heo raw_spin_lock_init(&nna->lock); 47615797b1c1STejun Heo INIT_LIST_HEAD(&nna->pending_pwqs); 476291ccc6e7STejun Heo } 476391ccc6e7STejun Heo 476491ccc6e7STejun Heo /* 476591ccc6e7STejun Heo * Each node's nr_active counter will be accessed mostly from its own node and 476691ccc6e7STejun Heo * should be allocated in the node. 476791ccc6e7STejun Heo */ 476891ccc6e7STejun Heo static int alloc_node_nr_active(struct wq_node_nr_active **nna_ar) 476991ccc6e7STejun Heo { 477091ccc6e7STejun Heo struct wq_node_nr_active *nna; 477191ccc6e7STejun Heo int node; 477291ccc6e7STejun Heo 477391ccc6e7STejun Heo for_each_node(node) { 477491ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, node); 477591ccc6e7STejun Heo if (!nna) 477691ccc6e7STejun Heo goto err_free; 477791ccc6e7STejun Heo init_node_nr_active(nna); 477891ccc6e7STejun Heo nna_ar[node] = nna; 477991ccc6e7STejun Heo } 478091ccc6e7STejun Heo 478191ccc6e7STejun Heo /* [nr_node_ids] is used as the fallback */ 478291ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, NUMA_NO_NODE); 478391ccc6e7STejun Heo if (!nna) 478491ccc6e7STejun Heo goto err_free; 478591ccc6e7STejun Heo init_node_nr_active(nna); 478691ccc6e7STejun Heo nna_ar[nr_node_ids] = nna; 478791ccc6e7STejun Heo 478891ccc6e7STejun Heo return 0; 478991ccc6e7STejun Heo 479091ccc6e7STejun Heo err_free: 479191ccc6e7STejun Heo free_node_nr_active(nna_ar); 479291ccc6e7STejun Heo return -ENOMEM; 479391ccc6e7STejun Heo } 479491ccc6e7STejun Heo 4795e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 4796e2dca7adSTejun Heo { 4797e2dca7adSTejun Heo struct workqueue_struct *wq = 4798e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 4799e2dca7adSTejun Heo 480091ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 480191ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 480291ccc6e7STejun Heo 4803669de8bdSBart Van Assche wq_free_lockdep(wq); 4804ee1ceef7STejun Heo free_percpu(wq->cpu_pwq); 4805e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 4806e2dca7adSTejun Heo kfree(wq); 4807e2dca7adSTejun Heo } 4808e2dca7adSTejun Heo 480929c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 481029c91e99STejun Heo { 481129c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 481229c91e99STejun Heo 48137cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 481429c91e99STejun Heo free_workqueue_attrs(pool->attrs); 481529c91e99STejun Heo kfree(pool); 481629c91e99STejun Heo } 481729c91e99STejun Heo 481829c91e99STejun Heo /** 481929c91e99STejun Heo * put_unbound_pool - put a worker_pool 482029c91e99STejun Heo * @pool: worker_pool to put 482129c91e99STejun Heo * 482224acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU 4823c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 4824c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 4825c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 4826a892caccSTejun Heo * 4827a892caccSTejun Heo * Should be called with wq_pool_mutex held. 482829c91e99STejun Heo */ 482929c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 483029c91e99STejun Heo { 483160f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 483229c91e99STejun Heo struct worker *worker; 48339680540cSYang Yingliang LIST_HEAD(cull_list); 4834e02b9312SValentin Schneider 4835a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4836a892caccSTejun Heo 4837a892caccSTejun Heo if (--pool->refcnt) 483829c91e99STejun Heo return; 483929c91e99STejun Heo 484029c91e99STejun Heo /* sanity checks */ 484161d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 4842a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 484329c91e99STejun Heo return; 484429c91e99STejun Heo 484529c91e99STejun Heo /* release id and unhash */ 484629c91e99STejun Heo if (pool->id >= 0) 484729c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 484829c91e99STejun Heo hash_del(&pool->hash_node); 484929c91e99STejun Heo 4850c5aa87bbSTejun Heo /* 4851692b4825STejun Heo * Become the manager and destroy all workers. This prevents 4852692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 4853692b4825STejun Heo * manager and @pool gets freed with the flag set. 48549ab03be4SValentin Schneider * 48559ab03be4SValentin Schneider * Having a concurrent manager is quite unlikely to happen as we can 48569ab03be4SValentin Schneider * only get here with 48579ab03be4SValentin Schneider * pwq->refcnt == pool->refcnt == 0 48589ab03be4SValentin Schneider * which implies no work queued to the pool, which implies no worker can 48599ab03be4SValentin Schneider * become the manager. However a worker could have taken the role of 48609ab03be4SValentin Schneider * manager before the refcnts dropped to 0, since maybe_create_worker() 48619ab03be4SValentin Schneider * drops pool->lock 4862c5aa87bbSTejun Heo */ 48639ab03be4SValentin Schneider while (true) { 48649ab03be4SValentin Schneider rcuwait_wait_event(&manager_wait, 48659ab03be4SValentin Schneider !(pool->flags & POOL_MANAGER_ACTIVE), 4866d8bb65abSSebastian Andrzej Siewior TASK_UNINTERRUPTIBLE); 4867e02b9312SValentin Schneider 4868e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 48699ab03be4SValentin Schneider raw_spin_lock_irq(&pool->lock); 48709ab03be4SValentin Schneider if (!(pool->flags & POOL_MANAGER_ACTIVE)) { 4871692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 48729ab03be4SValentin Schneider break; 48739ab03be4SValentin Schneider } 48749ab03be4SValentin Schneider raw_spin_unlock_irq(&pool->lock); 4875e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 48769ab03be4SValentin Schneider } 4877692b4825STejun Heo 48781037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 4879e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 488029c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 4881a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 488260f5a4bcSLai Jiangshan 4883e02b9312SValentin Schneider wake_dying_workers(&cull_list); 4884e02b9312SValentin Schneider 4885e02b9312SValentin Schneider if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) 488660f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 48871258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 488860f5a4bcSLai Jiangshan 488960f5a4bcSLai Jiangshan if (pool->detach_completion) 489060f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 489160f5a4bcSLai Jiangshan 489229c91e99STejun Heo /* shut down the timers */ 489329c91e99STejun Heo del_timer_sync(&pool->idle_timer); 48943f959aa3SValentin Schneider cancel_work_sync(&pool->idle_cull_work); 489529c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 489629c91e99STejun Heo 489724acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */ 489825b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 489929c91e99STejun Heo } 490029c91e99STejun Heo 490129c91e99STejun Heo /** 490229c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 490329c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 490429c91e99STejun Heo * 490529c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 490629c91e99STejun Heo * reference count and return it. If there already is a matching 490729c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 4908d185af30SYacine Belkadi * create a new one. 4909a892caccSTejun Heo * 4910a892caccSTejun Heo * Should be called with wq_pool_mutex held. 4911d185af30SYacine Belkadi * 4912d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 4913d185af30SYacine Belkadi * On failure, %NULL. 491429c91e99STejun Heo */ 491529c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 491629c91e99STejun Heo { 491784193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_NUMA]; 491829c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 491929c91e99STejun Heo struct worker_pool *pool; 492084193c07STejun Heo int pod, node = NUMA_NO_NODE; 492129c91e99STejun Heo 4922a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 492329c91e99STejun Heo 492429c91e99STejun Heo /* do we already have a matching pool? */ 492529c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 492629c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 492729c91e99STejun Heo pool->refcnt++; 49283fb1823cSLai Jiangshan return pool; 492929c91e99STejun Heo } 493029c91e99STejun Heo } 493129c91e99STejun Heo 49329546b29eSTejun Heo /* If __pod_cpumask is contained inside a NUMA pod, that's our node */ 493384193c07STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) { 49349546b29eSTejun Heo if (cpumask_subset(attrs->__pod_cpumask, pt->pod_cpus[pod])) { 493584193c07STejun Heo node = pt->pod_node[pod]; 4936e2273584SXunlei Pang break; 4937e2273584SXunlei Pang } 4938e2273584SXunlei Pang } 4939e2273584SXunlei Pang 494029c91e99STejun Heo /* nope, create a new one */ 494184193c07STejun Heo pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, node); 494229c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 494329c91e99STejun Heo goto fail; 494429c91e99STejun Heo 494584193c07STejun Heo pool->node = node; 49465de7a03cSTejun Heo copy_workqueue_attrs(pool->attrs, attrs); 49475de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 49482865a8fbSShaohua Li 494929c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 495029c91e99STejun Heo goto fail; 495129c91e99STejun Heo 495229c91e99STejun Heo /* create and start the initial worker */ 49533347fa09STejun Heo if (wq_online && !create_worker(pool)) 495429c91e99STejun Heo goto fail; 495529c91e99STejun Heo 495629c91e99STejun Heo /* install */ 495729c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 49583fb1823cSLai Jiangshan 495929c91e99STejun Heo return pool; 496029c91e99STejun Heo fail: 496129c91e99STejun Heo if (pool) 496229c91e99STejun Heo put_unbound_pool(pool); 496329c91e99STejun Heo return NULL; 496429c91e99STejun Heo } 496529c91e99STejun Heo 49668864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 49678864b4e5STejun Heo { 49688864b4e5STejun Heo kmem_cache_free(pwq_cache, 49698864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 49708864b4e5STejun Heo } 49718864b4e5STejun Heo 49728864b4e5STejun Heo /* 4973967b494eSTejun Heo * Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero 4974967b494eSTejun Heo * refcnt and needs to be destroyed. 49758864b4e5STejun Heo */ 4976687a9aa5STejun Heo static void pwq_release_workfn(struct kthread_work *work) 49778864b4e5STejun Heo { 49788864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 4979687a9aa5STejun Heo release_work); 49808864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 49818864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 4982b42b0bddSYang Yingliang bool is_last = false; 49838864b4e5STejun Heo 4984b42b0bddSYang Yingliang /* 4985687a9aa5STejun Heo * When @pwq is not linked, it doesn't hold any reference to the 4986b42b0bddSYang Yingliang * @wq, and @wq is invalid to access. 4987b42b0bddSYang Yingliang */ 4988b42b0bddSYang Yingliang if (!list_empty(&pwq->pwqs_node)) { 49893c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 49908864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 4991bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 49924c065dbcSWaiman Long 49934c065dbcSWaiman Long /* 49944c065dbcSWaiman Long * For ordered workqueue with a plugged dfl_pwq, restart it now. 49954c065dbcSWaiman Long */ 49964c065dbcSWaiman Long if (!is_last && (wq->flags & __WQ_ORDERED)) 49974c065dbcSWaiman Long unplug_oldest_pwq(wq); 49984c065dbcSWaiman Long 49993c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 5000b42b0bddSYang Yingliang } 50018864b4e5STejun Heo 5002687a9aa5STejun Heo if (wq->flags & WQ_UNBOUND) { 5003a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 50048864b4e5STejun Heo put_unbound_pool(pool); 5005a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 5006687a9aa5STejun Heo } 5007a892caccSTejun Heo 50085797b1c1STejun Heo if (!list_empty(&pwq->pending_node)) { 50095797b1c1STejun Heo struct wq_node_nr_active *nna = 50105797b1c1STejun Heo wq_node_nr_active(pwq->wq, pwq->pool->node); 50115797b1c1STejun Heo 50125797b1c1STejun Heo raw_spin_lock_irq(&nna->lock); 50135797b1c1STejun Heo list_del_init(&pwq->pending_node); 50145797b1c1STejun Heo raw_spin_unlock_irq(&nna->lock); 50155797b1c1STejun Heo } 50165797b1c1STejun Heo 501725b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 50188864b4e5STejun Heo 50198864b4e5STejun Heo /* 50208864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 5021e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 50228864b4e5STejun Heo */ 5023669de8bdSBart Van Assche if (is_last) { 5024669de8bdSBart Van Assche wq_unregister_lockdep(wq); 502525b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 50266029a918STejun Heo } 5027669de8bdSBart Van Assche } 50288864b4e5STejun Heo 502967dc8325SCai Huoqing /* initialize newly allocated @pwq which is associated with @wq and @pool */ 5030f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 5031f147f29eSTejun Heo struct worker_pool *pool) 5032d2c1d404STejun Heo { 5033e9a8e01fSTejun Heo BUG_ON((unsigned long)pwq & ~WORK_STRUCT_PWQ_MASK); 5034d2c1d404STejun Heo 5035e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 5036e50aba9aSTejun Heo 5037d2c1d404STejun Heo pwq->pool = pool; 5038d2c1d404STejun Heo pwq->wq = wq; 5039d2c1d404STejun Heo pwq->flush_color = -1; 50408864b4e5STejun Heo pwq->refcnt = 1; 5041f97a4a1aSLai Jiangshan INIT_LIST_HEAD(&pwq->inactive_works); 50425797b1c1STejun Heo INIT_LIST_HEAD(&pwq->pending_node); 50431befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 5044d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 5045687a9aa5STejun Heo kthread_init_work(&pwq->release_work, pwq_release_workfn); 5046f147f29eSTejun Heo } 5047d2c1d404STejun Heo 5048f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 50491befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 5050f147f29eSTejun Heo { 5051f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 5052f147f29eSTejun Heo 5053f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 505475ccf595STejun Heo 50551befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 50561befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 50571befcf30STejun Heo return; 50581befcf30STejun Heo 505929b1cb41SLai Jiangshan /* set the matching work_color */ 506075ccf595STejun Heo pwq->work_color = wq->work_color; 5061983ca25eSTejun Heo 5062983ca25eSTejun Heo /* link in @pwq */ 506326fb7e3dSWaiman Long list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs); 5064df2d5ae4STejun Heo } 50656029a918STejun Heo 5066f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 5067f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 5068f147f29eSTejun Heo const struct workqueue_attrs *attrs) 5069f147f29eSTejun Heo { 5070f147f29eSTejun Heo struct worker_pool *pool; 5071f147f29eSTejun Heo struct pool_workqueue *pwq; 5072f147f29eSTejun Heo 5073f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 5074f147f29eSTejun Heo 5075f147f29eSTejun Heo pool = get_unbound_pool(attrs); 5076f147f29eSTejun Heo if (!pool) 5077f147f29eSTejun Heo return NULL; 5078f147f29eSTejun Heo 5079e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 5080f147f29eSTejun Heo if (!pwq) { 5081f147f29eSTejun Heo put_unbound_pool(pool); 5082f147f29eSTejun Heo return NULL; 5083f147f29eSTejun Heo } 5084f147f29eSTejun Heo 5085f147f29eSTejun Heo init_pwq(pwq, wq, pool); 5086f147f29eSTejun Heo return pwq; 5087d2c1d404STejun Heo } 5088d2c1d404STejun Heo 50894c16bd32STejun Heo /** 5090fef59c9cSTejun Heo * wq_calc_pod_cpumask - calculate a wq_attrs' cpumask for a pod 5091042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 509284193c07STejun Heo * @cpu: the target CPU 50934c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 50944c16bd32STejun Heo * 5095fef59c9cSTejun Heo * Calculate the cpumask a workqueue with @attrs should use on @pod. If 5096fef59c9cSTejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during calculation. 50979546b29eSTejun Heo * The result is stored in @attrs->__pod_cpumask. 50984c16bd32STejun Heo * 5099fef59c9cSTejun Heo * If pod affinity is not enabled, @attrs->cpumask is always used. If enabled 5100fef59c9cSTejun Heo * and @pod has online CPUs requested by @attrs, the returned cpumask is the 5101fef59c9cSTejun Heo * intersection of the possible CPUs of @pod and @attrs->cpumask. 51024c16bd32STejun Heo * 5103fef59c9cSTejun Heo * The caller is responsible for ensuring that the cpumask of @pod stays stable. 51044c16bd32STejun Heo */ 51059546b29eSTejun Heo static void wq_calc_pod_cpumask(struct workqueue_attrs *attrs, int cpu, 51069546b29eSTejun Heo int cpu_going_down) 51074c16bd32STejun Heo { 510884193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 510984193c07STejun Heo int pod = pt->cpu_pod[cpu]; 51104c16bd32STejun Heo 5111fef59c9cSTejun Heo /* does @pod have any online CPUs @attrs wants? */ 51129546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, pt->pod_cpus[pod], attrs->cpumask); 51139546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->__pod_cpumask, cpu_online_mask); 51144c16bd32STejun Heo if (cpu_going_down >= 0) 51159546b29eSTejun Heo cpumask_clear_cpu(cpu_going_down, attrs->__pod_cpumask); 51164c16bd32STejun Heo 51179546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) { 51189546b29eSTejun Heo cpumask_copy(attrs->__pod_cpumask, attrs->cpumask); 511984193c07STejun Heo return; 512084193c07STejun Heo } 51214c16bd32STejun Heo 5122fef59c9cSTejun Heo /* yeap, return possible CPUs in @pod that @attrs wants */ 51239546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->cpumask, pt->pod_cpus[pod]); 51241ad0f0a7SMichael Bringmann 51259546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) 51261ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 51271ad0f0a7SMichael Bringmann "possible intersect\n"); 51284c16bd32STejun Heo } 51294c16bd32STejun Heo 51309f66cff2STejun Heo /* install @pwq into @wq and return the old pwq, @cpu < 0 for dfl_pwq */ 5131636b927eSTejun Heo static struct pool_workqueue *install_unbound_pwq(struct workqueue_struct *wq, 5132636b927eSTejun Heo int cpu, struct pool_workqueue *pwq) 51331befcf30STejun Heo { 51349f66cff2STejun Heo struct pool_workqueue __rcu **slot = unbound_pwq_slot(wq, cpu); 51351befcf30STejun Heo struct pool_workqueue *old_pwq; 51361befcf30STejun Heo 51375b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 51381befcf30STejun Heo lockdep_assert_held(&wq->mutex); 51391befcf30STejun Heo 51401befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 51411befcf30STejun Heo link_pwq(pwq); 51421befcf30STejun Heo 51439f66cff2STejun Heo old_pwq = rcu_access_pointer(*slot); 51449f66cff2STejun Heo rcu_assign_pointer(*slot, pwq); 51451befcf30STejun Heo return old_pwq; 51461befcf30STejun Heo } 51471befcf30STejun Heo 51482d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 51492d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 51502d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 51512d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 5152042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 51532d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 51542d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 51552d5f0764SLai Jiangshan }; 51562d5f0764SLai Jiangshan 51572d5f0764SLai Jiangshan /* free the resources after success or abort */ 51582d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 51592d5f0764SLai Jiangshan { 51602d5f0764SLai Jiangshan if (ctx) { 5161636b927eSTejun Heo int cpu; 51622d5f0764SLai Jiangshan 5163636b927eSTejun Heo for_each_possible_cpu(cpu) 5164636b927eSTejun Heo put_pwq_unlocked(ctx->pwq_tbl[cpu]); 51652d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 51662d5f0764SLai Jiangshan 51672d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 51682d5f0764SLai Jiangshan 51692d5f0764SLai Jiangshan kfree(ctx); 51702d5f0764SLai Jiangshan } 51712d5f0764SLai Jiangshan } 51722d5f0764SLai Jiangshan 51732d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 51742d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 51752d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 517699c621efSLai Jiangshan const struct workqueue_attrs *attrs, 517799c621efSLai Jiangshan const cpumask_var_t unbound_cpumask) 51782d5f0764SLai Jiangshan { 51792d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 51809546b29eSTejun Heo struct workqueue_attrs *new_attrs; 5181636b927eSTejun Heo int cpu; 51822d5f0764SLai Jiangshan 51832d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 51842d5f0764SLai Jiangshan 518584193c07STejun Heo if (WARN_ON(attrs->affn_scope < 0 || 518684193c07STejun Heo attrs->affn_scope >= WQ_AFFN_NR_TYPES)) 518784193c07STejun Heo return ERR_PTR(-EINVAL); 518884193c07STejun Heo 5189636b927eSTejun Heo ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_cpu_ids), GFP_KERNEL); 51902d5f0764SLai Jiangshan 5191be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs(); 51929546b29eSTejun Heo if (!ctx || !new_attrs) 51932d5f0764SLai Jiangshan goto out_free; 51942d5f0764SLai Jiangshan 5195042f7df1SLai Jiangshan /* 51962d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 51972d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 51982d5f0764SLai Jiangshan * it even if we don't use it immediately. 51992d5f0764SLai Jiangshan */ 52000f36ee24STejun Heo copy_workqueue_attrs(new_attrs, attrs); 52010f36ee24STejun Heo wqattrs_actualize_cpumask(new_attrs, unbound_cpumask); 52029546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 52032d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 52042d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 52052d5f0764SLai Jiangshan goto out_free; 52062d5f0764SLai Jiangshan 5207636b927eSTejun Heo for_each_possible_cpu(cpu) { 5208af73f5c9STejun Heo if (new_attrs->ordered) { 52092d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 5210636b927eSTejun Heo ctx->pwq_tbl[cpu] = ctx->dfl_pwq; 5211636b927eSTejun Heo } else { 52129546b29eSTejun Heo wq_calc_pod_cpumask(new_attrs, cpu, -1); 52139546b29eSTejun Heo ctx->pwq_tbl[cpu] = alloc_unbound_pwq(wq, new_attrs); 5214636b927eSTejun Heo if (!ctx->pwq_tbl[cpu]) 5215636b927eSTejun Heo goto out_free; 52162d5f0764SLai Jiangshan } 52172d5f0764SLai Jiangshan } 52182d5f0764SLai Jiangshan 5219042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 5220042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 5221042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 52229546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 52232d5f0764SLai Jiangshan ctx->attrs = new_attrs; 5224042f7df1SLai Jiangshan 52254c065dbcSWaiman Long /* 52264c065dbcSWaiman Long * For initialized ordered workqueues, there should only be one pwq 52274c065dbcSWaiman Long * (dfl_pwq). Set the plugged flag of ctx->dfl_pwq to suspend execution 52284c065dbcSWaiman Long * of newly queued work items until execution of older work items in 52294c065dbcSWaiman Long * the old pwq's have completed. 52304c065dbcSWaiman Long */ 52314c065dbcSWaiman Long if ((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)) 52324c065dbcSWaiman Long ctx->dfl_pwq->plugged = true; 52334c065dbcSWaiman Long 52342d5f0764SLai Jiangshan ctx->wq = wq; 52352d5f0764SLai Jiangshan return ctx; 52362d5f0764SLai Jiangshan 52372d5f0764SLai Jiangshan out_free: 52382d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 52392d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 524084193c07STejun Heo return ERR_PTR(-ENOMEM); 52412d5f0764SLai Jiangshan } 52422d5f0764SLai Jiangshan 52432d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 52442d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 52452d5f0764SLai Jiangshan { 5246636b927eSTejun Heo int cpu; 52472d5f0764SLai Jiangshan 52482d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 52492d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 52502d5f0764SLai Jiangshan 52512d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 52522d5f0764SLai Jiangshan 52539f66cff2STejun Heo /* save the previous pwqs and install the new ones */ 5254636b927eSTejun Heo for_each_possible_cpu(cpu) 5255636b927eSTejun Heo ctx->pwq_tbl[cpu] = install_unbound_pwq(ctx->wq, cpu, 5256636b927eSTejun Heo ctx->pwq_tbl[cpu]); 52579f66cff2STejun Heo ctx->dfl_pwq = install_unbound_pwq(ctx->wq, -1, ctx->dfl_pwq); 52582d5f0764SLai Jiangshan 52595797b1c1STejun Heo /* update node_nr_active->max */ 52605797b1c1STejun Heo wq_update_node_max_active(ctx->wq, -1); 52615797b1c1STejun Heo 5262d64f2fa0SJuri Lelli /* rescuer needs to respect wq cpumask changes */ 5263d64f2fa0SJuri Lelli if (ctx->wq->rescuer) 5264d64f2fa0SJuri Lelli set_cpus_allowed_ptr(ctx->wq->rescuer->task, 5265d64f2fa0SJuri Lelli unbound_effective_cpumask(ctx->wq)); 5266d64f2fa0SJuri Lelli 52672d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 52682d5f0764SLai Jiangshan } 52692d5f0764SLai Jiangshan 5270a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 5271a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 5272a0111cf6SLai Jiangshan { 5273a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 5274a0111cf6SLai Jiangshan 5275a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 5276a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 5277a0111cf6SLai Jiangshan return -EINVAL; 5278a0111cf6SLai Jiangshan 527999c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); 528084193c07STejun Heo if (IS_ERR(ctx)) 528184193c07STejun Heo return PTR_ERR(ctx); 5282a0111cf6SLai Jiangshan 5283a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 5284a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 5285a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 5286a0111cf6SLai Jiangshan 52876201171eSwanghaibin return 0; 5288a0111cf6SLai Jiangshan } 5289a0111cf6SLai Jiangshan 52909e8cd2f5STejun Heo /** 52919e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 52929e8cd2f5STejun Heo * @wq: the target workqueue 52939e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 52949e8cd2f5STejun Heo * 5295fef59c9cSTejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, this function maps 5296fef59c9cSTejun Heo * a separate pwq to each CPU pod with possibles CPUs in @attrs->cpumask so that 5297fef59c9cSTejun Heo * work items are affine to the pod it was issued on. Older pwqs are released as 5298fef59c9cSTejun Heo * in-flight work items finish. Note that a work item which repeatedly requeues 5299fef59c9cSTejun Heo * itself back-to-back will stay on its current pwq. 53009e8cd2f5STejun Heo * 5301d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 5302d185af30SYacine Belkadi * 5303ffd8bea8SSebastian Andrzej Siewior * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). 5304509b3204SDaniel Jordan * 5305d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 53069e8cd2f5STejun Heo */ 5307513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 53089e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 53099e8cd2f5STejun Heo { 5310a0111cf6SLai Jiangshan int ret; 53119e8cd2f5STejun Heo 5312509b3204SDaniel Jordan lockdep_assert_cpus_held(); 5313509b3204SDaniel Jordan 5314509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex); 5315a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 5316509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex); 53172d5f0764SLai Jiangshan 53182d5f0764SLai Jiangshan return ret; 53199e8cd2f5STejun Heo } 53209e8cd2f5STejun Heo 53214c16bd32STejun Heo /** 5322fef59c9cSTejun Heo * wq_update_pod - update pod affinity of a wq for CPU hot[un]plug 53234c16bd32STejun Heo * @wq: the target workqueue 53244cbfd3deSTejun Heo * @cpu: the CPU to update pool association for 53254cbfd3deSTejun Heo * @hotplug_cpu: the CPU coming up or going down 53264c16bd32STejun Heo * @online: whether @cpu is coming up or going down 53274c16bd32STejun Heo * 53284c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 5329fef59c9cSTejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update pod affinity of 53304c16bd32STejun Heo * @wq accordingly. 53314c16bd32STejun Heo * 53324c16bd32STejun Heo * 5333fef59c9cSTejun Heo * If pod affinity can't be adjusted due to memory allocation failure, it falls 5334fef59c9cSTejun Heo * back to @wq->dfl_pwq which may not be optimal but is always correct. 5335fef59c9cSTejun Heo * 5336fef59c9cSTejun Heo * Note that when the last allowed CPU of a pod goes offline for a workqueue 5337fef59c9cSTejun Heo * with a cpumask spanning multiple pods, the workers which were already 5338fef59c9cSTejun Heo * executing the work items for the workqueue will lose their CPU affinity and 5339fef59c9cSTejun Heo * may execute on any CPU. This is similar to how per-cpu workqueues behave on 5340fef59c9cSTejun Heo * CPU_DOWN. If a workqueue user wants strict affinity, it's the user's 5341fef59c9cSTejun Heo * responsibility to flush the work item from CPU_DOWN_PREPARE. 53424c16bd32STejun Heo */ 5343fef59c9cSTejun Heo static void wq_update_pod(struct workqueue_struct *wq, int cpu, 53444cbfd3deSTejun Heo int hotplug_cpu, bool online) 53454c16bd32STejun Heo { 53464cbfd3deSTejun Heo int off_cpu = online ? -1 : hotplug_cpu; 53474c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 53484c16bd32STejun Heo struct workqueue_attrs *target_attrs; 53494c16bd32STejun Heo 53504c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 53514c16bd32STejun Heo 535284193c07STejun Heo if (!(wq->flags & WQ_UNBOUND) || wq->unbound_attrs->ordered) 53534c16bd32STejun Heo return; 53544c16bd32STejun Heo 53554c16bd32STejun Heo /* 53564c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 53574c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 53584c16bd32STejun Heo * CPU hotplug exclusion. 53594c16bd32STejun Heo */ 5360fef59c9cSTejun Heo target_attrs = wq_update_pod_attrs_buf; 53614c16bd32STejun Heo 53624c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 53630f36ee24STejun Heo wqattrs_actualize_cpumask(target_attrs, wq_unbound_cpumask); 53644c16bd32STejun Heo 5365636b927eSTejun Heo /* nothing to do if the target cpumask matches the current pwq */ 53669546b29eSTejun Heo wq_calc_pod_cpumask(target_attrs, cpu, off_cpu); 53679f66cff2STejun Heo if (wqattrs_equal(target_attrs, unbound_pwq(wq, cpu)->pool->attrs)) 5368f7142ed4SLai Jiangshan return; 53694c16bd32STejun Heo 53704c16bd32STejun Heo /* create a new pwq */ 53714c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 53724c16bd32STejun Heo if (!pwq) { 5373fef59c9cSTejun Heo pr_warn("workqueue: allocation failed while updating CPU pod affinity of \"%s\"\n", 53744c16bd32STejun Heo wq->name); 537577f300b1SDaeseok Youn goto use_dfl_pwq; 53764c16bd32STejun Heo } 53774c16bd32STejun Heo 5378f7142ed4SLai Jiangshan /* Install the new pwq. */ 53794c16bd32STejun Heo mutex_lock(&wq->mutex); 5380636b927eSTejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 53814c16bd32STejun Heo goto out_unlock; 53824c16bd32STejun Heo 53834c16bd32STejun Heo use_dfl_pwq: 5384f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 53859f66cff2STejun Heo pwq = unbound_pwq(wq, -1); 53869f66cff2STejun Heo raw_spin_lock_irq(&pwq->pool->lock); 53879f66cff2STejun Heo get_pwq(pwq); 53889f66cff2STejun Heo raw_spin_unlock_irq(&pwq->pool->lock); 53899f66cff2STejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 53904c16bd32STejun Heo out_unlock: 53914c16bd32STejun Heo mutex_unlock(&wq->mutex); 53924c16bd32STejun Heo put_pwq_unlocked(old_pwq); 53934c16bd32STejun Heo } 53944c16bd32STejun Heo 539530cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 53961da177e4SLinus Torvalds { 539749e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 53988a2b7538STejun Heo int cpu, ret; 5399e1d8aa9fSFrederic Weisbecker 5400687a9aa5STejun Heo wq->cpu_pwq = alloc_percpu(struct pool_workqueue *); 5401ee1ceef7STejun Heo if (!wq->cpu_pwq) 5402687a9aa5STejun Heo goto enomem; 540330cdf249STejun Heo 5404636b927eSTejun Heo if (!(wq->flags & WQ_UNBOUND)) { 540530cdf249STejun Heo for_each_possible_cpu(cpu) { 54064cb1ef64STejun Heo struct pool_workqueue **pwq_p; 54074cb1ef64STejun Heo struct worker_pool __percpu *pools; 54084cb1ef64STejun Heo struct worker_pool *pool; 54094cb1ef64STejun Heo 54104cb1ef64STejun Heo if (wq->flags & WQ_BH) 54114cb1ef64STejun Heo pools = bh_worker_pools; 54124cb1ef64STejun Heo else 54134cb1ef64STejun Heo pools = cpu_worker_pools; 54144cb1ef64STejun Heo 54154cb1ef64STejun Heo pool = &(per_cpu_ptr(pools, cpu)[highpri]); 54164cb1ef64STejun Heo pwq_p = per_cpu_ptr(wq->cpu_pwq, cpu); 541730cdf249STejun Heo 5418687a9aa5STejun Heo *pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, 5419687a9aa5STejun Heo pool->node); 5420687a9aa5STejun Heo if (!*pwq_p) 5421687a9aa5STejun Heo goto enomem; 5422687a9aa5STejun Heo 5423687a9aa5STejun Heo init_pwq(*pwq_p, wq, pool); 5424f147f29eSTejun Heo 5425f147f29eSTejun Heo mutex_lock(&wq->mutex); 5426687a9aa5STejun Heo link_pwq(*pwq_p); 5427f147f29eSTejun Heo mutex_unlock(&wq->mutex); 542830cdf249STejun Heo } 542930cdf249STejun Heo return 0; 5430509b3204SDaniel Jordan } 5431509b3204SDaniel Jordan 5432ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 5433509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) { 54349f66cff2STejun Heo struct pool_workqueue *dfl_pwq; 54359f66cff2STejun Heo 54368a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 54378a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 54389f66cff2STejun Heo dfl_pwq = rcu_access_pointer(wq->dfl_pwq); 54399f66cff2STejun Heo WARN(!ret && (wq->pwqs.next != &dfl_pwq->pwqs_node || 54409f66cff2STejun Heo wq->pwqs.prev != &dfl_pwq->pwqs_node), 54418a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 54429e8cd2f5STejun Heo } else { 5443509b3204SDaniel Jordan ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 54449e8cd2f5STejun Heo } 5445ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 5446509b3204SDaniel Jordan 544764344553SZqiang /* for unbound pwq, flush the pwq_release_worker ensures that the 544864344553SZqiang * pwq_release_workfn() completes before calling kfree(wq). 544964344553SZqiang */ 545064344553SZqiang if (ret) 545164344553SZqiang kthread_flush_worker(pwq_release_worker); 545264344553SZqiang 5453509b3204SDaniel Jordan return ret; 5454687a9aa5STejun Heo 5455687a9aa5STejun Heo enomem: 5456687a9aa5STejun Heo if (wq->cpu_pwq) { 54577b42f401SZqiang for_each_possible_cpu(cpu) { 54587b42f401SZqiang struct pool_workqueue *pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 54597b42f401SZqiang 54607b42f401SZqiang if (pwq) 54617b42f401SZqiang kmem_cache_free(pwq_cache, pwq); 54627b42f401SZqiang } 5463687a9aa5STejun Heo free_percpu(wq->cpu_pwq); 5464687a9aa5STejun Heo wq->cpu_pwq = NULL; 5465687a9aa5STejun Heo } 5466687a9aa5STejun Heo return -ENOMEM; 54670f900049STejun Heo } 54680f900049STejun Heo 5469f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 5470f3421797STejun Heo const char *name) 5471b71ab8c2STejun Heo { 5472636b927eSTejun Heo if (max_active < 1 || max_active > WQ_MAX_ACTIVE) 5473044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 5474636b927eSTejun Heo max_active, name, 1, WQ_MAX_ACTIVE); 5475b71ab8c2STejun Heo 5476636b927eSTejun Heo return clamp_val(max_active, 1, WQ_MAX_ACTIVE); 5477b71ab8c2STejun Heo } 5478b71ab8c2STejun Heo 5479983c7515STejun Heo /* 5480983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 5481983c7515STejun Heo * to guarantee forward progress. 5482983c7515STejun Heo */ 5483983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 5484983c7515STejun Heo { 5485983c7515STejun Heo struct worker *rescuer; 5486b92b36eaSDan Carpenter int ret; 5487983c7515STejun Heo 5488983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 5489983c7515STejun Heo return 0; 5490983c7515STejun Heo 5491983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 54924c0736a7SPetr Mladek if (!rescuer) { 54934c0736a7SPetr Mladek pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", 54944c0736a7SPetr Mladek wq->name); 5495983c7515STejun Heo return -ENOMEM; 54964c0736a7SPetr Mladek } 5497983c7515STejun Heo 5498983c7515STejun Heo rescuer->rescue_wq = wq; 5499b6a46f72SAaron Tomlin rescuer->task = kthread_create(rescuer_thread, rescuer, "kworker/R-%s", wq->name); 5500f187b697SSean Fu if (IS_ERR(rescuer->task)) { 5501b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task); 55024c0736a7SPetr Mladek pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", 55034c0736a7SPetr Mladek wq->name, ERR_PTR(ret)); 5504983c7515STejun Heo kfree(rescuer); 5505b92b36eaSDan Carpenter return ret; 5506983c7515STejun Heo } 5507983c7515STejun Heo 5508983c7515STejun Heo wq->rescuer = rescuer; 550985f0ab43SJuri Lelli if (wq->flags & WQ_UNBOUND) 551049584bb8SWaiman Long kthread_bind_mask(rescuer->task, wq_unbound_cpumask); 551185f0ab43SJuri Lelli else 5512983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 5513983c7515STejun Heo wake_up_process(rescuer->task); 5514983c7515STejun Heo 5515983c7515STejun Heo return 0; 5516983c7515STejun Heo } 5517983c7515STejun Heo 5518a045a272STejun Heo /** 5519a045a272STejun Heo * wq_adjust_max_active - update a wq's max_active to the current setting 5520a045a272STejun Heo * @wq: target workqueue 5521a045a272STejun Heo * 5522a045a272STejun Heo * If @wq isn't freezing, set @wq->max_active to the saved_max_active and 5523a045a272STejun Heo * activate inactive work items accordingly. If @wq is freezing, clear 5524a045a272STejun Heo * @wq->max_active to zero. 5525a045a272STejun Heo */ 5526a045a272STejun Heo static void wq_adjust_max_active(struct workqueue_struct *wq) 5527a045a272STejun Heo { 5528c5404d4eSTejun Heo bool activated; 55295797b1c1STejun Heo int new_max, new_min; 5530a045a272STejun Heo 5531a045a272STejun Heo lockdep_assert_held(&wq->mutex); 5532a045a272STejun Heo 5533a045a272STejun Heo if ((wq->flags & WQ_FREEZABLE) && workqueue_freezing) { 55345797b1c1STejun Heo new_max = 0; 55355797b1c1STejun Heo new_min = 0; 55365797b1c1STejun Heo } else { 55375797b1c1STejun Heo new_max = wq->saved_max_active; 55385797b1c1STejun Heo new_min = wq->saved_min_active; 5539a045a272STejun Heo } 5540a045a272STejun Heo 55415797b1c1STejun Heo if (wq->max_active == new_max && wq->min_active == new_min) 5542a045a272STejun Heo return; 5543a045a272STejun Heo 5544a045a272STejun Heo /* 55455797b1c1STejun Heo * Update @wq->max/min_active and then kick inactive work items if more 5546a045a272STejun Heo * active work items are allowed. This doesn't break work item ordering 5547a045a272STejun Heo * because new work items are always queued behind existing inactive 5548a045a272STejun Heo * work items if there are any. 5549a045a272STejun Heo */ 55505797b1c1STejun Heo WRITE_ONCE(wq->max_active, new_max); 55515797b1c1STejun Heo WRITE_ONCE(wq->min_active, new_min); 55525797b1c1STejun Heo 55535797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) 55545797b1c1STejun Heo wq_update_node_max_active(wq, -1); 55555797b1c1STejun Heo 55565797b1c1STejun Heo if (new_max == 0) 55575797b1c1STejun Heo return; 5558a045a272STejun Heo 5559c5404d4eSTejun Heo /* 5560c5404d4eSTejun Heo * Round-robin through pwq's activating the first inactive work item 5561c5404d4eSTejun Heo * until max_active is filled. 5562c5404d4eSTejun Heo */ 5563c5404d4eSTejun Heo do { 5564c5404d4eSTejun Heo struct pool_workqueue *pwq; 5565c5404d4eSTejun Heo 5566c5404d4eSTejun Heo activated = false; 5567a045a272STejun Heo for_each_pwq(pwq, wq) { 5568c26e2f2eSTejun Heo unsigned long irq_flags; 5569a045a272STejun Heo 5570c5404d4eSTejun Heo /* can be called during early boot w/ irq disabled */ 5571c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, irq_flags); 55725797b1c1STejun Heo if (pwq_activate_first_inactive(pwq, true)) { 5573c5404d4eSTejun Heo activated = true; 5574a045a272STejun Heo kick_pool(pwq->pool); 5575c5404d4eSTejun Heo } 5576c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, irq_flags); 5577a045a272STejun Heo } 5578c5404d4eSTejun Heo } while (activated); 5579a045a272STejun Heo } 5580a045a272STejun Heo 5581a2775bbcSMathieu Malaterre __printf(1, 4) 5582669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt, 558397e37d7bSTejun Heo unsigned int flags, 5584669de8bdSBart Van Assche int max_active, ...) 55853af24433SOleg Nesterov { 5586ecf6881fSTejun Heo va_list args; 55873af24433SOleg Nesterov struct workqueue_struct *wq; 558891ccc6e7STejun Heo size_t wq_size; 558991ccc6e7STejun Heo int name_len; 5590b196be89STejun Heo 55914cb1ef64STejun Heo if (flags & WQ_BH) { 55924cb1ef64STejun Heo if (WARN_ON_ONCE(flags & ~__WQ_BH_ALLOWS)) 55934cb1ef64STejun Heo return NULL; 55944cb1ef64STejun Heo if (WARN_ON_ONCE(max_active)) 55954cb1ef64STejun Heo return NULL; 55964cb1ef64STejun Heo } 55974cb1ef64STejun Heo 5598cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 5599cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 5600cee22a15SViresh Kumar flags |= WQ_UNBOUND; 5601cee22a15SViresh Kumar 5602ecf6881fSTejun Heo /* allocate wq and format name */ 560391ccc6e7STejun Heo if (flags & WQ_UNBOUND) 560491ccc6e7STejun Heo wq_size = struct_size(wq, node_nr_active, nr_node_ids + 1); 560591ccc6e7STejun Heo else 560691ccc6e7STejun Heo wq_size = sizeof(*wq); 560791ccc6e7STejun Heo 560891ccc6e7STejun Heo wq = kzalloc(wq_size, GFP_KERNEL); 5609b196be89STejun Heo if (!wq) 5610d2c1d404STejun Heo return NULL; 5611b196be89STejun Heo 56126029a918STejun Heo if (flags & WQ_UNBOUND) { 5613be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs(); 56146029a918STejun Heo if (!wq->unbound_attrs) 56156029a918STejun Heo goto err_free_wq; 56166029a918STejun Heo } 56176029a918STejun Heo 5618669de8bdSBart Van Assche va_start(args, max_active); 561991ccc6e7STejun Heo name_len = vsnprintf(wq->name, sizeof(wq->name), fmt, args); 5620b196be89STejun Heo va_end(args); 56213af24433SOleg Nesterov 562291ccc6e7STejun Heo if (name_len >= WQ_NAME_LEN) 562391ccc6e7STejun Heo pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n", 562491ccc6e7STejun Heo wq->name); 562531c89007SAudra Mitchell 56264cb1ef64STejun Heo if (flags & WQ_BH) { 56274cb1ef64STejun Heo /* 56284cb1ef64STejun Heo * BH workqueues always share a single execution context per CPU 56294cb1ef64STejun Heo * and don't impose any max_active limit. 56304cb1ef64STejun Heo */ 56314cb1ef64STejun Heo max_active = INT_MAX; 56324cb1ef64STejun Heo } else { 5633d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 5634b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 56354cb1ef64STejun Heo } 56363af24433SOleg Nesterov 5637b196be89STejun Heo /* init wq */ 563897e37d7bSTejun Heo wq->flags = flags; 5639a045a272STejun Heo wq->max_active = max_active; 56405797b1c1STejun Heo wq->min_active = min(max_active, WQ_DFL_MIN_ACTIVE); 56415797b1c1STejun Heo wq->saved_max_active = wq->max_active; 56425797b1c1STejun Heo wq->saved_min_active = wq->min_active; 56433c25a55dSLai Jiangshan mutex_init(&wq->mutex); 5644112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 564530cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 564673f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 564773f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 5648493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 56493af24433SOleg Nesterov 5650669de8bdSBart Van Assche wq_init_lockdep(wq); 5651cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 56523af24433SOleg Nesterov 565391ccc6e7STejun Heo if (flags & WQ_UNBOUND) { 565491ccc6e7STejun Heo if (alloc_node_nr_active(wq->node_nr_active) < 0) 565582efcab3SBart Van Assche goto err_unreg_lockdep; 565691ccc6e7STejun Heo } 565791ccc6e7STejun Heo 565891ccc6e7STejun Heo if (alloc_and_link_pwqs(wq) < 0) 565991ccc6e7STejun Heo goto err_free_node_nr_active; 56601537663fSTejun Heo 566140c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 5662d2c1d404STejun Heo goto err_destroy; 5663e22bee78STejun Heo 5664226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 5665226223abSTejun Heo goto err_destroy; 5666226223abSTejun Heo 56676af8bf3dSOleg Nesterov /* 566868e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 566968e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 567068e13a67SLai Jiangshan * list. 56716af8bf3dSOleg Nesterov */ 567268e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5673a0a1a5fdSTejun Heo 5674a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5675a045a272STejun Heo wq_adjust_max_active(wq); 5676a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5677a0a1a5fdSTejun Heo 5678e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 5679a0a1a5fdSTejun Heo 568068e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 56813af24433SOleg Nesterov 56823af24433SOleg Nesterov return wq; 5683d2c1d404STejun Heo 568491ccc6e7STejun Heo err_free_node_nr_active: 568591ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 568691ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 568782efcab3SBart Van Assche err_unreg_lockdep: 5688009bb421SBart Van Assche wq_unregister_lockdep(wq); 5689009bb421SBart Van Assche wq_free_lockdep(wq); 569082efcab3SBart Van Assche err_free_wq: 56916029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 56924690c4abSTejun Heo kfree(wq); 5693d2c1d404STejun Heo return NULL; 5694d2c1d404STejun Heo err_destroy: 5695d2c1d404STejun Heo destroy_workqueue(wq); 56964690c4abSTejun Heo return NULL; 56971da177e4SLinus Torvalds } 5698669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue); 56991da177e4SLinus Torvalds 5700c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq) 5701c29eb853STejun Heo { 5702c29eb853STejun Heo int i; 5703c29eb853STejun Heo 5704c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 5705c29eb853STejun Heo if (pwq->nr_in_flight[i]) 5706c29eb853STejun Heo return true; 5707c29eb853STejun Heo 57089f66cff2STejun Heo if ((pwq != rcu_access_pointer(pwq->wq->dfl_pwq)) && (pwq->refcnt > 1)) 5709c29eb853STejun Heo return true; 5710afa87ce8STejun Heo if (!pwq_is_empty(pwq)) 5711c29eb853STejun Heo return true; 5712c29eb853STejun Heo 5713c29eb853STejun Heo return false; 5714c29eb853STejun Heo } 5715c29eb853STejun Heo 57163af24433SOleg Nesterov /** 57173af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 57183af24433SOleg Nesterov * @wq: target workqueue 57193af24433SOleg Nesterov * 57203af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 57213af24433SOleg Nesterov */ 57223af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 57233af24433SOleg Nesterov { 572449e3cf44STejun Heo struct pool_workqueue *pwq; 5725636b927eSTejun Heo int cpu; 57263af24433SOleg Nesterov 5727def98c84STejun Heo /* 5728def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't 5729def98c84STejun Heo * lead to sysfs name conflicts. 5730def98c84STejun Heo */ 5731def98c84STejun Heo workqueue_sysfs_unregister(wq); 5732def98c84STejun Heo 573333e3f0a3SRichard Clark /* mark the workqueue destruction is in progress */ 573433e3f0a3SRichard Clark mutex_lock(&wq->mutex); 573533e3f0a3SRichard Clark wq->flags |= __WQ_DESTROYING; 573633e3f0a3SRichard Clark mutex_unlock(&wq->mutex); 573733e3f0a3SRichard Clark 57389c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 57399c5a2ba7STejun Heo drain_workqueue(wq); 5740c8efcc25STejun Heo 5741def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ 5742def98c84STejun Heo if (wq->rescuer) { 5743def98c84STejun Heo struct worker *rescuer = wq->rescuer; 5744def98c84STejun Heo 5745def98c84STejun Heo /* this prevents new queueing */ 5746a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 5747def98c84STejun Heo wq->rescuer = NULL; 5748a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 5749def98c84STejun Heo 5750def98c84STejun Heo /* rescuer will empty maydays list before exiting */ 5751def98c84STejun Heo kthread_stop(rescuer->task); 57528efe1223STejun Heo kfree(rescuer); 5753def98c84STejun Heo } 5754def98c84STejun Heo 5755c29eb853STejun Heo /* 5756c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all 5757c29eb853STejun Heo * in-flight operations which may do put_pwq(). 5758c29eb853STejun Heo */ 5759c29eb853STejun Heo mutex_lock(&wq_pool_mutex); 5760b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 576149e3cf44STejun Heo for_each_pwq(pwq, wq) { 5762a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 5763c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) { 57641d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n", 5765e66b39afSTejun Heo __func__, wq->name); 5766c29eb853STejun Heo show_pwq(pwq); 5767a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 5768b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 5769c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 577055df0933SImran Khan show_one_workqueue(wq); 57716183c009STejun Heo return; 577276af4d93STejun Heo } 5773a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 577476af4d93STejun Heo } 5775b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 57766183c009STejun Heo 5777a0a1a5fdSTejun Heo /* 5778a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 5779a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 5780a0a1a5fdSTejun Heo */ 5781e2dca7adSTejun Heo list_del_rcu(&wq->list); 578268e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 57833af24433SOleg Nesterov 57848864b4e5STejun Heo /* 5785636b927eSTejun Heo * We're the sole accessor of @wq. Directly access cpu_pwq and dfl_pwq 5786636b927eSTejun Heo * to put the base refs. @wq will be auto-destroyed from the last 5787636b927eSTejun Heo * pwq_put. RCU read lock prevents @wq from going away from under us. 57888864b4e5STejun Heo */ 5789636b927eSTejun Heo rcu_read_lock(); 5790636b927eSTejun Heo 5791636b927eSTejun Heo for_each_possible_cpu(cpu) { 57929f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, cpu)); 57939f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, cpu), NULL); 57944c16bd32STejun Heo } 57954c16bd32STejun Heo 57969f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, -1)); 57979f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, -1), NULL); 5798636b927eSTejun Heo 5799636b927eSTejun Heo rcu_read_unlock(); 58003af24433SOleg Nesterov } 58013af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 58023af24433SOleg Nesterov 5803dcd989cbSTejun Heo /** 5804dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 5805dcd989cbSTejun Heo * @wq: target workqueue 5806dcd989cbSTejun Heo * @max_active: new max_active value. 5807dcd989cbSTejun Heo * 58085797b1c1STejun Heo * Set max_active of @wq to @max_active. See the alloc_workqueue() function 58095797b1c1STejun Heo * comment. 5810dcd989cbSTejun Heo * 5811dcd989cbSTejun Heo * CONTEXT: 5812dcd989cbSTejun Heo * Don't call from IRQ context. 5813dcd989cbSTejun Heo */ 5814dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 5815dcd989cbSTejun Heo { 58164cb1ef64STejun Heo /* max_active doesn't mean anything for BH workqueues */ 58174cb1ef64STejun Heo if (WARN_ON(wq->flags & WQ_BH)) 58184cb1ef64STejun Heo return; 58198719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 58203bc1e711STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED)) 58218719dceaSTejun Heo return; 58228719dceaSTejun Heo 5823f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 5824dcd989cbSTejun Heo 5825a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5826dcd989cbSTejun Heo 5827dcd989cbSTejun Heo wq->saved_max_active = max_active; 58285797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) 58295797b1c1STejun Heo wq->saved_min_active = min(wq->saved_min_active, max_active); 58305797b1c1STejun Heo 5831a045a272STejun Heo wq_adjust_max_active(wq); 5832dcd989cbSTejun Heo 5833a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5834dcd989cbSTejun Heo } 5835dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 5836dcd989cbSTejun Heo 5837dcd989cbSTejun Heo /** 58388f172181STejun Heo * workqueue_set_min_active - adjust min_active of an unbound workqueue 58398f172181STejun Heo * @wq: target unbound workqueue 58408f172181STejun Heo * @min_active: new min_active value 58418f172181STejun Heo * 58428f172181STejun Heo * Set min_active of an unbound workqueue. Unlike other types of workqueues, an 58438f172181STejun Heo * unbound workqueue is not guaranteed to be able to process max_active 58448f172181STejun Heo * interdependent work items. Instead, an unbound workqueue is guaranteed to be 58458f172181STejun Heo * able to process min_active number of interdependent work items which is 58468f172181STejun Heo * %WQ_DFL_MIN_ACTIVE by default. 58478f172181STejun Heo * 58488f172181STejun Heo * Use this function to adjust the min_active value between 0 and the current 58498f172181STejun Heo * max_active. 58508f172181STejun Heo */ 58518f172181STejun Heo void workqueue_set_min_active(struct workqueue_struct *wq, int min_active) 58528f172181STejun Heo { 58538f172181STejun Heo /* min_active is only meaningful for non-ordered unbound workqueues */ 58548f172181STejun Heo if (WARN_ON((wq->flags & (WQ_BH | WQ_UNBOUND | __WQ_ORDERED)) != 58558f172181STejun Heo WQ_UNBOUND)) 58568f172181STejun Heo return; 58578f172181STejun Heo 58588f172181STejun Heo mutex_lock(&wq->mutex); 58598f172181STejun Heo wq->saved_min_active = clamp(min_active, 0, wq->saved_max_active); 58608f172181STejun Heo wq_adjust_max_active(wq); 58618f172181STejun Heo mutex_unlock(&wq->mutex); 58628f172181STejun Heo } 58638f172181STejun Heo 58648f172181STejun Heo /** 586527d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 586627d4ee03SLukas Wunner * 586727d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 586827d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 586927d4ee03SLukas Wunner * 587027d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 587127d4ee03SLukas Wunner */ 587227d4ee03SLukas Wunner struct work_struct *current_work(void) 587327d4ee03SLukas Wunner { 587427d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 587527d4ee03SLukas Wunner 587627d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 587727d4ee03SLukas Wunner } 587827d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 587927d4ee03SLukas Wunner 588027d4ee03SLukas Wunner /** 5881e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 5882e6267616STejun Heo * 5883e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 5884e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 5885d185af30SYacine Belkadi * 5886d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 5887e6267616STejun Heo */ 5888e6267616STejun Heo bool current_is_workqueue_rescuer(void) 5889e6267616STejun Heo { 5890e6267616STejun Heo struct worker *worker = current_wq_worker(); 5891e6267616STejun Heo 58926a092dfdSLai Jiangshan return worker && worker->rescue_wq; 5893e6267616STejun Heo } 5894e6267616STejun Heo 5895e6267616STejun Heo /** 5896dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 5897dcd989cbSTejun Heo * @cpu: CPU in question 5898dcd989cbSTejun Heo * @wq: target workqueue 5899dcd989cbSTejun Heo * 5900dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 5901dcd989cbSTejun Heo * no synchronization around this function and the test result is 5902dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5903dcd989cbSTejun Heo * 5904d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 5905636b927eSTejun Heo * 5906636b927eSTejun Heo * With the exception of ordered workqueues, all workqueues have per-cpu 5907636b927eSTejun Heo * pool_workqueues, each with its own congested state. A workqueue being 5908636b927eSTejun Heo * congested on one CPU doesn't mean that the workqueue is contested on any 5909636b927eSTejun Heo * other CPUs. 5910d3251859STejun Heo * 5911d185af30SYacine Belkadi * Return: 5912dcd989cbSTejun Heo * %true if congested, %false otherwise. 5913dcd989cbSTejun Heo */ 5914d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 5915dcd989cbSTejun Heo { 59167fb98ea7STejun Heo struct pool_workqueue *pwq; 591776af4d93STejun Heo bool ret; 591876af4d93STejun Heo 591924acfb71SThomas Gleixner rcu_read_lock(); 592024acfb71SThomas Gleixner preempt_disable(); 59217fb98ea7STejun Heo 5922d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 5923d3251859STejun Heo cpu = smp_processor_id(); 5924d3251859STejun Heo 5925687a9aa5STejun Heo pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 5926f97a4a1aSLai Jiangshan ret = !list_empty(&pwq->inactive_works); 5927636b927eSTejun Heo 592824acfb71SThomas Gleixner preempt_enable(); 592924acfb71SThomas Gleixner rcu_read_unlock(); 593076af4d93STejun Heo 593176af4d93STejun Heo return ret; 5932dcd989cbSTejun Heo } 5933dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 5934dcd989cbSTejun Heo 5935dcd989cbSTejun Heo /** 5936dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 5937dcd989cbSTejun Heo * @work: the work to be tested 5938dcd989cbSTejun Heo * 5939dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 5940dcd989cbSTejun Heo * synchronization around this function and the test result is 5941dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5942dcd989cbSTejun Heo * 5943d185af30SYacine Belkadi * Return: 5944dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 5945dcd989cbSTejun Heo */ 5946dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 5947dcd989cbSTejun Heo { 5948fa1b54e6STejun Heo struct worker_pool *pool; 5949c26e2f2eSTejun Heo unsigned long irq_flags; 5950dcd989cbSTejun Heo unsigned int ret = 0; 5951dcd989cbSTejun Heo 5952dcd989cbSTejun Heo if (work_pending(work)) 5953dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 5954038366c5SLai Jiangshan 595524acfb71SThomas Gleixner rcu_read_lock(); 5956fa1b54e6STejun Heo pool = get_work_pool(work); 5957038366c5SLai Jiangshan if (pool) { 5958c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags); 5959c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 5960dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 5961c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags); 5962038366c5SLai Jiangshan } 596324acfb71SThomas Gleixner rcu_read_unlock(); 5964dcd989cbSTejun Heo 5965dcd989cbSTejun Heo return ret; 5966dcd989cbSTejun Heo } 5967dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 5968dcd989cbSTejun Heo 59693d1cb205STejun Heo /** 59703d1cb205STejun Heo * set_worker_desc - set description for the current work item 59713d1cb205STejun Heo * @fmt: printf-style format string 59723d1cb205STejun Heo * @...: arguments for the format string 59733d1cb205STejun Heo * 59743d1cb205STejun Heo * This function can be called by a running work function to describe what 59753d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 59763d1cb205STejun Heo * information will be printed out together to help debugging. The 59773d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 59783d1cb205STejun Heo */ 59793d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 59803d1cb205STejun Heo { 59813d1cb205STejun Heo struct worker *worker = current_wq_worker(); 59823d1cb205STejun Heo va_list args; 59833d1cb205STejun Heo 59843d1cb205STejun Heo if (worker) { 59853d1cb205STejun Heo va_start(args, fmt); 59863d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 59873d1cb205STejun Heo va_end(args); 59883d1cb205STejun Heo } 59893d1cb205STejun Heo } 59905c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 59913d1cb205STejun Heo 59923d1cb205STejun Heo /** 59933d1cb205STejun Heo * print_worker_info - print out worker information and description 59943d1cb205STejun Heo * @log_lvl: the log level to use when printing 59953d1cb205STejun Heo * @task: target task 59963d1cb205STejun Heo * 59973d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 59983d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 59993d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 60003d1cb205STejun Heo * 60013d1cb205STejun Heo * This function can be safely called on any task as long as the 60023d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 60033d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 60043d1cb205STejun Heo */ 60053d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 60063d1cb205STejun Heo { 60073d1cb205STejun Heo work_func_t *fn = NULL; 60083d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 60093d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 60103d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 60113d1cb205STejun Heo struct workqueue_struct *wq = NULL; 60123d1cb205STejun Heo struct worker *worker; 60133d1cb205STejun Heo 60143d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 60153d1cb205STejun Heo return; 60163d1cb205STejun Heo 60173d1cb205STejun Heo /* 60183d1cb205STejun Heo * This function is called without any synchronization and @task 60193d1cb205STejun Heo * could be in any state. Be careful with dereferences. 60203d1cb205STejun Heo */ 6021e700591aSPetr Mladek worker = kthread_probe_data(task); 60223d1cb205STejun Heo 60233d1cb205STejun Heo /* 60248bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 60258bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 60263d1cb205STejun Heo */ 6027fe557319SChristoph Hellwig copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); 6028fe557319SChristoph Hellwig copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); 6029fe557319SChristoph Hellwig copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); 6030fe557319SChristoph Hellwig copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); 6031fe557319SChristoph Hellwig copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); 60323d1cb205STejun Heo 60333d1cb205STejun Heo if (fn || name[0] || desc[0]) { 6034d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn); 60358bf89593STejun Heo if (strcmp(name, desc)) 60363d1cb205STejun Heo pr_cont(" (%s)", desc); 60373d1cb205STejun Heo pr_cont("\n"); 60383d1cb205STejun Heo } 60393d1cb205STejun Heo } 60403d1cb205STejun Heo 60413494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 60423494fc30STejun Heo { 60433494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 60443494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 60453494fc30STejun Heo pr_cont(" node=%d", pool->node); 60464cb1ef64STejun Heo pr_cont(" flags=0x%x", pool->flags); 60474cb1ef64STejun Heo if (pool->flags & POOL_BH) 60484cb1ef64STejun Heo pr_cont(" bh%s", 60494cb1ef64STejun Heo pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); 60504cb1ef64STejun Heo else 60514cb1ef64STejun Heo pr_cont(" nice=%d", pool->attrs->nice); 60524cb1ef64STejun Heo } 60534cb1ef64STejun Heo 60544cb1ef64STejun Heo static void pr_cont_worker_id(struct worker *worker) 60554cb1ef64STejun Heo { 60564cb1ef64STejun Heo struct worker_pool *pool = worker->pool; 60574cb1ef64STejun Heo 60584cb1ef64STejun Heo if (pool->flags & WQ_BH) 60594cb1ef64STejun Heo pr_cont("bh%s", 60604cb1ef64STejun Heo pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); 60614cb1ef64STejun Heo else 60624cb1ef64STejun Heo pr_cont("%d%s", task_pid_nr(worker->task), 60634cb1ef64STejun Heo worker->rescue_wq ? "(RESCUER)" : ""); 60643494fc30STejun Heo } 60653494fc30STejun Heo 6066c76feb0dSPaul E. McKenney struct pr_cont_work_struct { 6067c76feb0dSPaul E. McKenney bool comma; 6068c76feb0dSPaul E. McKenney work_func_t func; 6069c76feb0dSPaul E. McKenney long ctr; 6070c76feb0dSPaul E. McKenney }; 6071c76feb0dSPaul E. McKenney 6072c76feb0dSPaul E. McKenney static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) 6073c76feb0dSPaul E. McKenney { 6074c76feb0dSPaul E. McKenney if (!pcwsp->ctr) 6075c76feb0dSPaul E. McKenney goto out_record; 6076c76feb0dSPaul E. McKenney if (func == pcwsp->func) { 6077c76feb0dSPaul E. McKenney pcwsp->ctr++; 6078c76feb0dSPaul E. McKenney return; 6079c76feb0dSPaul E. McKenney } 6080c76feb0dSPaul E. McKenney if (pcwsp->ctr == 1) 6081c76feb0dSPaul E. McKenney pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); 6082c76feb0dSPaul E. McKenney else 6083c76feb0dSPaul E. McKenney pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); 6084c76feb0dSPaul E. McKenney pcwsp->ctr = 0; 6085c76feb0dSPaul E. McKenney out_record: 6086c76feb0dSPaul E. McKenney if ((long)func == -1L) 6087c76feb0dSPaul E. McKenney return; 6088c76feb0dSPaul E. McKenney pcwsp->comma = comma; 6089c76feb0dSPaul E. McKenney pcwsp->func = func; 6090c76feb0dSPaul E. McKenney pcwsp->ctr = 1; 6091c76feb0dSPaul E. McKenney } 6092c76feb0dSPaul E. McKenney 6093c76feb0dSPaul E. McKenney static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) 60943494fc30STejun Heo { 60953494fc30STejun Heo if (work->func == wq_barrier_func) { 60963494fc30STejun Heo struct wq_barrier *barr; 60973494fc30STejun Heo 60983494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 60993494fc30STejun Heo 6100c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 61013494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 61023494fc30STejun Heo task_pid_nr(barr->task)); 61033494fc30STejun Heo } else { 6104c76feb0dSPaul E. McKenney if (!comma) 6105c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 6106c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, work->func, pcwsp); 61073494fc30STejun Heo } 61083494fc30STejun Heo } 61093494fc30STejun Heo 61103494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 61113494fc30STejun Heo { 6112c76feb0dSPaul E. McKenney struct pr_cont_work_struct pcws = { .ctr = 0, }; 61133494fc30STejun Heo struct worker_pool *pool = pwq->pool; 61143494fc30STejun Heo struct work_struct *work; 61153494fc30STejun Heo struct worker *worker; 61163494fc30STejun Heo bool has_in_flight = false, has_pending = false; 61173494fc30STejun Heo int bkt; 61183494fc30STejun Heo 61193494fc30STejun Heo pr_info(" pwq %d:", pool->id); 61203494fc30STejun Heo pr_cont_pool_info(pool); 61213494fc30STejun Heo 6122a045a272STejun Heo pr_cont(" active=%d refcnt=%d%s\n", 6123a045a272STejun Heo pwq->nr_active, pwq->refcnt, 61243494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 61253494fc30STejun Heo 61263494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 61273494fc30STejun Heo if (worker->current_pwq == pwq) { 61283494fc30STejun Heo has_in_flight = true; 61293494fc30STejun Heo break; 61303494fc30STejun Heo } 61313494fc30STejun Heo } 61323494fc30STejun Heo if (has_in_flight) { 61333494fc30STejun Heo bool comma = false; 61343494fc30STejun Heo 61353494fc30STejun Heo pr_info(" in-flight:"); 61363494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 61373494fc30STejun Heo if (worker->current_pwq != pwq) 61383494fc30STejun Heo continue; 61393494fc30STejun Heo 61404cb1ef64STejun Heo pr_cont(" %s", comma ? "," : ""); 61414cb1ef64STejun Heo pr_cont_worker_id(worker); 61424cb1ef64STejun Heo pr_cont(":%ps", worker->current_func); 61433494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 6144c76feb0dSPaul E. McKenney pr_cont_work(false, work, &pcws); 6145c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 61463494fc30STejun Heo comma = true; 61473494fc30STejun Heo } 61483494fc30STejun Heo pr_cont("\n"); 61493494fc30STejun Heo } 61503494fc30STejun Heo 61513494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 61523494fc30STejun Heo if (get_work_pwq(work) == pwq) { 61533494fc30STejun Heo has_pending = true; 61543494fc30STejun Heo break; 61553494fc30STejun Heo } 61563494fc30STejun Heo } 61573494fc30STejun Heo if (has_pending) { 61583494fc30STejun Heo bool comma = false; 61593494fc30STejun Heo 61603494fc30STejun Heo pr_info(" pending:"); 61613494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 61623494fc30STejun Heo if (get_work_pwq(work) != pwq) 61633494fc30STejun Heo continue; 61643494fc30STejun Heo 6165c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 61663494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 61673494fc30STejun Heo } 6168c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 61693494fc30STejun Heo pr_cont("\n"); 61703494fc30STejun Heo } 61713494fc30STejun Heo 6172f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 61733494fc30STejun Heo bool comma = false; 61743494fc30STejun Heo 6175f97a4a1aSLai Jiangshan pr_info(" inactive:"); 6176f97a4a1aSLai Jiangshan list_for_each_entry(work, &pwq->inactive_works, entry) { 6177c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 61783494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 61793494fc30STejun Heo } 6180c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 61813494fc30STejun Heo pr_cont("\n"); 61823494fc30STejun Heo } 61833494fc30STejun Heo } 61843494fc30STejun Heo 61853494fc30STejun Heo /** 618655df0933SImran Khan * show_one_workqueue - dump state of specified workqueue 618755df0933SImran Khan * @wq: workqueue whose state will be printed 61883494fc30STejun Heo */ 618955df0933SImran Khan void show_one_workqueue(struct workqueue_struct *wq) 61903494fc30STejun Heo { 61913494fc30STejun Heo struct pool_workqueue *pwq; 61923494fc30STejun Heo bool idle = true; 6193c26e2f2eSTejun Heo unsigned long irq_flags; 61943494fc30STejun Heo 61953494fc30STejun Heo for_each_pwq(pwq, wq) { 6196afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 61973494fc30STejun Heo idle = false; 61983494fc30STejun Heo break; 61993494fc30STejun Heo } 62003494fc30STejun Heo } 620155df0933SImran Khan if (idle) /* Nothing to print for idle workqueue */ 620255df0933SImran Khan return; 62033494fc30STejun Heo 62043494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 62053494fc30STejun Heo 62063494fc30STejun Heo for_each_pwq(pwq, wq) { 6207c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, irq_flags); 6208afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 620957116ce1SJohan Hovold /* 621057116ce1SJohan Hovold * Defer printing to avoid deadlocks in console 621157116ce1SJohan Hovold * drivers that queue work while holding locks 621257116ce1SJohan Hovold * also taken in their write paths. 621357116ce1SJohan Hovold */ 621457116ce1SJohan Hovold printk_deferred_enter(); 62153494fc30STejun Heo show_pwq(pwq); 621657116ce1SJohan Hovold printk_deferred_exit(); 621757116ce1SJohan Hovold } 6218c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, irq_flags); 621962635ea8SSergey Senozhatsky /* 622062635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 622155df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 622262635ea8SSergey Senozhatsky * hard lockup. 622362635ea8SSergey Senozhatsky */ 622462635ea8SSergey Senozhatsky touch_nmi_watchdog(); 62253494fc30STejun Heo } 622655df0933SImran Khan 62273494fc30STejun Heo } 62283494fc30STejun Heo 622955df0933SImran Khan /** 623055df0933SImran Khan * show_one_worker_pool - dump state of specified worker pool 623155df0933SImran Khan * @pool: worker pool whose state will be printed 623255df0933SImran Khan */ 623355df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool) 623455df0933SImran Khan { 62353494fc30STejun Heo struct worker *worker; 62363494fc30STejun Heo bool first = true; 6237c26e2f2eSTejun Heo unsigned long irq_flags; 6238335a42ebSPetr Mladek unsigned long hung = 0; 62393494fc30STejun Heo 6240c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags); 62413494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 62423494fc30STejun Heo goto next_pool; 6243335a42ebSPetr Mladek 6244335a42ebSPetr Mladek /* How long the first pending work is waiting for a worker. */ 6245335a42ebSPetr Mladek if (!list_empty(&pool->worklist)) 6246335a42ebSPetr Mladek hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; 6247335a42ebSPetr Mladek 624857116ce1SJohan Hovold /* 624957116ce1SJohan Hovold * Defer printing to avoid deadlocks in console drivers that 625057116ce1SJohan Hovold * queue work while holding locks also taken in their write 625157116ce1SJohan Hovold * paths. 625257116ce1SJohan Hovold */ 625357116ce1SJohan Hovold printk_deferred_enter(); 62543494fc30STejun Heo pr_info("pool %d:", pool->id); 62553494fc30STejun Heo pr_cont_pool_info(pool); 6256335a42ebSPetr Mladek pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); 62573494fc30STejun Heo if (pool->manager) 62583494fc30STejun Heo pr_cont(" manager: %d", 62593494fc30STejun Heo task_pid_nr(pool->manager->task)); 62603494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 62614cb1ef64STejun Heo pr_cont(" %s", first ? "idle: " : ""); 62624cb1ef64STejun Heo pr_cont_worker_id(worker); 62633494fc30STejun Heo first = false; 62643494fc30STejun Heo } 62653494fc30STejun Heo pr_cont("\n"); 626657116ce1SJohan Hovold printk_deferred_exit(); 62673494fc30STejun Heo next_pool: 6268c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags); 626962635ea8SSergey Senozhatsky /* 627062635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 627155df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 627262635ea8SSergey Senozhatsky * hard lockup. 627362635ea8SSergey Senozhatsky */ 627462635ea8SSergey Senozhatsky touch_nmi_watchdog(); 627555df0933SImran Khan 62763494fc30STejun Heo } 62773494fc30STejun Heo 627855df0933SImran Khan /** 627955df0933SImran Khan * show_all_workqueues - dump workqueue state 628055df0933SImran Khan * 6281704bc669SJungseung Lee * Called from a sysrq handler and prints out all busy workqueues and pools. 628255df0933SImran Khan */ 628355df0933SImran Khan void show_all_workqueues(void) 628455df0933SImran Khan { 628555df0933SImran Khan struct workqueue_struct *wq; 628655df0933SImran Khan struct worker_pool *pool; 628755df0933SImran Khan int pi; 628855df0933SImran Khan 628955df0933SImran Khan rcu_read_lock(); 629055df0933SImran Khan 629155df0933SImran Khan pr_info("Showing busy workqueues and worker pools:\n"); 629255df0933SImran Khan 629355df0933SImran Khan list_for_each_entry_rcu(wq, &workqueues, list) 629455df0933SImran Khan show_one_workqueue(wq); 629555df0933SImran Khan 629655df0933SImran Khan for_each_pool(pool, pi) 629755df0933SImran Khan show_one_worker_pool(pool); 629855df0933SImran Khan 629924acfb71SThomas Gleixner rcu_read_unlock(); 63003494fc30STejun Heo } 63013494fc30STejun Heo 6302704bc669SJungseung Lee /** 6303704bc669SJungseung Lee * show_freezable_workqueues - dump freezable workqueue state 6304704bc669SJungseung Lee * 6305704bc669SJungseung Lee * Called from try_to_freeze_tasks() and prints out all freezable workqueues 6306704bc669SJungseung Lee * still busy. 6307704bc669SJungseung Lee */ 6308704bc669SJungseung Lee void show_freezable_workqueues(void) 6309704bc669SJungseung Lee { 6310704bc669SJungseung Lee struct workqueue_struct *wq; 6311704bc669SJungseung Lee 6312704bc669SJungseung Lee rcu_read_lock(); 6313704bc669SJungseung Lee 6314704bc669SJungseung Lee pr_info("Showing freezable workqueues that are still busy:\n"); 6315704bc669SJungseung Lee 6316704bc669SJungseung Lee list_for_each_entry_rcu(wq, &workqueues, list) { 6317704bc669SJungseung Lee if (!(wq->flags & WQ_FREEZABLE)) 6318704bc669SJungseung Lee continue; 6319704bc669SJungseung Lee show_one_workqueue(wq); 6320704bc669SJungseung Lee } 6321704bc669SJungseung Lee 6322704bc669SJungseung Lee rcu_read_unlock(); 6323704bc669SJungseung Lee } 6324704bc669SJungseung Lee 63256b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 63266b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 63276b59808bSTejun Heo { 63286b59808bSTejun Heo int off; 63296b59808bSTejun Heo 63306b59808bSTejun Heo /* always show the actual comm */ 63316b59808bSTejun Heo off = strscpy(buf, task->comm, size); 63326b59808bSTejun Heo if (off < 0) 63336b59808bSTejun Heo return; 63346b59808bSTejun Heo 6335197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 63366b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 63376b59808bSTejun Heo 6338197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 6339197f6accSTejun Heo struct worker *worker = kthread_data(task); 6340197f6accSTejun Heo struct worker_pool *pool = worker->pool; 63416b59808bSTejun Heo 63426b59808bSTejun Heo if (pool) { 6343a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 63446b59808bSTejun Heo /* 6345197f6accSTejun Heo * ->desc tracks information (wq name or 6346197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 6347197f6accSTejun Heo * current, prepend '+', otherwise '-'. 63486b59808bSTejun Heo */ 63496b59808bSTejun Heo if (worker->desc[0] != '\0') { 63506b59808bSTejun Heo if (worker->current_work) 63516b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 63526b59808bSTejun Heo worker->desc); 63536b59808bSTejun Heo else 63546b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 63556b59808bSTejun Heo worker->desc); 63566b59808bSTejun Heo } 6357a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 63586b59808bSTejun Heo } 6359197f6accSTejun Heo } 63606b59808bSTejun Heo 63616b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 63626b59808bSTejun Heo } 63636b59808bSTejun Heo 636466448bc2SMathieu Malaterre #ifdef CONFIG_SMP 636566448bc2SMathieu Malaterre 6366db7bccf4STejun Heo /* 6367db7bccf4STejun Heo * CPU hotplug. 6368db7bccf4STejun Heo * 6369e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 6370112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 6371706026c2STejun Heo * pool which make migrating pending and scheduled works very 6372e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 637394cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 6374e22bee78STejun Heo * blocked draining impractical. 6375e22bee78STejun Heo * 637624647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 6377628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 6378628c78e7STejun Heo * cpu comes back online. 6379db7bccf4STejun Heo */ 6380db7bccf4STejun Heo 6381e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 6382db7bccf4STejun Heo { 63834ce62e9eSTejun Heo struct worker_pool *pool; 6384db7bccf4STejun Heo struct worker *worker; 6385db7bccf4STejun Heo 6386f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 63871258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 6388a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 6389e22bee78STejun Heo 6390f2d5a0eeSTejun Heo /* 639192f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 639294cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 639311b45b0bSLai Jiangshan * must be on the cpu. After this, they may become diasporas. 6394b4ac9384SLai Jiangshan * And the preemption disabled section in their sched callbacks 6395b4ac9384SLai Jiangshan * are guaranteed to see WORKER_UNBOUND since the code here 6396b4ac9384SLai Jiangshan * is on the same cpu. 6397f2d5a0eeSTejun Heo */ 6398da028469SLai Jiangshan for_each_pool_worker(worker, pool) 6399403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 6400db7bccf4STejun Heo 640124647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 6402f2d5a0eeSTejun Heo 6403e22bee78STejun Heo /* 6404989442d7SLai Jiangshan * The handling of nr_running in sched callbacks are disabled 6405989442d7SLai Jiangshan * now. Zap nr_running. After this, nr_running stays zero and 6406989442d7SLai Jiangshan * need_more_worker() and keep_working() are always true as 6407989442d7SLai Jiangshan * long as the worklist is not empty. This pool now behaves as 6408989442d7SLai Jiangshan * an unbound (in terms of concurrency management) pool which 6409eb283428SLai Jiangshan * are served by workers tied to the pool. 6410e22bee78STejun Heo */ 6411bc35f7efSLai Jiangshan pool->nr_running = 0; 6412eb283428SLai Jiangshan 6413eb283428SLai Jiangshan /* 6414eb283428SLai Jiangshan * With concurrency management just turned off, a busy 6415eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 6416eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 6417eb283428SLai Jiangshan */ 64180219a352STejun Heo kick_pool(pool); 6419989442d7SLai Jiangshan 6420a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 6421989442d7SLai Jiangshan 6422793777bcSValentin Schneider for_each_pool_worker(worker, pool) 6423793777bcSValentin Schneider unbind_worker(worker); 6424989442d7SLai Jiangshan 6425989442d7SLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 6426eb283428SLai Jiangshan } 6427db7bccf4STejun Heo } 6428db7bccf4STejun Heo 6429bd7c089eSTejun Heo /** 6430bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 6431bd7c089eSTejun Heo * @pool: pool of interest 6432bd7c089eSTejun Heo * 6433a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 6434bd7c089eSTejun Heo */ 6435bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 6436bd7c089eSTejun Heo { 6437a9ab775bSTejun Heo struct worker *worker; 6438bd7c089eSTejun Heo 64391258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 6440bd7c089eSTejun Heo 6441bd7c089eSTejun Heo /* 6442a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 6443a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 6444402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 6445a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 6446a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 6447bd7c089eSTejun Heo */ 6448c63a2e52SValentin Schneider for_each_pool_worker(worker, pool) { 6449c63a2e52SValentin Schneider kthread_set_per_cpu(worker->task, pool->cpu); 6450c63a2e52SValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 64519546b29eSTejun Heo pool_allowed_cpus(pool)) < 0); 6452c63a2e52SValentin Schneider } 6453a9ab775bSTejun Heo 6454a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 6455f7c17d26SWanpeng Li 64563de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 6457a9ab775bSTejun Heo 6458da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 6459a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 6460a9ab775bSTejun Heo 6461a9ab775bSTejun Heo /* 6462a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 6463a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 6464a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 6465a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 6466a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 6467a9ab775bSTejun Heo * concurrency management. Note that when or whether 6468a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 6469a9ab775bSTejun Heo * 6470c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 6471a9ab775bSTejun Heo * tested without holding any lock in 64726d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may 6473a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 6474a9ab775bSTejun Heo * management operations. 6475bd7c089eSTejun Heo */ 6476a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 6477a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 6478a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 6479c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 6480bd7c089eSTejun Heo } 6481a9ab775bSTejun Heo 6482a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 6483bd7c089eSTejun Heo } 6484bd7c089eSTejun Heo 64857dbc725eSTejun Heo /** 64867dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 64877dbc725eSTejun Heo * @pool: unbound pool of interest 64887dbc725eSTejun Heo * @cpu: the CPU which is coming up 64897dbc725eSTejun Heo * 64907dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 64917dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 64927dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 64937dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 64947dbc725eSTejun Heo */ 64957dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 64967dbc725eSTejun Heo { 64977dbc725eSTejun Heo static cpumask_t cpumask; 64987dbc725eSTejun Heo struct worker *worker; 64997dbc725eSTejun Heo 65001258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 65017dbc725eSTejun Heo 65027dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 65037dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 65047dbc725eSTejun Heo return; 65057dbc725eSTejun Heo 65067dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 65077dbc725eSTejun Heo 65087dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 6509da028469SLai Jiangshan for_each_pool_worker(worker, pool) 6510d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 65117dbc725eSTejun Heo } 65127dbc725eSTejun Heo 65137ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 65141da177e4SLinus Torvalds { 65154ce62e9eSTejun Heo struct worker_pool *pool; 65161da177e4SLinus Torvalds 6517f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 65183ce63377STejun Heo if (pool->nr_workers) 65193ce63377STejun Heo continue; 6520051e1850SLai Jiangshan if (!create_worker(pool)) 65217ee681b2SThomas Gleixner return -ENOMEM; 65223af24433SOleg Nesterov } 65237ee681b2SThomas Gleixner return 0; 65247ee681b2SThomas Gleixner } 65251da177e4SLinus Torvalds 65267ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 65277ee681b2SThomas Gleixner { 65287ee681b2SThomas Gleixner struct worker_pool *pool; 65297ee681b2SThomas Gleixner struct workqueue_struct *wq; 65307ee681b2SThomas Gleixner int pi; 65317ee681b2SThomas Gleixner 653268e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 65337dbc725eSTejun Heo 65347dbc725eSTejun Heo for_each_pool(pool, pi) { 65354cb1ef64STejun Heo /* BH pools aren't affected by hotplug */ 65364cb1ef64STejun Heo if (pool->flags & POOL_BH) 65374cb1ef64STejun Heo continue; 653894cf58bbSTejun Heo 65394cb1ef64STejun Heo mutex_lock(&wq_pool_attach_mutex); 6540f05b558dSLai Jiangshan if (pool->cpu == cpu) 654194cf58bbSTejun Heo rebind_workers(pool); 6542f05b558dSLai Jiangshan else if (pool->cpu < 0) 65437dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 65441258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 654594cf58bbSTejun Heo } 65467dbc725eSTejun Heo 6547fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 65484cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 654984193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 655084193c07STejun Heo 655184193c07STejun Heo if (attrs) { 655284193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 65534cbfd3deSTejun Heo int tcpu; 65544cbfd3deSTejun Heo 655584193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 6556fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, true); 65575797b1c1STejun Heo 65585797b1c1STejun Heo mutex_lock(&wq->mutex); 65595797b1c1STejun Heo wq_update_node_max_active(wq, -1); 65605797b1c1STejun Heo mutex_unlock(&wq->mutex); 65614cbfd3deSTejun Heo } 65624cbfd3deSTejun Heo } 65634c16bd32STejun Heo 656468e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 65657ee681b2SThomas Gleixner return 0; 656665758202STejun Heo } 656765758202STejun Heo 65687ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 656965758202STejun Heo { 65704c16bd32STejun Heo struct workqueue_struct *wq; 65718db25e78STejun Heo 65724c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 6573e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 6574e8b3f8dbSLai Jiangshan return -1; 6575e8b3f8dbSLai Jiangshan 6576e8b3f8dbSLai Jiangshan unbind_workers(cpu); 65774c16bd32STejun Heo 6578fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 65794c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 65804cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 658184193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 658284193c07STejun Heo 658384193c07STejun Heo if (attrs) { 658484193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 65854cbfd3deSTejun Heo int tcpu; 65864cbfd3deSTejun Heo 658784193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 6588fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, false); 65895797b1c1STejun Heo 65905797b1c1STejun Heo mutex_lock(&wq->mutex); 65915797b1c1STejun Heo wq_update_node_max_active(wq, cpu); 65925797b1c1STejun Heo mutex_unlock(&wq->mutex); 65934cbfd3deSTejun Heo } 65944cbfd3deSTejun Heo } 65954c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 65964c16bd32STejun Heo 65977ee681b2SThomas Gleixner return 0; 659865758202STejun Heo } 659965758202STejun Heo 66002d3854a3SRusty Russell struct work_for_cpu { 6601ed48ece2STejun Heo struct work_struct work; 66022d3854a3SRusty Russell long (*fn)(void *); 66032d3854a3SRusty Russell void *arg; 66042d3854a3SRusty Russell long ret; 66052d3854a3SRusty Russell }; 66062d3854a3SRusty Russell 6607ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 66082d3854a3SRusty Russell { 6609ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 6610ed48ece2STejun Heo 66112d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 66122d3854a3SRusty Russell } 66132d3854a3SRusty Russell 66142d3854a3SRusty Russell /** 6615265f3ed0SFrederic Weisbecker * work_on_cpu_key - run a function in thread context on a particular cpu 66162d3854a3SRusty Russell * @cpu: the cpu to run on 66172d3854a3SRusty Russell * @fn: the function to run 66182d3854a3SRusty Russell * @arg: the function arg 6619265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 66202d3854a3SRusty Russell * 662131ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 66226b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 6623d185af30SYacine Belkadi * 6624d185af30SYacine Belkadi * Return: The value @fn returns. 66252d3854a3SRusty Russell */ 6626265f3ed0SFrederic Weisbecker long work_on_cpu_key(int cpu, long (*fn)(void *), 6627265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 66282d3854a3SRusty Russell { 6629ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 66302d3854a3SRusty Russell 6631265f3ed0SFrederic Weisbecker INIT_WORK_ONSTACK_KEY(&wfc.work, work_for_cpu_fn, key); 6632ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 663312997d1aSBjorn Helgaas flush_work(&wfc.work); 6634440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 66352d3854a3SRusty Russell return wfc.ret; 66362d3854a3SRusty Russell } 6637265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_key); 66380e8d6a93SThomas Gleixner 66390e8d6a93SThomas Gleixner /** 6640265f3ed0SFrederic Weisbecker * work_on_cpu_safe_key - run a function in thread context on a particular cpu 66410e8d6a93SThomas Gleixner * @cpu: the cpu to run on 66420e8d6a93SThomas Gleixner * @fn: the function to run 66430e8d6a93SThomas Gleixner * @arg: the function argument 6644265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 66450e8d6a93SThomas Gleixner * 66460e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 66470e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 66480e8d6a93SThomas Gleixner * 66490e8d6a93SThomas Gleixner * Return: The value @fn returns. 66500e8d6a93SThomas Gleixner */ 6651265f3ed0SFrederic Weisbecker long work_on_cpu_safe_key(int cpu, long (*fn)(void *), 6652265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 66530e8d6a93SThomas Gleixner { 66540e8d6a93SThomas Gleixner long ret = -ENODEV; 66550e8d6a93SThomas Gleixner 6656ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 66570e8d6a93SThomas Gleixner if (cpu_online(cpu)) 6658265f3ed0SFrederic Weisbecker ret = work_on_cpu_key(cpu, fn, arg, key); 6659ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 66600e8d6a93SThomas Gleixner return ret; 66610e8d6a93SThomas Gleixner } 6662265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_safe_key); 66632d3854a3SRusty Russell #endif /* CONFIG_SMP */ 66642d3854a3SRusty Russell 6665a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 6666e7577c50SRusty Russell 6667a0a1a5fdSTejun Heo /** 6668a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 6669a0a1a5fdSTejun Heo * 667058a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 6671f97a4a1aSLai Jiangshan * workqueues will queue new works to their inactive_works list instead of 6672706026c2STejun Heo * pool->worklist. 6673a0a1a5fdSTejun Heo * 6674a0a1a5fdSTejun Heo * CONTEXT: 6675a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 6676a0a1a5fdSTejun Heo */ 6677a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 6678a0a1a5fdSTejun Heo { 667924b8a847STejun Heo struct workqueue_struct *wq; 6680a0a1a5fdSTejun Heo 668168e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6682a0a1a5fdSTejun Heo 66836183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 6684a0a1a5fdSTejun Heo workqueue_freezing = true; 6685a0a1a5fdSTejun Heo 668624b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6687a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 6688a045a272STejun Heo wq_adjust_max_active(wq); 6689a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 6690a1056305STejun Heo } 66915bcab335STejun Heo 669268e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6693a0a1a5fdSTejun Heo } 6694a0a1a5fdSTejun Heo 6695a0a1a5fdSTejun Heo /** 669658a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 6697a0a1a5fdSTejun Heo * 6698a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 6699a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 6700a0a1a5fdSTejun Heo * 6701a0a1a5fdSTejun Heo * CONTEXT: 670268e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 6703a0a1a5fdSTejun Heo * 6704d185af30SYacine Belkadi * Return: 670558a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 670658a69cb4STejun Heo * is complete. 6707a0a1a5fdSTejun Heo */ 6708a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 6709a0a1a5fdSTejun Heo { 6710a0a1a5fdSTejun Heo bool busy = false; 671124b8a847STejun Heo struct workqueue_struct *wq; 671224b8a847STejun Heo struct pool_workqueue *pwq; 6713a0a1a5fdSTejun Heo 671468e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6715a0a1a5fdSTejun Heo 67166183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 6717a0a1a5fdSTejun Heo 671824b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 671924b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 672024b8a847STejun Heo continue; 6721a0a1a5fdSTejun Heo /* 6722a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 6723a0a1a5fdSTejun Heo * to peek without lock. 6724a0a1a5fdSTejun Heo */ 672524acfb71SThomas Gleixner rcu_read_lock(); 672624b8a847STejun Heo for_each_pwq(pwq, wq) { 67276183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 6728112202d9STejun Heo if (pwq->nr_active) { 6729a0a1a5fdSTejun Heo busy = true; 673024acfb71SThomas Gleixner rcu_read_unlock(); 6731a0a1a5fdSTejun Heo goto out_unlock; 6732a0a1a5fdSTejun Heo } 6733a0a1a5fdSTejun Heo } 673424acfb71SThomas Gleixner rcu_read_unlock(); 6735a0a1a5fdSTejun Heo } 6736a0a1a5fdSTejun Heo out_unlock: 673768e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6738a0a1a5fdSTejun Heo return busy; 6739a0a1a5fdSTejun Heo } 6740a0a1a5fdSTejun Heo 6741a0a1a5fdSTejun Heo /** 6742a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 6743a0a1a5fdSTejun Heo * 6744a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 6745706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 6746a0a1a5fdSTejun Heo * 6747a0a1a5fdSTejun Heo * CONTEXT: 6748a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 6749a0a1a5fdSTejun Heo */ 6750a0a1a5fdSTejun Heo void thaw_workqueues(void) 6751a0a1a5fdSTejun Heo { 675224b8a847STejun Heo struct workqueue_struct *wq; 6753a0a1a5fdSTejun Heo 675468e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6755a0a1a5fdSTejun Heo 6756a0a1a5fdSTejun Heo if (!workqueue_freezing) 6757a0a1a5fdSTejun Heo goto out_unlock; 6758a0a1a5fdSTejun Heo 675974b414eaSLai Jiangshan workqueue_freezing = false; 676024b8a847STejun Heo 676124b8a847STejun Heo /* restore max_active and repopulate worklist */ 676224b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6763a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 6764a045a272STejun Heo wq_adjust_max_active(wq); 6765a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 676624b8a847STejun Heo } 676724b8a847STejun Heo 6768a0a1a5fdSTejun Heo out_unlock: 676968e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6770a0a1a5fdSTejun Heo } 6771a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 6772a0a1a5fdSTejun Heo 677399c621efSLai Jiangshan static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) 6774042f7df1SLai Jiangshan { 6775042f7df1SLai Jiangshan LIST_HEAD(ctxs); 6776042f7df1SLai Jiangshan int ret = 0; 6777042f7df1SLai Jiangshan struct workqueue_struct *wq; 6778042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 6779042f7df1SLai Jiangshan 6780042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6781042f7df1SLai Jiangshan 6782042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 67838eb17dc1SWaiman Long if (!(wq->flags & WQ_UNBOUND) || (wq->flags & __WQ_DESTROYING)) 6784042f7df1SLai Jiangshan continue; 6785042f7df1SLai Jiangshan 678699c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); 678784193c07STejun Heo if (IS_ERR(ctx)) { 678884193c07STejun Heo ret = PTR_ERR(ctx); 6789042f7df1SLai Jiangshan break; 6790042f7df1SLai Jiangshan } 6791042f7df1SLai Jiangshan 6792042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 6793042f7df1SLai Jiangshan } 6794042f7df1SLai Jiangshan 6795042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 6796042f7df1SLai Jiangshan if (!ret) 6797042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 6798042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 6799042f7df1SLai Jiangshan } 6800042f7df1SLai Jiangshan 680199c621efSLai Jiangshan if (!ret) { 680299c621efSLai Jiangshan mutex_lock(&wq_pool_attach_mutex); 680399c621efSLai Jiangshan cpumask_copy(wq_unbound_cpumask, unbound_cpumask); 680499c621efSLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 680599c621efSLai Jiangshan } 6806042f7df1SLai Jiangshan return ret; 6807042f7df1SLai Jiangshan } 6808042f7df1SLai Jiangshan 6809042f7df1SLai Jiangshan /** 6810fe28f631SWaiman Long * workqueue_unbound_exclude_cpumask - Exclude given CPUs from unbound cpumask 6811fe28f631SWaiman Long * @exclude_cpumask: the cpumask to be excluded from wq_unbound_cpumask 6812fe28f631SWaiman Long * 6813fe28f631SWaiman Long * This function can be called from cpuset code to provide a set of isolated 6814fe28f631SWaiman Long * CPUs that should be excluded from wq_unbound_cpumask. The caller must hold 6815fe28f631SWaiman Long * either cpus_read_lock or cpus_write_lock. 6816fe28f631SWaiman Long */ 6817fe28f631SWaiman Long int workqueue_unbound_exclude_cpumask(cpumask_var_t exclude_cpumask) 6818fe28f631SWaiman Long { 6819fe28f631SWaiman Long cpumask_var_t cpumask; 6820fe28f631SWaiman Long int ret = 0; 6821fe28f631SWaiman Long 6822fe28f631SWaiman Long if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 6823fe28f631SWaiman Long return -ENOMEM; 6824fe28f631SWaiman Long 6825fe28f631SWaiman Long lockdep_assert_cpus_held(); 6826fe28f631SWaiman Long mutex_lock(&wq_pool_mutex); 6827fe28f631SWaiman Long 6828fe28f631SWaiman Long /* Save the current isolated cpumask & export it via sysfs */ 6829fe28f631SWaiman Long cpumask_copy(wq_isolated_cpumask, exclude_cpumask); 6830fe28f631SWaiman Long 6831fe28f631SWaiman Long /* 6832fe28f631SWaiman Long * If the operation fails, it will fall back to 6833fe28f631SWaiman Long * wq_requested_unbound_cpumask which is initially set to 6834fe28f631SWaiman Long * (HK_TYPE_WQ ∩ HK_TYPE_DOMAIN) house keeping mask and rewritten 6835fe28f631SWaiman Long * by any subsequent write to workqueue/cpumask sysfs file. 6836fe28f631SWaiman Long */ 6837fe28f631SWaiman Long if (!cpumask_andnot(cpumask, wq_requested_unbound_cpumask, exclude_cpumask)) 6838fe28f631SWaiman Long cpumask_copy(cpumask, wq_requested_unbound_cpumask); 6839fe28f631SWaiman Long if (!cpumask_equal(cpumask, wq_unbound_cpumask)) 6840fe28f631SWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 6841fe28f631SWaiman Long 6842fe28f631SWaiman Long mutex_unlock(&wq_pool_mutex); 6843fe28f631SWaiman Long free_cpumask_var(cpumask); 6844fe28f631SWaiman Long return ret; 6845fe28f631SWaiman Long } 6846fe28f631SWaiman Long 684763c5484eSTejun Heo static int parse_affn_scope(const char *val) 684863c5484eSTejun Heo { 684963c5484eSTejun Heo int i; 685063c5484eSTejun Heo 685163c5484eSTejun Heo for (i = 0; i < ARRAY_SIZE(wq_affn_names); i++) { 685263c5484eSTejun Heo if (!strncasecmp(val, wq_affn_names[i], strlen(wq_affn_names[i]))) 685363c5484eSTejun Heo return i; 685463c5484eSTejun Heo } 685563c5484eSTejun Heo return -EINVAL; 685663c5484eSTejun Heo } 685763c5484eSTejun Heo 685863c5484eSTejun Heo static int wq_affn_dfl_set(const char *val, const struct kernel_param *kp) 685963c5484eSTejun Heo { 6860523a301eSTejun Heo struct workqueue_struct *wq; 6861523a301eSTejun Heo int affn, cpu; 686263c5484eSTejun Heo 686363c5484eSTejun Heo affn = parse_affn_scope(val); 686463c5484eSTejun Heo if (affn < 0) 686563c5484eSTejun Heo return affn; 6866523a301eSTejun Heo if (affn == WQ_AFFN_DFL) 6867523a301eSTejun Heo return -EINVAL; 6868523a301eSTejun Heo 6869523a301eSTejun Heo cpus_read_lock(); 6870523a301eSTejun Heo mutex_lock(&wq_pool_mutex); 687163c5484eSTejun Heo 687263c5484eSTejun Heo wq_affn_dfl = affn; 6873523a301eSTejun Heo 6874523a301eSTejun Heo list_for_each_entry(wq, &workqueues, list) { 6875523a301eSTejun Heo for_each_online_cpu(cpu) { 6876523a301eSTejun Heo wq_update_pod(wq, cpu, cpu, true); 6877523a301eSTejun Heo } 6878523a301eSTejun Heo } 6879523a301eSTejun Heo 6880523a301eSTejun Heo mutex_unlock(&wq_pool_mutex); 6881523a301eSTejun Heo cpus_read_unlock(); 6882523a301eSTejun Heo 688363c5484eSTejun Heo return 0; 688463c5484eSTejun Heo } 688563c5484eSTejun Heo 688663c5484eSTejun Heo static int wq_affn_dfl_get(char *buffer, const struct kernel_param *kp) 688763c5484eSTejun Heo { 688863c5484eSTejun Heo return scnprintf(buffer, PAGE_SIZE, "%s\n", wq_affn_names[wq_affn_dfl]); 688963c5484eSTejun Heo } 689063c5484eSTejun Heo 689163c5484eSTejun Heo static const struct kernel_param_ops wq_affn_dfl_ops = { 689263c5484eSTejun Heo .set = wq_affn_dfl_set, 689363c5484eSTejun Heo .get = wq_affn_dfl_get, 689463c5484eSTejun Heo }; 689563c5484eSTejun Heo 689663c5484eSTejun Heo module_param_cb(default_affinity_scope, &wq_affn_dfl_ops, NULL, 0644); 689763c5484eSTejun Heo 68986ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 68996ba94429SFrederic Weisbecker /* 69006ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 69016ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 69026ba94429SFrederic Weisbecker * following attributes. 69036ba94429SFrederic Weisbecker * 69046ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 69056ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 69066ba94429SFrederic Weisbecker * 69076ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 69086ba94429SFrederic Weisbecker * 69096ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 69106ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 691163c5484eSTejun Heo * affinity_scope RW str : worker CPU affinity scope (cache, numa, none) 69128639ecebSTejun Heo * affinity_strict RW bool : worker CPU affinity is strict 69136ba94429SFrederic Weisbecker */ 69146ba94429SFrederic Weisbecker struct wq_device { 69156ba94429SFrederic Weisbecker struct workqueue_struct *wq; 69166ba94429SFrederic Weisbecker struct device dev; 69176ba94429SFrederic Weisbecker }; 69186ba94429SFrederic Weisbecker 69196ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 69206ba94429SFrederic Weisbecker { 69216ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 69226ba94429SFrederic Weisbecker 69236ba94429SFrederic Weisbecker return wq_dev->wq; 69246ba94429SFrederic Weisbecker } 69256ba94429SFrederic Weisbecker 69266ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 69276ba94429SFrederic Weisbecker char *buf) 69286ba94429SFrederic Weisbecker { 69296ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 69306ba94429SFrederic Weisbecker 69316ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 69326ba94429SFrederic Weisbecker } 69336ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 69346ba94429SFrederic Weisbecker 69356ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 69366ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 69376ba94429SFrederic Weisbecker { 69386ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 69396ba94429SFrederic Weisbecker 69406ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 69416ba94429SFrederic Weisbecker } 69426ba94429SFrederic Weisbecker 69436ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 69446ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 69456ba94429SFrederic Weisbecker size_t count) 69466ba94429SFrederic Weisbecker { 69476ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 69486ba94429SFrederic Weisbecker int val; 69496ba94429SFrederic Weisbecker 69506ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 69516ba94429SFrederic Weisbecker return -EINVAL; 69526ba94429SFrederic Weisbecker 69536ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 69546ba94429SFrederic Weisbecker return count; 69556ba94429SFrederic Weisbecker } 69566ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 69576ba94429SFrederic Weisbecker 69586ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 69596ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 69606ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 69616ba94429SFrederic Weisbecker NULL, 69626ba94429SFrederic Weisbecker }; 69636ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 69646ba94429SFrederic Weisbecker 696549277a5bSWaiman Long static void apply_wqattrs_lock(void) 696649277a5bSWaiman Long { 696749277a5bSWaiman Long /* CPUs should stay stable across pwq creations and installations */ 696849277a5bSWaiman Long cpus_read_lock(); 696949277a5bSWaiman Long mutex_lock(&wq_pool_mutex); 697049277a5bSWaiman Long } 697149277a5bSWaiman Long 697249277a5bSWaiman Long static void apply_wqattrs_unlock(void) 697349277a5bSWaiman Long { 697449277a5bSWaiman Long mutex_unlock(&wq_pool_mutex); 697549277a5bSWaiman Long cpus_read_unlock(); 697649277a5bSWaiman Long } 697749277a5bSWaiman Long 69786ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 69796ba94429SFrederic Weisbecker 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, "%d\n", wq->unbound_attrs->nice); 69866ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 69876ba94429SFrederic Weisbecker 69886ba94429SFrederic Weisbecker return written; 69896ba94429SFrederic Weisbecker } 69906ba94429SFrederic Weisbecker 69916ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 69926ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 69936ba94429SFrederic Weisbecker { 69946ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 69956ba94429SFrederic Weisbecker 6996899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6997899a94feSLai Jiangshan 6998be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs(); 69996ba94429SFrederic Weisbecker if (!attrs) 70006ba94429SFrederic Weisbecker return NULL; 70016ba94429SFrederic Weisbecker 70026ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 70036ba94429SFrederic Weisbecker return attrs; 70046ba94429SFrederic Weisbecker } 70056ba94429SFrederic Weisbecker 70066ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 70076ba94429SFrederic Weisbecker const char *buf, size_t count) 70086ba94429SFrederic Weisbecker { 70096ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 70106ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 7011d4d3e257SLai Jiangshan int ret = -ENOMEM; 7012d4d3e257SLai Jiangshan 7013d4d3e257SLai Jiangshan apply_wqattrs_lock(); 70146ba94429SFrederic Weisbecker 70156ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 70166ba94429SFrederic Weisbecker if (!attrs) 7017d4d3e257SLai Jiangshan goto out_unlock; 70186ba94429SFrederic Weisbecker 70196ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 70206ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 7021d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 70226ba94429SFrederic Weisbecker else 70236ba94429SFrederic Weisbecker ret = -EINVAL; 70246ba94429SFrederic Weisbecker 7025d4d3e257SLai Jiangshan out_unlock: 7026d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 70276ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 70286ba94429SFrederic Weisbecker return ret ?: count; 70296ba94429SFrederic Weisbecker } 70306ba94429SFrederic Weisbecker 70316ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 70326ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 70336ba94429SFrederic Weisbecker { 70346ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 70356ba94429SFrederic Weisbecker int written; 70366ba94429SFrederic Weisbecker 70376ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 70386ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 70396ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 70406ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 70416ba94429SFrederic Weisbecker return written; 70426ba94429SFrederic Weisbecker } 70436ba94429SFrederic Weisbecker 70446ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 70456ba94429SFrederic Weisbecker struct device_attribute *attr, 70466ba94429SFrederic Weisbecker const char *buf, size_t count) 70476ba94429SFrederic Weisbecker { 70486ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 70496ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 7050d4d3e257SLai Jiangshan int ret = -ENOMEM; 7051d4d3e257SLai Jiangshan 7052d4d3e257SLai Jiangshan apply_wqattrs_lock(); 70536ba94429SFrederic Weisbecker 70546ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 70556ba94429SFrederic Weisbecker if (!attrs) 7056d4d3e257SLai Jiangshan goto out_unlock; 70576ba94429SFrederic Weisbecker 70586ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 70596ba94429SFrederic Weisbecker if (!ret) 7060d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 70616ba94429SFrederic Weisbecker 7062d4d3e257SLai Jiangshan out_unlock: 7063d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 70646ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 70656ba94429SFrederic Weisbecker return ret ?: count; 70666ba94429SFrederic Weisbecker } 70676ba94429SFrederic Weisbecker 706863c5484eSTejun Heo static ssize_t wq_affn_scope_show(struct device *dev, 706963c5484eSTejun Heo struct device_attribute *attr, char *buf) 707063c5484eSTejun Heo { 707163c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 707263c5484eSTejun Heo int written; 707363c5484eSTejun Heo 707463c5484eSTejun Heo mutex_lock(&wq->mutex); 7075523a301eSTejun Heo if (wq->unbound_attrs->affn_scope == WQ_AFFN_DFL) 7076523a301eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s (%s)\n", 7077523a301eSTejun Heo wq_affn_names[WQ_AFFN_DFL], 7078523a301eSTejun Heo wq_affn_names[wq_affn_dfl]); 7079523a301eSTejun Heo else 708063c5484eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s\n", 708163c5484eSTejun Heo wq_affn_names[wq->unbound_attrs->affn_scope]); 708263c5484eSTejun Heo mutex_unlock(&wq->mutex); 708363c5484eSTejun Heo 708463c5484eSTejun Heo return written; 708563c5484eSTejun Heo } 708663c5484eSTejun Heo 708763c5484eSTejun Heo static ssize_t wq_affn_scope_store(struct device *dev, 708863c5484eSTejun Heo struct device_attribute *attr, 708963c5484eSTejun Heo const char *buf, size_t count) 709063c5484eSTejun Heo { 709163c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 709263c5484eSTejun Heo struct workqueue_attrs *attrs; 709363c5484eSTejun Heo int affn, ret = -ENOMEM; 709463c5484eSTejun Heo 709563c5484eSTejun Heo affn = parse_affn_scope(buf); 709663c5484eSTejun Heo if (affn < 0) 709763c5484eSTejun Heo return affn; 709863c5484eSTejun Heo 709963c5484eSTejun Heo apply_wqattrs_lock(); 710063c5484eSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 710163c5484eSTejun Heo if (attrs) { 710263c5484eSTejun Heo attrs->affn_scope = affn; 710363c5484eSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 710463c5484eSTejun Heo } 710563c5484eSTejun Heo apply_wqattrs_unlock(); 710663c5484eSTejun Heo free_workqueue_attrs(attrs); 710763c5484eSTejun Heo return ret ?: count; 710863c5484eSTejun Heo } 710963c5484eSTejun Heo 71108639ecebSTejun Heo static ssize_t wq_affinity_strict_show(struct device *dev, 71118639ecebSTejun Heo struct device_attribute *attr, char *buf) 71128639ecebSTejun Heo { 71138639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 71148639ecebSTejun Heo 71158639ecebSTejun Heo return scnprintf(buf, PAGE_SIZE, "%d\n", 71168639ecebSTejun Heo wq->unbound_attrs->affn_strict); 71178639ecebSTejun Heo } 71188639ecebSTejun Heo 71198639ecebSTejun Heo static ssize_t wq_affinity_strict_store(struct device *dev, 71208639ecebSTejun Heo struct device_attribute *attr, 71218639ecebSTejun Heo const char *buf, size_t count) 71228639ecebSTejun Heo { 71238639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 71248639ecebSTejun Heo struct workqueue_attrs *attrs; 71258639ecebSTejun Heo int v, ret = -ENOMEM; 71268639ecebSTejun Heo 71278639ecebSTejun Heo if (sscanf(buf, "%d", &v) != 1) 71288639ecebSTejun Heo return -EINVAL; 71298639ecebSTejun Heo 71308639ecebSTejun Heo apply_wqattrs_lock(); 71318639ecebSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 71328639ecebSTejun Heo if (attrs) { 71338639ecebSTejun Heo attrs->affn_strict = (bool)v; 71348639ecebSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 71358639ecebSTejun Heo } 71368639ecebSTejun Heo apply_wqattrs_unlock(); 71378639ecebSTejun Heo free_workqueue_attrs(attrs); 71388639ecebSTejun Heo return ret ?: count; 71398639ecebSTejun Heo } 71408639ecebSTejun Heo 71416ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 71426ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 71436ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 714463c5484eSTejun Heo __ATTR(affinity_scope, 0644, wq_affn_scope_show, wq_affn_scope_store), 71458639ecebSTejun Heo __ATTR(affinity_strict, 0644, wq_affinity_strict_show, wq_affinity_strict_store), 71466ba94429SFrederic Weisbecker __ATTR_NULL, 71476ba94429SFrederic Weisbecker }; 71486ba94429SFrederic Weisbecker 71495df9197eSRicardo B. Marliere static const struct bus_type wq_subsys = { 71506ba94429SFrederic Weisbecker .name = "workqueue", 71516ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 71526ba94429SFrederic Weisbecker }; 71536ba94429SFrederic Weisbecker 715449277a5bSWaiman Long /** 715549277a5bSWaiman Long * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 715649277a5bSWaiman Long * @cpumask: the cpumask to set 715749277a5bSWaiman Long * 715849277a5bSWaiman Long * The low-level workqueues cpumask is a global cpumask that limits 715949277a5bSWaiman Long * the affinity of all unbound workqueues. This function check the @cpumask 716049277a5bSWaiman Long * and apply it to all unbound workqueues and updates all pwqs of them. 716149277a5bSWaiman Long * 716249277a5bSWaiman Long * Return: 0 - Success 716349277a5bSWaiman Long * -EINVAL - Invalid @cpumask 716449277a5bSWaiman Long * -ENOMEM - Failed to allocate memory for attrs or pwqs. 716549277a5bSWaiman Long */ 716649277a5bSWaiman Long static int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 716749277a5bSWaiman Long { 716849277a5bSWaiman Long int ret = -EINVAL; 716949277a5bSWaiman Long 717049277a5bSWaiman Long /* 717149277a5bSWaiman Long * Not excluding isolated cpus on purpose. 717249277a5bSWaiman Long * If the user wishes to include them, we allow that. 717349277a5bSWaiman Long */ 717449277a5bSWaiman Long cpumask_and(cpumask, cpumask, cpu_possible_mask); 717549277a5bSWaiman Long if (!cpumask_empty(cpumask)) { 717649277a5bSWaiman Long apply_wqattrs_lock(); 717749277a5bSWaiman Long cpumask_copy(wq_requested_unbound_cpumask, cpumask); 717849277a5bSWaiman Long if (cpumask_equal(cpumask, wq_unbound_cpumask)) { 717949277a5bSWaiman Long ret = 0; 718049277a5bSWaiman Long goto out_unlock; 718149277a5bSWaiman Long } 718249277a5bSWaiman Long 718349277a5bSWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 718449277a5bSWaiman Long 718549277a5bSWaiman Long out_unlock: 718649277a5bSWaiman Long apply_wqattrs_unlock(); 718749277a5bSWaiman Long } 718849277a5bSWaiman Long 718949277a5bSWaiman Long return ret; 719049277a5bSWaiman Long } 719149277a5bSWaiman Long 7192fe28f631SWaiman Long static ssize_t __wq_cpumask_show(struct device *dev, 7193fe28f631SWaiman Long struct device_attribute *attr, char *buf, cpumask_var_t mask) 7194b05a7928SFrederic Weisbecker { 7195b05a7928SFrederic Weisbecker int written; 7196b05a7928SFrederic Weisbecker 7197042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 7198fe28f631SWaiman Long written = scnprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask)); 7199042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 7200b05a7928SFrederic Weisbecker 7201b05a7928SFrederic Weisbecker return written; 7202b05a7928SFrederic Weisbecker } 7203b05a7928SFrederic Weisbecker 7204fe28f631SWaiman Long static ssize_t wq_unbound_cpumask_show(struct device *dev, 7205fe28f631SWaiman Long struct device_attribute *attr, char *buf) 7206fe28f631SWaiman Long { 7207fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_unbound_cpumask); 7208fe28f631SWaiman Long } 7209fe28f631SWaiman Long 7210fe28f631SWaiman Long static ssize_t wq_requested_cpumask_show(struct device *dev, 7211fe28f631SWaiman Long struct device_attribute *attr, char *buf) 7212fe28f631SWaiman Long { 7213fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_requested_unbound_cpumask); 7214fe28f631SWaiman Long } 7215fe28f631SWaiman Long 7216fe28f631SWaiman Long static ssize_t wq_isolated_cpumask_show(struct device *dev, 7217fe28f631SWaiman Long struct device_attribute *attr, char *buf) 7218fe28f631SWaiman Long { 7219fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_isolated_cpumask); 7220fe28f631SWaiman Long } 7221fe28f631SWaiman Long 7222042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 7223042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 7224042f7df1SLai Jiangshan { 7225042f7df1SLai Jiangshan cpumask_var_t cpumask; 7226042f7df1SLai Jiangshan int ret; 7227042f7df1SLai Jiangshan 7228042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 7229042f7df1SLai Jiangshan return -ENOMEM; 7230042f7df1SLai Jiangshan 7231042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 7232042f7df1SLai Jiangshan if (!ret) 7233042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 7234042f7df1SLai Jiangshan 7235042f7df1SLai Jiangshan free_cpumask_var(cpumask); 7236042f7df1SLai Jiangshan return ret ? ret : count; 7237042f7df1SLai Jiangshan } 7238042f7df1SLai Jiangshan 7239fe28f631SWaiman Long static struct device_attribute wq_sysfs_cpumask_attrs[] = { 7240042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 7241fe28f631SWaiman Long wq_unbound_cpumask_store), 7242fe28f631SWaiman Long __ATTR(cpumask_requested, 0444, wq_requested_cpumask_show, NULL), 7243fe28f631SWaiman Long __ATTR(cpumask_isolated, 0444, wq_isolated_cpumask_show, NULL), 7244fe28f631SWaiman Long __ATTR_NULL, 7245fe28f631SWaiman Long }; 7246b05a7928SFrederic Weisbecker 72476ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 72486ba94429SFrederic Weisbecker { 7249686f6697SGreg Kroah-Hartman struct device *dev_root; 7250b05a7928SFrederic Weisbecker int err; 7251b05a7928SFrederic Weisbecker 7252b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 7253b05a7928SFrederic Weisbecker if (err) 7254b05a7928SFrederic Weisbecker return err; 7255b05a7928SFrederic Weisbecker 7256686f6697SGreg Kroah-Hartman dev_root = bus_get_dev_root(&wq_subsys); 7257686f6697SGreg Kroah-Hartman if (dev_root) { 7258fe28f631SWaiman Long struct device_attribute *attr; 7259fe28f631SWaiman Long 7260fe28f631SWaiman Long for (attr = wq_sysfs_cpumask_attrs; attr->attr.name; attr++) { 7261fe28f631SWaiman Long err = device_create_file(dev_root, attr); 7262fe28f631SWaiman Long if (err) 7263fe28f631SWaiman Long break; 7264fe28f631SWaiman Long } 7265686f6697SGreg Kroah-Hartman put_device(dev_root); 7266686f6697SGreg Kroah-Hartman } 7267686f6697SGreg Kroah-Hartman return err; 72686ba94429SFrederic Weisbecker } 72696ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 72706ba94429SFrederic Weisbecker 72716ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 72726ba94429SFrederic Weisbecker { 72736ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 72746ba94429SFrederic Weisbecker 72756ba94429SFrederic Weisbecker kfree(wq_dev); 72766ba94429SFrederic Weisbecker } 72776ba94429SFrederic Weisbecker 72786ba94429SFrederic Weisbecker /** 72796ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 72806ba94429SFrederic Weisbecker * @wq: the workqueue to register 72816ba94429SFrederic Weisbecker * 72826ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 72836ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 72846ba94429SFrederic Weisbecker * which is the preferred method. 72856ba94429SFrederic Weisbecker * 72866ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 72876ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 72886ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 72896ba94429SFrederic Weisbecker * attributes. 72906ba94429SFrederic Weisbecker * 72916ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 72926ba94429SFrederic Weisbecker */ 72936ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 72946ba94429SFrederic Weisbecker { 72956ba94429SFrederic Weisbecker struct wq_device *wq_dev; 72966ba94429SFrederic Weisbecker int ret; 72976ba94429SFrederic Weisbecker 72986ba94429SFrederic Weisbecker /* 72994c065dbcSWaiman Long * Adjusting max_active breaks ordering guarantee. Disallow exposing 73004c065dbcSWaiman Long * ordered workqueues. 73016ba94429SFrederic Weisbecker */ 73023bc1e711STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED)) 73036ba94429SFrederic Weisbecker return -EINVAL; 73046ba94429SFrederic Weisbecker 73056ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 73066ba94429SFrederic Weisbecker if (!wq_dev) 73076ba94429SFrederic Weisbecker return -ENOMEM; 73086ba94429SFrederic Weisbecker 73096ba94429SFrederic Weisbecker wq_dev->wq = wq; 73106ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 73116ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 731223217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 73136ba94429SFrederic Weisbecker 73146ba94429SFrederic Weisbecker /* 73156ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 73166ba94429SFrederic Weisbecker * everything is ready. 73176ba94429SFrederic Weisbecker */ 73186ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 73196ba94429SFrederic Weisbecker 73206ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 73216ba94429SFrederic Weisbecker if (ret) { 7322537f4146SArvind Yadav put_device(&wq_dev->dev); 73236ba94429SFrederic Weisbecker wq->wq_dev = NULL; 73246ba94429SFrederic Weisbecker return ret; 73256ba94429SFrederic Weisbecker } 73266ba94429SFrederic Weisbecker 73276ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 73286ba94429SFrederic Weisbecker struct device_attribute *attr; 73296ba94429SFrederic Weisbecker 73306ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 73316ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 73326ba94429SFrederic Weisbecker if (ret) { 73336ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 73346ba94429SFrederic Weisbecker wq->wq_dev = NULL; 73356ba94429SFrederic Weisbecker return ret; 73366ba94429SFrederic Weisbecker } 73376ba94429SFrederic Weisbecker } 73386ba94429SFrederic Weisbecker } 73396ba94429SFrederic Weisbecker 73406ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 73416ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 73426ba94429SFrederic Weisbecker return 0; 73436ba94429SFrederic Weisbecker } 73446ba94429SFrederic Weisbecker 73456ba94429SFrederic Weisbecker /** 73466ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 73476ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 73486ba94429SFrederic Weisbecker * 73496ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 73506ba94429SFrederic Weisbecker */ 73516ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 73526ba94429SFrederic Weisbecker { 73536ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 73546ba94429SFrederic Weisbecker 73556ba94429SFrederic Weisbecker if (!wq->wq_dev) 73566ba94429SFrederic Weisbecker return; 73576ba94429SFrederic Weisbecker 73586ba94429SFrederic Weisbecker wq->wq_dev = NULL; 73596ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 73606ba94429SFrederic Weisbecker } 73616ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 73626ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 73636ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 73646ba94429SFrederic Weisbecker 736582607adcSTejun Heo /* 736682607adcSTejun Heo * Workqueue watchdog. 736782607adcSTejun Heo * 736882607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 736982607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 737082607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 737182607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 737282607adcSTejun Heo * largely opaque. 737382607adcSTejun Heo * 737482607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 737582607adcSTejun Heo * state if some pools failed to make forward progress for a while where 737682607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 737782607adcSTejun Heo * 737882607adcSTejun Heo * This mechanism is controlled through the kernel parameter 737982607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 738082607adcSTejun Heo * corresponding sysfs parameter file. 738182607adcSTejun Heo */ 738282607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 738382607adcSTejun Heo 738482607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 73855cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 738682607adcSTejun Heo 738782607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 738882607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 738982607adcSTejun Heo 7390cd2440d6SPetr Mladek /* 7391cd2440d6SPetr Mladek * Show workers that might prevent the processing of pending work items. 7392cd2440d6SPetr Mladek * The only candidates are CPU-bound workers in the running state. 7393cd2440d6SPetr Mladek * Pending work items should be handled by another idle worker 7394cd2440d6SPetr Mladek * in all other situations. 7395cd2440d6SPetr Mladek */ 7396cd2440d6SPetr Mladek static void show_cpu_pool_hog(struct worker_pool *pool) 7397cd2440d6SPetr Mladek { 7398cd2440d6SPetr Mladek struct worker *worker; 7399c26e2f2eSTejun Heo unsigned long irq_flags; 7400cd2440d6SPetr Mladek int bkt; 7401cd2440d6SPetr Mladek 7402c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags); 7403cd2440d6SPetr Mladek 7404cd2440d6SPetr Mladek hash_for_each(pool->busy_hash, bkt, worker, hentry) { 7405cd2440d6SPetr Mladek if (task_is_running(worker->task)) { 7406cd2440d6SPetr Mladek /* 7407cd2440d6SPetr Mladek * Defer printing to avoid deadlocks in console 7408cd2440d6SPetr Mladek * drivers that queue work while holding locks 7409cd2440d6SPetr Mladek * also taken in their write paths. 7410cd2440d6SPetr Mladek */ 7411cd2440d6SPetr Mladek printk_deferred_enter(); 7412cd2440d6SPetr Mladek 7413cd2440d6SPetr Mladek pr_info("pool %d:\n", pool->id); 7414cd2440d6SPetr Mladek sched_show_task(worker->task); 7415cd2440d6SPetr Mladek 7416cd2440d6SPetr Mladek printk_deferred_exit(); 7417cd2440d6SPetr Mladek } 7418cd2440d6SPetr Mladek } 7419cd2440d6SPetr Mladek 7420c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags); 7421cd2440d6SPetr Mladek } 7422cd2440d6SPetr Mladek 7423cd2440d6SPetr Mladek static void show_cpu_pools_hogs(void) 7424cd2440d6SPetr Mladek { 7425cd2440d6SPetr Mladek struct worker_pool *pool; 7426cd2440d6SPetr Mladek int pi; 7427cd2440d6SPetr Mladek 7428cd2440d6SPetr Mladek pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); 7429cd2440d6SPetr Mladek 7430cd2440d6SPetr Mladek rcu_read_lock(); 7431cd2440d6SPetr Mladek 7432cd2440d6SPetr Mladek for_each_pool(pool, pi) { 7433cd2440d6SPetr Mladek if (pool->cpu_stall) 7434cd2440d6SPetr Mladek show_cpu_pool_hog(pool); 7435cd2440d6SPetr Mladek 7436cd2440d6SPetr Mladek } 7437cd2440d6SPetr Mladek 7438cd2440d6SPetr Mladek rcu_read_unlock(); 7439cd2440d6SPetr Mladek } 7440cd2440d6SPetr Mladek 744182607adcSTejun Heo static void wq_watchdog_reset_touched(void) 744282607adcSTejun Heo { 744382607adcSTejun Heo int cpu; 744482607adcSTejun Heo 744582607adcSTejun Heo wq_watchdog_touched = jiffies; 744682607adcSTejun Heo for_each_possible_cpu(cpu) 744782607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 744882607adcSTejun Heo } 744982607adcSTejun Heo 74505cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 745182607adcSTejun Heo { 745282607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 745382607adcSTejun Heo bool lockup_detected = false; 7454cd2440d6SPetr Mladek bool cpu_pool_stall = false; 7455940d71c6SSergey Senozhatsky unsigned long now = jiffies; 745682607adcSTejun Heo struct worker_pool *pool; 745782607adcSTejun Heo int pi; 745882607adcSTejun Heo 745982607adcSTejun Heo if (!thresh) 746082607adcSTejun Heo return; 746182607adcSTejun Heo 746282607adcSTejun Heo rcu_read_lock(); 746382607adcSTejun Heo 746482607adcSTejun Heo for_each_pool(pool, pi) { 746582607adcSTejun Heo unsigned long pool_ts, touched, ts; 746682607adcSTejun Heo 7467cd2440d6SPetr Mladek pool->cpu_stall = false; 746882607adcSTejun Heo if (list_empty(&pool->worklist)) 746982607adcSTejun Heo continue; 747082607adcSTejun Heo 7471940d71c6SSergey Senozhatsky /* 7472940d71c6SSergey Senozhatsky * If a virtual machine is stopped by the host it can look to 7473940d71c6SSergey Senozhatsky * the watchdog like a stall. 7474940d71c6SSergey Senozhatsky */ 7475940d71c6SSergey Senozhatsky kvm_check_and_clear_guest_paused(); 7476940d71c6SSergey Senozhatsky 747782607adcSTejun Heo /* get the latest of pool and touched timestamps */ 747889e28ce6SWang Qing if (pool->cpu >= 0) 747989e28ce6SWang Qing touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); 748089e28ce6SWang Qing else 748182607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 748289e28ce6SWang Qing pool_ts = READ_ONCE(pool->watchdog_ts); 748382607adcSTejun Heo 748482607adcSTejun Heo if (time_after(pool_ts, touched)) 748582607adcSTejun Heo ts = pool_ts; 748682607adcSTejun Heo else 748782607adcSTejun Heo ts = touched; 748882607adcSTejun Heo 748982607adcSTejun Heo /* did we stall? */ 7490940d71c6SSergey Senozhatsky if (time_after(now, ts + thresh)) { 749182607adcSTejun Heo lockup_detected = true; 74924cb1ef64STejun Heo if (pool->cpu >= 0 && !(pool->flags & POOL_BH)) { 7493cd2440d6SPetr Mladek pool->cpu_stall = true; 7494cd2440d6SPetr Mladek cpu_pool_stall = true; 7495cd2440d6SPetr Mladek } 749682607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 749782607adcSTejun Heo pr_cont_pool_info(pool); 749882607adcSTejun Heo pr_cont(" stuck for %us!\n", 7499940d71c6SSergey Senozhatsky jiffies_to_msecs(now - pool_ts) / 1000); 750082607adcSTejun Heo } 7501cd2440d6SPetr Mladek 7502cd2440d6SPetr Mladek 750382607adcSTejun Heo } 750482607adcSTejun Heo 750582607adcSTejun Heo rcu_read_unlock(); 750682607adcSTejun Heo 750782607adcSTejun Heo if (lockup_detected) 750855df0933SImran Khan show_all_workqueues(); 750982607adcSTejun Heo 7510cd2440d6SPetr Mladek if (cpu_pool_stall) 7511cd2440d6SPetr Mladek show_cpu_pools_hogs(); 7512cd2440d6SPetr Mladek 751382607adcSTejun Heo wq_watchdog_reset_touched(); 751482607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 751582607adcSTejun Heo } 751682607adcSTejun Heo 7517cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 751882607adcSTejun Heo { 751982607adcSTejun Heo if (cpu >= 0) 752082607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 752189e28ce6SWang Qing 752282607adcSTejun Heo wq_watchdog_touched = jiffies; 752382607adcSTejun Heo } 752482607adcSTejun Heo 752582607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 752682607adcSTejun Heo { 752782607adcSTejun Heo wq_watchdog_thresh = 0; 752882607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 752982607adcSTejun Heo 753082607adcSTejun Heo if (thresh) { 753182607adcSTejun Heo wq_watchdog_thresh = thresh; 753282607adcSTejun Heo wq_watchdog_reset_touched(); 753382607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 753482607adcSTejun Heo } 753582607adcSTejun Heo } 753682607adcSTejun Heo 753782607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 753882607adcSTejun Heo const struct kernel_param *kp) 753982607adcSTejun Heo { 754082607adcSTejun Heo unsigned long thresh; 754182607adcSTejun Heo int ret; 754282607adcSTejun Heo 754382607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 754482607adcSTejun Heo if (ret) 754582607adcSTejun Heo return ret; 754682607adcSTejun Heo 754782607adcSTejun Heo if (system_wq) 754882607adcSTejun Heo wq_watchdog_set_thresh(thresh); 754982607adcSTejun Heo else 755082607adcSTejun Heo wq_watchdog_thresh = thresh; 755182607adcSTejun Heo 755282607adcSTejun Heo return 0; 755382607adcSTejun Heo } 755482607adcSTejun Heo 755582607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 755682607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 755782607adcSTejun Heo .get = param_get_ulong, 755882607adcSTejun Heo }; 755982607adcSTejun Heo 756082607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 756182607adcSTejun Heo 0644); 756282607adcSTejun Heo 756382607adcSTejun Heo static void wq_watchdog_init(void) 756482607adcSTejun Heo { 75655cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 756682607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 756782607adcSTejun Heo } 756882607adcSTejun Heo 756982607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 757082607adcSTejun Heo 757182607adcSTejun Heo static inline void wq_watchdog_init(void) { } 757282607adcSTejun Heo 757382607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 757482607adcSTejun Heo 75752f34d733STejun Heo static void bh_pool_kick_normal(struct irq_work *irq_work) 75762f34d733STejun Heo { 75772f34d733STejun Heo raise_softirq_irqoff(TASKLET_SOFTIRQ); 75782f34d733STejun Heo } 75792f34d733STejun Heo 75802f34d733STejun Heo static void bh_pool_kick_highpri(struct irq_work *irq_work) 75812f34d733STejun Heo { 75822f34d733STejun Heo raise_softirq_irqoff(HI_SOFTIRQ); 75832f34d733STejun Heo } 75842f34d733STejun Heo 75854a6c5607STejun Heo static void __init restrict_unbound_cpumask(const char *name, const struct cpumask *mask) 75864a6c5607STejun Heo { 75874a6c5607STejun Heo if (!cpumask_intersects(wq_unbound_cpumask, mask)) { 75884a6c5607STejun Heo pr_warn("workqueue: Restricting unbound_cpumask (%*pb) with %s (%*pb) leaves no CPU, ignoring\n", 75894a6c5607STejun Heo cpumask_pr_args(wq_unbound_cpumask), name, cpumask_pr_args(mask)); 75904a6c5607STejun Heo return; 75914a6c5607STejun Heo } 75924a6c5607STejun Heo 75934a6c5607STejun Heo cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, mask); 75944a6c5607STejun Heo } 75954a6c5607STejun Heo 75962fcdb1b4STejun Heo static void __init init_cpu_worker_pool(struct worker_pool *pool, int cpu, int nice) 75972fcdb1b4STejun Heo { 75982fcdb1b4STejun Heo BUG_ON(init_worker_pool(pool)); 75992fcdb1b4STejun Heo pool->cpu = cpu; 76002fcdb1b4STejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 76012fcdb1b4STejun Heo cpumask_copy(pool->attrs->__pod_cpumask, cpumask_of(cpu)); 76022fcdb1b4STejun Heo pool->attrs->nice = nice; 76032fcdb1b4STejun Heo pool->attrs->affn_strict = true; 76042fcdb1b4STejun Heo pool->node = cpu_to_node(cpu); 76052fcdb1b4STejun Heo 76062fcdb1b4STejun Heo /* alloc pool ID */ 76072fcdb1b4STejun Heo mutex_lock(&wq_pool_mutex); 76082fcdb1b4STejun Heo BUG_ON(worker_pool_assign_id(pool)); 76092fcdb1b4STejun Heo mutex_unlock(&wq_pool_mutex); 76102fcdb1b4STejun Heo } 76112fcdb1b4STejun Heo 76123347fa09STejun Heo /** 76133347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 76143347fa09STejun Heo * 76152930155bSTejun Heo * This is the first step of three-staged workqueue subsystem initialization and 76162930155bSTejun Heo * invoked as soon as the bare basics - memory allocation, cpumasks and idr are 76172930155bSTejun Heo * up. It sets up all the data structures and system workqueues and allows early 76182930155bSTejun Heo * boot code to create workqueues and queue/cancel work items. Actual work item 76192930155bSTejun Heo * execution starts only after kthreads can be created and scheduled right 76202930155bSTejun Heo * before early initcalls. 76213347fa09STejun Heo */ 76222333e829SYu Chen void __init workqueue_init_early(void) 76231da177e4SLinus Torvalds { 762484193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 76257a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 76262f34d733STejun Heo void (*irq_work_fns[2])(struct irq_work *) = { bh_pool_kick_normal, 76272f34d733STejun Heo bh_pool_kick_highpri }; 76287a4e344cSTejun Heo int i, cpu; 7629c34056a3STejun Heo 763010cdb157SLai Jiangshan BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 7631e904e6c2STejun Heo 7632b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 7633fe28f631SWaiman Long BUG_ON(!alloc_cpumask_var(&wq_requested_unbound_cpumask, GFP_KERNEL)); 7634fe28f631SWaiman Long BUG_ON(!zalloc_cpumask_var(&wq_isolated_cpumask, GFP_KERNEL)); 7635b05a7928SFrederic Weisbecker 76364a6c5607STejun Heo cpumask_copy(wq_unbound_cpumask, cpu_possible_mask); 76374a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_WQ", housekeeping_cpumask(HK_TYPE_WQ)); 76384a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_DOMAIN", housekeeping_cpumask(HK_TYPE_DOMAIN)); 7639ace3c549Stiozhang if (!cpumask_empty(&wq_cmdline_cpumask)) 76404a6c5607STejun Heo restrict_unbound_cpumask("workqueue.unbound_cpus", &wq_cmdline_cpumask); 7641ace3c549Stiozhang 7642fe28f631SWaiman Long cpumask_copy(wq_requested_unbound_cpumask, wq_unbound_cpumask); 76438b03ae3cSTejun Heo 76448b03ae3cSTejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 7645e22bee78STejun Heo 76462930155bSTejun Heo wq_update_pod_attrs_buf = alloc_workqueue_attrs(); 76472930155bSTejun Heo BUG_ON(!wq_update_pod_attrs_buf); 76482930155bSTejun Heo 76497bd20b6bSMarcelo Tosatti /* 76507bd20b6bSMarcelo Tosatti * If nohz_full is enabled, set power efficient workqueue as unbound. 76517bd20b6bSMarcelo Tosatti * This allows workqueue items to be moved to HK CPUs. 76527bd20b6bSMarcelo Tosatti */ 76537bd20b6bSMarcelo Tosatti if (housekeeping_enabled(HK_TYPE_TICK)) 76547bd20b6bSMarcelo Tosatti wq_power_efficient = true; 76557bd20b6bSMarcelo Tosatti 765684193c07STejun Heo /* initialize WQ_AFFN_SYSTEM pods */ 765784193c07STejun Heo pt->pod_cpus = kcalloc(1, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 765884193c07STejun Heo pt->pod_node = kcalloc(1, sizeof(pt->pod_node[0]), GFP_KERNEL); 765984193c07STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 766084193c07STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node || !pt->cpu_pod); 766184193c07STejun Heo 766284193c07STejun Heo BUG_ON(!zalloc_cpumask_var_node(&pt->pod_cpus[0], GFP_KERNEL, NUMA_NO_NODE)); 766384193c07STejun Heo 766484193c07STejun Heo pt->nr_pods = 1; 766584193c07STejun Heo cpumask_copy(pt->pod_cpus[0], cpu_possible_mask); 766684193c07STejun Heo pt->pod_node[0] = NUMA_NO_NODE; 766784193c07STejun Heo pt->cpu_pod[0] = 0; 766884193c07STejun Heo 76694cb1ef64STejun Heo /* initialize BH and CPU pools */ 76701da177e4SLinus Torvalds for_each_possible_cpu(cpu) { 76711da177e4SLinus Torvalds struct worker_pool *pool; 76721da177e4SLinus Torvalds 76731da177e4SLinus Torvalds i = 0; 76744cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) { 76752f34d733STejun Heo init_cpu_worker_pool(pool, cpu, std_nice[i]); 76764cb1ef64STejun Heo pool->flags |= POOL_BH; 76772f34d733STejun Heo init_irq_work(bh_pool_irq_work(pool), irq_work_fns[i]); 76782f34d733STejun Heo i++; 76794cb1ef64STejun Heo } 76804cb1ef64STejun Heo 76814cb1ef64STejun Heo i = 0; 76822fcdb1b4STejun Heo for_each_cpu_worker_pool(pool, cpu) 76832fcdb1b4STejun Heo init_cpu_worker_pool(pool, cpu, std_nice[i++]); 76841da177e4SLinus Torvalds } 76851da177e4SLinus Torvalds 76868a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 768729c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 768829c91e99STejun Heo struct workqueue_attrs *attrs; 768929c91e99STejun Heo 7690be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 769129c91e99STejun Heo attrs->nice = std_nice[i]; 769229c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 76938a2b7538STejun Heo 76948a2b7538STejun Heo /* 76958a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 76968a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 76978a2b7538STejun Heo */ 7698be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 76998a2b7538STejun Heo attrs->nice = std_nice[i]; 7700af73f5c9STejun Heo attrs->ordered = true; 77018a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 770229c91e99STejun Heo } 770329c91e99STejun Heo 7704d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 77051aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 7706d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 7707f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 7708636b927eSTejun Heo WQ_MAX_ACTIVE); 770924d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 771024d51addSTejun Heo WQ_FREEZABLE, 0); 77110668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 77120668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 77138318d6a6SAudra Mitchell system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_pwr_efficient", 77140668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 77150668106cSViresh Kumar 0); 77164cb1ef64STejun Heo system_bh_wq = alloc_workqueue("events_bh", WQ_BH, 0); 77174cb1ef64STejun Heo system_bh_highpri_wq = alloc_workqueue("events_bh_highpri", 77184cb1ef64STejun Heo WQ_BH | WQ_HIGHPRI, 0); 77191aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 77200668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 77210668106cSViresh Kumar !system_power_efficient_wq || 77224cb1ef64STejun Heo !system_freezable_power_efficient_wq || 77234cb1ef64STejun Heo !system_bh_wq || !system_bh_highpri_wq); 77243347fa09STejun Heo } 77253347fa09STejun Heo 7726aa6fde93STejun Heo static void __init wq_cpu_intensive_thresh_init(void) 7727aa6fde93STejun Heo { 7728aa6fde93STejun Heo unsigned long thresh; 7729aa6fde93STejun Heo unsigned long bogo; 7730aa6fde93STejun Heo 7731dd64c873SZqiang pwq_release_worker = kthread_create_worker(0, "pool_workqueue_release"); 7732dd64c873SZqiang BUG_ON(IS_ERR(pwq_release_worker)); 7733dd64c873SZqiang 7734aa6fde93STejun Heo /* if the user set it to a specific value, keep it */ 7735aa6fde93STejun Heo if (wq_cpu_intensive_thresh_us != ULONG_MAX) 7736aa6fde93STejun Heo return; 7737aa6fde93STejun Heo 7738aa6fde93STejun Heo /* 7739aa6fde93STejun Heo * The default of 10ms is derived from the fact that most modern (as of 7740aa6fde93STejun Heo * 2023) processors can do a lot in 10ms and that it's just below what 7741aa6fde93STejun Heo * most consider human-perceivable. However, the kernel also runs on a 7742aa6fde93STejun Heo * lot slower CPUs including microcontrollers where the threshold is way 7743aa6fde93STejun Heo * too low. 7744aa6fde93STejun Heo * 7745aa6fde93STejun Heo * Let's scale up the threshold upto 1 second if BogoMips is below 4000. 7746aa6fde93STejun Heo * This is by no means accurate but it doesn't have to be. The mechanism 7747aa6fde93STejun Heo * is still useful even when the threshold is fully scaled up. Also, as 7748aa6fde93STejun Heo * the reports would usually be applicable to everyone, some machines 7749aa6fde93STejun Heo * operating on longer thresholds won't significantly diminish their 7750aa6fde93STejun Heo * usefulness. 7751aa6fde93STejun Heo */ 7752aa6fde93STejun Heo thresh = 10 * USEC_PER_MSEC; 7753aa6fde93STejun Heo 7754aa6fde93STejun Heo /* see init/calibrate.c for lpj -> BogoMIPS calculation */ 7755aa6fde93STejun Heo bogo = max_t(unsigned long, loops_per_jiffy / 500000 * HZ, 1); 7756aa6fde93STejun Heo if (bogo < 4000) 7757aa6fde93STejun Heo thresh = min_t(unsigned long, thresh * 4000 / bogo, USEC_PER_SEC); 7758aa6fde93STejun Heo 7759aa6fde93STejun Heo pr_debug("wq_cpu_intensive_thresh: lpj=%lu BogoMIPS=%lu thresh_us=%lu\n", 7760aa6fde93STejun Heo loops_per_jiffy, bogo, thresh); 7761aa6fde93STejun Heo 7762aa6fde93STejun Heo wq_cpu_intensive_thresh_us = thresh; 7763aa6fde93STejun Heo } 7764aa6fde93STejun Heo 77653347fa09STejun Heo /** 77663347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 77673347fa09STejun Heo * 77682930155bSTejun Heo * This is the second step of three-staged workqueue subsystem initialization 77692930155bSTejun Heo * and invoked as soon as kthreads can be created and scheduled. Workqueues have 77702930155bSTejun Heo * been created and work items queued on them, but there are no kworkers 77712930155bSTejun Heo * executing the work items yet. Populate the worker pools with the initial 77722930155bSTejun Heo * workers and enable future kworker creations. 77733347fa09STejun Heo */ 77742333e829SYu Chen void __init workqueue_init(void) 77753347fa09STejun Heo { 77762186d9f9STejun Heo struct workqueue_struct *wq; 77773347fa09STejun Heo struct worker_pool *pool; 77783347fa09STejun Heo int cpu, bkt; 77793347fa09STejun Heo 7780aa6fde93STejun Heo wq_cpu_intensive_thresh_init(); 7781aa6fde93STejun Heo 77822186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 77832186d9f9STejun Heo 77842930155bSTejun Heo /* 77852930155bSTejun Heo * Per-cpu pools created earlier could be missing node hint. Fix them 77862930155bSTejun Heo * up. Also, create a rescuer for workqueues that requested it. 77872930155bSTejun Heo */ 77882186d9f9STejun Heo for_each_possible_cpu(cpu) { 77894cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) 77902186d9f9STejun Heo pool->node = cpu_to_node(cpu); 77914cb1ef64STejun Heo for_each_cpu_worker_pool(pool, cpu) 77924cb1ef64STejun Heo pool->node = cpu_to_node(cpu); 77932186d9f9STejun Heo } 77942186d9f9STejun Heo 779540c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 779640c17f75STejun Heo WARN(init_rescuer(wq), 779740c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 779840c17f75STejun Heo wq->name); 779940c17f75STejun Heo } 78002186d9f9STejun Heo 78012186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 78022186d9f9STejun Heo 78034cb1ef64STejun Heo /* 78044cb1ef64STejun Heo * Create the initial workers. A BH pool has one pseudo worker that 78054cb1ef64STejun Heo * represents the shared BH execution context and thus doesn't get 78064cb1ef64STejun Heo * affected by hotplug events. Create the BH pseudo workers for all 78074cb1ef64STejun Heo * possible CPUs here. 78084cb1ef64STejun Heo */ 78094cb1ef64STejun Heo for_each_possible_cpu(cpu) 78104cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) 78114cb1ef64STejun Heo BUG_ON(!create_worker(pool)); 78124cb1ef64STejun Heo 78133347fa09STejun Heo for_each_online_cpu(cpu) { 78143347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 78153347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 78163347fa09STejun Heo BUG_ON(!create_worker(pool)); 78173347fa09STejun Heo } 78183347fa09STejun Heo } 78193347fa09STejun Heo 78203347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 78213347fa09STejun Heo BUG_ON(!create_worker(pool)); 78223347fa09STejun Heo 78233347fa09STejun Heo wq_online = true; 782482607adcSTejun Heo wq_watchdog_init(); 78251da177e4SLinus Torvalds } 7826c4f135d6STetsuo Handa 7827025e1684STejun Heo /* 7828025e1684STejun Heo * Initialize @pt by first initializing @pt->cpu_pod[] with pod IDs according to 7829025e1684STejun Heo * @cpu_shares_pod(). Each subset of CPUs that share a pod is assigned a unique 7830025e1684STejun Heo * and consecutive pod ID. The rest of @pt is initialized accordingly. 7831025e1684STejun Heo */ 7832025e1684STejun Heo static void __init init_pod_type(struct wq_pod_type *pt, 7833025e1684STejun Heo bool (*cpus_share_pod)(int, int)) 7834025e1684STejun Heo { 7835025e1684STejun Heo int cur, pre, cpu, pod; 7836025e1684STejun Heo 7837025e1684STejun Heo pt->nr_pods = 0; 7838025e1684STejun Heo 7839025e1684STejun Heo /* init @pt->cpu_pod[] according to @cpus_share_pod() */ 7840025e1684STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 7841025e1684STejun Heo BUG_ON(!pt->cpu_pod); 7842025e1684STejun Heo 7843025e1684STejun Heo for_each_possible_cpu(cur) { 7844025e1684STejun Heo for_each_possible_cpu(pre) { 7845025e1684STejun Heo if (pre >= cur) { 7846025e1684STejun Heo pt->cpu_pod[cur] = pt->nr_pods++; 7847025e1684STejun Heo break; 7848025e1684STejun Heo } 7849025e1684STejun Heo if (cpus_share_pod(cur, pre)) { 7850025e1684STejun Heo pt->cpu_pod[cur] = pt->cpu_pod[pre]; 7851025e1684STejun Heo break; 7852025e1684STejun Heo } 7853025e1684STejun Heo } 7854025e1684STejun Heo } 7855025e1684STejun Heo 7856025e1684STejun Heo /* init the rest to match @pt->cpu_pod[] */ 7857025e1684STejun Heo pt->pod_cpus = kcalloc(pt->nr_pods, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 7858025e1684STejun Heo pt->pod_node = kcalloc(pt->nr_pods, sizeof(pt->pod_node[0]), GFP_KERNEL); 7859025e1684STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node); 7860025e1684STejun Heo 7861025e1684STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) 7862025e1684STejun Heo BUG_ON(!zalloc_cpumask_var(&pt->pod_cpus[pod], GFP_KERNEL)); 7863025e1684STejun Heo 7864025e1684STejun Heo for_each_possible_cpu(cpu) { 7865025e1684STejun Heo cpumask_set_cpu(cpu, pt->pod_cpus[pt->cpu_pod[cpu]]); 7866025e1684STejun Heo pt->pod_node[pt->cpu_pod[cpu]] = cpu_to_node(cpu); 7867025e1684STejun Heo } 7868025e1684STejun Heo } 7869025e1684STejun Heo 787063c5484eSTejun Heo static bool __init cpus_dont_share(int cpu0, int cpu1) 787163c5484eSTejun Heo { 787263c5484eSTejun Heo return false; 787363c5484eSTejun Heo } 787463c5484eSTejun Heo 787563c5484eSTejun Heo static bool __init cpus_share_smt(int cpu0, int cpu1) 787663c5484eSTejun Heo { 787763c5484eSTejun Heo #ifdef CONFIG_SCHED_SMT 787863c5484eSTejun Heo return cpumask_test_cpu(cpu0, cpu_smt_mask(cpu1)); 787963c5484eSTejun Heo #else 788063c5484eSTejun Heo return false; 788163c5484eSTejun Heo #endif 788263c5484eSTejun Heo } 788363c5484eSTejun Heo 7884025e1684STejun Heo static bool __init cpus_share_numa(int cpu0, int cpu1) 7885025e1684STejun Heo { 7886025e1684STejun Heo return cpu_to_node(cpu0) == cpu_to_node(cpu1); 7887025e1684STejun Heo } 7888025e1684STejun Heo 78892930155bSTejun Heo /** 78902930155bSTejun Heo * workqueue_init_topology - initialize CPU pods for unbound workqueues 78912930155bSTejun Heo * 789296068b60SWang Jinchao * This is the third step of three-staged workqueue subsystem initialization and 78932930155bSTejun Heo * invoked after SMP and topology information are fully initialized. It 78942930155bSTejun Heo * initializes the unbound CPU pods accordingly. 78952930155bSTejun Heo */ 78962930155bSTejun Heo void __init workqueue_init_topology(void) 7897a86feae6STejun Heo { 78982930155bSTejun Heo struct workqueue_struct *wq; 7899025e1684STejun Heo int cpu; 7900a86feae6STejun Heo 790163c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CPU], cpus_dont_share); 790263c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_SMT], cpus_share_smt); 790363c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CACHE], cpus_share_cache); 7904025e1684STejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_NUMA], cpus_share_numa); 7905a86feae6STejun Heo 7906c5f8cd6cSTejun Heo wq_topo_initialized = true; 7907c5f8cd6cSTejun Heo 79082930155bSTejun Heo mutex_lock(&wq_pool_mutex); 7909a86feae6STejun Heo 7910a86feae6STejun Heo /* 79112930155bSTejun Heo * Workqueues allocated earlier would have all CPUs sharing the default 79122930155bSTejun Heo * worker pool. Explicitly call wq_update_pod() on all workqueue and CPU 79132930155bSTejun Heo * combinations to apply per-pod sharing. 79142930155bSTejun Heo */ 79152930155bSTejun Heo list_for_each_entry(wq, &workqueues, list) { 79165797b1c1STejun Heo for_each_online_cpu(cpu) 79172930155bSTejun Heo wq_update_pod(wq, cpu, cpu, true); 79185797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) { 79195797b1c1STejun Heo mutex_lock(&wq->mutex); 79205797b1c1STejun Heo wq_update_node_max_active(wq, -1); 79215797b1c1STejun Heo mutex_unlock(&wq->mutex); 79222930155bSTejun Heo } 79232930155bSTejun Heo } 79242930155bSTejun Heo 79252930155bSTejun Heo mutex_unlock(&wq_pool_mutex); 7926a86feae6STejun Heo } 7927a86feae6STejun Heo 792820bdedafSTetsuo Handa void __warn_flushing_systemwide_wq(void) 792920bdedafSTetsuo Handa { 793020bdedafSTetsuo Handa pr_warn("WARNING: Flushing system-wide workqueues will be prohibited in near future.\n"); 793120bdedafSTetsuo Handa dump_stack(); 793220bdedafSTetsuo Handa } 7933c4f135d6STetsuo Handa EXPORT_SYMBOL(__warn_flushing_systemwide_wq); 7934ace3c549Stiozhang 7935ace3c549Stiozhang static int __init workqueue_unbound_cpus_setup(char *str) 7936ace3c549Stiozhang { 7937ace3c549Stiozhang if (cpulist_parse(str, &wq_cmdline_cpumask) < 0) { 7938ace3c549Stiozhang cpumask_clear(&wq_cmdline_cpumask); 7939ace3c549Stiozhang pr_warn("workqueue.unbound_cpus: incorrect CPU range, using default\n"); 7940ace3c549Stiozhang } 7941ace3c549Stiozhang 7942ace3c549Stiozhang return 1; 7943ace3c549Stiozhang } 7944ace3c549Stiozhang __setup("workqueue.unbound_cpus=", workqueue_unbound_cpus_setup); 7945