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> 57e22bee78STejun Heo 58ea138446STejun Heo #include "workqueue_internal.h" 591da177e4SLinus Torvalds 60e563d0a7STejun Heo enum worker_pool_flags { 61bc2ae0f5STejun Heo /* 6224647570STejun Heo * worker_pool flags 63bc2ae0f5STejun Heo * 6424647570STejun Heo * A bound pool is either associated or disassociated with its CPU. 65bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 66bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 67bc2ae0f5STejun Heo * is in effect. 68bc2ae0f5STejun Heo * 69bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 70bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 7124647570STejun Heo * be executing on any CPU. The pool behaves as an unbound one. 72bc2ae0f5STejun Heo * 73bc3a1afcSTejun Heo * Note that DISASSOCIATED should be flipped only while holding 741258fae7STejun Heo * wq_pool_attach_mutex to avoid changing binding state while 754736cbf7SLai Jiangshan * worker_attach_to_pool() is in progress. 764cb1ef64STejun Heo * 774cb1ef64STejun Heo * As there can only be one concurrent BH execution context per CPU, a 784cb1ef64STejun Heo * BH pool is per-CPU and always DISASSOCIATED. 79bc2ae0f5STejun Heo */ 804cb1ef64STejun Heo POOL_BH = 1 << 0, /* is a BH pool */ 814cb1ef64STejun Heo POOL_MANAGER_ACTIVE = 1 << 1, /* being managed */ 8224647570STejun Heo POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ 83e563d0a7STejun Heo }; 84db7bccf4STejun Heo 85e563d0a7STejun Heo enum worker_flags { 86c8e55f36STejun Heo /* worker flags */ 87c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 88c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 89e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 90fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 91f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 92a9ab775bSTejun Heo WORKER_REBOUND = 1 << 8, /* worker was rebound */ 93e22bee78STejun Heo 94a9ab775bSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE | 95a9ab775bSTejun Heo WORKER_UNBOUND | WORKER_REBOUND, 96e563d0a7STejun Heo }; 97db7bccf4STejun Heo 98e563d0a7STejun Heo enum wq_internal_consts { 99e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 1004ce62e9eSTejun Heo 10129c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ 102c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 103db7bccf4STejun Heo 104e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 105e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 106e22bee78STejun Heo 1073233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 1083233cdbdSTejun Heo /* call for help after 10ms 1093233cdbdSTejun Heo (min two ticks) */ 110e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 111e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds /* 114e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 1158698a745SDongsheng Yang * all cpus. Give MIN_NICE. 116e22bee78STejun Heo */ 1178698a745SDongsheng Yang RESCUER_NICE_LEVEL = MIN_NICE, 1188698a745SDongsheng Yang HIGHPRI_NICE_LEVEL = MIN_NICE, 119ecf6881fSTejun Heo 12031c89007SAudra Mitchell WQ_NAME_LEN = 32, 121c8e55f36STejun Heo }; 122c8e55f36STejun Heo 1231da177e4SLinus Torvalds /* 1244cb1ef64STejun Heo * We don't want to trap softirq for too long. See MAX_SOFTIRQ_TIME and 1254cb1ef64STejun Heo * MAX_SOFTIRQ_RESTART in kernel/softirq.c. These are macros because 1264cb1ef64STejun Heo * msecs_to_jiffies() can't be an initializer. 1274cb1ef64STejun Heo */ 1284cb1ef64STejun Heo #define BH_WORKER_JIFFIES msecs_to_jiffies(2) 1294cb1ef64STejun Heo #define BH_WORKER_RESTARTS 10 1304cb1ef64STejun Heo 1314cb1ef64STejun Heo /* 1324690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1334690c4abSTejun Heo * 134e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 135e41e704bSTejun Heo * everyone else. 1364690c4abSTejun Heo * 137e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 138e22bee78STejun Heo * only be modified and accessed from the local cpu. 139e22bee78STejun Heo * 140d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1414690c4abSTejun Heo * 1425797b1c1STejun Heo * LN: pool->lock and wq_node_nr_active->lock protected for writes. Either for 1435797b1c1STejun Heo * reads. 1445797b1c1STejun Heo * 145bdf8b9bfSTejun Heo * K: Only modified by worker while holding pool->lock. Can be safely read by 146bdf8b9bfSTejun Heo * self, while holding pool->lock or from IRQ context if %current is the 147bdf8b9bfSTejun Heo * kworker. 148bdf8b9bfSTejun Heo * 149bdf8b9bfSTejun Heo * S: Only modified by worker self. 150bdf8b9bfSTejun Heo * 1511258fae7STejun Heo * A: wq_pool_attach_mutex protected. 152822d8405STejun Heo * 15368e13a67SLai Jiangshan * PL: wq_pool_mutex protected. 15476af4d93STejun Heo * 15524acfb71SThomas Gleixner * PR: wq_pool_mutex protected for writes. RCU protected for reads. 1565bcab335STejun Heo * 1575b95e1afSLai Jiangshan * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads. 1585b95e1afSLai Jiangshan * 1595b95e1afSLai Jiangshan * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or 16024acfb71SThomas Gleixner * RCU for reads. 1615b95e1afSLai Jiangshan * 1623c25a55dSLai Jiangshan * WQ: wq->mutex protected. 1633c25a55dSLai Jiangshan * 16424acfb71SThomas Gleixner * WR: wq->mutex protected for writes. RCU protected for reads. 1652e109a28STejun Heo * 166a045a272STejun Heo * WO: wq->mutex protected for writes. Updated with WRITE_ONCE() and can be read 167a045a272STejun Heo * with READ_ONCE() without locking. 168a045a272STejun Heo * 1692e109a28STejun Heo * MD: wq_mayday_lock protected. 170cd2440d6SPetr Mladek * 171cd2440d6SPetr Mladek * WD: Used internally by the watchdog. 1724690c4abSTejun Heo */ 1734690c4abSTejun Heo 1742eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 175c34056a3STejun Heo 176bd7bdd43STejun Heo struct worker_pool { 177a9b8a985SSebastian Andrzej Siewior raw_spinlock_t lock; /* the pool lock */ 178d84ff051STejun Heo int cpu; /* I: the associated cpu */ 179f3f90ad4STejun Heo int node; /* I: the associated node ID */ 1809daf9e67STejun Heo int id; /* I: pool ID */ 181bc8b50c2STejun Heo unsigned int flags; /* L: flags */ 182bd7bdd43STejun Heo 18382607adcSTejun Heo unsigned long watchdog_ts; /* L: watchdog timestamp */ 184cd2440d6SPetr Mladek bool cpu_stall; /* WD: stalled cpu bound pool */ 18582607adcSTejun Heo 186bc35f7efSLai Jiangshan /* 187bc35f7efSLai Jiangshan * The counter is incremented in a process context on the associated CPU 188bc35f7efSLai Jiangshan * w/ preemption disabled, and decremented or reset in the same context 189bc35f7efSLai Jiangshan * but w/ pool->lock held. The readers grab pool->lock and are 190bc35f7efSLai Jiangshan * guaranteed to see if the counter reached zero. 191bc35f7efSLai Jiangshan */ 192bc35f7efSLai Jiangshan int nr_running; 19384f91c62SLai Jiangshan 194bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 195ea1abd61SLai Jiangshan 1965826cc8fSLai Jiangshan int nr_workers; /* L: total number of workers */ 1975826cc8fSLai Jiangshan int nr_idle; /* L: currently idle workers */ 198bd7bdd43STejun Heo 1992c1f1a91SLai Jiangshan struct list_head idle_list; /* L: list of idle workers */ 200bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 2013f959aa3SValentin Schneider struct work_struct idle_cull_work; /* L: worker idle cleanup */ 2023f959aa3SValentin Schneider 203bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 204bd7bdd43STejun Heo 205c5aa87bbSTejun Heo /* a workers is either on busy_hash or idle_list, or the manager */ 206c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 207c9e7cf27STejun Heo /* L: hash of busy workers */ 208c9e7cf27STejun Heo 2092607d7a6STejun Heo struct worker *manager; /* L: purely informational */ 21092f9c5c4SLai Jiangshan struct list_head workers; /* A: attached workers */ 211e02b9312SValentin Schneider struct list_head dying_workers; /* A: workers about to die */ 21260f5a4bcSLai Jiangshan struct completion *detach_completion; /* all workers detached */ 213e19e397aSTejun Heo 2147cda9aaeSLai Jiangshan struct ida worker_ida; /* worker IDs for task name */ 215e19e397aSTejun Heo 2167a4e344cSTejun Heo struct workqueue_attrs *attrs; /* I: worker attributes */ 21768e13a67SLai Jiangshan struct hlist_node hash_node; /* PL: unbound_pool_hash node */ 21868e13a67SLai Jiangshan int refcnt; /* PL: refcnt for unbound pools */ 2197a4e344cSTejun Heo 220e19e397aSTejun Heo /* 22124acfb71SThomas Gleixner * Destruction of pool is RCU protected to allow dereferences 22229c91e99STejun Heo * from get_work_pool(). 22329c91e99STejun Heo */ 22429c91e99STejun Heo struct rcu_head rcu; 22584f91c62SLai Jiangshan }; 2268b03ae3cSTejun Heo 2278b03ae3cSTejun Heo /* 228725e8ec5STejun Heo * Per-pool_workqueue statistics. These can be monitored using 229725e8ec5STejun Heo * tools/workqueue/wq_monitor.py. 230725e8ec5STejun Heo */ 231725e8ec5STejun Heo enum pool_workqueue_stats { 232725e8ec5STejun Heo PWQ_STAT_STARTED, /* work items started execution */ 233725e8ec5STejun Heo PWQ_STAT_COMPLETED, /* work items completed execution */ 2348a1dd1e5STejun Heo PWQ_STAT_CPU_TIME, /* total CPU time consumed */ 235616db877STejun Heo PWQ_STAT_CPU_INTENSIVE, /* wq_cpu_intensive_thresh_us violations */ 236725e8ec5STejun Heo PWQ_STAT_CM_WAKEUP, /* concurrency-management worker wakeups */ 2378639ecebSTejun Heo PWQ_STAT_REPATRIATED, /* unbound workers brought back into scope */ 238725e8ec5STejun Heo PWQ_STAT_MAYDAY, /* maydays to rescuer */ 239725e8ec5STejun Heo PWQ_STAT_RESCUED, /* linked work items executed by rescuer */ 240725e8ec5STejun Heo 241725e8ec5STejun Heo PWQ_NR_STATS, 242725e8ec5STejun Heo }; 243725e8ec5STejun Heo 244725e8ec5STejun Heo /* 245112202d9STejun Heo * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS 246112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits 247112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the 248112202d9STejun Heo * number of flag bits. 2491da177e4SLinus Torvalds */ 250112202d9STejun Heo struct pool_workqueue { 251bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 2524690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 25373f53c4aSTejun Heo int work_color; /* L: current color */ 25473f53c4aSTejun Heo int flush_color; /* L: flushing color */ 2558864b4e5STejun Heo int refcnt; /* L: reference count */ 25673f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 25773f53c4aSTejun Heo /* L: nr of in_flight works */ 2584c065dbcSWaiman Long bool plugged; /* L: execution suspended */ 259018f3a13SLai Jiangshan 260018f3a13SLai Jiangshan /* 261018f3a13SLai Jiangshan * nr_active management and WORK_STRUCT_INACTIVE: 262018f3a13SLai Jiangshan * 263018f3a13SLai Jiangshan * When pwq->nr_active >= max_active, new work item is queued to 264018f3a13SLai Jiangshan * pwq->inactive_works instead of pool->worklist and marked with 265018f3a13SLai Jiangshan * WORK_STRUCT_INACTIVE. 266018f3a13SLai Jiangshan * 2675797b1c1STejun Heo * All work items marked with WORK_STRUCT_INACTIVE do not participate in 2685797b1c1STejun Heo * nr_active and all work items in pwq->inactive_works are marked with 2695797b1c1STejun Heo * WORK_STRUCT_INACTIVE. But not all WORK_STRUCT_INACTIVE work items are 2705797b1c1STejun Heo * in pwq->inactive_works. Some of them are ready to run in 2715797b1c1STejun Heo * pool->worklist or worker->scheduled. Those work itmes are only struct 2725797b1c1STejun Heo * wq_barrier which is used for flush_work() and should not participate 2735797b1c1STejun Heo * in nr_active. For non-barrier work item, it is marked with 2745797b1c1STejun Heo * WORK_STRUCT_INACTIVE iff it is in pwq->inactive_works. 275018f3a13SLai Jiangshan */ 2761e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 277f97a4a1aSLai Jiangshan struct list_head inactive_works; /* L: inactive works */ 2785797b1c1STejun Heo struct list_head pending_node; /* LN: node on wq_node_nr_active->pending_pwqs */ 2793c25a55dSLai Jiangshan struct list_head pwqs_node; /* WR: node on wq->pwqs */ 2802e109a28STejun Heo struct list_head mayday_node; /* MD: node on wq->maydays */ 2818864b4e5STejun Heo 282725e8ec5STejun Heo u64 stats[PWQ_NR_STATS]; 283725e8ec5STejun Heo 2848864b4e5STejun Heo /* 285967b494eSTejun Heo * Release of unbound pwq is punted to a kthread_worker. See put_pwq() 286687a9aa5STejun Heo * and pwq_release_workfn() for details. pool_workqueue itself is also 287687a9aa5STejun Heo * RCU protected so that the first pwq can be determined without 288967b494eSTejun Heo * grabbing wq->mutex. 2898864b4e5STejun Heo */ 290687a9aa5STejun Heo struct kthread_work release_work; 2918864b4e5STejun Heo struct rcu_head rcu; 292e904e6c2STejun Heo } __aligned(1 << WORK_STRUCT_FLAG_BITS); 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds /* 29573f53c4aSTejun Heo * Structure used to wait for workqueue flush. 29673f53c4aSTejun Heo */ 29773f53c4aSTejun Heo struct wq_flusher { 2983c25a55dSLai Jiangshan struct list_head list; /* WQ: list of flushers */ 2993c25a55dSLai Jiangshan int flush_color; /* WQ: flush color waiting for */ 30073f53c4aSTejun Heo struct completion done; /* flush completion */ 30173f53c4aSTejun Heo }; 3021da177e4SLinus Torvalds 303226223abSTejun Heo struct wq_device; 304226223abSTejun Heo 30573f53c4aSTejun Heo /* 30691ccc6e7STejun Heo * Unlike in a per-cpu workqueue where max_active limits its concurrency level 30791ccc6e7STejun Heo * on each CPU, in an unbound workqueue, max_active applies to the whole system. 30891ccc6e7STejun Heo * As sharing a single nr_active across multiple sockets can be very expensive, 30991ccc6e7STejun Heo * the counting and enforcement is per NUMA node. 3105797b1c1STejun Heo * 3115797b1c1STejun Heo * The following struct is used to enforce per-node max_active. When a pwq wants 3125797b1c1STejun Heo * to start executing a work item, it should increment ->nr using 3135797b1c1STejun Heo * tryinc_node_nr_active(). If acquisition fails due to ->nr already being over 3145797b1c1STejun Heo * ->max, the pwq is queued on ->pending_pwqs. As in-flight work items finish 3155797b1c1STejun Heo * and decrement ->nr, node_activate_pending_pwq() activates the pending pwqs in 3165797b1c1STejun Heo * round-robin order. 31791ccc6e7STejun Heo */ 31891ccc6e7STejun Heo struct wq_node_nr_active { 3195797b1c1STejun Heo int max; /* per-node max_active */ 3205797b1c1STejun Heo atomic_t nr; /* per-node nr_active */ 3215797b1c1STejun Heo raw_spinlock_t lock; /* nests inside pool locks */ 3225797b1c1STejun Heo struct list_head pending_pwqs; /* LN: pwqs with inactive works */ 32391ccc6e7STejun Heo }; 32491ccc6e7STejun Heo 32591ccc6e7STejun Heo /* 326c5aa87bbSTejun Heo * The externally visible workqueue. It relays the issued work items to 327c5aa87bbSTejun Heo * the appropriate worker_pool through its pool_workqueues. 3281da177e4SLinus Torvalds */ 3291da177e4SLinus Torvalds struct workqueue_struct { 3303c25a55dSLai Jiangshan struct list_head pwqs; /* WR: all pwqs of this wq */ 331e2dca7adSTejun Heo struct list_head list; /* PR: list of all workqueues */ 33273f53c4aSTejun Heo 3333c25a55dSLai Jiangshan struct mutex mutex; /* protects this wq */ 3343c25a55dSLai Jiangshan int work_color; /* WQ: current work color */ 3353c25a55dSLai Jiangshan int flush_color; /* WQ: current flush color */ 336112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 3373c25a55dSLai Jiangshan struct wq_flusher *first_flusher; /* WQ: first flusher */ 3383c25a55dSLai Jiangshan struct list_head flusher_queue; /* WQ: flush waiters */ 3393c25a55dSLai Jiangshan struct list_head flusher_overflow; /* WQ: flush overflow list */ 34073f53c4aSTejun Heo 3412e109a28STejun Heo struct list_head maydays; /* MD: pwqs requesting rescue */ 34230ae2fc0STejun Heo struct worker *rescuer; /* MD: rescue worker */ 343e22bee78STejun Heo 34487fc741eSLai Jiangshan int nr_drainers; /* WQ: drain in progress */ 3455797b1c1STejun Heo 3465797b1c1STejun Heo /* See alloc_workqueue() function comment for info on min/max_active */ 347a045a272STejun Heo int max_active; /* WO: max active works */ 3485797b1c1STejun Heo int min_active; /* WO: min active works */ 349a045a272STejun Heo int saved_max_active; /* WQ: saved max_active */ 3505797b1c1STejun Heo int saved_min_active; /* WQ: saved min_active */ 351226223abSTejun Heo 3525b95e1afSLai Jiangshan struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */ 3539f66cff2STejun Heo struct pool_workqueue __rcu *dfl_pwq; /* PW: only for unbound wqs */ 3546029a918STejun Heo 355226223abSTejun Heo #ifdef CONFIG_SYSFS 356226223abSTejun Heo struct wq_device *wq_dev; /* I: for sysfs interface */ 357226223abSTejun Heo #endif 3584e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 359669de8bdSBart Van Assche char *lock_name; 360669de8bdSBart Van Assche struct lock_class_key key; 3614e6045f1SJohannes Berg struct lockdep_map lockdep_map; 3624e6045f1SJohannes Berg #endif 363ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */ 3642728fd2fSTejun Heo 365e2dca7adSTejun Heo /* 36624acfb71SThomas Gleixner * Destruction of workqueue_struct is RCU protected to allow walking 36724acfb71SThomas Gleixner * the workqueues list without grabbing wq_pool_mutex. 368e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq. 369e2dca7adSTejun Heo */ 370e2dca7adSTejun Heo struct rcu_head rcu; 371e2dca7adSTejun Heo 3722728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */ 3732728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ 374636b927eSTejun Heo struct pool_workqueue __percpu __rcu **cpu_pwq; /* I: per-cpu pwqs */ 37591ccc6e7STejun Heo struct wq_node_nr_active *node_nr_active[]; /* I: per-node nr_active */ 3761da177e4SLinus Torvalds }; 3771da177e4SLinus Torvalds 378e904e6c2STejun Heo static struct kmem_cache *pwq_cache; 379e904e6c2STejun Heo 38084193c07STejun Heo /* 38184193c07STejun Heo * Each pod type describes how CPUs should be grouped for unbound workqueues. 38284193c07STejun Heo * See the comment above workqueue_attrs->affn_scope. 38384193c07STejun Heo */ 38484193c07STejun Heo struct wq_pod_type { 38584193c07STejun Heo int nr_pods; /* number of pods */ 38684193c07STejun Heo cpumask_var_t *pod_cpus; /* pod -> cpus */ 38784193c07STejun Heo int *pod_node; /* pod -> node */ 38884193c07STejun Heo int *cpu_pod; /* cpu -> pod */ 38984193c07STejun Heo }; 39084193c07STejun Heo 39184193c07STejun Heo static struct wq_pod_type wq_pod_types[WQ_AFFN_NR_TYPES]; 392523a301eSTejun Heo static enum wq_affn_scope wq_affn_dfl = WQ_AFFN_CACHE; 39363c5484eSTejun Heo 39463c5484eSTejun Heo static const char *wq_affn_names[WQ_AFFN_NR_TYPES] = { 395523a301eSTejun Heo [WQ_AFFN_DFL] = "default", 39663c5484eSTejun Heo [WQ_AFFN_CPU] = "cpu", 39763c5484eSTejun Heo [WQ_AFFN_SMT] = "smt", 39863c5484eSTejun Heo [WQ_AFFN_CACHE] = "cache", 39963c5484eSTejun Heo [WQ_AFFN_NUMA] = "numa", 40063c5484eSTejun Heo [WQ_AFFN_SYSTEM] = "system", 40163c5484eSTejun Heo }; 402bce90380STejun Heo 403c5f8cd6cSTejun Heo static bool wq_topo_initialized __read_mostly = false; 404c5f8cd6cSTejun Heo 405616db877STejun Heo /* 406616db877STejun Heo * Per-cpu work items which run for longer than the following threshold are 407616db877STejun Heo * automatically considered CPU intensive and excluded from concurrency 408616db877STejun Heo * management to prevent them from noticeably delaying other per-cpu work items. 409aa6fde93STejun Heo * ULONG_MAX indicates that the user hasn't overridden it with a boot parameter. 410aa6fde93STejun Heo * The actual value is initialized in wq_cpu_intensive_thresh_init(). 411616db877STejun Heo */ 412aa6fde93STejun Heo static unsigned long wq_cpu_intensive_thresh_us = ULONG_MAX; 413616db877STejun Heo module_param_named(cpu_intensive_thresh_us, wq_cpu_intensive_thresh_us, ulong, 0644); 414616db877STejun Heo 415cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 416552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 417cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 418cee22a15SViresh Kumar 419863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */ 4203347fa09STejun Heo 421fef59c9cSTejun Heo /* buf for wq_update_unbound_pod_attrs(), protected by CPU hotplug exclusion */ 422fef59c9cSTejun Heo static struct workqueue_attrs *wq_update_pod_attrs_buf; 4234c16bd32STejun Heo 42468e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 4251258fae7STejun Heo static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ 426a9b8a985SSebastian Andrzej Siewior static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 427d8bb65abSSebastian Andrzej Siewior /* wait for manager to go away */ 428d8bb65abSSebastian Andrzej Siewior static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait); 4295bcab335STejun Heo 430e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 43168e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 4327d19c5ceSTejun Heo 43399c621efSLai Jiangshan /* PL&A: allowable cpus for unbound wqs and work items */ 434ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 435ef557180SMike Galbraith 436fe28f631SWaiman Long /* PL: user requested unbound cpumask via sysfs */ 437fe28f631SWaiman Long static cpumask_var_t wq_requested_unbound_cpumask; 438fe28f631SWaiman Long 439fe28f631SWaiman Long /* PL: isolated cpumask to be excluded from unbound cpumask */ 440fe28f631SWaiman Long static cpumask_var_t wq_isolated_cpumask; 441fe28f631SWaiman Long 442ace3c549Stiozhang /* for further constrain wq_unbound_cpumask by cmdline parameter*/ 443ace3c549Stiozhang static struct cpumask wq_cmdline_cpumask __initdata; 444ace3c549Stiozhang 445ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 446ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 447b05a7928SFrederic Weisbecker 448f303fccbSTejun Heo /* 449f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 450f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 451f303fccbSTejun Heo * to uncover usages which depend on it. 452f303fccbSTejun Heo */ 453f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 454f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 455f303fccbSTejun Heo #else 456f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 457f303fccbSTejun Heo #endif 458f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 459f303fccbSTejun Heo 4604cb1ef64STejun Heo /* the BH worker pools */ 4614cb1ef64STejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], 4624cb1ef64STejun Heo bh_worker_pools); 4634cb1ef64STejun Heo 4647d19c5ceSTejun Heo /* the per-cpu worker pools */ 4654cb1ef64STejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], 4664cb1ef64STejun Heo cpu_worker_pools); 4677d19c5ceSTejun Heo 46868e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 4697d19c5ceSTejun Heo 47068e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 47129c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 47229c91e99STejun Heo 473c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 47429c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 47529c91e99STejun Heo 4768a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 4778a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 4788a2b7538STejun Heo 479967b494eSTejun Heo /* 480967b494eSTejun Heo * I: kthread_worker to release pwq's. pwq release needs to be bounced to a 481967b494eSTejun Heo * process context while holding a pool lock. Bounce to a dedicated kthread 482967b494eSTejun Heo * worker to avoid A-A deadlocks. 483967b494eSTejun Heo */ 48468279f9cSAlexey Dobriyan static struct kthread_worker *pwq_release_worker __ro_after_init; 485967b494eSTejun Heo 48668279f9cSAlexey Dobriyan struct workqueue_struct *system_wq __ro_after_init; 487ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 48868279f9cSAlexey Dobriyan struct workqueue_struct *system_highpri_wq __ro_after_init; 4891aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 49068279f9cSAlexey Dobriyan struct workqueue_struct *system_long_wq __ro_after_init; 491d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 49268279f9cSAlexey Dobriyan struct workqueue_struct *system_unbound_wq __ro_after_init; 493f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 49468279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_wq __ro_after_init; 49524d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 49668279f9cSAlexey Dobriyan struct workqueue_struct *system_power_efficient_wq __ro_after_init; 4970668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 49868279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_power_efficient_wq __ro_after_init; 4990668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 5004cb1ef64STejun Heo struct workqueue_struct *system_bh_wq; 5014cb1ef64STejun Heo EXPORT_SYMBOL_GPL(system_bh_wq); 5024cb1ef64STejun Heo struct workqueue_struct *system_bh_highpri_wq; 5034cb1ef64STejun Heo EXPORT_SYMBOL_GPL(system_bh_highpri_wq); 504d320c038STejun Heo 5057d19c5ceSTejun Heo static int worker_thread(void *__worker); 5066ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 507c29eb853STejun Heo static void show_pwq(struct pool_workqueue *pwq); 50855df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool); 5097d19c5ceSTejun Heo 51097bd2347STejun Heo #define CREATE_TRACE_POINTS 51197bd2347STejun Heo #include <trace/events/workqueue.h> 51297bd2347STejun Heo 51368e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 51424acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 515f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 51624acfb71SThomas Gleixner "RCU or wq_pool_mutex should be held") 5175bcab335STejun Heo 5185b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 51924acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 520f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 521f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 52224acfb71SThomas Gleixner "RCU, wq->mutex or wq_pool_mutex should be held") 5235b95e1afSLai Jiangshan 5244cb1ef64STejun Heo #define for_each_bh_worker_pool(pool, cpu) \ 5254cb1ef64STejun Heo for ((pool) = &per_cpu(bh_worker_pools, cpu)[0]; \ 5264cb1ef64STejun Heo (pool) < &per_cpu(bh_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 5274cb1ef64STejun Heo (pool)++) 5284cb1ef64STejun Heo 529f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 530f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 531f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 5327a62c2c8STejun Heo (pool)++) 5334ce62e9eSTejun Heo 53449e3cf44STejun Heo /** 53517116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 53617116969STejun Heo * @pool: iteration cursor 537611c92a0STejun Heo * @pi: integer used for iteration 538fa1b54e6STejun Heo * 53924acfb71SThomas Gleixner * This must be called either with wq_pool_mutex held or RCU read 54068e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 54168e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 542fa1b54e6STejun Heo * 543fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 544fa1b54e6STejun Heo * ignored. 54517116969STejun Heo */ 546611c92a0STejun Heo #define for_each_pool(pool, pi) \ 547611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 54868e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 549fa1b54e6STejun Heo else 55017116969STejun Heo 55117116969STejun Heo /** 552822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 553822d8405STejun Heo * @worker: iteration cursor 554822d8405STejun Heo * @pool: worker_pool to iterate workers of 555822d8405STejun Heo * 5561258fae7STejun Heo * This must be called with wq_pool_attach_mutex. 557822d8405STejun Heo * 558822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 559822d8405STejun Heo * ignored. 560822d8405STejun Heo */ 561da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 562da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 5631258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 564822d8405STejun Heo else 565822d8405STejun Heo 566822d8405STejun Heo /** 56749e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 56849e3cf44STejun Heo * @pwq: iteration cursor 56949e3cf44STejun Heo * @wq: the target workqueue 57076af4d93STejun Heo * 57124acfb71SThomas Gleixner * This must be called either with wq->mutex held or RCU read locked. 572794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 573794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 57476af4d93STejun Heo * 57576af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 57676af4d93STejun Heo * ignored. 57749e3cf44STejun Heo */ 57849e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 57949e9d1a9SSebastian Andrzej Siewior list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ 5805a644662SJoel Fernandes (Google) lockdep_is_held(&(wq->mutex))) 581f3421797STejun Heo 582dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 583dc186ad7SThomas Gleixner 584f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr; 585dc186ad7SThomas Gleixner 58699777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 58799777288SStanislaw Gruszka { 58899777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 58999777288SStanislaw Gruszka } 59099777288SStanislaw Gruszka 591b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 592b9fdac7fSDu, Changbin { 593b9fdac7fSDu, Changbin struct work_struct *work = addr; 594b9fdac7fSDu, Changbin 595b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 596b9fdac7fSDu, Changbin } 597b9fdac7fSDu, Changbin 598dc186ad7SThomas Gleixner /* 599dc186ad7SThomas Gleixner * fixup_init is called when: 600dc186ad7SThomas Gleixner * - an active object is initialized 601dc186ad7SThomas Gleixner */ 60202a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 603dc186ad7SThomas Gleixner { 604dc186ad7SThomas Gleixner struct work_struct *work = addr; 605dc186ad7SThomas Gleixner 606dc186ad7SThomas Gleixner switch (state) { 607dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 608dc186ad7SThomas Gleixner cancel_work_sync(work); 609dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 61002a982a6SDu, Changbin return true; 611dc186ad7SThomas Gleixner default: 61202a982a6SDu, Changbin return false; 613dc186ad7SThomas Gleixner } 614dc186ad7SThomas Gleixner } 615dc186ad7SThomas Gleixner 616dc186ad7SThomas Gleixner /* 617dc186ad7SThomas Gleixner * fixup_free is called when: 618dc186ad7SThomas Gleixner * - an active object is freed 619dc186ad7SThomas Gleixner */ 62002a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 621dc186ad7SThomas Gleixner { 622dc186ad7SThomas Gleixner struct work_struct *work = addr; 623dc186ad7SThomas Gleixner 624dc186ad7SThomas Gleixner switch (state) { 625dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 626dc186ad7SThomas Gleixner cancel_work_sync(work); 627dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 62802a982a6SDu, Changbin return true; 629dc186ad7SThomas Gleixner default: 63002a982a6SDu, Changbin return false; 631dc186ad7SThomas Gleixner } 632dc186ad7SThomas Gleixner } 633dc186ad7SThomas Gleixner 634f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr = { 635dc186ad7SThomas Gleixner .name = "work_struct", 63699777288SStanislaw Gruszka .debug_hint = work_debug_hint, 637b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 638dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 639dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 640dc186ad7SThomas Gleixner }; 641dc186ad7SThomas Gleixner 642dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 643dc186ad7SThomas Gleixner { 644dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 645dc186ad7SThomas Gleixner } 646dc186ad7SThomas Gleixner 647dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 648dc186ad7SThomas Gleixner { 649dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 650dc186ad7SThomas Gleixner } 651dc186ad7SThomas Gleixner 652dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 653dc186ad7SThomas Gleixner { 654dc186ad7SThomas Gleixner if (onstack) 655dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 656dc186ad7SThomas Gleixner else 657dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 658dc186ad7SThomas Gleixner } 659dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 660dc186ad7SThomas Gleixner 661dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 662dc186ad7SThomas Gleixner { 663dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 664dc186ad7SThomas Gleixner } 665dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 666dc186ad7SThomas Gleixner 667ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 668ea2e64f2SThomas Gleixner { 669ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 670ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 671ea2e64f2SThomas Gleixner } 672ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 673ea2e64f2SThomas Gleixner 674dc186ad7SThomas Gleixner #else 675dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 676dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 677dc186ad7SThomas Gleixner #endif 678dc186ad7SThomas Gleixner 6794e8b22bdSLi Bin /** 68067dc8325SCai Huoqing * worker_pool_assign_id - allocate ID and assign it to @pool 6814e8b22bdSLi Bin * @pool: the pool pointer of interest 6824e8b22bdSLi Bin * 6834e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 6844e8b22bdSLi Bin * successfully, -errno on failure. 6854e8b22bdSLi Bin */ 6869daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 6879daf9e67STejun Heo { 6889daf9e67STejun Heo int ret; 6899daf9e67STejun Heo 69068e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6915bcab335STejun Heo 6924e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 6934e8b22bdSLi Bin GFP_KERNEL); 694229641a6STejun Heo if (ret >= 0) { 695e68035fbSTejun Heo pool->id = ret; 696229641a6STejun Heo return 0; 697229641a6STejun Heo } 6989daf9e67STejun Heo return ret; 6999daf9e67STejun Heo } 7009daf9e67STejun Heo 7019f66cff2STejun Heo static struct pool_workqueue __rcu ** 7029f66cff2STejun Heo unbound_pwq_slot(struct workqueue_struct *wq, int cpu) 7039f66cff2STejun Heo { 7049f66cff2STejun Heo if (cpu >= 0) 7059f66cff2STejun Heo return per_cpu_ptr(wq->cpu_pwq, cpu); 7069f66cff2STejun Heo else 7079f66cff2STejun Heo return &wq->dfl_pwq; 7089f66cff2STejun Heo } 7099f66cff2STejun Heo 7109f66cff2STejun Heo /* @cpu < 0 for dfl_pwq */ 7119f66cff2STejun Heo static struct pool_workqueue *unbound_pwq(struct workqueue_struct *wq, int cpu) 7129f66cff2STejun Heo { 7139f66cff2STejun Heo return rcu_dereference_check(*unbound_pwq_slot(wq, cpu), 7149f66cff2STejun Heo lockdep_is_held(&wq_pool_mutex) || 7159f66cff2STejun Heo lockdep_is_held(&wq->mutex)); 7169f66cff2STejun Heo } 7179f66cff2STejun Heo 7185797b1c1STejun Heo /** 7195797b1c1STejun Heo * unbound_effective_cpumask - effective cpumask of an unbound workqueue 7205797b1c1STejun Heo * @wq: workqueue of interest 7215797b1c1STejun Heo * 7225797b1c1STejun Heo * @wq->unbound_attrs->cpumask contains the cpumask requested by the user which 7235797b1c1STejun Heo * is masked with wq_unbound_cpumask to determine the effective cpumask. The 7245797b1c1STejun Heo * default pwq is always mapped to the pool with the current effective cpumask. 7255797b1c1STejun Heo */ 7265797b1c1STejun Heo static struct cpumask *unbound_effective_cpumask(struct workqueue_struct *wq) 7275797b1c1STejun Heo { 7285797b1c1STejun Heo return unbound_pwq(wq, -1)->pool->attrs->__pod_cpumask; 7295797b1c1STejun Heo } 7305797b1c1STejun Heo 73173f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 73273f53c4aSTejun Heo { 73373f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 73473f53c4aSTejun Heo } 73573f53c4aSTejun Heo 736c4560c2cSLai Jiangshan static int get_work_color(unsigned long work_data) 73773f53c4aSTejun Heo { 738c4560c2cSLai Jiangshan return (work_data >> WORK_STRUCT_COLOR_SHIFT) & 73973f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 74073f53c4aSTejun Heo } 74173f53c4aSTejun Heo 74273f53c4aSTejun Heo static int work_next_color(int color) 74373f53c4aSTejun Heo { 74473f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 745a848e3b6SOleg Nesterov } 746a848e3b6SOleg Nesterov 7474594bf15SDavid Howells /* 748112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 749112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 7507c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 7517a22ad75STejun Heo * 752112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 753112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 754bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 755bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 7567a22ad75STejun Heo * 757112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 7587c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 759112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 7607c3eed5cSTejun Heo * available only while the work item is queued. 761bbb68dfaSTejun Heo * 762bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 763bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 764bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 765bbb68dfaSTejun Heo * try to steal the PENDING bit. 7664594bf15SDavid Howells */ 7677a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 7687a22ad75STejun Heo unsigned long flags) 7697a22ad75STejun Heo { 7706183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 7717a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 7727a22ad75STejun Heo } 7737a22ad75STejun Heo 774112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 7754690c4abSTejun Heo unsigned long extra_flags) 776365970a1SDavid Howells { 777112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 778112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 779365970a1SDavid Howells } 780365970a1SDavid Howells 7814468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 7824468a00fSLai Jiangshan int pool_id) 7834468a00fSLai Jiangshan { 7844468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 7854468a00fSLai Jiangshan WORK_STRUCT_PENDING); 7864468a00fSLai Jiangshan } 7874468a00fSLai Jiangshan 7887c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 7897c3eed5cSTejun Heo int pool_id) 7904d707b9fSOleg Nesterov { 79123657bb1STejun Heo /* 79223657bb1STejun Heo * The following wmb is paired with the implied mb in 79323657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 79423657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 79523657bb1STejun Heo * owner. 79623657bb1STejun Heo */ 79723657bb1STejun Heo smp_wmb(); 7987c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 799346c09f8SRoman Pen /* 800346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 801346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 802346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 8038bdc6201SLiu Song * reordering can lead to a missed execution on attempt to queue 804346c09f8SRoman Pen * the same @work. E.g. consider this case: 805346c09f8SRoman Pen * 806346c09f8SRoman Pen * CPU#0 CPU#1 807346c09f8SRoman Pen * ---------------------------- -------------------------------- 808346c09f8SRoman Pen * 809346c09f8SRoman Pen * 1 STORE event_indicated 810346c09f8SRoman Pen * 2 queue_work_on() { 811346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 812346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 813346c09f8SRoman Pen * 5 set_work_data() # clear bit 814346c09f8SRoman Pen * 6 smp_mb() 815346c09f8SRoman Pen * 7 work->current_func() { 816346c09f8SRoman Pen * 8 LOAD event_indicated 817346c09f8SRoman Pen * } 818346c09f8SRoman Pen * 819346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 820346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 821346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 822346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 823346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 824346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 825346c09f8SRoman Pen * before actual STORE. 826346c09f8SRoman Pen */ 827346c09f8SRoman Pen smp_mb(); 8284d707b9fSOleg Nesterov } 8294d707b9fSOleg Nesterov 8307a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 831365970a1SDavid Howells { 8327c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 8337c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 8347a22ad75STejun Heo } 8357a22ad75STejun Heo 836afa4bb77SLinus Torvalds static inline struct pool_workqueue *work_struct_pwq(unsigned long data) 837afa4bb77SLinus Torvalds { 838afa4bb77SLinus Torvalds return (struct pool_workqueue *)(data & WORK_STRUCT_WQ_DATA_MASK); 839afa4bb77SLinus Torvalds } 840afa4bb77SLinus Torvalds 841112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 8427a22ad75STejun Heo { 843e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 8447a22ad75STejun Heo 845112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 846afa4bb77SLinus Torvalds return work_struct_pwq(data); 847e120153dSTejun Heo else 848e120153dSTejun Heo return NULL; 8497a22ad75STejun Heo } 8507a22ad75STejun Heo 8517c3eed5cSTejun Heo /** 8527c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 8537c3eed5cSTejun Heo * @work: the work item of interest 8547c3eed5cSTejun Heo * 85568e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 85624acfb71SThomas Gleixner * access under RCU read lock. As such, this function should be 85724acfb71SThomas Gleixner * called under wq_pool_mutex or inside of a rcu_read_lock() region. 858fa1b54e6STejun Heo * 859fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 860fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 861fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 862fa1b54e6STejun Heo * returned pool is and stays online. 863d185af30SYacine Belkadi * 864d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 8657c3eed5cSTejun Heo */ 8667c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 8677a22ad75STejun Heo { 868e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 8697c3eed5cSTejun Heo int pool_id; 8707a22ad75STejun Heo 87168e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 872fa1b54e6STejun Heo 873112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 874afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool; 8757a22ad75STejun Heo 8767c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 8777c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 8787a22ad75STejun Heo return NULL; 8797a22ad75STejun Heo 880fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 8817c3eed5cSTejun Heo } 8827c3eed5cSTejun Heo 8837c3eed5cSTejun Heo /** 8847c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 8857c3eed5cSTejun Heo * @work: the work item of interest 8867c3eed5cSTejun Heo * 887d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 8887c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 8897c3eed5cSTejun Heo */ 8907c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 8917c3eed5cSTejun Heo { 89254d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 8937c3eed5cSTejun Heo 894112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 895afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool->id; 89654d5b7d0SLai Jiangshan 89754d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 8987c3eed5cSTejun Heo } 8997c3eed5cSTejun Heo 900bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 901bbb68dfaSTejun Heo { 9027c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 903bbb68dfaSTejun Heo 9047c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 9057c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 906bbb68dfaSTejun Heo } 907bbb68dfaSTejun Heo 908bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 909bbb68dfaSTejun Heo { 910bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 911bbb68dfaSTejun Heo 912112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 913bbb68dfaSTejun Heo } 914bbb68dfaSTejun Heo 915e22bee78STejun Heo /* 9163270476aSTejun Heo * Policy functions. These define the policies on how the global worker 9173270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 918d565ed63STejun Heo * they're being called with pool->lock held. 919e22bee78STejun Heo */ 920e22bee78STejun Heo 921e22bee78STejun Heo /* 922e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 923e22bee78STejun Heo * running workers. 924974271c4STejun Heo * 925974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 926706026c2STejun Heo * function will always return %true for unbound pools as long as the 927974271c4STejun Heo * worklist isn't empty. 928e22bee78STejun Heo */ 92963d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 930e22bee78STejun Heo { 9310219a352STejun Heo return !list_empty(&pool->worklist) && !pool->nr_running; 932e22bee78STejun Heo } 933e22bee78STejun Heo 934e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 93563d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 936e22bee78STejun Heo { 93763d95a91STejun Heo return pool->nr_idle; 938e22bee78STejun Heo } 939e22bee78STejun Heo 940e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 94163d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 942e22bee78STejun Heo { 943bc35f7efSLai Jiangshan return !list_empty(&pool->worklist) && (pool->nr_running <= 1); 944e22bee78STejun Heo } 945e22bee78STejun Heo 946e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 94763d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 948e22bee78STejun Heo { 94963d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 950e22bee78STejun Heo } 951e22bee78STejun Heo 952e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 95363d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 954e22bee78STejun Heo { 955692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 95663d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 95763d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 958e22bee78STejun Heo 959e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 960e22bee78STejun Heo } 961e22bee78STejun Heo 9624690c4abSTejun Heo /** 963e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 964cb444766STejun Heo * @worker: self 965d302f017STejun Heo * @flags: flags to set 966d302f017STejun Heo * 967228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 968d302f017STejun Heo */ 969228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 970d302f017STejun Heo { 971bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 972e22bee78STejun Heo 973bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 974cb444766STejun Heo 975228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 976e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 977e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 978bc35f7efSLai Jiangshan pool->nr_running--; 979e22bee78STejun Heo } 980e22bee78STejun Heo 981d302f017STejun Heo worker->flags |= flags; 982d302f017STejun Heo } 983d302f017STejun Heo 984d302f017STejun Heo /** 985e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 986cb444766STejun Heo * @worker: self 987d302f017STejun Heo * @flags: flags to clear 988d302f017STejun Heo * 989e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 990d302f017STejun Heo */ 991d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 992d302f017STejun Heo { 99363d95a91STejun Heo struct worker_pool *pool = worker->pool; 994e22bee78STejun Heo unsigned int oflags = worker->flags; 995e22bee78STejun Heo 996bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 997cb444766STejun Heo 998d302f017STejun Heo worker->flags &= ~flags; 999e22bee78STejun Heo 100042c025f3STejun Heo /* 100142c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 100242c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 100342c025f3STejun Heo * of multiple flags, not a single flag. 100442c025f3STejun Heo */ 1005e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 1006e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 1007bc35f7efSLai Jiangshan pool->nr_running++; 1008d302f017STejun Heo } 1009d302f017STejun Heo 1010797e8345STejun Heo /* Return the first idle worker. Called with pool->lock held. */ 1011797e8345STejun Heo static struct worker *first_idle_worker(struct worker_pool *pool) 1012797e8345STejun Heo { 1013797e8345STejun Heo if (unlikely(list_empty(&pool->idle_list))) 1014797e8345STejun Heo return NULL; 1015797e8345STejun Heo 1016797e8345STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 1017797e8345STejun Heo } 1018797e8345STejun Heo 1019797e8345STejun Heo /** 1020797e8345STejun Heo * worker_enter_idle - enter idle state 1021797e8345STejun Heo * @worker: worker which is entering idle state 1022797e8345STejun Heo * 1023797e8345STejun Heo * @worker is entering idle state. Update stats and idle timer if 1024797e8345STejun Heo * necessary. 1025797e8345STejun Heo * 1026797e8345STejun Heo * LOCKING: 1027797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1028797e8345STejun Heo */ 1029797e8345STejun Heo static void worker_enter_idle(struct worker *worker) 1030797e8345STejun Heo { 1031797e8345STejun Heo struct worker_pool *pool = worker->pool; 1032797e8345STejun Heo 1033797e8345STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 1034797e8345STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 1035797e8345STejun Heo (worker->hentry.next || worker->hentry.pprev))) 1036797e8345STejun Heo return; 1037797e8345STejun Heo 1038797e8345STejun Heo /* can't use worker_set_flags(), also called from create_worker() */ 1039797e8345STejun Heo worker->flags |= WORKER_IDLE; 1040797e8345STejun Heo pool->nr_idle++; 1041797e8345STejun Heo worker->last_active = jiffies; 1042797e8345STejun Heo 1043797e8345STejun Heo /* idle_list is LIFO */ 1044797e8345STejun Heo list_add(&worker->entry, &pool->idle_list); 1045797e8345STejun Heo 1046797e8345STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1047797e8345STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1048797e8345STejun Heo 1049797e8345STejun Heo /* Sanity check nr_running. */ 1050797e8345STejun Heo WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); 1051797e8345STejun Heo } 1052797e8345STejun Heo 1053797e8345STejun Heo /** 1054797e8345STejun Heo * worker_leave_idle - leave idle state 1055797e8345STejun Heo * @worker: worker which is leaving idle state 1056797e8345STejun Heo * 1057797e8345STejun Heo * @worker is leaving idle state. Update stats. 1058797e8345STejun Heo * 1059797e8345STejun Heo * LOCKING: 1060797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1061797e8345STejun Heo */ 1062797e8345STejun Heo static void worker_leave_idle(struct worker *worker) 1063797e8345STejun Heo { 1064797e8345STejun Heo struct worker_pool *pool = worker->pool; 1065797e8345STejun Heo 1066797e8345STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 1067797e8345STejun Heo return; 1068797e8345STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1069797e8345STejun Heo pool->nr_idle--; 1070797e8345STejun Heo list_del_init(&worker->entry); 1071797e8345STejun Heo } 1072797e8345STejun Heo 1073797e8345STejun Heo /** 1074797e8345STejun Heo * find_worker_executing_work - find worker which is executing a work 1075797e8345STejun Heo * @pool: pool of interest 1076797e8345STejun Heo * @work: work to find worker for 1077797e8345STejun Heo * 1078797e8345STejun Heo * Find a worker which is executing @work on @pool by searching 1079797e8345STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 1080797e8345STejun Heo * to match, its current execution should match the address of @work and 1081797e8345STejun Heo * its work function. This is to avoid unwanted dependency between 1082797e8345STejun Heo * unrelated work executions through a work item being recycled while still 1083797e8345STejun Heo * being executed. 1084797e8345STejun Heo * 1085797e8345STejun Heo * This is a bit tricky. A work item may be freed once its execution 1086797e8345STejun Heo * starts and nothing prevents the freed area from being recycled for 1087797e8345STejun Heo * another work item. If the same work item address ends up being reused 1088797e8345STejun Heo * before the original execution finishes, workqueue will identify the 1089797e8345STejun Heo * recycled work item as currently executing and make it wait until the 1090797e8345STejun Heo * current execution finishes, introducing an unwanted dependency. 1091797e8345STejun Heo * 1092797e8345STejun Heo * This function checks the work item address and work function to avoid 1093797e8345STejun Heo * false positives. Note that this isn't complete as one may construct a 1094797e8345STejun Heo * work function which can introduce dependency onto itself through a 1095797e8345STejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 1096797e8345STejun Heo * foot that badly, there's only so much we can do, and if such deadlock 1097797e8345STejun Heo * actually occurs, it should be easy to locate the culprit work function. 1098797e8345STejun Heo * 1099797e8345STejun Heo * CONTEXT: 1100797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1101797e8345STejun Heo * 1102797e8345STejun Heo * Return: 1103797e8345STejun Heo * Pointer to worker which is executing @work if found, %NULL 1104797e8345STejun Heo * otherwise. 1105797e8345STejun Heo */ 1106797e8345STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 1107797e8345STejun Heo struct work_struct *work) 1108797e8345STejun Heo { 1109797e8345STejun Heo struct worker *worker; 1110797e8345STejun Heo 1111797e8345STejun Heo hash_for_each_possible(pool->busy_hash, worker, hentry, 1112797e8345STejun Heo (unsigned long)work) 1113797e8345STejun Heo if (worker->current_work == work && 1114797e8345STejun Heo worker->current_func == work->func) 1115797e8345STejun Heo return worker; 1116797e8345STejun Heo 1117797e8345STejun Heo return NULL; 1118797e8345STejun Heo } 1119797e8345STejun Heo 1120797e8345STejun Heo /** 1121797e8345STejun Heo * move_linked_works - move linked works to a list 1122797e8345STejun Heo * @work: start of series of works to be scheduled 1123797e8345STejun Heo * @head: target list to append @work to 1124797e8345STejun Heo * @nextp: out parameter for nested worklist walking 1125797e8345STejun Heo * 1126873eaca6STejun Heo * Schedule linked works starting from @work to @head. Work series to be 1127873eaca6STejun Heo * scheduled starts at @work and includes any consecutive work with 1128873eaca6STejun Heo * WORK_STRUCT_LINKED set in its predecessor. See assign_work() for details on 1129873eaca6STejun Heo * @nextp. 1130797e8345STejun Heo * 1131797e8345STejun Heo * CONTEXT: 1132797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1133797e8345STejun Heo */ 1134797e8345STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1135797e8345STejun Heo struct work_struct **nextp) 1136797e8345STejun Heo { 1137797e8345STejun Heo struct work_struct *n; 1138797e8345STejun Heo 1139797e8345STejun Heo /* 1140797e8345STejun Heo * Linked worklist will always end before the end of the list, 1141797e8345STejun Heo * use NULL for list head. 1142797e8345STejun Heo */ 1143797e8345STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1144797e8345STejun Heo list_move_tail(&work->entry, head); 1145797e8345STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1146797e8345STejun Heo break; 1147797e8345STejun Heo } 1148797e8345STejun Heo 1149797e8345STejun Heo /* 1150797e8345STejun Heo * If we're already inside safe list traversal and have moved 1151797e8345STejun Heo * multiple works to the scheduled queue, the next position 1152797e8345STejun Heo * needs to be updated. 1153797e8345STejun Heo */ 1154797e8345STejun Heo if (nextp) 1155797e8345STejun Heo *nextp = n; 1156797e8345STejun Heo } 1157797e8345STejun Heo 1158797e8345STejun Heo /** 1159873eaca6STejun Heo * assign_work - assign a work item and its linked work items to a worker 1160873eaca6STejun Heo * @work: work to assign 1161873eaca6STejun Heo * @worker: worker to assign to 1162873eaca6STejun Heo * @nextp: out parameter for nested worklist walking 1163873eaca6STejun Heo * 1164873eaca6STejun Heo * Assign @work and its linked work items to @worker. If @work is already being 1165873eaca6STejun Heo * executed by another worker in the same pool, it'll be punted there. 1166873eaca6STejun Heo * 1167873eaca6STejun Heo * If @nextp is not NULL, it's updated to point to the next work of the last 1168873eaca6STejun Heo * scheduled work. This allows assign_work() to be nested inside 1169873eaca6STejun Heo * list_for_each_entry_safe(). 1170873eaca6STejun Heo * 1171873eaca6STejun Heo * Returns %true if @work was successfully assigned to @worker. %false if @work 1172873eaca6STejun Heo * was punted to another worker already executing it. 1173873eaca6STejun Heo */ 1174873eaca6STejun Heo static bool assign_work(struct work_struct *work, struct worker *worker, 1175873eaca6STejun Heo struct work_struct **nextp) 1176873eaca6STejun Heo { 1177873eaca6STejun Heo struct worker_pool *pool = worker->pool; 1178873eaca6STejun Heo struct worker *collision; 1179873eaca6STejun Heo 1180873eaca6STejun Heo lockdep_assert_held(&pool->lock); 1181873eaca6STejun Heo 1182873eaca6STejun Heo /* 1183873eaca6STejun Heo * A single work shouldn't be executed concurrently by multiple workers. 1184873eaca6STejun Heo * __queue_work() ensures that @work doesn't jump to a different pool 1185873eaca6STejun Heo * while still running in the previous pool. Here, we should ensure that 1186873eaca6STejun Heo * @work is not executed concurrently by multiple workers from the same 1187873eaca6STejun Heo * pool. Check whether anyone is already processing the work. If so, 1188873eaca6STejun Heo * defer the work to the currently executing one. 1189873eaca6STejun Heo */ 1190873eaca6STejun Heo collision = find_worker_executing_work(pool, work); 1191873eaca6STejun Heo if (unlikely(collision)) { 1192873eaca6STejun Heo move_linked_works(work, &collision->scheduled, nextp); 1193873eaca6STejun Heo return false; 1194873eaca6STejun Heo } 1195873eaca6STejun Heo 1196873eaca6STejun Heo move_linked_works(work, &worker->scheduled, nextp); 1197873eaca6STejun Heo return true; 1198873eaca6STejun Heo } 1199873eaca6STejun Heo 1200873eaca6STejun Heo /** 12010219a352STejun Heo * kick_pool - wake up an idle worker if necessary 12020219a352STejun Heo * @pool: pool to kick 1203797e8345STejun Heo * 12040219a352STejun Heo * @pool may have pending work items. Wake up worker if necessary. Returns 12050219a352STejun Heo * whether a worker was woken up. 1206797e8345STejun Heo */ 12070219a352STejun Heo static bool kick_pool(struct worker_pool *pool) 1208797e8345STejun Heo { 1209797e8345STejun Heo struct worker *worker = first_idle_worker(pool); 12108639ecebSTejun Heo struct task_struct *p; 1211797e8345STejun Heo 12120219a352STejun Heo lockdep_assert_held(&pool->lock); 12130219a352STejun Heo 12140219a352STejun Heo if (!need_more_worker(pool) || !worker) 12150219a352STejun Heo return false; 12160219a352STejun Heo 12174cb1ef64STejun Heo if (pool->flags & POOL_BH) { 12184cb1ef64STejun Heo if (pool->attrs->nice == HIGHPRI_NICE_LEVEL) 12194cb1ef64STejun Heo raise_softirq_irqoff(HI_SOFTIRQ); 12204cb1ef64STejun Heo else 12214cb1ef64STejun Heo raise_softirq_irqoff(TASKLET_SOFTIRQ); 12224cb1ef64STejun Heo return true; 12234cb1ef64STejun Heo } 12244cb1ef64STejun Heo 12258639ecebSTejun Heo p = worker->task; 12268639ecebSTejun Heo 12278639ecebSTejun Heo #ifdef CONFIG_SMP 12288639ecebSTejun Heo /* 12298639ecebSTejun Heo * Idle @worker is about to execute @work and waking up provides an 12308639ecebSTejun Heo * opportunity to migrate @worker at a lower cost by setting the task's 12318639ecebSTejun Heo * wake_cpu field. Let's see if we want to move @worker to improve 12328639ecebSTejun Heo * execution locality. 12338639ecebSTejun Heo * 12348639ecebSTejun Heo * We're waking the worker that went idle the latest and there's some 12358639ecebSTejun Heo * chance that @worker is marked idle but hasn't gone off CPU yet. If 12368639ecebSTejun Heo * so, setting the wake_cpu won't do anything. As this is a best-effort 12378639ecebSTejun Heo * optimization and the race window is narrow, let's leave as-is for 12388639ecebSTejun Heo * now. If this becomes pronounced, we can skip over workers which are 12398639ecebSTejun Heo * still on cpu when picking an idle worker. 12408639ecebSTejun Heo * 12418639ecebSTejun Heo * If @pool has non-strict affinity, @worker might have ended up outside 12428639ecebSTejun Heo * its affinity scope. Repatriate. 12438639ecebSTejun Heo */ 12448639ecebSTejun Heo if (!pool->attrs->affn_strict && 12458639ecebSTejun Heo !cpumask_test_cpu(p->wake_cpu, pool->attrs->__pod_cpumask)) { 12468639ecebSTejun Heo struct work_struct *work = list_first_entry(&pool->worklist, 12478639ecebSTejun Heo struct work_struct, entry); 12488639ecebSTejun Heo p->wake_cpu = cpumask_any_distribute(pool->attrs->__pod_cpumask); 12498639ecebSTejun Heo get_work_pwq(work)->stats[PWQ_STAT_REPATRIATED]++; 12508639ecebSTejun Heo } 12518639ecebSTejun Heo #endif 12528639ecebSTejun Heo wake_up_process(p); 12530219a352STejun Heo return true; 1254797e8345STejun Heo } 1255797e8345STejun Heo 125663638450STejun Heo #ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT 125763638450STejun Heo 125863638450STejun Heo /* 125963638450STejun Heo * Concurrency-managed per-cpu work items that hog CPU for longer than 126063638450STejun Heo * wq_cpu_intensive_thresh_us trigger the automatic CPU_INTENSIVE mechanism, 126163638450STejun Heo * which prevents them from stalling other concurrency-managed work items. If a 126263638450STejun Heo * work function keeps triggering this mechanism, it's likely that the work item 126363638450STejun Heo * should be using an unbound workqueue instead. 126463638450STejun Heo * 126563638450STejun Heo * wq_cpu_intensive_report() tracks work functions which trigger such conditions 126663638450STejun Heo * and report them so that they can be examined and converted to use unbound 126763638450STejun Heo * workqueues as appropriate. To avoid flooding the console, each violating work 126863638450STejun Heo * function is tracked and reported with exponential backoff. 126963638450STejun Heo */ 127063638450STejun Heo #define WCI_MAX_ENTS 128 127163638450STejun Heo 127263638450STejun Heo struct wci_ent { 127363638450STejun Heo work_func_t func; 127463638450STejun Heo atomic64_t cnt; 127563638450STejun Heo struct hlist_node hash_node; 127663638450STejun Heo }; 127763638450STejun Heo 127863638450STejun Heo static struct wci_ent wci_ents[WCI_MAX_ENTS]; 127963638450STejun Heo static int wci_nr_ents; 128063638450STejun Heo static DEFINE_RAW_SPINLOCK(wci_lock); 128163638450STejun Heo static DEFINE_HASHTABLE(wci_hash, ilog2(WCI_MAX_ENTS)); 128263638450STejun Heo 128363638450STejun Heo static struct wci_ent *wci_find_ent(work_func_t func) 128463638450STejun Heo { 128563638450STejun Heo struct wci_ent *ent; 128663638450STejun Heo 128763638450STejun Heo hash_for_each_possible_rcu(wci_hash, ent, hash_node, 128863638450STejun Heo (unsigned long)func) { 128963638450STejun Heo if (ent->func == func) 129063638450STejun Heo return ent; 129163638450STejun Heo } 129263638450STejun Heo return NULL; 129363638450STejun Heo } 129463638450STejun Heo 129563638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) 129663638450STejun Heo { 129763638450STejun Heo struct wci_ent *ent; 129863638450STejun Heo 129963638450STejun Heo restart: 130063638450STejun Heo ent = wci_find_ent(func); 130163638450STejun Heo if (ent) { 130263638450STejun Heo u64 cnt; 130363638450STejun Heo 130463638450STejun Heo /* 130563638450STejun Heo * Start reporting from the fourth time and back off 130663638450STejun Heo * exponentially. 130763638450STejun Heo */ 130863638450STejun Heo cnt = atomic64_inc_return_relaxed(&ent->cnt); 130963638450STejun Heo if (cnt >= 4 && is_power_of_2(cnt)) 131063638450STejun Heo printk_deferred(KERN_WARNING "workqueue: %ps hogged CPU for >%luus %llu times, consider switching to WQ_UNBOUND\n", 131163638450STejun Heo ent->func, wq_cpu_intensive_thresh_us, 131263638450STejun Heo atomic64_read(&ent->cnt)); 131363638450STejun Heo return; 131463638450STejun Heo } 131563638450STejun Heo 131663638450STejun Heo /* 131763638450STejun Heo * @func is a new violation. Allocate a new entry for it. If wcn_ents[] 131863638450STejun Heo * is exhausted, something went really wrong and we probably made enough 131963638450STejun Heo * noise already. 132063638450STejun Heo */ 132163638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) 132263638450STejun Heo return; 132363638450STejun Heo 132463638450STejun Heo raw_spin_lock(&wci_lock); 132563638450STejun Heo 132663638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) { 132763638450STejun Heo raw_spin_unlock(&wci_lock); 132863638450STejun Heo return; 132963638450STejun Heo } 133063638450STejun Heo 133163638450STejun Heo if (wci_find_ent(func)) { 133263638450STejun Heo raw_spin_unlock(&wci_lock); 133363638450STejun Heo goto restart; 133463638450STejun Heo } 133563638450STejun Heo 133663638450STejun Heo ent = &wci_ents[wci_nr_ents++]; 133763638450STejun Heo ent->func = func; 133863638450STejun Heo atomic64_set(&ent->cnt, 1); 133963638450STejun Heo hash_add_rcu(wci_hash, &ent->hash_node, (unsigned long)func); 134063638450STejun Heo 134163638450STejun Heo raw_spin_unlock(&wci_lock); 134263638450STejun Heo } 134363638450STejun Heo 134463638450STejun Heo #else /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 134563638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) {} 134663638450STejun Heo #endif /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 134763638450STejun Heo 1348c54d5046STejun Heo /** 13491da177e4SLinus Torvalds * wq_worker_running - a worker is running again 13501da177e4SLinus Torvalds * @task: task waking up 13511da177e4SLinus Torvalds * 13521da177e4SLinus Torvalds * This function is called when a worker returns from schedule() 13531da177e4SLinus Torvalds */ 13541da177e4SLinus Torvalds void wq_worker_running(struct task_struct *task) 13551da177e4SLinus Torvalds { 13561da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 13571da177e4SLinus Torvalds 1358c8f6219bSZqiang if (!READ_ONCE(worker->sleeping)) 13591da177e4SLinus Torvalds return; 13601da177e4SLinus Torvalds 13611da177e4SLinus Torvalds /* 13621da177e4SLinus Torvalds * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check 13631da177e4SLinus Torvalds * and the nr_running increment below, we may ruin the nr_running reset 13641da177e4SLinus Torvalds * and leave with an unexpected pool->nr_running == 1 on the newly unbound 13651da177e4SLinus Torvalds * pool. Protect against such race. 13661da177e4SLinus Torvalds */ 13671da177e4SLinus Torvalds preempt_disable(); 13681da177e4SLinus Torvalds if (!(worker->flags & WORKER_NOT_RUNNING)) 13691da177e4SLinus Torvalds worker->pool->nr_running++; 13701da177e4SLinus Torvalds preempt_enable(); 1371616db877STejun Heo 1372616db877STejun Heo /* 1373616db877STejun Heo * CPU intensive auto-detection cares about how long a work item hogged 1374616db877STejun Heo * CPU without sleeping. Reset the starting timestamp on wakeup. 1375616db877STejun Heo */ 1376616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 1377616db877STejun Heo 1378c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 0); 13791da177e4SLinus Torvalds } 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds /** 13821da177e4SLinus Torvalds * wq_worker_sleeping - a worker is going to sleep 13831da177e4SLinus Torvalds * @task: task going to sleep 13841da177e4SLinus Torvalds * 13851da177e4SLinus Torvalds * This function is called from schedule() when a busy worker is 13861da177e4SLinus Torvalds * going to sleep. 13871da177e4SLinus Torvalds */ 13881da177e4SLinus Torvalds void wq_worker_sleeping(struct task_struct *task) 13891da177e4SLinus Torvalds { 13901da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 13911da177e4SLinus Torvalds struct worker_pool *pool; 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds /* 13941da177e4SLinus Torvalds * Rescuers, which may not have all the fields set up like normal 13951da177e4SLinus Torvalds * workers, also reach here, let's not access anything before 13961da177e4SLinus Torvalds * checking NOT_RUNNING. 13971da177e4SLinus Torvalds */ 13981da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) 13991da177e4SLinus Torvalds return; 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds pool = worker->pool; 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds /* Return if preempted before wq_worker_running() was reached */ 1404c8f6219bSZqiang if (READ_ONCE(worker->sleeping)) 14051da177e4SLinus Torvalds return; 14061da177e4SLinus Torvalds 1407c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 1); 14081da177e4SLinus Torvalds raw_spin_lock_irq(&pool->lock); 14091da177e4SLinus Torvalds 14101da177e4SLinus Torvalds /* 14111da177e4SLinus Torvalds * Recheck in case unbind_workers() preempted us. We don't 14121da177e4SLinus Torvalds * want to decrement nr_running after the worker is unbound 14131da177e4SLinus Torvalds * and nr_running has been reset. 14141da177e4SLinus Torvalds */ 14151da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) { 14161da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 14171da177e4SLinus Torvalds return; 14181da177e4SLinus Torvalds } 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds pool->nr_running--; 14210219a352STejun Heo if (kick_pool(pool)) 1422725e8ec5STejun Heo worker->current_pwq->stats[PWQ_STAT_CM_WAKEUP]++; 14230219a352STejun Heo 14241da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds /** 1428616db877STejun Heo * wq_worker_tick - a scheduler tick occurred while a kworker is running 1429616db877STejun Heo * @task: task currently running 1430616db877STejun Heo * 1431616db877STejun Heo * Called from scheduler_tick(). We're in the IRQ context and the current 1432616db877STejun Heo * worker's fields which follow the 'K' locking rule can be accessed safely. 1433616db877STejun Heo */ 1434616db877STejun Heo void wq_worker_tick(struct task_struct *task) 1435616db877STejun Heo { 1436616db877STejun Heo struct worker *worker = kthread_data(task); 1437616db877STejun Heo struct pool_workqueue *pwq = worker->current_pwq; 1438616db877STejun Heo struct worker_pool *pool = worker->pool; 1439616db877STejun Heo 1440616db877STejun Heo if (!pwq) 1441616db877STejun Heo return; 1442616db877STejun Heo 14438a1dd1e5STejun Heo pwq->stats[PWQ_STAT_CPU_TIME] += TICK_USEC; 14448a1dd1e5STejun Heo 144518c8ae81SZqiang if (!wq_cpu_intensive_thresh_us) 144618c8ae81SZqiang return; 144718c8ae81SZqiang 1448616db877STejun Heo /* 1449616db877STejun Heo * If the current worker is concurrency managed and hogged the CPU for 1450616db877STejun Heo * longer than wq_cpu_intensive_thresh_us, it's automatically marked 1451616db877STejun Heo * CPU_INTENSIVE to avoid stalling other concurrency-managed work items. 1452c8f6219bSZqiang * 1453c8f6219bSZqiang * Set @worker->sleeping means that @worker is in the process of 1454c8f6219bSZqiang * switching out voluntarily and won't be contributing to 1455c8f6219bSZqiang * @pool->nr_running until it wakes up. As wq_worker_sleeping() also 1456c8f6219bSZqiang * decrements ->nr_running, setting CPU_INTENSIVE here can lead to 1457c8f6219bSZqiang * double decrements. The task is releasing the CPU anyway. Let's skip. 1458c8f6219bSZqiang * We probably want to make this prettier in the future. 1459616db877STejun Heo */ 1460c8f6219bSZqiang if ((worker->flags & WORKER_NOT_RUNNING) || READ_ONCE(worker->sleeping) || 1461616db877STejun Heo worker->task->se.sum_exec_runtime - worker->current_at < 1462616db877STejun Heo wq_cpu_intensive_thresh_us * NSEC_PER_USEC) 1463616db877STejun Heo return; 1464616db877STejun Heo 1465616db877STejun Heo raw_spin_lock(&pool->lock); 1466616db877STejun Heo 1467616db877STejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE); 146863638450STejun Heo wq_cpu_intensive_report(worker->current_func); 1469616db877STejun Heo pwq->stats[PWQ_STAT_CPU_INTENSIVE]++; 1470616db877STejun Heo 14710219a352STejun Heo if (kick_pool(pool)) 1472616db877STejun Heo pwq->stats[PWQ_STAT_CM_WAKEUP]++; 1473616db877STejun Heo 1474616db877STejun Heo raw_spin_unlock(&pool->lock); 1475616db877STejun Heo } 1476616db877STejun Heo 1477616db877STejun Heo /** 14781da177e4SLinus Torvalds * wq_worker_last_func - retrieve worker's last work function 14791da177e4SLinus Torvalds * @task: Task to retrieve last work function of. 14801da177e4SLinus Torvalds * 14811da177e4SLinus Torvalds * Determine the last function a worker executed. This is called from 14821da177e4SLinus Torvalds * the scheduler to get a worker's last known identity. 14831da177e4SLinus Torvalds * 14841da177e4SLinus Torvalds * CONTEXT: 14859b41ea72SAndrew Morton * raw_spin_lock_irq(rq->lock) 14861da177e4SLinus Torvalds * 14871da177e4SLinus Torvalds * This function is called during schedule() when a kworker is going 1488f756d5e2SNathan Lynch * to sleep. It's used by psi to identify aggregation workers during 1489f756d5e2SNathan Lynch * dequeuing, to allow periodic aggregation to shut-off when that 14901da177e4SLinus Torvalds * worker is the last task in the system or cgroup to go to sleep. 14911da177e4SLinus Torvalds * 14921da177e4SLinus Torvalds * As this function doesn't involve any workqueue-related locking, it 14931da177e4SLinus Torvalds * only returns stable values when called from inside the scheduler's 14941da177e4SLinus Torvalds * queuing and dequeuing paths, when @task, which must be a kworker, 14951da177e4SLinus Torvalds * is guaranteed to not be processing any works. 1496365970a1SDavid Howells * 1497365970a1SDavid Howells * Return: 1498365970a1SDavid Howells * The last work function %current executed as a worker, NULL if it 1499365970a1SDavid Howells * hasn't executed any work yet. 1500365970a1SDavid Howells */ 1501365970a1SDavid Howells work_func_t wq_worker_last_func(struct task_struct *task) 1502365970a1SDavid Howells { 1503365970a1SDavid Howells struct worker *worker = kthread_data(task); 1504365970a1SDavid Howells 1505365970a1SDavid Howells return worker->last_func; 1506365970a1SDavid Howells } 1507365970a1SDavid Howells 1508d302f017STejun Heo /** 150991ccc6e7STejun Heo * wq_node_nr_active - Determine wq_node_nr_active to use 151091ccc6e7STejun Heo * @wq: workqueue of interest 151191ccc6e7STejun Heo * @node: NUMA node, can be %NUMA_NO_NODE 151291ccc6e7STejun Heo * 151391ccc6e7STejun Heo * Determine wq_node_nr_active to use for @wq on @node. Returns: 151491ccc6e7STejun Heo * 151591ccc6e7STejun Heo * - %NULL for per-cpu workqueues as they don't need to use shared nr_active. 151691ccc6e7STejun Heo * 151791ccc6e7STejun Heo * - node_nr_active[nr_node_ids] if @node is %NUMA_NO_NODE. 151891ccc6e7STejun Heo * 151991ccc6e7STejun Heo * - Otherwise, node_nr_active[@node]. 152091ccc6e7STejun Heo */ 152191ccc6e7STejun Heo static struct wq_node_nr_active *wq_node_nr_active(struct workqueue_struct *wq, 152291ccc6e7STejun Heo int node) 152391ccc6e7STejun Heo { 152491ccc6e7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 152591ccc6e7STejun Heo return NULL; 152691ccc6e7STejun Heo 152791ccc6e7STejun Heo if (node == NUMA_NO_NODE) 152891ccc6e7STejun Heo node = nr_node_ids; 152991ccc6e7STejun Heo 153091ccc6e7STejun Heo return wq->node_nr_active[node]; 153191ccc6e7STejun Heo } 153291ccc6e7STejun Heo 153391ccc6e7STejun Heo /** 15345797b1c1STejun Heo * wq_update_node_max_active - Update per-node max_actives to use 15355797b1c1STejun Heo * @wq: workqueue to update 15365797b1c1STejun Heo * @off_cpu: CPU that's going down, -1 if a CPU is not going down 15375797b1c1STejun Heo * 15385797b1c1STejun Heo * Update @wq->node_nr_active[]->max. @wq must be unbound. max_active is 15395797b1c1STejun Heo * distributed among nodes according to the proportions of numbers of online 15405797b1c1STejun Heo * cpus. The result is always between @wq->min_active and max_active. 15415797b1c1STejun Heo */ 15425797b1c1STejun Heo static void wq_update_node_max_active(struct workqueue_struct *wq, int off_cpu) 15435797b1c1STejun Heo { 15445797b1c1STejun Heo struct cpumask *effective = unbound_effective_cpumask(wq); 15455797b1c1STejun Heo int min_active = READ_ONCE(wq->min_active); 15465797b1c1STejun Heo int max_active = READ_ONCE(wq->max_active); 15475797b1c1STejun Heo int total_cpus, node; 15485797b1c1STejun Heo 15495797b1c1STejun Heo lockdep_assert_held(&wq->mutex); 15505797b1c1STejun Heo 1551c5f8cd6cSTejun Heo if (!wq_topo_initialized) 1552c5f8cd6cSTejun Heo return; 1553c5f8cd6cSTejun Heo 155415930da4STejun Heo if (off_cpu >= 0 && !cpumask_test_cpu(off_cpu, effective)) 15555797b1c1STejun Heo off_cpu = -1; 15565797b1c1STejun Heo 15575797b1c1STejun Heo total_cpus = cpumask_weight_and(effective, cpu_online_mask); 15585797b1c1STejun Heo if (off_cpu >= 0) 15595797b1c1STejun Heo total_cpus--; 15605797b1c1STejun Heo 15615797b1c1STejun Heo for_each_node(node) { 15625797b1c1STejun Heo int node_cpus; 15635797b1c1STejun Heo 15645797b1c1STejun Heo node_cpus = cpumask_weight_and(effective, cpumask_of_node(node)); 15655797b1c1STejun Heo if (off_cpu >= 0 && cpu_to_node(off_cpu) == node) 15665797b1c1STejun Heo node_cpus--; 15675797b1c1STejun Heo 15685797b1c1STejun Heo wq_node_nr_active(wq, node)->max = 15695797b1c1STejun Heo clamp(DIV_ROUND_UP(max_active * node_cpus, total_cpus), 15705797b1c1STejun Heo min_active, max_active); 15715797b1c1STejun Heo } 15725797b1c1STejun Heo 15735797b1c1STejun Heo wq_node_nr_active(wq, NUMA_NO_NODE)->max = min_active; 15745797b1c1STejun Heo } 15755797b1c1STejun Heo 15765797b1c1STejun Heo /** 15778864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 15788864b4e5STejun Heo * @pwq: pool_workqueue to get 15798864b4e5STejun Heo * 15808864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 15818864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 15828864b4e5STejun Heo */ 15838864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 15848864b4e5STejun Heo { 15858864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 15868864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 15878864b4e5STejun Heo pwq->refcnt++; 15888864b4e5STejun Heo } 15898864b4e5STejun Heo 15908864b4e5STejun Heo /** 15918864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 15928864b4e5STejun Heo * @pwq: pool_workqueue to put 15938864b4e5STejun Heo * 15948864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 15958864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 15968864b4e5STejun Heo */ 15978864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 15988864b4e5STejun Heo { 15998864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 16008864b4e5STejun Heo if (likely(--pwq->refcnt)) 16018864b4e5STejun Heo return; 16028864b4e5STejun Heo /* 1603967b494eSTejun Heo * @pwq can't be released under pool->lock, bounce to a dedicated 1604967b494eSTejun Heo * kthread_worker to avoid A-A deadlocks. 16058864b4e5STejun Heo */ 1606687a9aa5STejun Heo kthread_queue_work(pwq_release_worker, &pwq->release_work); 16078864b4e5STejun Heo } 16088864b4e5STejun Heo 1609dce90d47STejun Heo /** 1610dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1611dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1612dce90d47STejun Heo * 1613dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1614dce90d47STejun Heo */ 1615dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1616dce90d47STejun Heo { 1617dce90d47STejun Heo if (pwq) { 1618dce90d47STejun Heo /* 161924acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the 1620dce90d47STejun Heo * following lock operations are safe. 1621dce90d47STejun Heo */ 1622a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 1623dce90d47STejun Heo put_pwq(pwq); 1624a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 1625dce90d47STejun Heo } 1626dce90d47STejun Heo } 1627dce90d47STejun Heo 1628afa87ce8STejun Heo static bool pwq_is_empty(struct pool_workqueue *pwq) 1629afa87ce8STejun Heo { 1630afa87ce8STejun Heo return !pwq->nr_active && list_empty(&pwq->inactive_works); 1631afa87ce8STejun Heo } 1632afa87ce8STejun Heo 16334c638030STejun Heo static void __pwq_activate_work(struct pool_workqueue *pwq, 16344c638030STejun Heo struct work_struct *work) 1635bf4ede01STejun Heo { 16361c270b79STejun Heo unsigned long *wdb = work_data_bits(work); 16371c270b79STejun Heo 16381c270b79STejun Heo WARN_ON_ONCE(!(*wdb & WORK_STRUCT_INACTIVE)); 1639bf4ede01STejun Heo trace_workqueue_activate_work(work); 164082607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 164182607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1642112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 16431c270b79STejun Heo __clear_bit(WORK_STRUCT_INACTIVE_BIT, wdb); 16444c638030STejun Heo } 16454c638030STejun Heo 16464c638030STejun Heo /** 16474c638030STejun Heo * pwq_activate_work - Activate a work item if inactive 16484c638030STejun Heo * @pwq: pool_workqueue @work belongs to 16494c638030STejun Heo * @work: work item to activate 16504c638030STejun Heo * 16514c638030STejun Heo * Returns %true if activated. %false if already active. 16524c638030STejun Heo */ 16534c638030STejun Heo static bool pwq_activate_work(struct pool_workqueue *pwq, 16544c638030STejun Heo struct work_struct *work) 16554c638030STejun Heo { 16564c638030STejun Heo struct worker_pool *pool = pwq->pool; 165791ccc6e7STejun Heo struct wq_node_nr_active *nna; 16584c638030STejun Heo 16594c638030STejun Heo lockdep_assert_held(&pool->lock); 16604c638030STejun Heo 16614c638030STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_INACTIVE)) 16624c638030STejun Heo return false; 16634c638030STejun Heo 166491ccc6e7STejun Heo nna = wq_node_nr_active(pwq->wq, pool->node); 166591ccc6e7STejun Heo if (nna) 166691ccc6e7STejun Heo atomic_inc(&nna->nr); 166791ccc6e7STejun Heo 1668112202d9STejun Heo pwq->nr_active++; 16694c638030STejun Heo __pwq_activate_work(pwq, work); 16704c638030STejun Heo return true; 1671bf4ede01STejun Heo } 1672bf4ede01STejun Heo 16735797b1c1STejun Heo static bool tryinc_node_nr_active(struct wq_node_nr_active *nna) 16745797b1c1STejun Heo { 16755797b1c1STejun Heo int max = READ_ONCE(nna->max); 16765797b1c1STejun Heo 16775797b1c1STejun Heo while (true) { 16785797b1c1STejun Heo int old, tmp; 16795797b1c1STejun Heo 16805797b1c1STejun Heo old = atomic_read(&nna->nr); 16815797b1c1STejun Heo if (old >= max) 16825797b1c1STejun Heo return false; 16835797b1c1STejun Heo tmp = atomic_cmpxchg_relaxed(&nna->nr, old, old + 1); 16845797b1c1STejun Heo if (tmp == old) 16855797b1c1STejun Heo return true; 16865797b1c1STejun Heo } 16875797b1c1STejun Heo } 16885797b1c1STejun Heo 16891c270b79STejun Heo /** 16901c270b79STejun Heo * pwq_tryinc_nr_active - Try to increment nr_active for a pwq 16911c270b79STejun Heo * @pwq: pool_workqueue of interest 16925797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level 16931c270b79STejun Heo * 16941c270b79STejun Heo * Try to increment nr_active for @pwq. Returns %true if an nr_active count is 16951c270b79STejun Heo * successfully obtained. %false otherwise. 16961c270b79STejun Heo */ 16975797b1c1STejun Heo static bool pwq_tryinc_nr_active(struct pool_workqueue *pwq, bool fill) 16983aa62497SLai Jiangshan { 16991c270b79STejun Heo struct workqueue_struct *wq = pwq->wq; 17001c270b79STejun Heo struct worker_pool *pool = pwq->pool; 170191ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(wq, pool->node); 17025797b1c1STejun Heo bool obtained = false; 17031c270b79STejun Heo 17041c270b79STejun Heo lockdep_assert_held(&pool->lock); 17051c270b79STejun Heo 17065797b1c1STejun Heo if (!nna) { 17074cb1ef64STejun Heo /* BH or per-cpu workqueue, pwq->nr_active is sufficient */ 17081c270b79STejun Heo obtained = pwq->nr_active < READ_ONCE(wq->max_active); 17095797b1c1STejun Heo goto out; 171091ccc6e7STejun Heo } 17115797b1c1STejun Heo 17124c065dbcSWaiman Long if (unlikely(pwq->plugged)) 17134c065dbcSWaiman Long return false; 17144c065dbcSWaiman Long 17155797b1c1STejun Heo /* 17165797b1c1STejun Heo * Unbound workqueue uses per-node shared nr_active $nna. If @pwq is 17175797b1c1STejun Heo * already waiting on $nna, pwq_dec_nr_active() will maintain the 17185797b1c1STejun Heo * concurrency level. Don't jump the line. 17195797b1c1STejun Heo * 17205797b1c1STejun Heo * We need to ignore the pending test after max_active has increased as 17215797b1c1STejun Heo * pwq_dec_nr_active() can only maintain the concurrency level but not 17225797b1c1STejun Heo * increase it. This is indicated by @fill. 17235797b1c1STejun Heo */ 17245797b1c1STejun Heo if (!list_empty(&pwq->pending_node) && likely(!fill)) 17255797b1c1STejun Heo goto out; 17265797b1c1STejun Heo 17275797b1c1STejun Heo obtained = tryinc_node_nr_active(nna); 17285797b1c1STejun Heo if (obtained) 17295797b1c1STejun Heo goto out; 17305797b1c1STejun Heo 17315797b1c1STejun Heo /* 17325797b1c1STejun Heo * Lockless acquisition failed. Lock, add ourself to $nna->pending_pwqs 17335797b1c1STejun Heo * and try again. The smp_mb() is paired with the implied memory barrier 17345797b1c1STejun Heo * of atomic_dec_return() in pwq_dec_nr_active() to ensure that either 17355797b1c1STejun Heo * we see the decremented $nna->nr or they see non-empty 17365797b1c1STejun Heo * $nna->pending_pwqs. 17375797b1c1STejun Heo */ 17385797b1c1STejun Heo raw_spin_lock(&nna->lock); 17395797b1c1STejun Heo 17405797b1c1STejun Heo if (list_empty(&pwq->pending_node)) 17415797b1c1STejun Heo list_add_tail(&pwq->pending_node, &nna->pending_pwqs); 17425797b1c1STejun Heo else if (likely(!fill)) 17435797b1c1STejun Heo goto out_unlock; 17445797b1c1STejun Heo 17455797b1c1STejun Heo smp_mb(); 17465797b1c1STejun Heo 17475797b1c1STejun Heo obtained = tryinc_node_nr_active(nna); 17485797b1c1STejun Heo 17495797b1c1STejun Heo /* 17505797b1c1STejun Heo * If @fill, @pwq might have already been pending. Being spuriously 17515797b1c1STejun Heo * pending in cold paths doesn't affect anything. Let's leave it be. 17525797b1c1STejun Heo */ 17535797b1c1STejun Heo if (obtained && likely(!fill)) 17545797b1c1STejun Heo list_del_init(&pwq->pending_node); 17555797b1c1STejun Heo 17565797b1c1STejun Heo out_unlock: 17575797b1c1STejun Heo raw_spin_unlock(&nna->lock); 17585797b1c1STejun Heo out: 17595797b1c1STejun Heo if (obtained) 17605797b1c1STejun Heo pwq->nr_active++; 17611c270b79STejun Heo return obtained; 17621c270b79STejun Heo } 17631c270b79STejun Heo 17641c270b79STejun Heo /** 17651c270b79STejun Heo * pwq_activate_first_inactive - Activate the first inactive work item on a pwq 17661c270b79STejun Heo * @pwq: pool_workqueue of interest 17675797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level 17681c270b79STejun Heo * 17691c270b79STejun Heo * Activate the first inactive work item of @pwq if available and allowed by 17701c270b79STejun Heo * max_active limit. 17711c270b79STejun Heo * 17721c270b79STejun Heo * Returns %true if an inactive work item has been activated. %false if no 17731c270b79STejun Heo * inactive work item is found or max_active limit is reached. 17741c270b79STejun Heo */ 17755797b1c1STejun Heo static bool pwq_activate_first_inactive(struct pool_workqueue *pwq, bool fill) 17761c270b79STejun Heo { 17771c270b79STejun Heo struct work_struct *work = 17781c270b79STejun Heo list_first_entry_or_null(&pwq->inactive_works, 17793aa62497SLai Jiangshan struct work_struct, entry); 17803aa62497SLai Jiangshan 17815797b1c1STejun Heo if (work && pwq_tryinc_nr_active(pwq, fill)) { 17821c270b79STejun Heo __pwq_activate_work(pwq, work); 17831c270b79STejun Heo return true; 17841c270b79STejun Heo } else { 17851c270b79STejun Heo return false; 17861c270b79STejun Heo } 17871c270b79STejun Heo } 17881c270b79STejun Heo 17891c270b79STejun Heo /** 1790*516d3dc9SWaiman Long * unplug_oldest_pwq - unplug the oldest pool_workqueue 1791*516d3dc9SWaiman Long * @wq: workqueue_struct where its oldest pwq is to be unplugged 17924c065dbcSWaiman Long * 1793*516d3dc9SWaiman Long * This function should only be called for ordered workqueues where only the 1794*516d3dc9SWaiman Long * oldest pwq is unplugged, the others are plugged to suspend execution to 1795*516d3dc9SWaiman Long * ensure proper work item ordering:: 17964c065dbcSWaiman Long * 17974c065dbcSWaiman Long * dfl_pwq --------------+ [P] - plugged 17984c065dbcSWaiman Long * | 17994c065dbcSWaiman Long * v 18004c065dbcSWaiman Long * pwqs -> A -> B [P] -> C [P] (newest) 18014c065dbcSWaiman Long * | | | 18024c065dbcSWaiman Long * 1 3 5 18034c065dbcSWaiman Long * | | | 18044c065dbcSWaiman Long * 2 4 6 1805*516d3dc9SWaiman Long * 1806*516d3dc9SWaiman Long * When the oldest pwq is drained and removed, this function should be called 1807*516d3dc9SWaiman Long * to unplug the next oldest one to start its work item execution. Note that 1808*516d3dc9SWaiman Long * pwq's are linked into wq->pwqs with the oldest first, so the first one in 1809*516d3dc9SWaiman Long * the list is the oldest. 18104c065dbcSWaiman Long */ 18114c065dbcSWaiman Long static void unplug_oldest_pwq(struct workqueue_struct *wq) 18124c065dbcSWaiman Long { 18134c065dbcSWaiman Long struct pool_workqueue *pwq; 18144c065dbcSWaiman Long 18154c065dbcSWaiman Long lockdep_assert_held(&wq->mutex); 18164c065dbcSWaiman Long 18174c065dbcSWaiman Long /* Caller should make sure that pwqs isn't empty before calling */ 18184c065dbcSWaiman Long pwq = list_first_entry_or_null(&wq->pwqs, struct pool_workqueue, 18194c065dbcSWaiman Long pwqs_node); 18204c065dbcSWaiman Long raw_spin_lock_irq(&pwq->pool->lock); 18214c065dbcSWaiman Long if (pwq->plugged) { 18224c065dbcSWaiman Long pwq->plugged = false; 18234c065dbcSWaiman Long if (pwq_activate_first_inactive(pwq, true)) 18244c065dbcSWaiman Long kick_pool(pwq->pool); 18254c065dbcSWaiman Long } 18264c065dbcSWaiman Long raw_spin_unlock_irq(&pwq->pool->lock); 18274c065dbcSWaiman Long } 18284c065dbcSWaiman Long 18294c065dbcSWaiman Long /** 18305797b1c1STejun Heo * node_activate_pending_pwq - Activate a pending pwq on a wq_node_nr_active 18315797b1c1STejun Heo * @nna: wq_node_nr_active to activate a pending pwq for 18325797b1c1STejun Heo * @caller_pool: worker_pool the caller is locking 18335797b1c1STejun Heo * 18345797b1c1STejun Heo * Activate a pwq in @nna->pending_pwqs. Called with @caller_pool locked. 18355797b1c1STejun Heo * @caller_pool may be unlocked and relocked to lock other worker_pools. 18365797b1c1STejun Heo */ 18375797b1c1STejun Heo static void node_activate_pending_pwq(struct wq_node_nr_active *nna, 18385797b1c1STejun Heo struct worker_pool *caller_pool) 18395797b1c1STejun Heo { 18405797b1c1STejun Heo struct worker_pool *locked_pool = caller_pool; 18415797b1c1STejun Heo struct pool_workqueue *pwq; 18425797b1c1STejun Heo struct work_struct *work; 18435797b1c1STejun Heo 18445797b1c1STejun Heo lockdep_assert_held(&caller_pool->lock); 18455797b1c1STejun Heo 18465797b1c1STejun Heo raw_spin_lock(&nna->lock); 18475797b1c1STejun Heo retry: 18485797b1c1STejun Heo pwq = list_first_entry_or_null(&nna->pending_pwqs, 18495797b1c1STejun Heo struct pool_workqueue, pending_node); 18505797b1c1STejun Heo if (!pwq) 18515797b1c1STejun Heo goto out_unlock; 18525797b1c1STejun Heo 18535797b1c1STejun Heo /* 18545797b1c1STejun Heo * If @pwq is for a different pool than @locked_pool, we need to lock 18555797b1c1STejun Heo * @pwq->pool->lock. Let's trylock first. If unsuccessful, do the unlock 18565797b1c1STejun Heo * / lock dance. For that, we also need to release @nna->lock as it's 18575797b1c1STejun Heo * nested inside pool locks. 18585797b1c1STejun Heo */ 18595797b1c1STejun Heo if (pwq->pool != locked_pool) { 18605797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock); 18615797b1c1STejun Heo locked_pool = pwq->pool; 18625797b1c1STejun Heo if (!raw_spin_trylock(&locked_pool->lock)) { 18635797b1c1STejun Heo raw_spin_unlock(&nna->lock); 18645797b1c1STejun Heo raw_spin_lock(&locked_pool->lock); 18655797b1c1STejun Heo raw_spin_lock(&nna->lock); 18665797b1c1STejun Heo goto retry; 18675797b1c1STejun Heo } 18685797b1c1STejun Heo } 18695797b1c1STejun Heo 18705797b1c1STejun Heo /* 18715797b1c1STejun Heo * $pwq may not have any inactive work items due to e.g. cancellations. 18725797b1c1STejun Heo * Drop it from pending_pwqs and see if there's another one. 18735797b1c1STejun Heo */ 18745797b1c1STejun Heo work = list_first_entry_or_null(&pwq->inactive_works, 18755797b1c1STejun Heo struct work_struct, entry); 18765797b1c1STejun Heo if (!work) { 18775797b1c1STejun Heo list_del_init(&pwq->pending_node); 18785797b1c1STejun Heo goto retry; 18795797b1c1STejun Heo } 18805797b1c1STejun Heo 18815797b1c1STejun Heo /* 18825797b1c1STejun Heo * Acquire an nr_active count and activate the inactive work item. If 18835797b1c1STejun Heo * $pwq still has inactive work items, rotate it to the end of the 18845797b1c1STejun Heo * pending_pwqs so that we round-robin through them. This means that 18855797b1c1STejun Heo * inactive work items are not activated in queueing order which is fine 18865797b1c1STejun Heo * given that there has never been any ordering across different pwqs. 18875797b1c1STejun Heo */ 18885797b1c1STejun Heo if (likely(tryinc_node_nr_active(nna))) { 18895797b1c1STejun Heo pwq->nr_active++; 18905797b1c1STejun Heo __pwq_activate_work(pwq, work); 18915797b1c1STejun Heo 18925797b1c1STejun Heo if (list_empty(&pwq->inactive_works)) 18935797b1c1STejun Heo list_del_init(&pwq->pending_node); 18945797b1c1STejun Heo else 18955797b1c1STejun Heo list_move_tail(&pwq->pending_node, &nna->pending_pwqs); 18965797b1c1STejun Heo 18975797b1c1STejun Heo /* if activating a foreign pool, make sure it's running */ 18985797b1c1STejun Heo if (pwq->pool != caller_pool) 18995797b1c1STejun Heo kick_pool(pwq->pool); 19005797b1c1STejun Heo } 19015797b1c1STejun Heo 19025797b1c1STejun Heo out_unlock: 19035797b1c1STejun Heo raw_spin_unlock(&nna->lock); 19045797b1c1STejun Heo if (locked_pool != caller_pool) { 19055797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock); 19065797b1c1STejun Heo raw_spin_lock(&caller_pool->lock); 19075797b1c1STejun Heo } 19085797b1c1STejun Heo } 19095797b1c1STejun Heo 19105797b1c1STejun Heo /** 19111c270b79STejun Heo * pwq_dec_nr_active - Retire an active count 19121c270b79STejun Heo * @pwq: pool_workqueue of interest 19131c270b79STejun Heo * 19141c270b79STejun Heo * Decrement @pwq's nr_active and try to activate the first inactive work item. 19155797b1c1STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock. 19161c270b79STejun Heo */ 19171c270b79STejun Heo static void pwq_dec_nr_active(struct pool_workqueue *pwq) 19181c270b79STejun Heo { 19191c270b79STejun Heo struct worker_pool *pool = pwq->pool; 192091ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(pwq->wq, pool->node); 19211c270b79STejun Heo 19221c270b79STejun Heo lockdep_assert_held(&pool->lock); 19231c270b79STejun Heo 192491ccc6e7STejun Heo /* 192591ccc6e7STejun Heo * @pwq->nr_active should be decremented for both percpu and unbound 192691ccc6e7STejun Heo * workqueues. 192791ccc6e7STejun Heo */ 19281c270b79STejun Heo pwq->nr_active--; 192991ccc6e7STejun Heo 193091ccc6e7STejun Heo /* 193191ccc6e7STejun Heo * For a percpu workqueue, it's simple. Just need to kick the first 193291ccc6e7STejun Heo * inactive work item on @pwq itself. 193391ccc6e7STejun Heo */ 193491ccc6e7STejun Heo if (!nna) { 19355797b1c1STejun Heo pwq_activate_first_inactive(pwq, false); 193691ccc6e7STejun Heo return; 193791ccc6e7STejun Heo } 193891ccc6e7STejun Heo 19395797b1c1STejun Heo /* 19405797b1c1STejun Heo * If @pwq is for an unbound workqueue, it's more complicated because 19415797b1c1STejun Heo * multiple pwqs and pools may be sharing the nr_active count. When a 19425797b1c1STejun Heo * pwq needs to wait for an nr_active count, it puts itself on 19435797b1c1STejun Heo * $nna->pending_pwqs. The following atomic_dec_return()'s implied 19445797b1c1STejun Heo * memory barrier is paired with smp_mb() in pwq_tryinc_nr_active() to 19455797b1c1STejun Heo * guarantee that either we see non-empty pending_pwqs or they see 19465797b1c1STejun Heo * decremented $nna->nr. 19475797b1c1STejun Heo * 19485797b1c1STejun Heo * $nna->max may change as CPUs come online/offline and @pwq->wq's 19495797b1c1STejun Heo * max_active gets updated. However, it is guaranteed to be equal to or 19505797b1c1STejun Heo * larger than @pwq->wq->min_active which is above zero unless freezing. 19515797b1c1STejun Heo * This maintains the forward progress guarantee. 19525797b1c1STejun Heo */ 19535797b1c1STejun Heo if (atomic_dec_return(&nna->nr) >= READ_ONCE(nna->max)) 19545797b1c1STejun Heo return; 19555797b1c1STejun Heo 19565797b1c1STejun Heo if (!list_empty(&nna->pending_pwqs)) 19575797b1c1STejun Heo node_activate_pending_pwq(nna, pool); 19583aa62497SLai Jiangshan } 19593aa62497SLai Jiangshan 1960bf4ede01STejun Heo /** 1961112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1962112202d9STejun Heo * @pwq: pwq of interest 1963c4560c2cSLai Jiangshan * @work_data: work_data of work which left the queue 1964bf4ede01STejun Heo * 1965bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1966112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1967bf4ede01STejun Heo * 1968dd6c3c54STejun Heo * NOTE: 1969dd6c3c54STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock 1970dd6c3c54STejun Heo * and thus should be called after all other state updates for the in-flight 1971dd6c3c54STejun Heo * work item is complete. 1972dd6c3c54STejun Heo * 1973bf4ede01STejun Heo * CONTEXT: 1974a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1975bf4ede01STejun Heo */ 1976c4560c2cSLai Jiangshan static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) 1977bf4ede01STejun Heo { 1978c4560c2cSLai Jiangshan int color = get_work_color(work_data); 1979c4560c2cSLai Jiangshan 19801c270b79STejun Heo if (!(work_data & WORK_STRUCT_INACTIVE)) 19811c270b79STejun Heo pwq_dec_nr_active(pwq); 1982018f3a13SLai Jiangshan 1983018f3a13SLai Jiangshan pwq->nr_in_flight[color]--; 1984bf4ede01STejun Heo 1985bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 1986112202d9STejun Heo if (likely(pwq->flush_color != color)) 19878864b4e5STejun Heo goto out_put; 1988bf4ede01STejun Heo 1989bf4ede01STejun Heo /* are there still in-flight works? */ 1990112202d9STejun Heo if (pwq->nr_in_flight[color]) 19918864b4e5STejun Heo goto out_put; 1992bf4ede01STejun Heo 1993112202d9STejun Heo /* this pwq is done, clear flush_color */ 1994112202d9STejun Heo pwq->flush_color = -1; 1995bf4ede01STejun Heo 1996bf4ede01STejun Heo /* 1997112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 1998bf4ede01STejun Heo * will handle the rest. 1999bf4ede01STejun Heo */ 2000112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 2001112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 20028864b4e5STejun Heo out_put: 20038864b4e5STejun Heo put_pwq(pwq); 2004bf4ede01STejun Heo } 2005bf4ede01STejun Heo 200636e227d2STejun Heo /** 2007bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 200836e227d2STejun Heo * @work: work item to steal 200936e227d2STejun Heo * @is_dwork: @work is a delayed_work 2010bbb68dfaSTejun Heo * @flags: place to store irq state 201136e227d2STejun Heo * 201236e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 2013d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 201436e227d2STejun Heo * 2015d185af30SYacine Belkadi * Return: 20163eb6b31bSMauro Carvalho Chehab * 20173eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 201836e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 201936e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 202036e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 2021bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 2022bbb68dfaSTejun Heo * for arbitrarily long 20233eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 202436e227d2STejun Heo * 2025d185af30SYacine Belkadi * Note: 2026bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 2027e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 2028e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 2029e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 2030bbb68dfaSTejun Heo * 2031bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 2032bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 2033bbb68dfaSTejun Heo * 2034e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 2035bf4ede01STejun Heo */ 2036bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 2037bbb68dfaSTejun Heo unsigned long *flags) 2038bf4ede01STejun Heo { 2039d565ed63STejun Heo struct worker_pool *pool; 2040112202d9STejun Heo struct pool_workqueue *pwq; 2041bf4ede01STejun Heo 2042bbb68dfaSTejun Heo local_irq_save(*flags); 2043bbb68dfaSTejun Heo 204436e227d2STejun Heo /* try to steal the timer if it exists */ 204536e227d2STejun Heo if (is_dwork) { 204636e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 204736e227d2STejun Heo 2048e0aecdd8STejun Heo /* 2049e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 2050e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 2051e0aecdd8STejun Heo * running on the local CPU. 2052e0aecdd8STejun Heo */ 205336e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 205436e227d2STejun Heo return 1; 205536e227d2STejun Heo } 205636e227d2STejun Heo 205736e227d2STejun Heo /* try to claim PENDING the normal way */ 2058bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 2059bf4ede01STejun Heo return 0; 2060bf4ede01STejun Heo 206124acfb71SThomas Gleixner rcu_read_lock(); 2062bf4ede01STejun Heo /* 2063bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 2064bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 2065bf4ede01STejun Heo */ 2066d565ed63STejun Heo pool = get_work_pool(work); 2067d565ed63STejun Heo if (!pool) 2068bbb68dfaSTejun Heo goto fail; 2069bf4ede01STejun Heo 2070a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pool->lock); 2071bf4ede01STejun Heo /* 2072112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 2073112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 2074112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 2075112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 2076112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 20770b3dae68SLai Jiangshan * item is currently queued on that pool. 2078bf4ede01STejun Heo */ 2079112202d9STejun Heo pwq = get_work_pwq(work); 2080112202d9STejun Heo if (pwq && pwq->pool == pool) { 2081c70e1779STejun Heo unsigned long work_data; 2082c70e1779STejun Heo 2083bf4ede01STejun Heo debug_work_deactivate(work); 20843aa62497SLai Jiangshan 20853aa62497SLai Jiangshan /* 2086018f3a13SLai Jiangshan * A cancelable inactive work item must be in the 2087018f3a13SLai Jiangshan * pwq->inactive_works since a queued barrier can't be 2088018f3a13SLai Jiangshan * canceled (see the comments in insert_wq_barrier()). 2089018f3a13SLai Jiangshan * 2090f97a4a1aSLai Jiangshan * An inactive work item cannot be grabbed directly because 2091d812796eSLai Jiangshan * it might have linked barrier work items which, if left 2092f97a4a1aSLai Jiangshan * on the inactive_works list, will confuse pwq->nr_active 209316062836STejun Heo * management later on and cause stall. Make sure the work 209416062836STejun Heo * item is activated before grabbing. 20953aa62497SLai Jiangshan */ 20964c638030STejun Heo pwq_activate_work(pwq, work); 20973aa62497SLai Jiangshan 2098bf4ede01STejun Heo list_del_init(&work->entry); 209936e227d2STejun Heo 2100c70e1779STejun Heo /* 2101c70e1779STejun Heo * work->data points to pwq iff queued. Let's point to pool. As 2102c70e1779STejun Heo * this destroys work->data needed by the next step, stash it. 2103c70e1779STejun Heo */ 2104c70e1779STejun Heo work_data = *work_data_bits(work); 21054468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 21064468a00fSLai Jiangshan 2107dd6c3c54STejun Heo /* must be the last step, see the function comment */ 2108c70e1779STejun Heo pwq_dec_nr_in_flight(pwq, work_data); 2109dd6c3c54STejun Heo 2110a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 211124acfb71SThomas Gleixner rcu_read_unlock(); 211236e227d2STejun Heo return 1; 2113bf4ede01STejun Heo } 2114a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 2115bbb68dfaSTejun Heo fail: 211624acfb71SThomas Gleixner rcu_read_unlock(); 2117bbb68dfaSTejun Heo local_irq_restore(*flags); 2118bbb68dfaSTejun Heo if (work_is_canceling(work)) 2119bbb68dfaSTejun Heo return -ENOENT; 2120bbb68dfaSTejun Heo cpu_relax(); 212136e227d2STejun Heo return -EAGAIN; 2122bf4ede01STejun Heo } 2123bf4ede01STejun Heo 2124bf4ede01STejun Heo /** 2125706026c2STejun Heo * insert_work - insert a work into a pool 2126112202d9STejun Heo * @pwq: pwq @work belongs to 21274690c4abSTejun Heo * @work: work to insert 21284690c4abSTejun Heo * @head: insertion point 21294690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 21304690c4abSTejun Heo * 2131112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 2132706026c2STejun Heo * work_struct flags. 21334690c4abSTejun Heo * 21344690c4abSTejun Heo * CONTEXT: 2135a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2136365970a1SDavid Howells */ 2137112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 2138112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 2139b89deed3SOleg Nesterov { 2140fe089f87STejun Heo debug_work_activate(work); 2141e1d8aa9fSFrederic Weisbecker 2142e89a85d6SWalter Wu /* record the work call stack in order to print it in KASAN reports */ 2143f70da745SMarco Elver kasan_record_aux_stack_noalloc(work); 2144e89a85d6SWalter Wu 21454690c4abSTejun Heo /* we own @work, set data and link */ 2146112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 21471a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 21488864b4e5STejun Heo get_pwq(pwq); 2149b89deed3SOleg Nesterov } 2150b89deed3SOleg Nesterov 2151c8efcc25STejun Heo /* 2152c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 21538d03ecfeSTejun Heo * same workqueue. 2154c8efcc25STejun Heo */ 2155c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 2156c8efcc25STejun Heo { 2157c8efcc25STejun Heo struct worker *worker; 2158c8efcc25STejun Heo 21598d03ecfeSTejun Heo worker = current_wq_worker(); 2160c8efcc25STejun Heo /* 2161bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If 21628d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 2163c8efcc25STejun Heo */ 2164112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 2165c8efcc25STejun Heo } 2166c8efcc25STejun Heo 2167ef557180SMike Galbraith /* 2168ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 2169ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 2170ef557180SMike Galbraith * avoid perturbing sensitive tasks. 2171ef557180SMike Galbraith */ 2172ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 2173ef557180SMike Galbraith { 2174ef557180SMike Galbraith int new_cpu; 2175ef557180SMike Galbraith 2176f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 2177ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 2178ef557180SMike Galbraith return cpu; 2179a8ec5880SAmmar Faizi } else { 2180a8ec5880SAmmar Faizi pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); 2181f303fccbSTejun Heo } 2182f303fccbSTejun Heo 2183ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 2184ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 2185ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 2186ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 2187ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 2188ef557180SMike Galbraith return cpu; 2189ef557180SMike Galbraith } 2190ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 2191ef557180SMike Galbraith 2192ef557180SMike Galbraith return new_cpu; 2193ef557180SMike Galbraith } 2194ef557180SMike Galbraith 2195d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 21961da177e4SLinus Torvalds struct work_struct *work) 21971da177e4SLinus Torvalds { 2198112202d9STejun Heo struct pool_workqueue *pwq; 2199fe089f87STejun Heo struct worker_pool *last_pool, *pool; 22008a2e8e5dSTejun Heo unsigned int work_flags; 2201b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 22028930cabaSTejun Heo 22038930cabaSTejun Heo /* 22048930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 22058930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 22068930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 22078930cabaSTejun Heo * happen with IRQ disabled. 22088930cabaSTejun Heo */ 22098e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 22101da177e4SLinus Torvalds 22111e19ffc6STejun Heo 221233e3f0a3SRichard Clark /* 221333e3f0a3SRichard Clark * For a draining wq, only works from the same workqueue are 221433e3f0a3SRichard Clark * allowed. The __WQ_DESTROYING helps to spot the issue that 221533e3f0a3SRichard Clark * queues a new work item to a wq after destroy_workqueue(wq). 221633e3f0a3SRichard Clark */ 221733e3f0a3SRichard Clark if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && 221833e3f0a3SRichard Clark WARN_ON_ONCE(!is_chained_work(wq)))) 2219e41e704bSTejun Heo return; 222024acfb71SThomas Gleixner rcu_read_lock(); 22219e8cd2f5STejun Heo retry: 2222aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */ 2223636b927eSTejun Heo if (req_cpu == WORK_CPU_UNBOUND) { 2224636b927eSTejun Heo if (wq->flags & WQ_UNBOUND) 2225ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 2226636b927eSTejun Heo else 2227aa202f1fSHillf Danton cpu = raw_smp_processor_id(); 2228aa202f1fSHillf Danton } 2229f3421797STejun Heo 2230636b927eSTejun Heo pwq = rcu_dereference(*per_cpu_ptr(wq->cpu_pwq, cpu)); 2231fe089f87STejun Heo pool = pwq->pool; 2232fe089f87STejun Heo 223318aa9effSTejun Heo /* 2234c9178087STejun Heo * If @work was previously on a different pool, it might still be 2235c9178087STejun Heo * running there, in which case the work needs to be queued on that 2236c9178087STejun Heo * pool to guarantee non-reentrancy. 223718aa9effSTejun Heo */ 2238c9e7cf27STejun Heo last_pool = get_work_pool(work); 2239fe089f87STejun Heo if (last_pool && last_pool != pool) { 224018aa9effSTejun Heo struct worker *worker; 224118aa9effSTejun Heo 2242a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&last_pool->lock); 224318aa9effSTejun Heo 2244c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 224518aa9effSTejun Heo 2246112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 2247c9178087STejun Heo pwq = worker->current_pwq; 2248fe089f87STejun Heo pool = pwq->pool; 2249fe089f87STejun Heo WARN_ON_ONCE(pool != last_pool); 22508594fadeSLai Jiangshan } else { 225118aa9effSTejun Heo /* meh... not running there, queue here */ 2252a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&last_pool->lock); 2253fe089f87STejun Heo raw_spin_lock(&pool->lock); 225418aa9effSTejun Heo } 22558930cabaSTejun Heo } else { 2256fe089f87STejun Heo raw_spin_lock(&pool->lock); 22578930cabaSTejun Heo } 2258502ca9d8STejun Heo 22599e8cd2f5STejun Heo /* 2260636b927eSTejun Heo * pwq is determined and locked. For unbound pools, we could have raced 2261636b927eSTejun Heo * with pwq release and it could already be dead. If its refcnt is zero, 2262636b927eSTejun Heo * repeat pwq selection. Note that unbound pwqs never die without 2263636b927eSTejun Heo * another pwq replacing it in cpu_pwq or while work items are executing 2264636b927eSTejun Heo * on it, so the retrying is guaranteed to make forward-progress. 22659e8cd2f5STejun Heo */ 22669e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 22679e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 2268fe089f87STejun Heo raw_spin_unlock(&pool->lock); 22699e8cd2f5STejun Heo cpu_relax(); 22709e8cd2f5STejun Heo goto retry; 22719e8cd2f5STejun Heo } 22729e8cd2f5STejun Heo /* oops */ 22739e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 22749e8cd2f5STejun Heo wq->name, cpu); 22759e8cd2f5STejun Heo } 22769e8cd2f5STejun Heo 2277112202d9STejun Heo /* pwq determined, queue */ 2278112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 2279502ca9d8STejun Heo 228024acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry))) 228124acfb71SThomas Gleixner goto out; 22821e19ffc6STejun Heo 2283112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 2284112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 22851e19ffc6STejun Heo 2286a045a272STejun Heo /* 2287a045a272STejun Heo * Limit the number of concurrently active work items to max_active. 2288a045a272STejun Heo * @work must also queue behind existing inactive work items to maintain 2289a045a272STejun Heo * ordering when max_active changes. See wq_adjust_max_active(). 2290a045a272STejun Heo */ 22915797b1c1STejun Heo if (list_empty(&pwq->inactive_works) && pwq_tryinc_nr_active(pwq, false)) { 2292fe089f87STejun Heo if (list_empty(&pool->worklist)) 2293fe089f87STejun Heo pool->watchdog_ts = jiffies; 2294fe089f87STejun Heo 2295cdadf009STejun Heo trace_workqueue_activate_work(work); 2296fe089f87STejun Heo insert_work(pwq, work, &pool->worklist, work_flags); 22970219a352STejun Heo kick_pool(pool); 22988a2e8e5dSTejun Heo } else { 2299f97a4a1aSLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 2300fe089f87STejun Heo insert_work(pwq, work, &pwq->inactive_works, work_flags); 23018a2e8e5dSTejun Heo } 23021e19ffc6STejun Heo 230324acfb71SThomas Gleixner out: 2304fe089f87STejun Heo raw_spin_unlock(&pool->lock); 230524acfb71SThomas Gleixner rcu_read_unlock(); 23061da177e4SLinus Torvalds } 23071da177e4SLinus Torvalds 23080fcb78c2SRolf Eike Beer /** 2309c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 2310c1a220e7SZhang Rui * @cpu: CPU number to execute work on 2311c1a220e7SZhang Rui * @wq: workqueue to use 2312c1a220e7SZhang Rui * @work: work to queue 2313c1a220e7SZhang Rui * 2314c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 2315443378f0SPaul E. McKenney * can't go away. Callers that fail to ensure that the specified 2316443378f0SPaul E. McKenney * CPU cannot go away will execute on a randomly chosen CPU. 2317854f5cc5SPaul E. McKenney * But note well that callers specifying a CPU that never has been 2318854f5cc5SPaul E. McKenney * online will get a splat. 2319d185af30SYacine Belkadi * 2320d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 2321c1a220e7SZhang Rui */ 2322d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 2323d4283e93STejun Heo struct work_struct *work) 2324c1a220e7SZhang Rui { 2325d4283e93STejun Heo bool ret = false; 23268930cabaSTejun Heo unsigned long flags; 23278930cabaSTejun Heo 23288930cabaSTejun Heo local_irq_save(flags); 2329c1a220e7SZhang Rui 233022df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 23314690c4abSTejun Heo __queue_work(cpu, wq, work); 2332d4283e93STejun Heo ret = true; 2333c1a220e7SZhang Rui } 23348930cabaSTejun Heo 23358930cabaSTejun Heo local_irq_restore(flags); 2336c1a220e7SZhang Rui return ret; 2337c1a220e7SZhang Rui } 2338ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 2339c1a220e7SZhang Rui 23408204e0c1SAlexander Duyck /** 2341fef59c9cSTejun Heo * select_numa_node_cpu - Select a CPU based on NUMA node 23428204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from 23438204e0c1SAlexander Duyck * 23448204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given 23458204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return 23468204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any 23478204e0c1SAlexander Duyck * available CPU if we need to schedule this work. 23488204e0c1SAlexander Duyck */ 2349fef59c9cSTejun Heo static int select_numa_node_cpu(int node) 23508204e0c1SAlexander Duyck { 23518204e0c1SAlexander Duyck int cpu; 23528204e0c1SAlexander Duyck 23538204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */ 23548204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) 23558204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 23568204e0c1SAlexander Duyck 23578204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */ 23588204e0c1SAlexander Duyck cpu = raw_smp_processor_id(); 23598204e0c1SAlexander Duyck if (node == cpu_to_node(cpu)) 23608204e0c1SAlexander Duyck return cpu; 23618204e0c1SAlexander Duyck 23628204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */ 23638204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 23648204e0c1SAlexander Duyck 23658204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */ 23668204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; 23678204e0c1SAlexander Duyck } 23688204e0c1SAlexander Duyck 23698204e0c1SAlexander Duyck /** 23708204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node 23718204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for 23728204e0c1SAlexander Duyck * @wq: workqueue to use 23738204e0c1SAlexander Duyck * @work: work to queue 23748204e0c1SAlexander Duyck * 23758204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic 23768204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given 23778204e0c1SAlexander Duyck * NUMA node. 23788204e0c1SAlexander Duyck * 23798204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto 23808204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is 23818204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior. 23828204e0c1SAlexander Duyck * 23838204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the 23848204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we 23858204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU. 23868204e0c1SAlexander Duyck * 23878204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise. 23888204e0c1SAlexander Duyck */ 23898204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq, 23908204e0c1SAlexander Duyck struct work_struct *work) 23918204e0c1SAlexander Duyck { 23928204e0c1SAlexander Duyck unsigned long flags; 23938204e0c1SAlexander Duyck bool ret = false; 23948204e0c1SAlexander Duyck 23958204e0c1SAlexander Duyck /* 23968204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues. 23978204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given 23988204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node. 23998204e0c1SAlexander Duyck * 24008204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in 24018204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for 24028204e0c1SAlexander Duyck * some round robin type logic. 24038204e0c1SAlexander Duyck */ 24048204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 24058204e0c1SAlexander Duyck 24068204e0c1SAlexander Duyck local_irq_save(flags); 24078204e0c1SAlexander Duyck 24088204e0c1SAlexander Duyck if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 2409fef59c9cSTejun Heo int cpu = select_numa_node_cpu(node); 24108204e0c1SAlexander Duyck 24118204e0c1SAlexander Duyck __queue_work(cpu, wq, work); 24128204e0c1SAlexander Duyck ret = true; 24138204e0c1SAlexander Duyck } 24148204e0c1SAlexander Duyck 24158204e0c1SAlexander Duyck local_irq_restore(flags); 24168204e0c1SAlexander Duyck return ret; 24178204e0c1SAlexander Duyck } 24188204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node); 24198204e0c1SAlexander Duyck 24208c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 24211da177e4SLinus Torvalds { 24228c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 24231da177e4SLinus Torvalds 2424e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 242560c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 24261da177e4SLinus Torvalds } 24271438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 24281da177e4SLinus Torvalds 24297beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 243052bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 24311da177e4SLinus Torvalds { 24327beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 24337beb2edfSTejun Heo struct work_struct *work = &dwork->work; 24341da177e4SLinus Torvalds 2435637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 24364b243563SSami Tolvanen WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 2437fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 2438fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 24397beb2edfSTejun Heo 24408852aac2STejun Heo /* 24418852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 24428852aac2STejun Heo * both optimization and correctness. The earliest @timer can 24438852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 24448852aac2STejun Heo * on that there's no such delay when @delay is 0. 24458852aac2STejun Heo */ 24468852aac2STejun Heo if (!delay) { 24478852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 24488852aac2STejun Heo return; 24498852aac2STejun Heo } 24508852aac2STejun Heo 245160c057bcSLai Jiangshan dwork->wq = wq; 24521265057fSTejun Heo dwork->cpu = cpu; 24537beb2edfSTejun Heo timer->expires = jiffies + delay; 24547beb2edfSTejun Heo 2455aae17ebbSLeonardo Bras if (housekeeping_enabled(HK_TYPE_TIMER)) { 2456aae17ebbSLeonardo Bras /* If the current cpu is a housekeeping cpu, use it. */ 2457aae17ebbSLeonardo Bras cpu = smp_processor_id(); 2458aae17ebbSLeonardo Bras if (!housekeeping_test_cpu(cpu, HK_TYPE_TIMER)) 2459aae17ebbSLeonardo Bras cpu = housekeeping_any_cpu(HK_TYPE_TIMER); 24607beb2edfSTejun Heo add_timer_on(timer, cpu); 2461aae17ebbSLeonardo Bras } else { 2462aae17ebbSLeonardo Bras if (likely(cpu == WORK_CPU_UNBOUND)) 2463041bd12eSTejun Heo add_timer(timer); 2464aae17ebbSLeonardo Bras else 2465aae17ebbSLeonardo Bras add_timer_on(timer, cpu); 2466aae17ebbSLeonardo Bras } 24677beb2edfSTejun Heo } 24681da177e4SLinus Torvalds 24690fcb78c2SRolf Eike Beer /** 24700fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 24710fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 24720fcb78c2SRolf Eike Beer * @wq: workqueue to use 2473af9997e4SRandy Dunlap * @dwork: work to queue 24740fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 24750fcb78c2SRolf Eike Beer * 2476d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 2477715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 2478715f1300STejun Heo * execution. 24790fcb78c2SRolf Eike Beer */ 2480d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 248152bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 24827a6bc1cdSVenkatesh Pallipadi { 248352bad64dSDavid Howells struct work_struct *work = &dwork->work; 2484d4283e93STejun Heo bool ret = false; 24858930cabaSTejun Heo unsigned long flags; 24868930cabaSTejun Heo 24878930cabaSTejun Heo /* read the comment in __queue_work() */ 24888930cabaSTejun Heo local_irq_save(flags); 24897a6bc1cdSVenkatesh Pallipadi 249022df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 24917beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 2492d4283e93STejun Heo ret = true; 24937a6bc1cdSVenkatesh Pallipadi } 24948930cabaSTejun Heo 24958930cabaSTejun Heo local_irq_restore(flags); 24967a6bc1cdSVenkatesh Pallipadi return ret; 24977a6bc1cdSVenkatesh Pallipadi } 2498ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 24991da177e4SLinus Torvalds 2500c8e55f36STejun Heo /** 25018376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 25028376fe22STejun Heo * @cpu: CPU number to execute work on 25038376fe22STejun Heo * @wq: workqueue to use 25048376fe22STejun Heo * @dwork: work to queue 25058376fe22STejun Heo * @delay: number of jiffies to wait before queueing 25068376fe22STejun Heo * 25078376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 25088376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 25098376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 25108376fe22STejun Heo * current state. 25118376fe22STejun Heo * 2512d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 25138376fe22STejun Heo * pending and its timer was modified. 25148376fe22STejun Heo * 2515e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 25168376fe22STejun Heo * See try_to_grab_pending() for details. 25178376fe22STejun Heo */ 25188376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 25198376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 25208376fe22STejun Heo { 25218376fe22STejun Heo unsigned long flags; 25228376fe22STejun Heo int ret; 25238376fe22STejun Heo 25248376fe22STejun Heo do { 25258376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 25268376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 25278376fe22STejun Heo 25288376fe22STejun Heo if (likely(ret >= 0)) { 25298376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 25308376fe22STejun Heo local_irq_restore(flags); 25318376fe22STejun Heo } 25328376fe22STejun Heo 25338376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 25348376fe22STejun Heo return ret; 25358376fe22STejun Heo } 25368376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 25378376fe22STejun Heo 253805f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 253905f0fe6bSTejun Heo { 254005f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 254105f0fe6bSTejun Heo 254205f0fe6bSTejun Heo /* read the comment in __queue_work() */ 254305f0fe6bSTejun Heo local_irq_disable(); 254405f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 254505f0fe6bSTejun Heo local_irq_enable(); 254605f0fe6bSTejun Heo } 254705f0fe6bSTejun Heo 254805f0fe6bSTejun Heo /** 254905f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 255005f0fe6bSTejun Heo * @wq: workqueue to use 255105f0fe6bSTejun Heo * @rwork: work to queue 255205f0fe6bSTejun Heo * 255305f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 255405f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 2555bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the 255605f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 255705f0fe6bSTejun Heo */ 255805f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 255905f0fe6bSTejun Heo { 256005f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 256105f0fe6bSTejun Heo 256205f0fe6bSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 256305f0fe6bSTejun Heo rwork->wq = wq; 2564a7e30c0eSUladzislau Rezki call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); 256505f0fe6bSTejun Heo return true; 256605f0fe6bSTejun Heo } 256705f0fe6bSTejun Heo 256805f0fe6bSTejun Heo return false; 256905f0fe6bSTejun Heo } 257005f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 257105f0fe6bSTejun Heo 2572f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 2573c34056a3STejun Heo { 2574c34056a3STejun Heo struct worker *worker; 2575c34056a3STejun Heo 2576f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 2577c8e55f36STejun Heo if (worker) { 2578c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 2579affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 2580da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 2581e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 2582e22bee78STejun Heo worker->flags = WORKER_PREP; 2583c8e55f36STejun Heo } 2584c34056a3STejun Heo return worker; 2585c34056a3STejun Heo } 2586c34056a3STejun Heo 25879546b29eSTejun Heo static cpumask_t *pool_allowed_cpus(struct worker_pool *pool) 25889546b29eSTejun Heo { 25898639ecebSTejun Heo if (pool->cpu < 0 && pool->attrs->affn_strict) 25909546b29eSTejun Heo return pool->attrs->__pod_cpumask; 25918639ecebSTejun Heo else 25928639ecebSTejun Heo return pool->attrs->cpumask; 25939546b29eSTejun Heo } 25949546b29eSTejun Heo 2595c34056a3STejun Heo /** 25964736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 25974736cbf7SLai Jiangshan * @worker: worker to be attached 25984736cbf7SLai Jiangshan * @pool: the target pool 25994736cbf7SLai Jiangshan * 26004736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 26014736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 26024736cbf7SLai Jiangshan * cpu-[un]hotplugs. 26034736cbf7SLai Jiangshan */ 26044736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 26054736cbf7SLai Jiangshan struct worker_pool *pool) 26064736cbf7SLai Jiangshan { 26071258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 26084736cbf7SLai Jiangshan 26094736cbf7SLai Jiangshan /* 26104cb1ef64STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains stable 26114cb1ef64STejun Heo * across this function. See the comments above the flag definition for 26124cb1ef64STejun Heo * details. BH workers are, while per-CPU, always DISASSOCIATED. 26134736cbf7SLai Jiangshan */ 26144cb1ef64STejun Heo if (pool->flags & POOL_DISASSOCIATED) { 26154736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 26164cb1ef64STejun Heo } else { 26174cb1ef64STejun Heo WARN_ON_ONCE(pool->flags & POOL_BH); 26185c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu); 26194cb1ef64STejun Heo } 26204736cbf7SLai Jiangshan 2621640f17c8SPeter Zijlstra if (worker->rescue_wq) 26229546b29eSTejun Heo set_cpus_allowed_ptr(worker->task, pool_allowed_cpus(pool)); 2623640f17c8SPeter Zijlstra 26244736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 2625a2d812a2STejun Heo worker->pool = pool; 26264736cbf7SLai Jiangshan 26271258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 26284736cbf7SLai Jiangshan } 26294736cbf7SLai Jiangshan 26304736cbf7SLai Jiangshan /** 263160f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 263260f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 263360f5a4bcSLai Jiangshan * 26344736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 26354736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 26364736cbf7SLai Jiangshan * other reference to the pool. 263760f5a4bcSLai Jiangshan */ 2638a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 263960f5a4bcSLai Jiangshan { 2640a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 264160f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 264260f5a4bcSLai Jiangshan 26434cb1ef64STejun Heo /* there is one permanent BH worker per CPU which should never detach */ 26444cb1ef64STejun Heo WARN_ON_ONCE(pool->flags & POOL_BH); 26454cb1ef64STejun Heo 26461258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 2647a2d812a2STejun Heo 26485c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, -1); 2649da028469SLai Jiangshan list_del(&worker->node); 2650a2d812a2STejun Heo worker->pool = NULL; 2651a2d812a2STejun Heo 2652e02b9312SValentin Schneider if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) 265360f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 26541258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 265560f5a4bcSLai Jiangshan 2656b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 2657b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 2658b62c0751SLai Jiangshan 265960f5a4bcSLai Jiangshan if (detach_completion) 266060f5a4bcSLai Jiangshan complete(detach_completion); 266160f5a4bcSLai Jiangshan } 266260f5a4bcSLai Jiangshan 266360f5a4bcSLai Jiangshan /** 2664c34056a3STejun Heo * create_worker - create a new workqueue worker 266563d95a91STejun Heo * @pool: pool the new worker will belong to 2666c34056a3STejun Heo * 2667051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 2668c34056a3STejun Heo * 2669c34056a3STejun Heo * CONTEXT: 2670c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 2671c34056a3STejun Heo * 2672d185af30SYacine Belkadi * Return: 2673c34056a3STejun Heo * Pointer to the newly created worker. 2674c34056a3STejun Heo */ 2675bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 2676c34056a3STejun Heo { 2677e441b56fSZhen Lei struct worker *worker; 2678e441b56fSZhen Lei int id; 26795d9c7a1eSLucy Mielke char id_buf[23]; 2680c34056a3STejun Heo 26817cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 2682e441b56fSZhen Lei id = ida_alloc(&pool->worker_ida, GFP_KERNEL); 26833f0ea0b8SPetr Mladek if (id < 0) { 26843f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", 26853f0ea0b8SPetr Mladek ERR_PTR(id)); 2686e441b56fSZhen Lei return NULL; 26873f0ea0b8SPetr Mladek } 2688c34056a3STejun Heo 2689f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 26903f0ea0b8SPetr Mladek if (!worker) { 26913f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker\n"); 2692c34056a3STejun Heo goto fail; 26933f0ea0b8SPetr Mladek } 2694c34056a3STejun Heo 2695c34056a3STejun Heo worker->id = id; 2696c34056a3STejun Heo 26974cb1ef64STejun Heo if (!(pool->flags & POOL_BH)) { 269829c91e99STejun Heo if (pool->cpu >= 0) 2699e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 2700e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 2701f3421797STejun Heo else 2702e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 2703e3c916a4STejun Heo 27044cb1ef64STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, 27054cb1ef64STejun Heo pool->node, "kworker/%s", id_buf); 27063f0ea0b8SPetr Mladek if (IS_ERR(worker->task)) { 270760f54038SPetr Mladek if (PTR_ERR(worker->task) == -EINTR) { 270860f54038SPetr Mladek pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", 270960f54038SPetr Mladek id_buf); 271060f54038SPetr Mladek } else { 27113f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to create a worker thread: %pe", 27123f0ea0b8SPetr Mladek worker->task); 271360f54038SPetr Mladek } 2714c34056a3STejun Heo goto fail; 27153f0ea0b8SPetr Mladek } 2716c34056a3STejun Heo 271791151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 27189546b29eSTejun Heo kthread_bind_mask(worker->task, pool_allowed_cpus(pool)); 27194cb1ef64STejun Heo } 272091151228SOleg Nesterov 2721da028469SLai Jiangshan /* successful, attach the worker to the pool */ 27224736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 2723822d8405STejun Heo 2724051e1850SLai Jiangshan /* start the newly created worker */ 2725a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 27260219a352STejun Heo 2727051e1850SLai Jiangshan worker->pool->nr_workers++; 2728051e1850SLai Jiangshan worker_enter_idle(worker); 27290219a352STejun Heo 27300219a352STejun Heo /* 27310219a352STejun Heo * @worker is waiting on a completion in kthread() and will trigger hung 27326a229b0eSTejun Heo * check if not woken up soon. As kick_pool() is noop if @pool is empty, 27336a229b0eSTejun Heo * wake it up explicitly. 27340219a352STejun Heo */ 27354cb1ef64STejun Heo if (worker->task) 2736051e1850SLai Jiangshan wake_up_process(worker->task); 27370219a352STejun Heo 2738a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2739051e1850SLai Jiangshan 2740c34056a3STejun Heo return worker; 2741822d8405STejun Heo 2742c34056a3STejun Heo fail: 2743e441b56fSZhen Lei ida_free(&pool->worker_ida, id); 2744c34056a3STejun Heo kfree(worker); 2745c34056a3STejun Heo return NULL; 2746c34056a3STejun Heo } 2747c34056a3STejun Heo 2748793777bcSValentin Schneider static void unbind_worker(struct worker *worker) 2749793777bcSValentin Schneider { 2750793777bcSValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2751793777bcSValentin Schneider 2752793777bcSValentin Schneider kthread_set_per_cpu(worker->task, -1); 2753793777bcSValentin Schneider if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) 2754793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); 2755793777bcSValentin Schneider else 2756793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); 2757793777bcSValentin Schneider } 2758793777bcSValentin Schneider 2759e02b9312SValentin Schneider static void wake_dying_workers(struct list_head *cull_list) 2760e02b9312SValentin Schneider { 2761e02b9312SValentin Schneider struct worker *worker, *tmp; 2762e02b9312SValentin Schneider 2763e02b9312SValentin Schneider list_for_each_entry_safe(worker, tmp, cull_list, entry) { 2764e02b9312SValentin Schneider list_del_init(&worker->entry); 2765e02b9312SValentin Schneider unbind_worker(worker); 2766e02b9312SValentin Schneider /* 2767e02b9312SValentin Schneider * If the worker was somehow already running, then it had to be 2768e02b9312SValentin Schneider * in pool->idle_list when set_worker_dying() happened or we 2769e02b9312SValentin Schneider * wouldn't have gotten here. 2770c34056a3STejun Heo * 2771e02b9312SValentin Schneider * Thus, the worker must either have observed the WORKER_DIE 2772e02b9312SValentin Schneider * flag, or have set its state to TASK_IDLE. Either way, the 2773e02b9312SValentin Schneider * below will be observed by the worker and is safe to do 2774e02b9312SValentin Schneider * outside of pool->lock. 2775e02b9312SValentin Schneider */ 2776e02b9312SValentin Schneider wake_up_process(worker->task); 2777e02b9312SValentin Schneider } 2778e02b9312SValentin Schneider } 2779e02b9312SValentin Schneider 2780e02b9312SValentin Schneider /** 2781e02b9312SValentin Schneider * set_worker_dying - Tag a worker for destruction 2782e02b9312SValentin Schneider * @worker: worker to be destroyed 2783e02b9312SValentin Schneider * @list: transfer worker away from its pool->idle_list and into list 2784e02b9312SValentin Schneider * 2785e02b9312SValentin Schneider * Tag @worker for destruction and adjust @pool stats accordingly. The worker 2786e02b9312SValentin Schneider * should be idle. 2787c8e55f36STejun Heo * 2788c8e55f36STejun Heo * CONTEXT: 2789a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2790c34056a3STejun Heo */ 2791e02b9312SValentin Schneider static void set_worker_dying(struct worker *worker, struct list_head *list) 2792c34056a3STejun Heo { 2793bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2794c34056a3STejun Heo 2795cd549687STejun Heo lockdep_assert_held(&pool->lock); 2796e02b9312SValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2797cd549687STejun Heo 2798c34056a3STejun Heo /* sanity check frenzy */ 27996183c009STejun Heo if (WARN_ON(worker->current_work) || 280073eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 280173eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 28026183c009STejun Heo return; 2803c34056a3STejun Heo 2804bd7bdd43STejun Heo pool->nr_workers--; 2805bd7bdd43STejun Heo pool->nr_idle--; 2806c8e55f36STejun Heo 2807cb444766STejun Heo worker->flags |= WORKER_DIE; 2808e02b9312SValentin Schneider 2809e02b9312SValentin Schneider list_move(&worker->entry, list); 2810e02b9312SValentin Schneider list_move(&worker->node, &pool->dying_workers); 2811c34056a3STejun Heo } 2812c34056a3STejun Heo 28133f959aa3SValentin Schneider /** 28143f959aa3SValentin Schneider * idle_worker_timeout - check if some idle workers can now be deleted. 28153f959aa3SValentin Schneider * @t: The pool's idle_timer that just expired 28163f959aa3SValentin Schneider * 28173f959aa3SValentin Schneider * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in 28183f959aa3SValentin Schneider * worker_leave_idle(), as a worker flicking between idle and active while its 28193f959aa3SValentin Schneider * pool is at the too_many_workers() tipping point would cause too much timer 28203f959aa3SValentin Schneider * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let 28213f959aa3SValentin Schneider * it expire and re-evaluate things from there. 28223f959aa3SValentin Schneider */ 282332a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 2824e22bee78STejun Heo { 282532a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 28263f959aa3SValentin Schneider bool do_cull = false; 28273f959aa3SValentin Schneider 28283f959aa3SValentin Schneider if (work_pending(&pool->idle_cull_work)) 28293f959aa3SValentin Schneider return; 28303f959aa3SValentin Schneider 28313f959aa3SValentin Schneider raw_spin_lock_irq(&pool->lock); 28323f959aa3SValentin Schneider 28333f959aa3SValentin Schneider if (too_many_workers(pool)) { 28343f959aa3SValentin Schneider struct worker *worker; 28353f959aa3SValentin Schneider unsigned long expires; 28363f959aa3SValentin Schneider 28373f959aa3SValentin Schneider /* idle_list is kept in LIFO order, check the last one */ 28383f959aa3SValentin Schneider worker = list_entry(pool->idle_list.prev, struct worker, entry); 28393f959aa3SValentin Schneider expires = worker->last_active + IDLE_WORKER_TIMEOUT; 28403f959aa3SValentin Schneider do_cull = !time_before(jiffies, expires); 28413f959aa3SValentin Schneider 28423f959aa3SValentin Schneider if (!do_cull) 28433f959aa3SValentin Schneider mod_timer(&pool->idle_timer, expires); 28443f959aa3SValentin Schneider } 28453f959aa3SValentin Schneider raw_spin_unlock_irq(&pool->lock); 28463f959aa3SValentin Schneider 28473f959aa3SValentin Schneider if (do_cull) 28483f959aa3SValentin Schneider queue_work(system_unbound_wq, &pool->idle_cull_work); 28493f959aa3SValentin Schneider } 28503f959aa3SValentin Schneider 28513f959aa3SValentin Schneider /** 28523f959aa3SValentin Schneider * idle_cull_fn - cull workers that have been idle for too long. 28533f959aa3SValentin Schneider * @work: the pool's work for handling these idle workers 28543f959aa3SValentin Schneider * 28553f959aa3SValentin Schneider * This goes through a pool's idle workers and gets rid of those that have been 28563f959aa3SValentin Schneider * idle for at least IDLE_WORKER_TIMEOUT seconds. 2857e02b9312SValentin Schneider * 2858e02b9312SValentin Schneider * We don't want to disturb isolated CPUs because of a pcpu kworker being 2859e02b9312SValentin Schneider * culled, so this also resets worker affinity. This requires a sleepable 2860e02b9312SValentin Schneider * context, hence the split between timer callback and work item. 28613f959aa3SValentin Schneider */ 28623f959aa3SValentin Schneider static void idle_cull_fn(struct work_struct *work) 28633f959aa3SValentin Schneider { 28643f959aa3SValentin Schneider struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); 28659680540cSYang Yingliang LIST_HEAD(cull_list); 2866e22bee78STejun Heo 2867e02b9312SValentin Schneider /* 2868e02b9312SValentin Schneider * Grabbing wq_pool_attach_mutex here ensures an already-running worker 2869e02b9312SValentin Schneider * cannot proceed beyong worker_detach_from_pool() in its self-destruct 2870e02b9312SValentin Schneider * path. This is required as a previously-preempted worker could run after 2871e02b9312SValentin Schneider * set_worker_dying() has happened but before wake_dying_workers() did. 2872e02b9312SValentin Schneider */ 2873e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 2874a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2875e22bee78STejun Heo 28763347fc9fSLai Jiangshan while (too_many_workers(pool)) { 2877e22bee78STejun Heo struct worker *worker; 2878e22bee78STejun Heo unsigned long expires; 2879e22bee78STejun Heo 288063d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 2881e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 2882e22bee78STejun Heo 28833347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 288463d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 28853347fc9fSLai Jiangshan break; 2886e22bee78STejun Heo } 28873347fc9fSLai Jiangshan 2888e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 2889e22bee78STejun Heo } 2890e22bee78STejun Heo 2891a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2892e02b9312SValentin Schneider wake_dying_workers(&cull_list); 2893e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 2894e22bee78STejun Heo } 2895e22bee78STejun Heo 2896493a1724STejun Heo static void send_mayday(struct work_struct *work) 2897e22bee78STejun Heo { 2898112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2899112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 2900493a1724STejun Heo 29012e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 2902e22bee78STejun Heo 2903493008a8STejun Heo if (!wq->rescuer) 2904493a1724STejun Heo return; 2905e22bee78STejun Heo 2906e22bee78STejun Heo /* mayday mayday mayday */ 2907493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 290877668c8bSLai Jiangshan /* 290977668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 291077668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 291177668c8bSLai Jiangshan * rescuer is done with it. 291277668c8bSLai Jiangshan */ 291377668c8bSLai Jiangshan get_pwq(pwq); 2914493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2915e22bee78STejun Heo wake_up_process(wq->rescuer->task); 2916725e8ec5STejun Heo pwq->stats[PWQ_STAT_MAYDAY]++; 2917493a1724STejun Heo } 2918e22bee78STejun Heo } 2919e22bee78STejun Heo 292032a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 2921e22bee78STejun Heo { 292232a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 2923e22bee78STejun Heo struct work_struct *work; 2924e22bee78STejun Heo 2925a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2926a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ 2927e22bee78STejun Heo 292863d95a91STejun Heo if (need_to_create_worker(pool)) { 2929e22bee78STejun Heo /* 2930e22bee78STejun Heo * We've been trying to create a new worker but 2931e22bee78STejun Heo * haven't been successful. We might be hitting an 2932e22bee78STejun Heo * allocation deadlock. Send distress signals to 2933e22bee78STejun Heo * rescuers. 2934e22bee78STejun Heo */ 293563d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 2936e22bee78STejun Heo send_mayday(work); 2937e22bee78STejun Heo } 2938e22bee78STejun Heo 2939a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2940a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2941e22bee78STejun Heo 294263d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 2943e22bee78STejun Heo } 2944e22bee78STejun Heo 2945e22bee78STejun Heo /** 2946e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 294763d95a91STejun Heo * @pool: pool to create a new worker for 2948e22bee78STejun Heo * 294963d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 2950e22bee78STejun Heo * have at least one idle worker on return from this function. If 2951e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 295263d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 2953e22bee78STejun Heo * possible allocation deadlock. 2954e22bee78STejun Heo * 2955c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 2956c5aa87bbSTejun Heo * may_start_working() %true. 2957e22bee78STejun Heo * 2958e22bee78STejun Heo * LOCKING: 2959a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2960e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 2961e22bee78STejun Heo * manager. 2962e22bee78STejun Heo */ 296329187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 2964d565ed63STejun Heo __releases(&pool->lock) 2965d565ed63STejun Heo __acquires(&pool->lock) 2966e22bee78STejun Heo { 2967e22bee78STejun Heo restart: 2968a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 29699f9c2364STejun Heo 2970e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 297163d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 2972e22bee78STejun Heo 2973e22bee78STejun Heo while (true) { 2974051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 2975e22bee78STejun Heo break; 2976e22bee78STejun Heo 2977e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 29789f9c2364STejun Heo 297963d95a91STejun Heo if (!need_to_create_worker(pool)) 2980e22bee78STejun Heo break; 2981e22bee78STejun Heo } 2982e22bee78STejun Heo 298363d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 2984a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2985051e1850SLai Jiangshan /* 2986051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 2987051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 2988051e1850SLai Jiangshan * already become busy. 2989051e1850SLai Jiangshan */ 299063d95a91STejun Heo if (need_to_create_worker(pool)) 2991e22bee78STejun Heo goto restart; 2992e22bee78STejun Heo } 2993e22bee78STejun Heo 2994e22bee78STejun Heo /** 2995e22bee78STejun Heo * manage_workers - manage worker pool 2996e22bee78STejun Heo * @worker: self 2997e22bee78STejun Heo * 2998706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 2999e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 3000706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 3001e22bee78STejun Heo * 3002e22bee78STejun Heo * The caller can safely start processing works on false return. On 3003e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 3004e22bee78STejun Heo * and may_start_working() is true. 3005e22bee78STejun Heo * 3006e22bee78STejun Heo * CONTEXT: 3007a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3008e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 3009e22bee78STejun Heo * 3010d185af30SYacine Belkadi * Return: 301129187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 301229187a9eSTejun Heo * start processing works, %true if management function was performed and 301329187a9eSTejun Heo * the conditions that the caller verified before calling the function may 301429187a9eSTejun Heo * no longer be true. 3015e22bee78STejun Heo */ 3016e22bee78STejun Heo static bool manage_workers(struct worker *worker) 3017e22bee78STejun Heo { 301863d95a91STejun Heo struct worker_pool *pool = worker->pool; 3019e22bee78STejun Heo 3020692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 302129187a9eSTejun Heo return false; 3022692b4825STejun Heo 3023692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 30242607d7a6STejun Heo pool->manager = worker; 3025e22bee78STejun Heo 302629187a9eSTejun Heo maybe_create_worker(pool); 3027e22bee78STejun Heo 30282607d7a6STejun Heo pool->manager = NULL; 3029692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 3030d8bb65abSSebastian Andrzej Siewior rcuwait_wake_up(&manager_wait); 303129187a9eSTejun Heo return true; 3032e22bee78STejun Heo } 3033e22bee78STejun Heo 3034a62428c0STejun Heo /** 3035a62428c0STejun Heo * process_one_work - process single work 3036c34056a3STejun Heo * @worker: self 3037a62428c0STejun Heo * @work: work to process 3038a62428c0STejun Heo * 3039a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 3040a62428c0STejun Heo * process a single work including synchronization against and 3041a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 3042a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 3043a62428c0STejun Heo * call this function to process a work. 3044a62428c0STejun Heo * 3045a62428c0STejun Heo * CONTEXT: 3046a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which is released and regrabbed. 3047a62428c0STejun Heo */ 3048c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 3049d565ed63STejun Heo __releases(&pool->lock) 3050d565ed63STejun Heo __acquires(&pool->lock) 30511da177e4SLinus Torvalds { 3052112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 3053bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 3054c4560c2cSLai Jiangshan unsigned long work_data; 3055c35aea39STejun Heo int lockdep_start_depth, rcu_start_depth; 30564e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 30574e6045f1SJohannes Berg /* 3058a62428c0STejun Heo * It is permissible to free the struct work_struct from 3059a62428c0STejun Heo * inside the function that is called from it, this we need to 3060a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 3061a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 3062a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 30634e6045f1SJohannes Berg */ 30644d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 30654d82a1deSPeter Zijlstra 30664d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 30674e6045f1SJohannes Berg #endif 3068807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 306985327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 3070ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 307125511a47STejun Heo 30728930cabaSTejun Heo /* claim and dequeue */ 3073dc186ad7SThomas Gleixner debug_work_deactivate(work); 3074c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 3075c34056a3STejun Heo worker->current_work = work; 3076a2c1c57bSTejun Heo worker->current_func = work->func; 3077112202d9STejun Heo worker->current_pwq = pwq; 30784cb1ef64STejun Heo if (worker->task) 3079616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 3080c4560c2cSLai Jiangshan work_data = *work_data_bits(work); 3081d812796eSLai Jiangshan worker->current_color = get_work_color(work_data); 30827a22ad75STejun Heo 30838bf89593STejun Heo /* 30848bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 30858bf89593STejun Heo * overridden through set_worker_desc(). 30868bf89593STejun Heo */ 30878bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 30888bf89593STejun Heo 3089a62428c0STejun Heo list_del_init(&work->entry); 3090a62428c0STejun Heo 3091649027d7STejun Heo /* 3092228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 3093228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 3094228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 3095228f1d00SLai Jiangshan * execution of the pending work items. 3096fb0e7bebSTejun Heo */ 3097616db877STejun Heo if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE)) 3098228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 3099fb0e7bebSTejun Heo 3100974271c4STejun Heo /* 31010219a352STejun Heo * Kick @pool if necessary. It's always noop for per-cpu worker pools 31020219a352STejun Heo * since nr_running would always be >= 1 at this point. This is used to 31030219a352STejun Heo * chain execution of the pending work items for WORKER_NOT_RUNNING 31040219a352STejun Heo * workers such as the UNBOUND and CPU_INTENSIVE ones. 3105974271c4STejun Heo */ 31060219a352STejun Heo kick_pool(pool); 3107974271c4STejun Heo 31088930cabaSTejun Heo /* 31097c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 3110d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 311123657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 311223657bb1STejun Heo * disabled. 31138930cabaSTejun Heo */ 31147c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 31151da177e4SLinus Torvalds 3116fe48ba7dSMirsad Goran Todorovac pwq->stats[PWQ_STAT_STARTED]++; 3117a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3118365970a1SDavid Howells 3119c35aea39STejun Heo rcu_start_depth = rcu_preempt_depth(); 3120c35aea39STejun Heo lockdep_start_depth = lockdep_depth(current); 3121a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 31223295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 3123e6f3faa7SPeter Zijlstra /* 3124f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 3125f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 3126e6f3faa7SPeter Zijlstra * 3127e6f3faa7SPeter Zijlstra * However, that would result in: 3128e6f3faa7SPeter Zijlstra * 3129e6f3faa7SPeter Zijlstra * A(W1) 3130e6f3faa7SPeter Zijlstra * WFC(C) 3131e6f3faa7SPeter Zijlstra * A(W1) 3132e6f3faa7SPeter Zijlstra * C(C) 3133e6f3faa7SPeter Zijlstra * 3134e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 3135e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 3136e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 3137f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 3138e6f3faa7SPeter Zijlstra * these locks. 3139e6f3faa7SPeter Zijlstra * 3140e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 3141e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 3142e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 3143e6f3faa7SPeter Zijlstra */ 3144f52be570SPeter Zijlstra lockdep_invariant_state(true); 3145e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 3146a2c1c57bSTejun Heo worker->current_func(work); 3147e36c886aSArjan van de Ven /* 3148e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 3149e36c886aSArjan van de Ven * point will only record its address. 3150e36c886aSArjan van de Ven */ 31511c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func); 3152725e8ec5STejun Heo pwq->stats[PWQ_STAT_COMPLETED]++; 31533295f0efSIngo Molnar lock_map_release(&lockdep_map); 3154112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 31551da177e4SLinus Torvalds 3156c35aea39STejun Heo if (unlikely((worker->task && in_atomic()) || 3157c35aea39STejun Heo lockdep_depth(current) != lockdep_start_depth || 3158c35aea39STejun Heo rcu_preempt_depth() != rcu_start_depth)) { 3159c35aea39STejun Heo pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n" 3160c35aea39STejun Heo " preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n", 3161c35aea39STejun Heo current->comm, task_pid_nr(current), preempt_count(), 3162c35aea39STejun Heo lockdep_start_depth, lockdep_depth(current), 3163c35aea39STejun Heo rcu_start_depth, rcu_preempt_depth(), 3164c35aea39STejun Heo worker->current_func); 3165d5abe669SPeter Zijlstra debug_show_held_locks(current); 3166d5abe669SPeter Zijlstra dump_stack(); 3167d5abe669SPeter Zijlstra } 3168d5abe669SPeter Zijlstra 3169b22ce278STejun Heo /* 3170025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION 3171b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 3172b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 3173b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 3174789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 3175789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 3176b22ce278STejun Heo */ 31774cb1ef64STejun Heo if (worker->task) 3178a7e6425eSPaul E. McKenney cond_resched(); 3179b22ce278STejun Heo 3180a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3181a62428c0STejun Heo 3182616db877STejun Heo /* 3183616db877STejun Heo * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked 3184616db877STejun Heo * CPU intensive by wq_worker_tick() if @work hogged CPU longer than 3185616db877STejun Heo * wq_cpu_intensive_thresh_us. Clear it. 3186616db877STejun Heo */ 3187fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 3188fb0e7bebSTejun Heo 31891b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 31901b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 31911b69ac6bSJohannes Weiner 3192a62428c0STejun Heo /* we're done with it, release */ 319342f8570fSSasha Levin hash_del(&worker->hentry); 3194c34056a3STejun Heo worker->current_work = NULL; 3195a2c1c57bSTejun Heo worker->current_func = NULL; 3196112202d9STejun Heo worker->current_pwq = NULL; 3197d812796eSLai Jiangshan worker->current_color = INT_MAX; 3198dd6c3c54STejun Heo 3199dd6c3c54STejun Heo /* must be the last step, see the function comment */ 3200c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, work_data); 32011da177e4SLinus Torvalds } 32021da177e4SLinus Torvalds 3203affee4b2STejun Heo /** 3204affee4b2STejun Heo * process_scheduled_works - process scheduled works 3205affee4b2STejun Heo * @worker: self 3206affee4b2STejun Heo * 3207affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 3208affee4b2STejun Heo * may change while processing a work, so this function repeatedly 3209affee4b2STejun Heo * fetches a work from the top and executes it. 3210affee4b2STejun Heo * 3211affee4b2STejun Heo * CONTEXT: 3212a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3213affee4b2STejun Heo * multiple times. 3214affee4b2STejun Heo */ 3215affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 32161da177e4SLinus Torvalds { 3217c0ab017dSTejun Heo struct work_struct *work; 3218c0ab017dSTejun Heo bool first = true; 3219c0ab017dSTejun Heo 3220c0ab017dSTejun Heo while ((work = list_first_entry_or_null(&worker->scheduled, 3221c0ab017dSTejun Heo struct work_struct, entry))) { 3222c0ab017dSTejun Heo if (first) { 3223c0ab017dSTejun Heo worker->pool->watchdog_ts = jiffies; 3224c0ab017dSTejun Heo first = false; 3225c0ab017dSTejun Heo } 3226c34056a3STejun Heo process_one_work(worker, work); 3227a62428c0STejun Heo } 32281da177e4SLinus Torvalds } 32291da177e4SLinus Torvalds 3230197f6accSTejun Heo static void set_pf_worker(bool val) 3231197f6accSTejun Heo { 3232197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 3233197f6accSTejun Heo if (val) 3234197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 3235197f6accSTejun Heo else 3236197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 3237197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 3238197f6accSTejun Heo } 3239197f6accSTejun Heo 32404690c4abSTejun Heo /** 32414690c4abSTejun Heo * worker_thread - the worker thread function 3242c34056a3STejun Heo * @__worker: self 32434690c4abSTejun Heo * 3244c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 3245c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 3246c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 3247c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 3248c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 3249d185af30SYacine Belkadi * 3250d185af30SYacine Belkadi * Return: 0 32514690c4abSTejun Heo */ 3252c34056a3STejun Heo static int worker_thread(void *__worker) 32531da177e4SLinus Torvalds { 3254c34056a3STejun Heo struct worker *worker = __worker; 3255bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 32561da177e4SLinus Torvalds 3257e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 3258197f6accSTejun Heo set_pf_worker(true); 3259c8e55f36STejun Heo woke_up: 3260a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3261affee4b2STejun Heo 3262a9ab775bSTejun Heo /* am I supposed to die? */ 3263a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 3264a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3265197f6accSTejun Heo set_pf_worker(false); 326660f5a4bcSLai Jiangshan 326760f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 3268e441b56fSZhen Lei ida_free(&pool->worker_ida, worker->id); 3269a2d812a2STejun Heo worker_detach_from_pool(worker); 3270e02b9312SValentin Schneider WARN_ON_ONCE(!list_empty(&worker->entry)); 327160f5a4bcSLai Jiangshan kfree(worker); 3272c8e55f36STejun Heo return 0; 3273c8e55f36STejun Heo } 3274c8e55f36STejun Heo 3275c8e55f36STejun Heo worker_leave_idle(worker); 3276db7bccf4STejun Heo recheck: 3277e22bee78STejun Heo /* no more worker necessary? */ 327863d95a91STejun Heo if (!need_more_worker(pool)) 3279e22bee78STejun Heo goto sleep; 3280e22bee78STejun Heo 3281e22bee78STejun Heo /* do we need to manage? */ 328263d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 3283e22bee78STejun Heo goto recheck; 3284e22bee78STejun Heo 3285c8e55f36STejun Heo /* 3286c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 3287c8e55f36STejun Heo * preparing to process a work or actually processing it. 3288c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 3289c8e55f36STejun Heo */ 32906183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 3291c8e55f36STejun Heo 3292e22bee78STejun Heo /* 3293a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 3294a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 3295a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 3296a9ab775bSTejun Heo * management if applicable and concurrency management is restored 3297a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 3298e22bee78STejun Heo */ 3299a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 3300e22bee78STejun Heo 3301e22bee78STejun Heo do { 3302affee4b2STejun Heo struct work_struct *work = 3303bd7bdd43STejun Heo list_first_entry(&pool->worklist, 3304affee4b2STejun Heo struct work_struct, entry); 3305affee4b2STejun Heo 3306873eaca6STejun Heo if (assign_work(work, worker, NULL)) 3307affee4b2STejun Heo process_scheduled_works(worker); 330863d95a91STejun Heo } while (keep_working(pool)); 3309affee4b2STejun Heo 3310228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 3311d313dd85STejun Heo sleep: 3312c8e55f36STejun Heo /* 3313d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 3314d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 3315d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 3316d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 3317d565ed63STejun Heo * event. 3318c8e55f36STejun Heo */ 3319c8e55f36STejun Heo worker_enter_idle(worker); 3320c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 3321a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 33221da177e4SLinus Torvalds schedule(); 3323c8e55f36STejun Heo goto woke_up; 33241da177e4SLinus Torvalds } 33251da177e4SLinus Torvalds 3326e22bee78STejun Heo /** 3327e22bee78STejun Heo * rescuer_thread - the rescuer thread function 3328111c225aSTejun Heo * @__rescuer: self 3329e22bee78STejun Heo * 3330e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 3331493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 3332e22bee78STejun Heo * 3333706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 3334e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 3335e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 3336e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 3337e22bee78STejun Heo * the problem rescuer solves. 3338e22bee78STejun Heo * 3339706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 3340706026c2STejun Heo * workqueues which have works queued on the pool and let them process 3341e22bee78STejun Heo * those works so that forward progress can be guaranteed. 3342e22bee78STejun Heo * 3343e22bee78STejun Heo * This should happen rarely. 3344d185af30SYacine Belkadi * 3345d185af30SYacine Belkadi * Return: 0 3346e22bee78STejun Heo */ 3347111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 3348e22bee78STejun Heo { 3349111c225aSTejun Heo struct worker *rescuer = __rescuer; 3350111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 33514d595b86SLai Jiangshan bool should_stop; 3352e22bee78STejun Heo 3353e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 3354111c225aSTejun Heo 3355111c225aSTejun Heo /* 3356111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 3357111c225aSTejun Heo * doesn't participate in concurrency management. 3358111c225aSTejun Heo */ 3359197f6accSTejun Heo set_pf_worker(true); 3360e22bee78STejun Heo repeat: 3361c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 33621da177e4SLinus Torvalds 33634d595b86SLai Jiangshan /* 33644d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 33654d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 33664d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 33674d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 33684d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 33694d595b86SLai Jiangshan * list is always empty on exit. 33704d595b86SLai Jiangshan */ 33714d595b86SLai Jiangshan should_stop = kthread_should_stop(); 33721da177e4SLinus Torvalds 3373493a1724STejun Heo /* see whether any pwq is asking for help */ 3374a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 3375493a1724STejun Heo 3376493a1724STejun Heo while (!list_empty(&wq->maydays)) { 3377493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 3378493a1724STejun Heo struct pool_workqueue, mayday_node); 3379112202d9STejun Heo struct worker_pool *pool = pwq->pool; 3380e22bee78STejun Heo struct work_struct *work, *n; 3381e22bee78STejun Heo 3382e22bee78STejun Heo __set_current_state(TASK_RUNNING); 3383493a1724STejun Heo list_del_init(&pwq->mayday_node); 3384493a1724STejun Heo 3385a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3386e22bee78STejun Heo 338751697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 338851697d39SLai Jiangshan 3389a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3390e22bee78STejun Heo 3391e22bee78STejun Heo /* 3392e22bee78STejun Heo * Slurp in all works issued via this workqueue and 3393e22bee78STejun Heo * process'em. 3394e22bee78STejun Heo */ 3395873eaca6STejun Heo WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); 339682607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 3397873eaca6STejun Heo if (get_work_pwq(work) == pwq && 3398873eaca6STejun Heo assign_work(work, rescuer, &n)) 3399725e8ec5STejun Heo pwq->stats[PWQ_STAT_RESCUED]++; 340082607adcSTejun Heo } 3401e22bee78STejun Heo 3402873eaca6STejun Heo if (!list_empty(&rescuer->scheduled)) { 3403e22bee78STejun Heo process_scheduled_works(rescuer); 34047576958aSTejun Heo 34057576958aSTejun Heo /* 3406008847f6SNeilBrown * The above execution of rescued work items could 3407008847f6SNeilBrown * have created more to rescue through 3408f97a4a1aSLai Jiangshan * pwq_activate_first_inactive() or chained 3409008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 3410008847f6SNeilBrown * that such back-to-back work items, which may be 3411008847f6SNeilBrown * being used to relieve memory pressure, don't 3412008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 3413008847f6SNeilBrown */ 34144f3f4cf3SLai Jiangshan if (pwq->nr_active && need_to_create_worker(pool)) { 3415a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); 3416e66b39afSTejun Heo /* 3417e66b39afSTejun Heo * Queue iff we aren't racing destruction 3418e66b39afSTejun Heo * and somebody else hasn't queued it already. 3419e66b39afSTejun Heo */ 3420e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) { 3421008847f6SNeilBrown get_pwq(pwq); 3422e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 3423e66b39afSTejun Heo } 3424a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 3425008847f6SNeilBrown } 3426008847f6SNeilBrown } 3427008847f6SNeilBrown 3428008847f6SNeilBrown /* 342977668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 343013b1d625SLai Jiangshan * go away while we're still attached to it. 343177668c8bSLai Jiangshan */ 343277668c8bSLai Jiangshan put_pwq(pwq); 343377668c8bSLai Jiangshan 343477668c8bSLai Jiangshan /* 34350219a352STejun Heo * Leave this pool. Notify regular workers; otherwise, we end up 34360219a352STejun Heo * with 0 concurrency and stalling the execution. 34377576958aSTejun Heo */ 34380219a352STejun Heo kick_pool(pool); 34397576958aSTejun Heo 3440a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 344113b1d625SLai Jiangshan 3442a2d812a2STejun Heo worker_detach_from_pool(rescuer); 344313b1d625SLai Jiangshan 3444a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 34451da177e4SLinus Torvalds } 34461da177e4SLinus Torvalds 3447a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3448493a1724STejun Heo 34494d595b86SLai Jiangshan if (should_stop) { 34504d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 3451197f6accSTejun Heo set_pf_worker(false); 34524d595b86SLai Jiangshan return 0; 34534d595b86SLai Jiangshan } 34544d595b86SLai Jiangshan 3455111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 3456111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 3457e22bee78STejun Heo schedule(); 3458e22bee78STejun Heo goto repeat; 34591da177e4SLinus Torvalds } 34601da177e4SLinus Torvalds 34614cb1ef64STejun Heo static void bh_worker(struct worker *worker) 34624cb1ef64STejun Heo { 34634cb1ef64STejun Heo struct worker_pool *pool = worker->pool; 34644cb1ef64STejun Heo int nr_restarts = BH_WORKER_RESTARTS; 34654cb1ef64STejun Heo unsigned long end = jiffies + BH_WORKER_JIFFIES; 34664cb1ef64STejun Heo 34674cb1ef64STejun Heo raw_spin_lock_irq(&pool->lock); 34684cb1ef64STejun Heo worker_leave_idle(worker); 34694cb1ef64STejun Heo 34704cb1ef64STejun Heo /* 34714cb1ef64STejun Heo * This function follows the structure of worker_thread(). See there for 34724cb1ef64STejun Heo * explanations on each step. 34734cb1ef64STejun Heo */ 34744cb1ef64STejun Heo if (!need_more_worker(pool)) 34754cb1ef64STejun Heo goto done; 34764cb1ef64STejun Heo 34774cb1ef64STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 34784cb1ef64STejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 34794cb1ef64STejun Heo 34804cb1ef64STejun Heo do { 34814cb1ef64STejun Heo struct work_struct *work = 34824cb1ef64STejun Heo list_first_entry(&pool->worklist, 34834cb1ef64STejun Heo struct work_struct, entry); 34844cb1ef64STejun Heo 34854cb1ef64STejun Heo if (assign_work(work, worker, NULL)) 34864cb1ef64STejun Heo process_scheduled_works(worker); 34874cb1ef64STejun Heo } while (keep_working(pool) && 34884cb1ef64STejun Heo --nr_restarts && time_before(jiffies, end)); 34894cb1ef64STejun Heo 34904cb1ef64STejun Heo worker_set_flags(worker, WORKER_PREP); 34914cb1ef64STejun Heo done: 34924cb1ef64STejun Heo worker_enter_idle(worker); 34934cb1ef64STejun Heo kick_pool(pool); 34944cb1ef64STejun Heo raw_spin_unlock_irq(&pool->lock); 34954cb1ef64STejun Heo } 34964cb1ef64STejun Heo 34974cb1ef64STejun Heo /* 34984cb1ef64STejun Heo * TODO: Convert all tasklet users to workqueue and use softirq directly. 34994cb1ef64STejun Heo * 35004cb1ef64STejun Heo * This is currently called from tasklet[_hi]action() and thus is also called 35014cb1ef64STejun Heo * whenever there are tasklets to run. Let's do an early exit if there's nothing 35024cb1ef64STejun Heo * queued. Once conversion from tasklet is complete, the need_more_worker() test 35034cb1ef64STejun Heo * can be dropped. 35044cb1ef64STejun Heo * 35054cb1ef64STejun Heo * After full conversion, we'll add worker->softirq_action, directly use the 35064cb1ef64STejun Heo * softirq action and obtain the worker pointer from the softirq_action pointer. 35074cb1ef64STejun Heo */ 35084cb1ef64STejun Heo void workqueue_softirq_action(bool highpri) 35094cb1ef64STejun Heo { 35104cb1ef64STejun Heo struct worker_pool *pool = 35114cb1ef64STejun Heo &per_cpu(bh_worker_pools, smp_processor_id())[highpri]; 35124cb1ef64STejun Heo if (need_more_worker(pool)) 35134cb1ef64STejun Heo bh_worker(list_first_entry(&pool->workers, struct worker, node)); 35144cb1ef64STejun Heo } 35154cb1ef64STejun Heo 3516fca839c0STejun Heo /** 3517fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 3518fca839c0STejun Heo * @target_wq: workqueue being flushed 3519fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 3520fca839c0STejun Heo * 3521fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 3522fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 3523fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 3524fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 3525fca839c0STejun Heo * a deadlock. 3526fca839c0STejun Heo */ 3527fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 3528fca839c0STejun Heo struct work_struct *target_work) 3529fca839c0STejun Heo { 3530fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 3531fca839c0STejun Heo struct worker *worker; 3532fca839c0STejun Heo 3533fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 3534fca839c0STejun Heo return; 3535fca839c0STejun Heo 3536fca839c0STejun Heo worker = current_wq_worker(); 3537fca839c0STejun Heo 3538fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 3539d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", 3540fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 354123d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 354223d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 3543d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", 3544fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 3545fca839c0STejun Heo target_wq->name, target_func); 3546fca839c0STejun Heo } 3547fca839c0STejun Heo 3548fc2e4d70SOleg Nesterov struct wq_barrier { 3549fc2e4d70SOleg Nesterov struct work_struct work; 3550fc2e4d70SOleg Nesterov struct completion done; 35512607d7a6STejun Heo struct task_struct *task; /* purely informational */ 3552fc2e4d70SOleg Nesterov }; 3553fc2e4d70SOleg Nesterov 3554fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 3555fc2e4d70SOleg Nesterov { 3556fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 3557fc2e4d70SOleg Nesterov complete(&barr->done); 3558fc2e4d70SOleg Nesterov } 3559fc2e4d70SOleg Nesterov 35604690c4abSTejun Heo /** 35614690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 3562112202d9STejun Heo * @pwq: pwq to insert barrier into 35634690c4abSTejun Heo * @barr: wq_barrier to insert 3564affee4b2STejun Heo * @target: target work to attach @barr to 3565affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 35664690c4abSTejun Heo * 3567affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 3568affee4b2STejun Heo * @target finishes execution. Please note that the ordering 3569affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 3570affee4b2STejun Heo * cpu. 3571affee4b2STejun Heo * 3572affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 3573affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 3574affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 3575affee4b2STejun Heo * flag of the previous work while there must be a valid next work 3576affee4b2STejun Heo * after a work with LINKED flag set. 3577affee4b2STejun Heo * 3578affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 3579112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 35804690c4abSTejun Heo * 35814690c4abSTejun Heo * CONTEXT: 3582a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 35834690c4abSTejun Heo */ 3584112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 3585affee4b2STejun Heo struct wq_barrier *barr, 3586affee4b2STejun Heo struct work_struct *target, struct worker *worker) 3587fc2e4d70SOleg Nesterov { 35884cb1ef64STejun Heo static __maybe_unused struct lock_class_key bh_key, thr_key; 3589d812796eSLai Jiangshan unsigned int work_flags = 0; 3590d812796eSLai Jiangshan unsigned int work_color; 3591affee4b2STejun Heo struct list_head *head; 3592affee4b2STejun Heo 3593dc186ad7SThomas Gleixner /* 3594d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 3595dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 3596dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 3597dc186ad7SThomas Gleixner * might deadlock. 35984cb1ef64STejun Heo * 35994cb1ef64STejun Heo * BH and threaded workqueues need separate lockdep keys to avoid 36004cb1ef64STejun Heo * spuriously triggering "inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} 36014cb1ef64STejun Heo * usage". 3602dc186ad7SThomas Gleixner */ 36034cb1ef64STejun Heo INIT_WORK_ONSTACK_KEY(&barr->work, wq_barrier_func, 36044cb1ef64STejun Heo (pwq->wq->flags & WQ_BH) ? &bh_key : &thr_key); 360522df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 360652fa5bc5SBoqun Feng 3607fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 3608fd1a5b04SByungchul Park 36092607d7a6STejun Heo barr->task = current; 361083c22520SOleg Nesterov 36115797b1c1STejun Heo /* The barrier work item does not participate in nr_active. */ 3612018f3a13SLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 3613018f3a13SLai Jiangshan 3614affee4b2STejun Heo /* 3615affee4b2STejun Heo * If @target is currently being executed, schedule the 3616affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 3617affee4b2STejun Heo */ 3618d812796eSLai Jiangshan if (worker) { 3619affee4b2STejun Heo head = worker->scheduled.next; 3620d812796eSLai Jiangshan work_color = worker->current_color; 3621d812796eSLai Jiangshan } else { 3622affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 3623affee4b2STejun Heo 3624affee4b2STejun Heo head = target->entry.next; 3625affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 3626d21cece0SLai Jiangshan work_flags |= *bits & WORK_STRUCT_LINKED; 3627d812796eSLai Jiangshan work_color = get_work_color(*bits); 3628affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 3629affee4b2STejun Heo } 3630affee4b2STejun Heo 3631d812796eSLai Jiangshan pwq->nr_in_flight[work_color]++; 3632d812796eSLai Jiangshan work_flags |= work_color_to_flags(work_color); 3633d812796eSLai Jiangshan 3634d21cece0SLai Jiangshan insert_work(pwq, &barr->work, head, work_flags); 3635fc2e4d70SOleg Nesterov } 3636fc2e4d70SOleg Nesterov 363773f53c4aSTejun Heo /** 3638112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 363973f53c4aSTejun Heo * @wq: workqueue being flushed 364073f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 364173f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 364273f53c4aSTejun Heo * 3643112202d9STejun Heo * Prepare pwqs for workqueue flushing. 364473f53c4aSTejun Heo * 3645112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 3646112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 3647112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 3648112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 3649112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 365073f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 365173f53c4aSTejun Heo * 365273f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 365373f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 365473f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 365573f53c4aSTejun Heo * is returned. 365673f53c4aSTejun Heo * 3657112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 365873f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 365973f53c4aSTejun Heo * advanced to @work_color. 366073f53c4aSTejun Heo * 366173f53c4aSTejun Heo * CONTEXT: 36623c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 366373f53c4aSTejun Heo * 3664d185af30SYacine Belkadi * Return: 366573f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 366673f53c4aSTejun Heo * otherwise. 366773f53c4aSTejun Heo */ 3668112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 366973f53c4aSTejun Heo int flush_color, int work_color) 36701da177e4SLinus Torvalds { 367173f53c4aSTejun Heo bool wait = false; 367249e3cf44STejun Heo struct pool_workqueue *pwq; 36731da177e4SLinus Torvalds 367473f53c4aSTejun Heo if (flush_color >= 0) { 36756183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 3676112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 3677dc186ad7SThomas Gleixner } 367814441960SOleg Nesterov 367949e3cf44STejun Heo for_each_pwq(pwq, wq) { 3680112202d9STejun Heo struct worker_pool *pool = pwq->pool; 36811da177e4SLinus Torvalds 3682a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 368373f53c4aSTejun Heo 368473f53c4aSTejun Heo if (flush_color >= 0) { 36856183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 368673f53c4aSTejun Heo 3687112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 3688112202d9STejun Heo pwq->flush_color = flush_color; 3689112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 369073f53c4aSTejun Heo wait = true; 36911da177e4SLinus Torvalds } 369273f53c4aSTejun Heo } 369373f53c4aSTejun Heo 369473f53c4aSTejun Heo if (work_color >= 0) { 36956183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 3696112202d9STejun Heo pwq->work_color = work_color; 369773f53c4aSTejun Heo } 369873f53c4aSTejun Heo 3699a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 37001da177e4SLinus Torvalds } 37011da177e4SLinus Torvalds 3702112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 370373f53c4aSTejun Heo complete(&wq->first_flusher->done); 370473f53c4aSTejun Heo 370573f53c4aSTejun Heo return wait; 370683c22520SOleg Nesterov } 37071da177e4SLinus Torvalds 3708c35aea39STejun Heo static void touch_wq_lockdep_map(struct workqueue_struct *wq) 3709c35aea39STejun Heo { 37104cb1ef64STejun Heo #ifdef CONFIG_LOCKDEP 37114cb1ef64STejun Heo if (wq->flags & WQ_BH) 37124cb1ef64STejun Heo local_bh_disable(); 37134cb1ef64STejun Heo 3714c35aea39STejun Heo lock_map_acquire(&wq->lockdep_map); 3715c35aea39STejun Heo lock_map_release(&wq->lockdep_map); 37164cb1ef64STejun Heo 37174cb1ef64STejun Heo if (wq->flags & WQ_BH) 37184cb1ef64STejun Heo local_bh_enable(); 37194cb1ef64STejun Heo #endif 3720c35aea39STejun Heo } 3721c35aea39STejun Heo 3722c35aea39STejun Heo static void touch_work_lockdep_map(struct work_struct *work, 3723c35aea39STejun Heo struct workqueue_struct *wq) 3724c35aea39STejun Heo { 37254cb1ef64STejun Heo #ifdef CONFIG_LOCKDEP 37264cb1ef64STejun Heo if (wq->flags & WQ_BH) 37274cb1ef64STejun Heo local_bh_disable(); 37284cb1ef64STejun Heo 3729c35aea39STejun Heo lock_map_acquire(&work->lockdep_map); 3730c35aea39STejun Heo lock_map_release(&work->lockdep_map); 37314cb1ef64STejun Heo 37324cb1ef64STejun Heo if (wq->flags & WQ_BH) 37334cb1ef64STejun Heo local_bh_enable(); 37344cb1ef64STejun Heo #endif 3735c35aea39STejun Heo } 3736c35aea39STejun Heo 37370fcb78c2SRolf Eike Beer /** 3738c4f135d6STetsuo Handa * __flush_workqueue - ensure that any scheduled work has run to completion. 37390fcb78c2SRolf Eike Beer * @wq: workqueue to flush 37401da177e4SLinus Torvalds * 3741c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 3742c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 37431da177e4SLinus Torvalds */ 3744c4f135d6STetsuo Handa void __flush_workqueue(struct workqueue_struct *wq) 37451da177e4SLinus Torvalds { 374673f53c4aSTejun Heo struct wq_flusher this_flusher = { 374773f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 374873f53c4aSTejun Heo .flush_color = -1, 3749fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 375073f53c4aSTejun Heo }; 375173f53c4aSTejun Heo int next_color; 3752b1f4ec17SOleg Nesterov 37533347fa09STejun Heo if (WARN_ON(!wq_online)) 37543347fa09STejun Heo return; 37553347fa09STejun Heo 3756c35aea39STejun Heo touch_wq_lockdep_map(wq); 375787915adcSJohannes Berg 37583c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 375973f53c4aSTejun Heo 376073f53c4aSTejun Heo /* 376173f53c4aSTejun Heo * Start-to-wait phase 376273f53c4aSTejun Heo */ 376373f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 376473f53c4aSTejun Heo 376573f53c4aSTejun Heo if (next_color != wq->flush_color) { 376673f53c4aSTejun Heo /* 376773f53c4aSTejun Heo * Color space is not full. The current work_color 376873f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 376973f53c4aSTejun Heo * by one. 377073f53c4aSTejun Heo */ 37716183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 377273f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 377373f53c4aSTejun Heo wq->work_color = next_color; 377473f53c4aSTejun Heo 377573f53c4aSTejun Heo if (!wq->first_flusher) { 377673f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 37776183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 377873f53c4aSTejun Heo 377973f53c4aSTejun Heo wq->first_flusher = &this_flusher; 378073f53c4aSTejun Heo 3781112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 378273f53c4aSTejun Heo wq->work_color)) { 378373f53c4aSTejun Heo /* nothing to flush, done */ 378473f53c4aSTejun Heo wq->flush_color = next_color; 378573f53c4aSTejun Heo wq->first_flusher = NULL; 378673f53c4aSTejun Heo goto out_unlock; 378773f53c4aSTejun Heo } 378873f53c4aSTejun Heo } else { 378973f53c4aSTejun Heo /* wait in queue */ 37906183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 379173f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 3792112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 379373f53c4aSTejun Heo } 379473f53c4aSTejun Heo } else { 379573f53c4aSTejun Heo /* 379673f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 379773f53c4aSTejun Heo * The next flush completion will assign us 379873f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 379973f53c4aSTejun Heo */ 380073f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 380173f53c4aSTejun Heo } 380273f53c4aSTejun Heo 3803fca839c0STejun Heo check_flush_dependency(wq, NULL); 3804fca839c0STejun Heo 38053c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 380673f53c4aSTejun Heo 380773f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 380873f53c4aSTejun Heo 380973f53c4aSTejun Heo /* 381073f53c4aSTejun Heo * Wake-up-and-cascade phase 381173f53c4aSTejun Heo * 381273f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 381373f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 381473f53c4aSTejun Heo */ 381500d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher) 381673f53c4aSTejun Heo return; 381773f53c4aSTejun Heo 38183c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 381973f53c4aSTejun Heo 38204ce48b37STejun Heo /* we might have raced, check again with mutex held */ 38214ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 38224ce48b37STejun Heo goto out_unlock; 38234ce48b37STejun Heo 382400d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL); 382573f53c4aSTejun Heo 38266183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 38276183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 382873f53c4aSTejun Heo 382973f53c4aSTejun Heo while (true) { 383073f53c4aSTejun Heo struct wq_flusher *next, *tmp; 383173f53c4aSTejun Heo 383273f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 383373f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 383473f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 383573f53c4aSTejun Heo break; 383673f53c4aSTejun Heo list_del_init(&next->list); 383773f53c4aSTejun Heo complete(&next->done); 383873f53c4aSTejun Heo } 383973f53c4aSTejun Heo 38406183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 384173f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 384273f53c4aSTejun Heo 384373f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 384473f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 384573f53c4aSTejun Heo 384673f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 384773f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 384873f53c4aSTejun Heo /* 384973f53c4aSTejun Heo * Assign the same color to all overflowed 385073f53c4aSTejun Heo * flushers, advance work_color and append to 385173f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 385273f53c4aSTejun Heo * phase for these overflowed flushers. 385373f53c4aSTejun Heo */ 385473f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 385573f53c4aSTejun Heo tmp->flush_color = wq->work_color; 385673f53c4aSTejun Heo 385773f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 385873f53c4aSTejun Heo 385973f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 386073f53c4aSTejun Heo &wq->flusher_queue); 3861112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 386273f53c4aSTejun Heo } 386373f53c4aSTejun Heo 386473f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 38656183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 386673f53c4aSTejun Heo break; 386773f53c4aSTejun Heo } 386873f53c4aSTejun Heo 386973f53c4aSTejun Heo /* 387073f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 3871112202d9STejun Heo * the new first flusher and arm pwqs. 387273f53c4aSTejun Heo */ 38736183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 38746183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 387573f53c4aSTejun Heo 387673f53c4aSTejun Heo list_del_init(&next->list); 387773f53c4aSTejun Heo wq->first_flusher = next; 387873f53c4aSTejun Heo 3879112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 388073f53c4aSTejun Heo break; 388173f53c4aSTejun Heo 388273f53c4aSTejun Heo /* 388373f53c4aSTejun Heo * Meh... this color is already done, clear first 388473f53c4aSTejun Heo * flusher and repeat cascading. 388573f53c4aSTejun Heo */ 388673f53c4aSTejun Heo wq->first_flusher = NULL; 388773f53c4aSTejun Heo } 388873f53c4aSTejun Heo 388973f53c4aSTejun Heo out_unlock: 38903c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 38911da177e4SLinus Torvalds } 3892c4f135d6STetsuo Handa EXPORT_SYMBOL(__flush_workqueue); 38931da177e4SLinus Torvalds 38949c5a2ba7STejun Heo /** 38959c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 38969c5a2ba7STejun Heo * @wq: workqueue to drain 38979c5a2ba7STejun Heo * 38989c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 38999c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 39009c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 3901b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 39029c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 39039c5a2ba7STejun Heo * takes too long. 39049c5a2ba7STejun Heo */ 39059c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 39069c5a2ba7STejun Heo { 39079c5a2ba7STejun Heo unsigned int flush_cnt = 0; 390849e3cf44STejun Heo struct pool_workqueue *pwq; 39099c5a2ba7STejun Heo 39109c5a2ba7STejun Heo /* 39119c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 39129c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 3913618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 39149c5a2ba7STejun Heo */ 391587fc741eSLai Jiangshan mutex_lock(&wq->mutex); 39169c5a2ba7STejun Heo if (!wq->nr_drainers++) 3917618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 391887fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 39199c5a2ba7STejun Heo reflush: 3920c4f135d6STetsuo Handa __flush_workqueue(wq); 39219c5a2ba7STejun Heo 3922b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 392376af4d93STejun Heo 392449e3cf44STejun Heo for_each_pwq(pwq, wq) { 3925fa2563e4SThomas Tuttle bool drained; 39269c5a2ba7STejun Heo 3927a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 3928afa87ce8STejun Heo drained = pwq_is_empty(pwq); 3929a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 3930fa2563e4SThomas Tuttle 3931fa2563e4SThomas Tuttle if (drained) 39329c5a2ba7STejun Heo continue; 39339c5a2ba7STejun Heo 39349c5a2ba7STejun Heo if (++flush_cnt == 10 || 39359c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 3936e9ad2eb3SStephen Zhang pr_warn("workqueue %s: %s() isn't complete after %u tries\n", 3937e9ad2eb3SStephen Zhang wq->name, __func__, flush_cnt); 393876af4d93STejun Heo 3939b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 39409c5a2ba7STejun Heo goto reflush; 39419c5a2ba7STejun Heo } 39429c5a2ba7STejun Heo 39439c5a2ba7STejun Heo if (!--wq->nr_drainers) 3944618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 394587fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 39469c5a2ba7STejun Heo } 39479c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 39489c5a2ba7STejun Heo 3949d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 3950d6e89786SJohannes Berg bool from_cancel) 3951baf59022STejun Heo { 3952baf59022STejun Heo struct worker *worker = NULL; 3953c9e7cf27STejun Heo struct worker_pool *pool; 3954112202d9STejun Heo struct pool_workqueue *pwq; 3955c35aea39STejun Heo struct workqueue_struct *wq; 3956baf59022STejun Heo 3957baf59022STejun Heo might_sleep(); 3958baf59022STejun Heo 395924acfb71SThomas Gleixner rcu_read_lock(); 3960fa1b54e6STejun Heo pool = get_work_pool(work); 3961fa1b54e6STejun Heo if (!pool) { 396224acfb71SThomas Gleixner rcu_read_unlock(); 3963fa1b54e6STejun Heo return false; 3964fa1b54e6STejun Heo } 3965fa1b54e6STejun Heo 3966a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 39670b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 3968112202d9STejun Heo pwq = get_work_pwq(work); 3969112202d9STejun Heo if (pwq) { 3970112202d9STejun Heo if (unlikely(pwq->pool != pool)) 3971baf59022STejun Heo goto already_gone; 3972606a5020STejun Heo } else { 3973c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 3974baf59022STejun Heo if (!worker) 3975baf59022STejun Heo goto already_gone; 3976112202d9STejun Heo pwq = worker->current_pwq; 3977606a5020STejun Heo } 3978baf59022STejun Heo 3979c35aea39STejun Heo wq = pwq->wq; 3980c35aea39STejun Heo check_flush_dependency(wq, work); 3981fca839c0STejun Heo 3982112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 3983a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3984baf59022STejun Heo 3985c35aea39STejun Heo touch_work_lockdep_map(work, wq); 3986c35aea39STejun Heo 3987e159489bSTejun Heo /* 3988a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 3989a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 3990a1d14934SPeter Zijlstra * 3991a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 3992a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 3993a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 3994a1d14934SPeter Zijlstra * forward progress. 3995e159489bSTejun Heo */ 3996c35aea39STejun Heo if (!from_cancel && (wq->saved_max_active == 1 || wq->rescuer)) 3997c35aea39STejun Heo touch_wq_lockdep_map(wq); 3998c35aea39STejun Heo 399924acfb71SThomas Gleixner rcu_read_unlock(); 4000baf59022STejun Heo return true; 4001baf59022STejun Heo already_gone: 4002a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 400324acfb71SThomas Gleixner rcu_read_unlock(); 4004baf59022STejun Heo return false; 4005baf59022STejun Heo } 4006baf59022STejun Heo 4007d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 4008d6e89786SJohannes Berg { 4009d6e89786SJohannes Berg struct wq_barrier barr; 4010d6e89786SJohannes Berg 4011d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 4012d6e89786SJohannes Berg return false; 4013d6e89786SJohannes Berg 40144d43d395STetsuo Handa if (WARN_ON(!work->func)) 40154d43d395STetsuo Handa return false; 40164d43d395STetsuo Handa 4017d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 4018d6e89786SJohannes Berg wait_for_completion(&barr.done); 4019d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 4020d6e89786SJohannes Berg return true; 4021d6e89786SJohannes Berg } else { 4022d6e89786SJohannes Berg return false; 4023d6e89786SJohannes Berg } 4024d6e89786SJohannes Berg } 4025d6e89786SJohannes Berg 4026db700897SOleg Nesterov /** 4027401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 4028401a8d04STejun Heo * @work: the work to flush 4029db700897SOleg Nesterov * 4030606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 4031606a5020STejun Heo * on return if it hasn't been requeued since flush started. 4032401a8d04STejun Heo * 4033d185af30SYacine Belkadi * Return: 4034401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 4035401a8d04STejun Heo * %false if it was already idle. 4036db700897SOleg Nesterov */ 4037401a8d04STejun Heo bool flush_work(struct work_struct *work) 4038db700897SOleg Nesterov { 4039d6e89786SJohannes Berg return __flush_work(work, false); 4040606a5020STejun Heo } 4041db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 4042db700897SOleg Nesterov 40438603e1b3STejun Heo struct cwt_wait { 4044ac6424b9SIngo Molnar wait_queue_entry_t wait; 40458603e1b3STejun Heo struct work_struct *work; 40468603e1b3STejun Heo }; 40478603e1b3STejun Heo 4048ac6424b9SIngo Molnar static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 40498603e1b3STejun Heo { 40508603e1b3STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 40518603e1b3STejun Heo 40528603e1b3STejun Heo if (cwait->work != key) 40538603e1b3STejun Heo return 0; 40548603e1b3STejun Heo return autoremove_wake_function(wait, mode, sync, key); 40558603e1b3STejun Heo } 40568603e1b3STejun Heo 405736e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 4058401a8d04STejun Heo { 40598603e1b3STejun Heo static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); 4060bbb68dfaSTejun Heo unsigned long flags; 40611f1f642eSOleg Nesterov int ret; 40621f1f642eSOleg Nesterov 40631f1f642eSOleg Nesterov do { 4064bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 4065bbb68dfaSTejun Heo /* 40668603e1b3STejun Heo * If someone else is already canceling, wait for it to 40678603e1b3STejun Heo * finish. flush_work() doesn't work for PREEMPT_NONE 40688603e1b3STejun Heo * because we may get scheduled between @work's completion 40698603e1b3STejun Heo * and the other canceling task resuming and clearing 40708603e1b3STejun Heo * CANCELING - flush_work() will return false immediately 40718603e1b3STejun Heo * as @work is no longer busy, try_to_grab_pending() will 40728603e1b3STejun Heo * return -ENOENT as @work is still being canceled and the 40738603e1b3STejun Heo * other canceling task won't be able to clear CANCELING as 40748603e1b3STejun Heo * we're hogging the CPU. 40758603e1b3STejun Heo * 40768603e1b3STejun Heo * Let's wait for completion using a waitqueue. As this 40778603e1b3STejun Heo * may lead to the thundering herd problem, use a custom 40788603e1b3STejun Heo * wake function which matches @work along with exclusive 40798603e1b3STejun Heo * wait and wakeup. 4080bbb68dfaSTejun Heo */ 40818603e1b3STejun Heo if (unlikely(ret == -ENOENT)) { 40828603e1b3STejun Heo struct cwt_wait cwait; 40838603e1b3STejun Heo 40848603e1b3STejun Heo init_wait(&cwait.wait); 40858603e1b3STejun Heo cwait.wait.func = cwt_wakefn; 40868603e1b3STejun Heo cwait.work = work; 40878603e1b3STejun Heo 40888603e1b3STejun Heo prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, 40898603e1b3STejun Heo TASK_UNINTERRUPTIBLE); 40908603e1b3STejun Heo if (work_is_canceling(work)) 40918603e1b3STejun Heo schedule(); 40928603e1b3STejun Heo finish_wait(&cancel_waitq, &cwait.wait); 40938603e1b3STejun Heo } 40941f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 40951f1f642eSOleg Nesterov 4096bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 4097bbb68dfaSTejun Heo mark_work_canceling(work); 4098bbb68dfaSTejun Heo local_irq_restore(flags); 4099bbb68dfaSTejun Heo 41003347fa09STejun Heo /* 41013347fa09STejun Heo * This allows canceling during early boot. We know that @work 41023347fa09STejun Heo * isn't executing. 41033347fa09STejun Heo */ 41043347fa09STejun Heo if (wq_online) 4105d6e89786SJohannes Berg __flush_work(work, true); 41063347fa09STejun Heo 41077a22ad75STejun Heo clear_work_data(work); 41088603e1b3STejun Heo 41098603e1b3STejun Heo /* 41108603e1b3STejun Heo * Paired with prepare_to_wait() above so that either 41118603e1b3STejun Heo * waitqueue_active() is visible here or !work_is_canceling() is 41128603e1b3STejun Heo * visible there. 41138603e1b3STejun Heo */ 41148603e1b3STejun Heo smp_mb(); 41158603e1b3STejun Heo if (waitqueue_active(&cancel_waitq)) 41168603e1b3STejun Heo __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); 41178603e1b3STejun Heo 41181f1f642eSOleg Nesterov return ret; 41191f1f642eSOleg Nesterov } 41201f1f642eSOleg Nesterov 41216e84d644SOleg Nesterov /** 4122401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 4123401a8d04STejun Heo * @work: the work to cancel 41246e84d644SOleg Nesterov * 4125401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 4126401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 4127401a8d04STejun Heo * another workqueue. On return from this function, @work is 4128401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 41291f1f642eSOleg Nesterov * 4130401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 4131401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 41326e84d644SOleg Nesterov * 4133401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 41346e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 4135401a8d04STejun Heo * 4136d185af30SYacine Belkadi * Return: 4137401a8d04STejun Heo * %true if @work was pending, %false otherwise. 41386e84d644SOleg Nesterov */ 4139401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 41406e84d644SOleg Nesterov { 414136e227d2STejun Heo return __cancel_work_timer(work, false); 4142b89deed3SOleg Nesterov } 414328e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 4144b89deed3SOleg Nesterov 41456e84d644SOleg Nesterov /** 4146401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 4147401a8d04STejun Heo * @dwork: the delayed work to flush 41486e84d644SOleg Nesterov * 4149401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 4150401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 4151401a8d04STejun Heo * considers the last queueing instance of @dwork. 41521f1f642eSOleg Nesterov * 4153d185af30SYacine Belkadi * Return: 4154401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 4155401a8d04STejun Heo * %false if it was already idle. 41566e84d644SOleg Nesterov */ 4157401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 4158401a8d04STejun Heo { 41598930cabaSTejun Heo local_irq_disable(); 4160401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 416160c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 41628930cabaSTejun Heo local_irq_enable(); 4163401a8d04STejun Heo return flush_work(&dwork->work); 4164401a8d04STejun Heo } 4165401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 4166401a8d04STejun Heo 416705f0fe6bSTejun Heo /** 416805f0fe6bSTejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 416905f0fe6bSTejun Heo * @rwork: the rcu work to flush 417005f0fe6bSTejun Heo * 417105f0fe6bSTejun Heo * Return: 417205f0fe6bSTejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 417305f0fe6bSTejun Heo * %false if it was already idle. 417405f0fe6bSTejun Heo */ 417505f0fe6bSTejun Heo bool flush_rcu_work(struct rcu_work *rwork) 417605f0fe6bSTejun Heo { 417705f0fe6bSTejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 417805f0fe6bSTejun Heo rcu_barrier(); 417905f0fe6bSTejun Heo flush_work(&rwork->work); 418005f0fe6bSTejun Heo return true; 418105f0fe6bSTejun Heo } else { 418205f0fe6bSTejun Heo return flush_work(&rwork->work); 418305f0fe6bSTejun Heo } 418405f0fe6bSTejun Heo } 418505f0fe6bSTejun Heo EXPORT_SYMBOL(flush_rcu_work); 418605f0fe6bSTejun Heo 4187f72b8792SJens Axboe static bool __cancel_work(struct work_struct *work, bool is_dwork) 4188f72b8792SJens Axboe { 4189f72b8792SJens Axboe unsigned long flags; 4190f72b8792SJens Axboe int ret; 4191f72b8792SJens Axboe 4192f72b8792SJens Axboe do { 4193f72b8792SJens Axboe ret = try_to_grab_pending(work, is_dwork, &flags); 4194f72b8792SJens Axboe } while (unlikely(ret == -EAGAIN)); 4195f72b8792SJens Axboe 4196f72b8792SJens Axboe if (unlikely(ret < 0)) 4197f72b8792SJens Axboe return false; 4198f72b8792SJens Axboe 4199f72b8792SJens Axboe set_work_pool_and_clear_pending(work, get_work_pool_id(work)); 4200f72b8792SJens Axboe local_irq_restore(flags); 4201f72b8792SJens Axboe return ret; 4202f72b8792SJens Axboe } 4203f72b8792SJens Axboe 420473b4b532SAndrey Grodzovsky /* 420573b4b532SAndrey Grodzovsky * See cancel_delayed_work() 420673b4b532SAndrey Grodzovsky */ 420773b4b532SAndrey Grodzovsky bool cancel_work(struct work_struct *work) 420873b4b532SAndrey Grodzovsky { 420973b4b532SAndrey Grodzovsky return __cancel_work(work, false); 421073b4b532SAndrey Grodzovsky } 421173b4b532SAndrey Grodzovsky EXPORT_SYMBOL(cancel_work); 421273b4b532SAndrey Grodzovsky 4213401a8d04STejun Heo /** 421457b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 421557b30ae7STejun Heo * @dwork: delayed_work to cancel 421609383498STejun Heo * 4217d185af30SYacine Belkadi * Kill off a pending delayed_work. 4218d185af30SYacine Belkadi * 4219d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 4220d185af30SYacine Belkadi * pending. 4221d185af30SYacine Belkadi * 4222d185af30SYacine Belkadi * Note: 4223d185af30SYacine Belkadi * The work callback function may still be running on return, unless 4224d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 4225d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 422609383498STejun Heo * 422757b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 422809383498STejun Heo */ 422957b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 423009383498STejun Heo { 4231f72b8792SJens Axboe return __cancel_work(&dwork->work, true); 423209383498STejun Heo } 423357b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 423409383498STejun Heo 423509383498STejun Heo /** 4236401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 4237401a8d04STejun Heo * @dwork: the delayed work cancel 4238401a8d04STejun Heo * 4239401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 4240401a8d04STejun Heo * 4241d185af30SYacine Belkadi * Return: 4242401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 4243401a8d04STejun Heo */ 4244401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 42456e84d644SOleg Nesterov { 424636e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 42476e84d644SOleg Nesterov } 4248f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 42491da177e4SLinus Torvalds 42500fcb78c2SRolf Eike Beer /** 425131ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 4252b6136773SAndrew Morton * @func: the function to call 4253b6136773SAndrew Morton * 425431ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 425531ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 4256b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 425731ddd871STejun Heo * 4258d185af30SYacine Belkadi * Return: 425931ddd871STejun Heo * 0 on success, -errno on failure. 4260b6136773SAndrew Morton */ 426165f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 426215316ba8SChristoph Lameter { 426315316ba8SChristoph Lameter int cpu; 426438f51568SNamhyung Kim struct work_struct __percpu *works; 426515316ba8SChristoph Lameter 4266b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 4267b6136773SAndrew Morton if (!works) 426815316ba8SChristoph Lameter return -ENOMEM; 4269b6136773SAndrew Morton 4270ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 427193981800STejun Heo 427215316ba8SChristoph Lameter for_each_online_cpu(cpu) { 42739bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 42749bfb1839SIngo Molnar 42759bfb1839SIngo Molnar INIT_WORK(work, func); 42768de6d308SOleg Nesterov schedule_work_on(cpu, work); 427715316ba8SChristoph Lameter } 427893981800STejun Heo 427993981800STejun Heo for_each_online_cpu(cpu) 42808616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 428193981800STejun Heo 4282ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4283b6136773SAndrew Morton free_percpu(works); 428415316ba8SChristoph Lameter return 0; 428515316ba8SChristoph Lameter } 428615316ba8SChristoph Lameter 4287eef6a7d5SAlan Stern /** 42881fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 42891fa44ecaSJames Bottomley * @fn: the function to execute 42901fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 42911fa44ecaSJames Bottomley * be available when the work executes) 42921fa44ecaSJames Bottomley * 42931fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 42941fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 42951fa44ecaSJames Bottomley * 4296d185af30SYacine Belkadi * Return: 0 - function was executed 42971fa44ecaSJames Bottomley * 1 - function was scheduled for execution 42981fa44ecaSJames Bottomley */ 429965f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 43001fa44ecaSJames Bottomley { 43011fa44ecaSJames Bottomley if (!in_interrupt()) { 430265f27f38SDavid Howells fn(&ew->work); 43031fa44ecaSJames Bottomley return 0; 43041fa44ecaSJames Bottomley } 43051fa44ecaSJames Bottomley 430665f27f38SDavid Howells INIT_WORK(&ew->work, fn); 43071fa44ecaSJames Bottomley schedule_work(&ew->work); 43081fa44ecaSJames Bottomley 43091fa44ecaSJames Bottomley return 1; 43101fa44ecaSJames Bottomley } 43111fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 43121fa44ecaSJames Bottomley 43137a4e344cSTejun Heo /** 43147a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 43157a4e344cSTejun Heo * @attrs: workqueue_attrs to free 43167a4e344cSTejun Heo * 43177a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 43187a4e344cSTejun Heo */ 4319513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs) 43207a4e344cSTejun Heo { 43217a4e344cSTejun Heo if (attrs) { 43227a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 43239546b29eSTejun Heo free_cpumask_var(attrs->__pod_cpumask); 43247a4e344cSTejun Heo kfree(attrs); 43257a4e344cSTejun Heo } 43267a4e344cSTejun Heo } 43277a4e344cSTejun Heo 43287a4e344cSTejun Heo /** 43297a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 43307a4e344cSTejun Heo * 43317a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 4332d185af30SYacine Belkadi * return it. 4333d185af30SYacine Belkadi * 4334d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 43357a4e344cSTejun Heo */ 4336513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void) 43377a4e344cSTejun Heo { 43387a4e344cSTejun Heo struct workqueue_attrs *attrs; 43397a4e344cSTejun Heo 4340be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 43417a4e344cSTejun Heo if (!attrs) 43427a4e344cSTejun Heo goto fail; 4343be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) 43447a4e344cSTejun Heo goto fail; 43459546b29eSTejun Heo if (!alloc_cpumask_var(&attrs->__pod_cpumask, GFP_KERNEL)) 43469546b29eSTejun Heo goto fail; 43477a4e344cSTejun Heo 434813e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 4349523a301eSTejun Heo attrs->affn_scope = WQ_AFFN_DFL; 43507a4e344cSTejun Heo return attrs; 43517a4e344cSTejun Heo fail: 43527a4e344cSTejun Heo free_workqueue_attrs(attrs); 43537a4e344cSTejun Heo return NULL; 43547a4e344cSTejun Heo } 43557a4e344cSTejun Heo 435629c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 435729c91e99STejun Heo const struct workqueue_attrs *from) 435829c91e99STejun Heo { 435929c91e99STejun Heo to->nice = from->nice; 436029c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 43619546b29eSTejun Heo cpumask_copy(to->__pod_cpumask, from->__pod_cpumask); 43628639ecebSTejun Heo to->affn_strict = from->affn_strict; 436384193c07STejun Heo 43642865a8fbSShaohua Li /* 436584193c07STejun Heo * Unlike hash and equality test, copying shouldn't ignore wq-only 436684193c07STejun Heo * fields as copying is used for both pool and wq attrs. Instead, 436784193c07STejun Heo * get_unbound_pool() explicitly clears the fields. 43682865a8fbSShaohua Li */ 436984193c07STejun Heo to->affn_scope = from->affn_scope; 4370af73f5c9STejun Heo to->ordered = from->ordered; 437129c91e99STejun Heo } 437229c91e99STejun Heo 43735de7a03cSTejun Heo /* 43745de7a03cSTejun Heo * Some attrs fields are workqueue-only. Clear them for worker_pool's. See the 43755de7a03cSTejun Heo * comments in 'struct workqueue_attrs' definition. 43765de7a03cSTejun Heo */ 43775de7a03cSTejun Heo static void wqattrs_clear_for_pool(struct workqueue_attrs *attrs) 43785de7a03cSTejun Heo { 437984193c07STejun Heo attrs->affn_scope = WQ_AFFN_NR_TYPES; 43805de7a03cSTejun Heo attrs->ordered = false; 43815de7a03cSTejun Heo } 43825de7a03cSTejun Heo 438329c91e99STejun Heo /* hash value of the content of @attr */ 438429c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 438529c91e99STejun Heo { 438629c91e99STejun Heo u32 hash = 0; 438729c91e99STejun Heo 438829c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 438913e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 439013e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 43919546b29eSTejun Heo hash = jhash(cpumask_bits(attrs->__pod_cpumask), 43929546b29eSTejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 43938639ecebSTejun Heo hash = jhash_1word(attrs->affn_strict, hash); 439429c91e99STejun Heo return hash; 439529c91e99STejun Heo } 439629c91e99STejun Heo 439729c91e99STejun Heo /* content equality test */ 439829c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 439929c91e99STejun Heo const struct workqueue_attrs *b) 440029c91e99STejun Heo { 440129c91e99STejun Heo if (a->nice != b->nice) 440229c91e99STejun Heo return false; 440329c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 440429c91e99STejun Heo return false; 44059546b29eSTejun Heo if (!cpumask_equal(a->__pod_cpumask, b->__pod_cpumask)) 44069546b29eSTejun Heo return false; 44078639ecebSTejun Heo if (a->affn_strict != b->affn_strict) 44088639ecebSTejun Heo return false; 440929c91e99STejun Heo return true; 441029c91e99STejun Heo } 441129c91e99STejun Heo 44120f36ee24STejun Heo /* Update @attrs with actually available CPUs */ 44130f36ee24STejun Heo static void wqattrs_actualize_cpumask(struct workqueue_attrs *attrs, 44140f36ee24STejun Heo const cpumask_t *unbound_cpumask) 44150f36ee24STejun Heo { 44160f36ee24STejun Heo /* 44170f36ee24STejun Heo * Calculate the effective CPU mask of @attrs given @unbound_cpumask. If 44180f36ee24STejun Heo * @attrs->cpumask doesn't overlap with @unbound_cpumask, we fallback to 44190f36ee24STejun Heo * @unbound_cpumask. 44200f36ee24STejun Heo */ 44210f36ee24STejun Heo cpumask_and(attrs->cpumask, attrs->cpumask, unbound_cpumask); 44220f36ee24STejun Heo if (unlikely(cpumask_empty(attrs->cpumask))) 44230f36ee24STejun Heo cpumask_copy(attrs->cpumask, unbound_cpumask); 44240f36ee24STejun Heo } 44250f36ee24STejun Heo 442684193c07STejun Heo /* find wq_pod_type to use for @attrs */ 442784193c07STejun Heo static const struct wq_pod_type * 442884193c07STejun Heo wqattrs_pod_type(const struct workqueue_attrs *attrs) 442984193c07STejun Heo { 4430523a301eSTejun Heo enum wq_affn_scope scope; 4431523a301eSTejun Heo struct wq_pod_type *pt; 4432523a301eSTejun Heo 4433523a301eSTejun Heo /* to synchronize access to wq_affn_dfl */ 4434523a301eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4435523a301eSTejun Heo 4436523a301eSTejun Heo if (attrs->affn_scope == WQ_AFFN_DFL) 4437523a301eSTejun Heo scope = wq_affn_dfl; 4438523a301eSTejun Heo else 4439523a301eSTejun Heo scope = attrs->affn_scope; 4440523a301eSTejun Heo 4441523a301eSTejun Heo pt = &wq_pod_types[scope]; 444284193c07STejun Heo 444384193c07STejun Heo if (!WARN_ON_ONCE(attrs->affn_scope == WQ_AFFN_NR_TYPES) && 444484193c07STejun Heo likely(pt->nr_pods)) 444584193c07STejun Heo return pt; 444684193c07STejun Heo 444784193c07STejun Heo /* 444884193c07STejun Heo * Before workqueue_init_topology(), only SYSTEM is available which is 444984193c07STejun Heo * initialized in workqueue_init_early(). 445084193c07STejun Heo */ 445184193c07STejun Heo pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 445284193c07STejun Heo BUG_ON(!pt->nr_pods); 445384193c07STejun Heo return pt; 445484193c07STejun Heo } 445584193c07STejun Heo 44567a4e344cSTejun Heo /** 44577a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 44587a4e344cSTejun Heo * @pool: worker_pool to initialize 44597a4e344cSTejun Heo * 4460402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 4461d185af30SYacine Belkadi * 4462d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 446329c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 446429c91e99STejun Heo * on @pool safely to release it. 44657a4e344cSTejun Heo */ 44667a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 44674e1a1f9aSTejun Heo { 4468a9b8a985SSebastian Andrzej Siewior raw_spin_lock_init(&pool->lock); 446929c91e99STejun Heo pool->id = -1; 447029c91e99STejun Heo pool->cpu = -1; 4471f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 44724e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 447382607adcSTejun Heo pool->watchdog_ts = jiffies; 44744e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 44754e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 44764e1a1f9aSTejun Heo hash_init(pool->busy_hash); 44774e1a1f9aSTejun Heo 447832a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 44793f959aa3SValentin Schneider INIT_WORK(&pool->idle_cull_work, idle_cull_fn); 44804e1a1f9aSTejun Heo 448132a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 44824e1a1f9aSTejun Heo 4483da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 4484e02b9312SValentin Schneider INIT_LIST_HEAD(&pool->dying_workers); 44857a4e344cSTejun Heo 44867cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 448729c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 448829c91e99STejun Heo pool->refcnt = 1; 448929c91e99STejun Heo 449029c91e99STejun Heo /* shouldn't fail above this point */ 4491be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs(); 44927a4e344cSTejun Heo if (!pool->attrs) 44937a4e344cSTejun Heo return -ENOMEM; 44945de7a03cSTejun Heo 44955de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 44965de7a03cSTejun Heo 44977a4e344cSTejun Heo return 0; 44984e1a1f9aSTejun Heo } 44994e1a1f9aSTejun Heo 4500669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP 4501669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4502669de8bdSBart Van Assche { 4503669de8bdSBart Van Assche char *lock_name; 4504669de8bdSBart Van Assche 4505669de8bdSBart Van Assche lockdep_register_key(&wq->key); 4506669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); 4507669de8bdSBart Van Assche if (!lock_name) 4508669de8bdSBart Van Assche lock_name = wq->name; 450969a106c0SQian Cai 451069a106c0SQian Cai wq->lock_name = lock_name; 4511669de8bdSBart Van Assche lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); 4512669de8bdSBart Van Assche } 4513669de8bdSBart Van Assche 4514669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4515669de8bdSBart Van Assche { 4516669de8bdSBart Van Assche lockdep_unregister_key(&wq->key); 4517669de8bdSBart Van Assche } 4518669de8bdSBart Van Assche 4519669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4520669de8bdSBart Van Assche { 4521669de8bdSBart Van Assche if (wq->lock_name != wq->name) 4522669de8bdSBart Van Assche kfree(wq->lock_name); 4523669de8bdSBart Van Assche } 4524669de8bdSBart Van Assche #else 4525669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4526669de8bdSBart Van Assche { 4527669de8bdSBart Van Assche } 4528669de8bdSBart Van Assche 4529669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4530669de8bdSBart Van Assche { 4531669de8bdSBart Van Assche } 4532669de8bdSBart Van Assche 4533669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4534669de8bdSBart Van Assche { 4535669de8bdSBart Van Assche } 4536669de8bdSBart Van Assche #endif 4537669de8bdSBart Van Assche 453891ccc6e7STejun Heo static void free_node_nr_active(struct wq_node_nr_active **nna_ar) 453991ccc6e7STejun Heo { 454091ccc6e7STejun Heo int node; 454191ccc6e7STejun Heo 454291ccc6e7STejun Heo for_each_node(node) { 454391ccc6e7STejun Heo kfree(nna_ar[node]); 454491ccc6e7STejun Heo nna_ar[node] = NULL; 454591ccc6e7STejun Heo } 454691ccc6e7STejun Heo 454791ccc6e7STejun Heo kfree(nna_ar[nr_node_ids]); 454891ccc6e7STejun Heo nna_ar[nr_node_ids] = NULL; 454991ccc6e7STejun Heo } 455091ccc6e7STejun Heo 455191ccc6e7STejun Heo static void init_node_nr_active(struct wq_node_nr_active *nna) 455291ccc6e7STejun Heo { 4553c5f8cd6cSTejun Heo nna->max = WQ_DFL_MIN_ACTIVE; 455491ccc6e7STejun Heo atomic_set(&nna->nr, 0); 45555797b1c1STejun Heo raw_spin_lock_init(&nna->lock); 45565797b1c1STejun Heo INIT_LIST_HEAD(&nna->pending_pwqs); 455791ccc6e7STejun Heo } 455891ccc6e7STejun Heo 455991ccc6e7STejun Heo /* 456091ccc6e7STejun Heo * Each node's nr_active counter will be accessed mostly from its own node and 456191ccc6e7STejun Heo * should be allocated in the node. 456291ccc6e7STejun Heo */ 456391ccc6e7STejun Heo static int alloc_node_nr_active(struct wq_node_nr_active **nna_ar) 456491ccc6e7STejun Heo { 456591ccc6e7STejun Heo struct wq_node_nr_active *nna; 456691ccc6e7STejun Heo int node; 456791ccc6e7STejun Heo 456891ccc6e7STejun Heo for_each_node(node) { 456991ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, node); 457091ccc6e7STejun Heo if (!nna) 457191ccc6e7STejun Heo goto err_free; 457291ccc6e7STejun Heo init_node_nr_active(nna); 457391ccc6e7STejun Heo nna_ar[node] = nna; 457491ccc6e7STejun Heo } 457591ccc6e7STejun Heo 457691ccc6e7STejun Heo /* [nr_node_ids] is used as the fallback */ 457791ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, NUMA_NO_NODE); 457891ccc6e7STejun Heo if (!nna) 457991ccc6e7STejun Heo goto err_free; 458091ccc6e7STejun Heo init_node_nr_active(nna); 458191ccc6e7STejun Heo nna_ar[nr_node_ids] = nna; 458291ccc6e7STejun Heo 458391ccc6e7STejun Heo return 0; 458491ccc6e7STejun Heo 458591ccc6e7STejun Heo err_free: 458691ccc6e7STejun Heo free_node_nr_active(nna_ar); 458791ccc6e7STejun Heo return -ENOMEM; 458891ccc6e7STejun Heo } 458991ccc6e7STejun Heo 4590e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 4591e2dca7adSTejun Heo { 4592e2dca7adSTejun Heo struct workqueue_struct *wq = 4593e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 4594e2dca7adSTejun Heo 459591ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 459691ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 459791ccc6e7STejun Heo 4598669de8bdSBart Van Assche wq_free_lockdep(wq); 4599ee1ceef7STejun Heo free_percpu(wq->cpu_pwq); 4600e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 4601e2dca7adSTejun Heo kfree(wq); 4602e2dca7adSTejun Heo } 4603e2dca7adSTejun Heo 460429c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 460529c91e99STejun Heo { 460629c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 460729c91e99STejun Heo 46087cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 460929c91e99STejun Heo free_workqueue_attrs(pool->attrs); 461029c91e99STejun Heo kfree(pool); 461129c91e99STejun Heo } 461229c91e99STejun Heo 461329c91e99STejun Heo /** 461429c91e99STejun Heo * put_unbound_pool - put a worker_pool 461529c91e99STejun Heo * @pool: worker_pool to put 461629c91e99STejun Heo * 461724acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU 4618c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 4619c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 4620c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 4621a892caccSTejun Heo * 4622a892caccSTejun Heo * Should be called with wq_pool_mutex held. 462329c91e99STejun Heo */ 462429c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 462529c91e99STejun Heo { 462660f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 462729c91e99STejun Heo struct worker *worker; 46289680540cSYang Yingliang LIST_HEAD(cull_list); 4629e02b9312SValentin Schneider 4630a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4631a892caccSTejun Heo 4632a892caccSTejun Heo if (--pool->refcnt) 463329c91e99STejun Heo return; 463429c91e99STejun Heo 463529c91e99STejun Heo /* sanity checks */ 463661d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 4637a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 463829c91e99STejun Heo return; 463929c91e99STejun Heo 464029c91e99STejun Heo /* release id and unhash */ 464129c91e99STejun Heo if (pool->id >= 0) 464229c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 464329c91e99STejun Heo hash_del(&pool->hash_node); 464429c91e99STejun Heo 4645c5aa87bbSTejun Heo /* 4646692b4825STejun Heo * Become the manager and destroy all workers. This prevents 4647692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 4648692b4825STejun Heo * manager and @pool gets freed with the flag set. 46499ab03be4SValentin Schneider * 46509ab03be4SValentin Schneider * Having a concurrent manager is quite unlikely to happen as we can 46519ab03be4SValentin Schneider * only get here with 46529ab03be4SValentin Schneider * pwq->refcnt == pool->refcnt == 0 46539ab03be4SValentin Schneider * which implies no work queued to the pool, which implies no worker can 46549ab03be4SValentin Schneider * become the manager. However a worker could have taken the role of 46559ab03be4SValentin Schneider * manager before the refcnts dropped to 0, since maybe_create_worker() 46569ab03be4SValentin Schneider * drops pool->lock 4657c5aa87bbSTejun Heo */ 46589ab03be4SValentin Schneider while (true) { 46599ab03be4SValentin Schneider rcuwait_wait_event(&manager_wait, 46609ab03be4SValentin Schneider !(pool->flags & POOL_MANAGER_ACTIVE), 4661d8bb65abSSebastian Andrzej Siewior TASK_UNINTERRUPTIBLE); 4662e02b9312SValentin Schneider 4663e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 46649ab03be4SValentin Schneider raw_spin_lock_irq(&pool->lock); 46659ab03be4SValentin Schneider if (!(pool->flags & POOL_MANAGER_ACTIVE)) { 4666692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 46679ab03be4SValentin Schneider break; 46689ab03be4SValentin Schneider } 46699ab03be4SValentin Schneider raw_spin_unlock_irq(&pool->lock); 4670e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 46719ab03be4SValentin Schneider } 4672692b4825STejun Heo 46731037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 4674e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 467529c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 4676a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 467760f5a4bcSLai Jiangshan 4678e02b9312SValentin Schneider wake_dying_workers(&cull_list); 4679e02b9312SValentin Schneider 4680e02b9312SValentin Schneider if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) 468160f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 46821258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 468360f5a4bcSLai Jiangshan 468460f5a4bcSLai Jiangshan if (pool->detach_completion) 468560f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 468660f5a4bcSLai Jiangshan 468729c91e99STejun Heo /* shut down the timers */ 468829c91e99STejun Heo del_timer_sync(&pool->idle_timer); 46893f959aa3SValentin Schneider cancel_work_sync(&pool->idle_cull_work); 469029c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 469129c91e99STejun Heo 469224acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */ 469325b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 469429c91e99STejun Heo } 469529c91e99STejun Heo 469629c91e99STejun Heo /** 469729c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 469829c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 469929c91e99STejun Heo * 470029c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 470129c91e99STejun Heo * reference count and return it. If there already is a matching 470229c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 4703d185af30SYacine Belkadi * create a new one. 4704a892caccSTejun Heo * 4705a892caccSTejun Heo * Should be called with wq_pool_mutex held. 4706d185af30SYacine Belkadi * 4707d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 4708d185af30SYacine Belkadi * On failure, %NULL. 470929c91e99STejun Heo */ 471029c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 471129c91e99STejun Heo { 471284193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_NUMA]; 471329c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 471429c91e99STejun Heo struct worker_pool *pool; 471584193c07STejun Heo int pod, node = NUMA_NO_NODE; 471629c91e99STejun Heo 4717a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 471829c91e99STejun Heo 471929c91e99STejun Heo /* do we already have a matching pool? */ 472029c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 472129c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 472229c91e99STejun Heo pool->refcnt++; 47233fb1823cSLai Jiangshan return pool; 472429c91e99STejun Heo } 472529c91e99STejun Heo } 472629c91e99STejun Heo 47279546b29eSTejun Heo /* If __pod_cpumask is contained inside a NUMA pod, that's our node */ 472884193c07STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) { 47299546b29eSTejun Heo if (cpumask_subset(attrs->__pod_cpumask, pt->pod_cpus[pod])) { 473084193c07STejun Heo node = pt->pod_node[pod]; 4731e2273584SXunlei Pang break; 4732e2273584SXunlei Pang } 4733e2273584SXunlei Pang } 4734e2273584SXunlei Pang 473529c91e99STejun Heo /* nope, create a new one */ 473684193c07STejun Heo pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, node); 473729c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 473829c91e99STejun Heo goto fail; 473929c91e99STejun Heo 474084193c07STejun Heo pool->node = node; 47415de7a03cSTejun Heo copy_workqueue_attrs(pool->attrs, attrs); 47425de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 47432865a8fbSShaohua Li 474429c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 474529c91e99STejun Heo goto fail; 474629c91e99STejun Heo 474729c91e99STejun Heo /* create and start the initial worker */ 47483347fa09STejun Heo if (wq_online && !create_worker(pool)) 474929c91e99STejun Heo goto fail; 475029c91e99STejun Heo 475129c91e99STejun Heo /* install */ 475229c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 47533fb1823cSLai Jiangshan 475429c91e99STejun Heo return pool; 475529c91e99STejun Heo fail: 475629c91e99STejun Heo if (pool) 475729c91e99STejun Heo put_unbound_pool(pool); 475829c91e99STejun Heo return NULL; 475929c91e99STejun Heo } 476029c91e99STejun Heo 47618864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 47628864b4e5STejun Heo { 47638864b4e5STejun Heo kmem_cache_free(pwq_cache, 47648864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 47658864b4e5STejun Heo } 47668864b4e5STejun Heo 47678864b4e5STejun Heo /* 4768967b494eSTejun Heo * Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero 4769967b494eSTejun Heo * refcnt and needs to be destroyed. 47708864b4e5STejun Heo */ 4771687a9aa5STejun Heo static void pwq_release_workfn(struct kthread_work *work) 47728864b4e5STejun Heo { 47738864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 4774687a9aa5STejun Heo release_work); 47758864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 47768864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 4777b42b0bddSYang Yingliang bool is_last = false; 47788864b4e5STejun Heo 4779b42b0bddSYang Yingliang /* 4780687a9aa5STejun Heo * When @pwq is not linked, it doesn't hold any reference to the 4781b42b0bddSYang Yingliang * @wq, and @wq is invalid to access. 4782b42b0bddSYang Yingliang */ 4783b42b0bddSYang Yingliang if (!list_empty(&pwq->pwqs_node)) { 47843c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 47858864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 4786bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 47874c065dbcSWaiman Long 47884c065dbcSWaiman Long /* 47894c065dbcSWaiman Long * For ordered workqueue with a plugged dfl_pwq, restart it now. 47904c065dbcSWaiman Long */ 47914c065dbcSWaiman Long if (!is_last && (wq->flags & __WQ_ORDERED)) 47924c065dbcSWaiman Long unplug_oldest_pwq(wq); 47934c065dbcSWaiman Long 47943c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 4795b42b0bddSYang Yingliang } 47968864b4e5STejun Heo 4797687a9aa5STejun Heo if (wq->flags & WQ_UNBOUND) { 4798a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 47998864b4e5STejun Heo put_unbound_pool(pool); 4800a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 4801687a9aa5STejun Heo } 4802a892caccSTejun Heo 48035797b1c1STejun Heo if (!list_empty(&pwq->pending_node)) { 48045797b1c1STejun Heo struct wq_node_nr_active *nna = 48055797b1c1STejun Heo wq_node_nr_active(pwq->wq, pwq->pool->node); 48065797b1c1STejun Heo 48075797b1c1STejun Heo raw_spin_lock_irq(&nna->lock); 48085797b1c1STejun Heo list_del_init(&pwq->pending_node); 48095797b1c1STejun Heo raw_spin_unlock_irq(&nna->lock); 48105797b1c1STejun Heo } 48115797b1c1STejun Heo 481225b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 48138864b4e5STejun Heo 48148864b4e5STejun Heo /* 48158864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 4816e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 48178864b4e5STejun Heo */ 4818669de8bdSBart Van Assche if (is_last) { 4819669de8bdSBart Van Assche wq_unregister_lockdep(wq); 482025b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 48216029a918STejun Heo } 4822669de8bdSBart Van Assche } 48238864b4e5STejun Heo 482467dc8325SCai Huoqing /* initialize newly allocated @pwq which is associated with @wq and @pool */ 4825f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 4826f147f29eSTejun Heo struct worker_pool *pool) 4827d2c1d404STejun Heo { 4828d2c1d404STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 4829d2c1d404STejun Heo 4830e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 4831e50aba9aSTejun Heo 4832d2c1d404STejun Heo pwq->pool = pool; 4833d2c1d404STejun Heo pwq->wq = wq; 4834d2c1d404STejun Heo pwq->flush_color = -1; 48358864b4e5STejun Heo pwq->refcnt = 1; 4836f97a4a1aSLai Jiangshan INIT_LIST_HEAD(&pwq->inactive_works); 48375797b1c1STejun Heo INIT_LIST_HEAD(&pwq->pending_node); 48381befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 4839d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 4840687a9aa5STejun Heo kthread_init_work(&pwq->release_work, pwq_release_workfn); 4841f147f29eSTejun Heo } 4842d2c1d404STejun Heo 4843f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 48441befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 4845f147f29eSTejun Heo { 4846f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 4847f147f29eSTejun Heo 4848f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 484975ccf595STejun Heo 48501befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 48511befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 48521befcf30STejun Heo return; 48531befcf30STejun Heo 485429b1cb41SLai Jiangshan /* set the matching work_color */ 485575ccf595STejun Heo pwq->work_color = wq->work_color; 4856983ca25eSTejun Heo 4857983ca25eSTejun Heo /* link in @pwq */ 485826fb7e3dSWaiman Long list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs); 4859df2d5ae4STejun Heo } 48606029a918STejun Heo 4861f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 4862f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 4863f147f29eSTejun Heo const struct workqueue_attrs *attrs) 4864f147f29eSTejun Heo { 4865f147f29eSTejun Heo struct worker_pool *pool; 4866f147f29eSTejun Heo struct pool_workqueue *pwq; 4867f147f29eSTejun Heo 4868f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4869f147f29eSTejun Heo 4870f147f29eSTejun Heo pool = get_unbound_pool(attrs); 4871f147f29eSTejun Heo if (!pool) 4872f147f29eSTejun Heo return NULL; 4873f147f29eSTejun Heo 4874e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 4875f147f29eSTejun Heo if (!pwq) { 4876f147f29eSTejun Heo put_unbound_pool(pool); 4877f147f29eSTejun Heo return NULL; 4878f147f29eSTejun Heo } 4879f147f29eSTejun Heo 4880f147f29eSTejun Heo init_pwq(pwq, wq, pool); 4881f147f29eSTejun Heo return pwq; 4882d2c1d404STejun Heo } 4883d2c1d404STejun Heo 48844c16bd32STejun Heo /** 4885fef59c9cSTejun Heo * wq_calc_pod_cpumask - calculate a wq_attrs' cpumask for a pod 4886042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 488784193c07STejun Heo * @cpu: the target CPU 48884c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 48894c16bd32STejun Heo * 4890fef59c9cSTejun Heo * Calculate the cpumask a workqueue with @attrs should use on @pod. If 4891fef59c9cSTejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during calculation. 48929546b29eSTejun Heo * The result is stored in @attrs->__pod_cpumask. 48934c16bd32STejun Heo * 4894fef59c9cSTejun Heo * If pod affinity is not enabled, @attrs->cpumask is always used. If enabled 4895fef59c9cSTejun Heo * and @pod has online CPUs requested by @attrs, the returned cpumask is the 4896fef59c9cSTejun Heo * intersection of the possible CPUs of @pod and @attrs->cpumask. 48974c16bd32STejun Heo * 4898fef59c9cSTejun Heo * The caller is responsible for ensuring that the cpumask of @pod stays stable. 48994c16bd32STejun Heo */ 49009546b29eSTejun Heo static void wq_calc_pod_cpumask(struct workqueue_attrs *attrs, int cpu, 49019546b29eSTejun Heo int cpu_going_down) 49024c16bd32STejun Heo { 490384193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 490484193c07STejun Heo int pod = pt->cpu_pod[cpu]; 49054c16bd32STejun Heo 4906fef59c9cSTejun Heo /* does @pod have any online CPUs @attrs wants? */ 49079546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, pt->pod_cpus[pod], attrs->cpumask); 49089546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->__pod_cpumask, cpu_online_mask); 49094c16bd32STejun Heo if (cpu_going_down >= 0) 49109546b29eSTejun Heo cpumask_clear_cpu(cpu_going_down, attrs->__pod_cpumask); 49114c16bd32STejun Heo 49129546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) { 49139546b29eSTejun Heo cpumask_copy(attrs->__pod_cpumask, attrs->cpumask); 491484193c07STejun Heo return; 491584193c07STejun Heo } 49164c16bd32STejun Heo 4917fef59c9cSTejun Heo /* yeap, return possible CPUs in @pod that @attrs wants */ 49189546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->cpumask, pt->pod_cpus[pod]); 49191ad0f0a7SMichael Bringmann 49209546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) 49211ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 49221ad0f0a7SMichael Bringmann "possible intersect\n"); 49234c16bd32STejun Heo } 49244c16bd32STejun Heo 49259f66cff2STejun Heo /* install @pwq into @wq and return the old pwq, @cpu < 0 for dfl_pwq */ 4926636b927eSTejun Heo static struct pool_workqueue *install_unbound_pwq(struct workqueue_struct *wq, 4927636b927eSTejun Heo int cpu, struct pool_workqueue *pwq) 49281befcf30STejun Heo { 49299f66cff2STejun Heo struct pool_workqueue __rcu **slot = unbound_pwq_slot(wq, cpu); 49301befcf30STejun Heo struct pool_workqueue *old_pwq; 49311befcf30STejun Heo 49325b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 49331befcf30STejun Heo lockdep_assert_held(&wq->mutex); 49341befcf30STejun Heo 49351befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 49361befcf30STejun Heo link_pwq(pwq); 49371befcf30STejun Heo 49389f66cff2STejun Heo old_pwq = rcu_access_pointer(*slot); 49399f66cff2STejun Heo rcu_assign_pointer(*slot, pwq); 49401befcf30STejun Heo return old_pwq; 49411befcf30STejun Heo } 49421befcf30STejun Heo 49432d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 49442d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 49452d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 49462d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 4947042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 49482d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 49492d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 49502d5f0764SLai Jiangshan }; 49512d5f0764SLai Jiangshan 49522d5f0764SLai Jiangshan /* free the resources after success or abort */ 49532d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 49542d5f0764SLai Jiangshan { 49552d5f0764SLai Jiangshan if (ctx) { 4956636b927eSTejun Heo int cpu; 49572d5f0764SLai Jiangshan 4958636b927eSTejun Heo for_each_possible_cpu(cpu) 4959636b927eSTejun Heo put_pwq_unlocked(ctx->pwq_tbl[cpu]); 49602d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 49612d5f0764SLai Jiangshan 49622d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 49632d5f0764SLai Jiangshan 49642d5f0764SLai Jiangshan kfree(ctx); 49652d5f0764SLai Jiangshan } 49662d5f0764SLai Jiangshan } 49672d5f0764SLai Jiangshan 49682d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 49692d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 49702d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 497199c621efSLai Jiangshan const struct workqueue_attrs *attrs, 497299c621efSLai Jiangshan const cpumask_var_t unbound_cpumask) 49732d5f0764SLai Jiangshan { 49742d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 49759546b29eSTejun Heo struct workqueue_attrs *new_attrs; 4976636b927eSTejun Heo int cpu; 49772d5f0764SLai Jiangshan 49782d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 49792d5f0764SLai Jiangshan 498084193c07STejun Heo if (WARN_ON(attrs->affn_scope < 0 || 498184193c07STejun Heo attrs->affn_scope >= WQ_AFFN_NR_TYPES)) 498284193c07STejun Heo return ERR_PTR(-EINVAL); 498384193c07STejun Heo 4984636b927eSTejun Heo ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_cpu_ids), GFP_KERNEL); 49852d5f0764SLai Jiangshan 4986be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs(); 49879546b29eSTejun Heo if (!ctx || !new_attrs) 49882d5f0764SLai Jiangshan goto out_free; 49892d5f0764SLai Jiangshan 4990042f7df1SLai Jiangshan /* 49912d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 49922d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 49932d5f0764SLai Jiangshan * it even if we don't use it immediately. 49942d5f0764SLai Jiangshan */ 49950f36ee24STejun Heo copy_workqueue_attrs(new_attrs, attrs); 49960f36ee24STejun Heo wqattrs_actualize_cpumask(new_attrs, unbound_cpumask); 49979546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 49982d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 49992d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 50002d5f0764SLai Jiangshan goto out_free; 50012d5f0764SLai Jiangshan 5002636b927eSTejun Heo for_each_possible_cpu(cpu) { 5003af73f5c9STejun Heo if (new_attrs->ordered) { 50042d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 5005636b927eSTejun Heo ctx->pwq_tbl[cpu] = ctx->dfl_pwq; 5006636b927eSTejun Heo } else { 50079546b29eSTejun Heo wq_calc_pod_cpumask(new_attrs, cpu, -1); 50089546b29eSTejun Heo ctx->pwq_tbl[cpu] = alloc_unbound_pwq(wq, new_attrs); 5009636b927eSTejun Heo if (!ctx->pwq_tbl[cpu]) 5010636b927eSTejun Heo goto out_free; 50112d5f0764SLai Jiangshan } 50122d5f0764SLai Jiangshan } 50132d5f0764SLai Jiangshan 5014042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 5015042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 5016042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 50179546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 50182d5f0764SLai Jiangshan ctx->attrs = new_attrs; 5019042f7df1SLai Jiangshan 50204c065dbcSWaiman Long /* 50214c065dbcSWaiman Long * For initialized ordered workqueues, there should only be one pwq 50224c065dbcSWaiman Long * (dfl_pwq). Set the plugged flag of ctx->dfl_pwq to suspend execution 50234c065dbcSWaiman Long * of newly queued work items until execution of older work items in 50244c065dbcSWaiman Long * the old pwq's have completed. 50254c065dbcSWaiman Long */ 50264c065dbcSWaiman Long if ((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)) 50274c065dbcSWaiman Long ctx->dfl_pwq->plugged = true; 50284c065dbcSWaiman Long 50292d5f0764SLai Jiangshan ctx->wq = wq; 50302d5f0764SLai Jiangshan return ctx; 50312d5f0764SLai Jiangshan 50322d5f0764SLai Jiangshan out_free: 50332d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 50342d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 503584193c07STejun Heo return ERR_PTR(-ENOMEM); 50362d5f0764SLai Jiangshan } 50372d5f0764SLai Jiangshan 50382d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 50392d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 50402d5f0764SLai Jiangshan { 5041636b927eSTejun Heo int cpu; 50422d5f0764SLai Jiangshan 50432d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 50442d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 50452d5f0764SLai Jiangshan 50462d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 50472d5f0764SLai Jiangshan 50489f66cff2STejun Heo /* save the previous pwqs and install the new ones */ 5049636b927eSTejun Heo for_each_possible_cpu(cpu) 5050636b927eSTejun Heo ctx->pwq_tbl[cpu] = install_unbound_pwq(ctx->wq, cpu, 5051636b927eSTejun Heo ctx->pwq_tbl[cpu]); 50529f66cff2STejun Heo ctx->dfl_pwq = install_unbound_pwq(ctx->wq, -1, ctx->dfl_pwq); 50532d5f0764SLai Jiangshan 50545797b1c1STejun Heo /* update node_nr_active->max */ 50555797b1c1STejun Heo wq_update_node_max_active(ctx->wq, -1); 50565797b1c1STejun Heo 5057d64f2fa0SJuri Lelli /* rescuer needs to respect wq cpumask changes */ 5058d64f2fa0SJuri Lelli if (ctx->wq->rescuer) 5059d64f2fa0SJuri Lelli set_cpus_allowed_ptr(ctx->wq->rescuer->task, 5060d64f2fa0SJuri Lelli unbound_effective_cpumask(ctx->wq)); 5061d64f2fa0SJuri Lelli 50622d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 50632d5f0764SLai Jiangshan } 50642d5f0764SLai Jiangshan 5065a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 5066a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 5067a0111cf6SLai Jiangshan { 5068a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 5069a0111cf6SLai Jiangshan 5070a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 5071a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 5072a0111cf6SLai Jiangshan return -EINVAL; 5073a0111cf6SLai Jiangshan 507499c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); 507584193c07STejun Heo if (IS_ERR(ctx)) 507684193c07STejun Heo return PTR_ERR(ctx); 5077a0111cf6SLai Jiangshan 5078a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 5079a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 5080a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 5081a0111cf6SLai Jiangshan 50826201171eSwanghaibin return 0; 5083a0111cf6SLai Jiangshan } 5084a0111cf6SLai Jiangshan 50859e8cd2f5STejun Heo /** 50869e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 50879e8cd2f5STejun Heo * @wq: the target workqueue 50889e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 50899e8cd2f5STejun Heo * 5090fef59c9cSTejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, this function maps 5091fef59c9cSTejun Heo * a separate pwq to each CPU pod with possibles CPUs in @attrs->cpumask so that 5092fef59c9cSTejun Heo * work items are affine to the pod it was issued on. Older pwqs are released as 5093fef59c9cSTejun Heo * in-flight work items finish. Note that a work item which repeatedly requeues 5094fef59c9cSTejun Heo * itself back-to-back will stay on its current pwq. 50959e8cd2f5STejun Heo * 5096d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 5097d185af30SYacine Belkadi * 5098ffd8bea8SSebastian Andrzej Siewior * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). 5099509b3204SDaniel Jordan * 5100d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 51019e8cd2f5STejun Heo */ 5102513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 51039e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 51049e8cd2f5STejun Heo { 5105a0111cf6SLai Jiangshan int ret; 51069e8cd2f5STejun Heo 5107509b3204SDaniel Jordan lockdep_assert_cpus_held(); 5108509b3204SDaniel Jordan 5109509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex); 5110a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 5111509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex); 51122d5f0764SLai Jiangshan 51132d5f0764SLai Jiangshan return ret; 51149e8cd2f5STejun Heo } 51159e8cd2f5STejun Heo 51164c16bd32STejun Heo /** 5117fef59c9cSTejun Heo * wq_update_pod - update pod affinity of a wq for CPU hot[un]plug 51184c16bd32STejun Heo * @wq: the target workqueue 51194cbfd3deSTejun Heo * @cpu: the CPU to update pool association for 51204cbfd3deSTejun Heo * @hotplug_cpu: the CPU coming up or going down 51214c16bd32STejun Heo * @online: whether @cpu is coming up or going down 51224c16bd32STejun Heo * 51234c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 5124fef59c9cSTejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update pod affinity of 51254c16bd32STejun Heo * @wq accordingly. 51264c16bd32STejun Heo * 51274c16bd32STejun Heo * 5128fef59c9cSTejun Heo * If pod affinity can't be adjusted due to memory allocation failure, it falls 5129fef59c9cSTejun Heo * back to @wq->dfl_pwq which may not be optimal but is always correct. 5130fef59c9cSTejun Heo * 5131fef59c9cSTejun Heo * Note that when the last allowed CPU of a pod goes offline for a workqueue 5132fef59c9cSTejun Heo * with a cpumask spanning multiple pods, the workers which were already 5133fef59c9cSTejun Heo * executing the work items for the workqueue will lose their CPU affinity and 5134fef59c9cSTejun Heo * may execute on any CPU. This is similar to how per-cpu workqueues behave on 5135fef59c9cSTejun Heo * CPU_DOWN. If a workqueue user wants strict affinity, it's the user's 5136fef59c9cSTejun Heo * responsibility to flush the work item from CPU_DOWN_PREPARE. 51374c16bd32STejun Heo */ 5138fef59c9cSTejun Heo static void wq_update_pod(struct workqueue_struct *wq, int cpu, 51394cbfd3deSTejun Heo int hotplug_cpu, bool online) 51404c16bd32STejun Heo { 51414cbfd3deSTejun Heo int off_cpu = online ? -1 : hotplug_cpu; 51424c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 51434c16bd32STejun Heo struct workqueue_attrs *target_attrs; 51444c16bd32STejun Heo 51454c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 51464c16bd32STejun Heo 514784193c07STejun Heo if (!(wq->flags & WQ_UNBOUND) || wq->unbound_attrs->ordered) 51484c16bd32STejun Heo return; 51494c16bd32STejun Heo 51504c16bd32STejun Heo /* 51514c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 51524c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 51534c16bd32STejun Heo * CPU hotplug exclusion. 51544c16bd32STejun Heo */ 5155fef59c9cSTejun Heo target_attrs = wq_update_pod_attrs_buf; 51564c16bd32STejun Heo 51574c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 51580f36ee24STejun Heo wqattrs_actualize_cpumask(target_attrs, wq_unbound_cpumask); 51594c16bd32STejun Heo 5160636b927eSTejun Heo /* nothing to do if the target cpumask matches the current pwq */ 51619546b29eSTejun Heo wq_calc_pod_cpumask(target_attrs, cpu, off_cpu); 51629f66cff2STejun Heo if (wqattrs_equal(target_attrs, unbound_pwq(wq, cpu)->pool->attrs)) 5163f7142ed4SLai Jiangshan return; 51644c16bd32STejun Heo 51654c16bd32STejun Heo /* create a new pwq */ 51664c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 51674c16bd32STejun Heo if (!pwq) { 5168fef59c9cSTejun Heo pr_warn("workqueue: allocation failed while updating CPU pod affinity of \"%s\"\n", 51694c16bd32STejun Heo wq->name); 517077f300b1SDaeseok Youn goto use_dfl_pwq; 51714c16bd32STejun Heo } 51724c16bd32STejun Heo 5173f7142ed4SLai Jiangshan /* Install the new pwq. */ 51744c16bd32STejun Heo mutex_lock(&wq->mutex); 5175636b927eSTejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 51764c16bd32STejun Heo goto out_unlock; 51774c16bd32STejun Heo 51784c16bd32STejun Heo use_dfl_pwq: 5179f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 51809f66cff2STejun Heo pwq = unbound_pwq(wq, -1); 51819f66cff2STejun Heo raw_spin_lock_irq(&pwq->pool->lock); 51829f66cff2STejun Heo get_pwq(pwq); 51839f66cff2STejun Heo raw_spin_unlock_irq(&pwq->pool->lock); 51849f66cff2STejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 51854c16bd32STejun Heo out_unlock: 51864c16bd32STejun Heo mutex_unlock(&wq->mutex); 51874c16bd32STejun Heo put_pwq_unlocked(old_pwq); 51884c16bd32STejun Heo } 51894c16bd32STejun Heo 519030cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 51911da177e4SLinus Torvalds { 519249e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 51938a2b7538STejun Heo int cpu, ret; 5194e1d8aa9fSFrederic Weisbecker 5195687a9aa5STejun Heo wq->cpu_pwq = alloc_percpu(struct pool_workqueue *); 5196ee1ceef7STejun Heo if (!wq->cpu_pwq) 5197687a9aa5STejun Heo goto enomem; 519830cdf249STejun Heo 5199636b927eSTejun Heo if (!(wq->flags & WQ_UNBOUND)) { 520030cdf249STejun Heo for_each_possible_cpu(cpu) { 52014cb1ef64STejun Heo struct pool_workqueue **pwq_p; 52024cb1ef64STejun Heo struct worker_pool __percpu *pools; 52034cb1ef64STejun Heo struct worker_pool *pool; 52044cb1ef64STejun Heo 52054cb1ef64STejun Heo if (wq->flags & WQ_BH) 52064cb1ef64STejun Heo pools = bh_worker_pools; 52074cb1ef64STejun Heo else 52084cb1ef64STejun Heo pools = cpu_worker_pools; 52094cb1ef64STejun Heo 52104cb1ef64STejun Heo pool = &(per_cpu_ptr(pools, cpu)[highpri]); 52114cb1ef64STejun Heo pwq_p = per_cpu_ptr(wq->cpu_pwq, cpu); 521230cdf249STejun Heo 5213687a9aa5STejun Heo *pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, 5214687a9aa5STejun Heo pool->node); 5215687a9aa5STejun Heo if (!*pwq_p) 5216687a9aa5STejun Heo goto enomem; 5217687a9aa5STejun Heo 5218687a9aa5STejun Heo init_pwq(*pwq_p, wq, pool); 5219f147f29eSTejun Heo 5220f147f29eSTejun Heo mutex_lock(&wq->mutex); 5221687a9aa5STejun Heo link_pwq(*pwq_p); 5222f147f29eSTejun Heo mutex_unlock(&wq->mutex); 522330cdf249STejun Heo } 522430cdf249STejun Heo return 0; 5225509b3204SDaniel Jordan } 5226509b3204SDaniel Jordan 5227ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 5228509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) { 52299f66cff2STejun Heo struct pool_workqueue *dfl_pwq; 52309f66cff2STejun Heo 52318a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 52328a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 52339f66cff2STejun Heo dfl_pwq = rcu_access_pointer(wq->dfl_pwq); 52349f66cff2STejun Heo WARN(!ret && (wq->pwqs.next != &dfl_pwq->pwqs_node || 52359f66cff2STejun Heo wq->pwqs.prev != &dfl_pwq->pwqs_node), 52368a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 52379e8cd2f5STejun Heo } else { 5238509b3204SDaniel Jordan ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 52399e8cd2f5STejun Heo } 5240ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 5241509b3204SDaniel Jordan 524264344553SZqiang /* for unbound pwq, flush the pwq_release_worker ensures that the 524364344553SZqiang * pwq_release_workfn() completes before calling kfree(wq). 524464344553SZqiang */ 524564344553SZqiang if (ret) 524664344553SZqiang kthread_flush_worker(pwq_release_worker); 524764344553SZqiang 5248509b3204SDaniel Jordan return ret; 5249687a9aa5STejun Heo 5250687a9aa5STejun Heo enomem: 5251687a9aa5STejun Heo if (wq->cpu_pwq) { 52527b42f401SZqiang for_each_possible_cpu(cpu) { 52537b42f401SZqiang struct pool_workqueue *pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 52547b42f401SZqiang 52557b42f401SZqiang if (pwq) 52567b42f401SZqiang kmem_cache_free(pwq_cache, pwq); 52577b42f401SZqiang } 5258687a9aa5STejun Heo free_percpu(wq->cpu_pwq); 5259687a9aa5STejun Heo wq->cpu_pwq = NULL; 5260687a9aa5STejun Heo } 5261687a9aa5STejun Heo return -ENOMEM; 52620f900049STejun Heo } 52630f900049STejun Heo 5264f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 5265f3421797STejun Heo const char *name) 5266b71ab8c2STejun Heo { 5267636b927eSTejun Heo if (max_active < 1 || max_active > WQ_MAX_ACTIVE) 5268044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 5269636b927eSTejun Heo max_active, name, 1, WQ_MAX_ACTIVE); 5270b71ab8c2STejun Heo 5271636b927eSTejun Heo return clamp_val(max_active, 1, WQ_MAX_ACTIVE); 5272b71ab8c2STejun Heo } 5273b71ab8c2STejun Heo 5274983c7515STejun Heo /* 5275983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 5276983c7515STejun Heo * to guarantee forward progress. 5277983c7515STejun Heo */ 5278983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 5279983c7515STejun Heo { 5280983c7515STejun Heo struct worker *rescuer; 5281b92b36eaSDan Carpenter int ret; 5282983c7515STejun Heo 5283983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 5284983c7515STejun Heo return 0; 5285983c7515STejun Heo 5286983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 52874c0736a7SPetr Mladek if (!rescuer) { 52884c0736a7SPetr Mladek pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", 52894c0736a7SPetr Mladek wq->name); 5290983c7515STejun Heo return -ENOMEM; 52914c0736a7SPetr Mladek } 5292983c7515STejun Heo 5293983c7515STejun Heo rescuer->rescue_wq = wq; 5294b6a46f72SAaron Tomlin rescuer->task = kthread_create(rescuer_thread, rescuer, "kworker/R-%s", wq->name); 5295f187b697SSean Fu if (IS_ERR(rescuer->task)) { 5296b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task); 52974c0736a7SPetr Mladek pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", 52984c0736a7SPetr Mladek wq->name, ERR_PTR(ret)); 5299983c7515STejun Heo kfree(rescuer); 5300b92b36eaSDan Carpenter return ret; 5301983c7515STejun Heo } 5302983c7515STejun Heo 5303983c7515STejun Heo wq->rescuer = rescuer; 530485f0ab43SJuri Lelli if (wq->flags & WQ_UNBOUND) 530549584bb8SWaiman Long kthread_bind_mask(rescuer->task, wq_unbound_cpumask); 530685f0ab43SJuri Lelli else 5307983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 5308983c7515STejun Heo wake_up_process(rescuer->task); 5309983c7515STejun Heo 5310983c7515STejun Heo return 0; 5311983c7515STejun Heo } 5312983c7515STejun Heo 5313a045a272STejun Heo /** 5314a045a272STejun Heo * wq_adjust_max_active - update a wq's max_active to the current setting 5315a045a272STejun Heo * @wq: target workqueue 5316a045a272STejun Heo * 5317a045a272STejun Heo * If @wq isn't freezing, set @wq->max_active to the saved_max_active and 5318a045a272STejun Heo * activate inactive work items accordingly. If @wq is freezing, clear 5319a045a272STejun Heo * @wq->max_active to zero. 5320a045a272STejun Heo */ 5321a045a272STejun Heo static void wq_adjust_max_active(struct workqueue_struct *wq) 5322a045a272STejun Heo { 5323c5404d4eSTejun Heo bool activated; 53245797b1c1STejun Heo int new_max, new_min; 5325a045a272STejun Heo 5326a045a272STejun Heo lockdep_assert_held(&wq->mutex); 5327a045a272STejun Heo 5328a045a272STejun Heo if ((wq->flags & WQ_FREEZABLE) && workqueue_freezing) { 53295797b1c1STejun Heo new_max = 0; 53305797b1c1STejun Heo new_min = 0; 53315797b1c1STejun Heo } else { 53325797b1c1STejun Heo new_max = wq->saved_max_active; 53335797b1c1STejun Heo new_min = wq->saved_min_active; 5334a045a272STejun Heo } 5335a045a272STejun Heo 53365797b1c1STejun Heo if (wq->max_active == new_max && wq->min_active == new_min) 5337a045a272STejun Heo return; 5338a045a272STejun Heo 5339a045a272STejun Heo /* 53405797b1c1STejun Heo * Update @wq->max/min_active and then kick inactive work items if more 5341a045a272STejun Heo * active work items are allowed. This doesn't break work item ordering 5342a045a272STejun Heo * because new work items are always queued behind existing inactive 5343a045a272STejun Heo * work items if there are any. 5344a045a272STejun Heo */ 53455797b1c1STejun Heo WRITE_ONCE(wq->max_active, new_max); 53465797b1c1STejun Heo WRITE_ONCE(wq->min_active, new_min); 53475797b1c1STejun Heo 53485797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) 53495797b1c1STejun Heo wq_update_node_max_active(wq, -1); 53505797b1c1STejun Heo 53515797b1c1STejun Heo if (new_max == 0) 53525797b1c1STejun Heo return; 5353a045a272STejun Heo 5354c5404d4eSTejun Heo /* 5355c5404d4eSTejun Heo * Round-robin through pwq's activating the first inactive work item 5356c5404d4eSTejun Heo * until max_active is filled. 5357c5404d4eSTejun Heo */ 5358c5404d4eSTejun Heo do { 5359c5404d4eSTejun Heo struct pool_workqueue *pwq; 5360c5404d4eSTejun Heo 5361c5404d4eSTejun Heo activated = false; 5362a045a272STejun Heo for_each_pwq(pwq, wq) { 5363a045a272STejun Heo unsigned long flags; 5364a045a272STejun Heo 5365c5404d4eSTejun Heo /* can be called during early boot w/ irq disabled */ 5366a045a272STejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, flags); 53675797b1c1STejun Heo if (pwq_activate_first_inactive(pwq, true)) { 5368c5404d4eSTejun Heo activated = true; 5369a045a272STejun Heo kick_pool(pwq->pool); 5370c5404d4eSTejun Heo } 5371a045a272STejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 5372a045a272STejun Heo } 5373c5404d4eSTejun Heo } while (activated); 5374a045a272STejun Heo } 5375a045a272STejun Heo 5376a2775bbcSMathieu Malaterre __printf(1, 4) 5377669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt, 537897e37d7bSTejun Heo unsigned int flags, 5379669de8bdSBart Van Assche int max_active, ...) 53803af24433SOleg Nesterov { 5381ecf6881fSTejun Heo va_list args; 53823af24433SOleg Nesterov struct workqueue_struct *wq; 538391ccc6e7STejun Heo size_t wq_size; 538491ccc6e7STejun Heo int name_len; 5385b196be89STejun Heo 53864cb1ef64STejun Heo if (flags & WQ_BH) { 53874cb1ef64STejun Heo if (WARN_ON_ONCE(flags & ~__WQ_BH_ALLOWS)) 53884cb1ef64STejun Heo return NULL; 53894cb1ef64STejun Heo if (WARN_ON_ONCE(max_active)) 53904cb1ef64STejun Heo return NULL; 53914cb1ef64STejun Heo } 53924cb1ef64STejun Heo 5393cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 5394cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 5395cee22a15SViresh Kumar flags |= WQ_UNBOUND; 5396cee22a15SViresh Kumar 5397ecf6881fSTejun Heo /* allocate wq and format name */ 539891ccc6e7STejun Heo if (flags & WQ_UNBOUND) 539991ccc6e7STejun Heo wq_size = struct_size(wq, node_nr_active, nr_node_ids + 1); 540091ccc6e7STejun Heo else 540191ccc6e7STejun Heo wq_size = sizeof(*wq); 540291ccc6e7STejun Heo 540391ccc6e7STejun Heo wq = kzalloc(wq_size, GFP_KERNEL); 5404b196be89STejun Heo if (!wq) 5405d2c1d404STejun Heo return NULL; 5406b196be89STejun Heo 54076029a918STejun Heo if (flags & WQ_UNBOUND) { 5408be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs(); 54096029a918STejun Heo if (!wq->unbound_attrs) 54106029a918STejun Heo goto err_free_wq; 54116029a918STejun Heo } 54126029a918STejun Heo 5413669de8bdSBart Van Assche va_start(args, max_active); 541491ccc6e7STejun Heo name_len = vsnprintf(wq->name, sizeof(wq->name), fmt, args); 5415b196be89STejun Heo va_end(args); 54163af24433SOleg Nesterov 541791ccc6e7STejun Heo if (name_len >= WQ_NAME_LEN) 541891ccc6e7STejun Heo pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n", 541991ccc6e7STejun Heo wq->name); 542031c89007SAudra Mitchell 54214cb1ef64STejun Heo if (flags & WQ_BH) { 54224cb1ef64STejun Heo /* 54234cb1ef64STejun Heo * BH workqueues always share a single execution context per CPU 54244cb1ef64STejun Heo * and don't impose any max_active limit. 54254cb1ef64STejun Heo */ 54264cb1ef64STejun Heo max_active = INT_MAX; 54274cb1ef64STejun Heo } else { 5428d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 5429b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 54304cb1ef64STejun Heo } 54313af24433SOleg Nesterov 5432b196be89STejun Heo /* init wq */ 543397e37d7bSTejun Heo wq->flags = flags; 5434a045a272STejun Heo wq->max_active = max_active; 54355797b1c1STejun Heo wq->min_active = min(max_active, WQ_DFL_MIN_ACTIVE); 54365797b1c1STejun Heo wq->saved_max_active = wq->max_active; 54375797b1c1STejun Heo wq->saved_min_active = wq->min_active; 54383c25a55dSLai Jiangshan mutex_init(&wq->mutex); 5439112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 544030cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 544173f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 544273f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 5443493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 54443af24433SOleg Nesterov 5445669de8bdSBart Van Assche wq_init_lockdep(wq); 5446cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 54473af24433SOleg Nesterov 544891ccc6e7STejun Heo if (flags & WQ_UNBOUND) { 544991ccc6e7STejun Heo if (alloc_node_nr_active(wq->node_nr_active) < 0) 545082efcab3SBart Van Assche goto err_unreg_lockdep; 545191ccc6e7STejun Heo } 545291ccc6e7STejun Heo 545391ccc6e7STejun Heo if (alloc_and_link_pwqs(wq) < 0) 545491ccc6e7STejun Heo goto err_free_node_nr_active; 54551537663fSTejun Heo 545640c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 5457d2c1d404STejun Heo goto err_destroy; 5458e22bee78STejun Heo 5459226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 5460226223abSTejun Heo goto err_destroy; 5461226223abSTejun Heo 54626af8bf3dSOleg Nesterov /* 546368e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 546468e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 546568e13a67SLai Jiangshan * list. 54666af8bf3dSOleg Nesterov */ 546768e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5468a0a1a5fdSTejun Heo 5469a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5470a045a272STejun Heo wq_adjust_max_active(wq); 5471a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5472a0a1a5fdSTejun Heo 5473e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 5474a0a1a5fdSTejun Heo 547568e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 54763af24433SOleg Nesterov 54773af24433SOleg Nesterov return wq; 5478d2c1d404STejun Heo 547991ccc6e7STejun Heo err_free_node_nr_active: 548091ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 548191ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 548282efcab3SBart Van Assche err_unreg_lockdep: 5483009bb421SBart Van Assche wq_unregister_lockdep(wq); 5484009bb421SBart Van Assche wq_free_lockdep(wq); 548582efcab3SBart Van Assche err_free_wq: 54866029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 54874690c4abSTejun Heo kfree(wq); 5488d2c1d404STejun Heo return NULL; 5489d2c1d404STejun Heo err_destroy: 5490d2c1d404STejun Heo destroy_workqueue(wq); 54914690c4abSTejun Heo return NULL; 54921da177e4SLinus Torvalds } 5493669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue); 54941da177e4SLinus Torvalds 5495c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq) 5496c29eb853STejun Heo { 5497c29eb853STejun Heo int i; 5498c29eb853STejun Heo 5499c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 5500c29eb853STejun Heo if (pwq->nr_in_flight[i]) 5501c29eb853STejun Heo return true; 5502c29eb853STejun Heo 55039f66cff2STejun Heo if ((pwq != rcu_access_pointer(pwq->wq->dfl_pwq)) && (pwq->refcnt > 1)) 5504c29eb853STejun Heo return true; 5505afa87ce8STejun Heo if (!pwq_is_empty(pwq)) 5506c29eb853STejun Heo return true; 5507c29eb853STejun Heo 5508c29eb853STejun Heo return false; 5509c29eb853STejun Heo } 5510c29eb853STejun Heo 55113af24433SOleg Nesterov /** 55123af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 55133af24433SOleg Nesterov * @wq: target workqueue 55143af24433SOleg Nesterov * 55153af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 55163af24433SOleg Nesterov */ 55173af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 55183af24433SOleg Nesterov { 551949e3cf44STejun Heo struct pool_workqueue *pwq; 5520636b927eSTejun Heo int cpu; 55213af24433SOleg Nesterov 5522def98c84STejun Heo /* 5523def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't 5524def98c84STejun Heo * lead to sysfs name conflicts. 5525def98c84STejun Heo */ 5526def98c84STejun Heo workqueue_sysfs_unregister(wq); 5527def98c84STejun Heo 552833e3f0a3SRichard Clark /* mark the workqueue destruction is in progress */ 552933e3f0a3SRichard Clark mutex_lock(&wq->mutex); 553033e3f0a3SRichard Clark wq->flags |= __WQ_DESTROYING; 553133e3f0a3SRichard Clark mutex_unlock(&wq->mutex); 553233e3f0a3SRichard Clark 55339c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 55349c5a2ba7STejun Heo drain_workqueue(wq); 5535c8efcc25STejun Heo 5536def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ 5537def98c84STejun Heo if (wq->rescuer) { 5538def98c84STejun Heo struct worker *rescuer = wq->rescuer; 5539def98c84STejun Heo 5540def98c84STejun Heo /* this prevents new queueing */ 5541a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 5542def98c84STejun Heo wq->rescuer = NULL; 5543a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 5544def98c84STejun Heo 5545def98c84STejun Heo /* rescuer will empty maydays list before exiting */ 5546def98c84STejun Heo kthread_stop(rescuer->task); 55478efe1223STejun Heo kfree(rescuer); 5548def98c84STejun Heo } 5549def98c84STejun Heo 5550c29eb853STejun Heo /* 5551c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all 5552c29eb853STejun Heo * in-flight operations which may do put_pwq(). 5553c29eb853STejun Heo */ 5554c29eb853STejun Heo mutex_lock(&wq_pool_mutex); 5555b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 555649e3cf44STejun Heo for_each_pwq(pwq, wq) { 5557a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 5558c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) { 55591d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n", 5560e66b39afSTejun Heo __func__, wq->name); 5561c29eb853STejun Heo show_pwq(pwq); 5562a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 5563b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 5564c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 556555df0933SImran Khan show_one_workqueue(wq); 55666183c009STejun Heo return; 556776af4d93STejun Heo } 5568a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 556976af4d93STejun Heo } 5570b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 55716183c009STejun Heo 5572a0a1a5fdSTejun Heo /* 5573a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 5574a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 5575a0a1a5fdSTejun Heo */ 5576e2dca7adSTejun Heo list_del_rcu(&wq->list); 557768e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 55783af24433SOleg Nesterov 55798864b4e5STejun Heo /* 5580636b927eSTejun Heo * We're the sole accessor of @wq. Directly access cpu_pwq and dfl_pwq 5581636b927eSTejun Heo * to put the base refs. @wq will be auto-destroyed from the last 5582636b927eSTejun Heo * pwq_put. RCU read lock prevents @wq from going away from under us. 55838864b4e5STejun Heo */ 5584636b927eSTejun Heo rcu_read_lock(); 5585636b927eSTejun Heo 5586636b927eSTejun Heo for_each_possible_cpu(cpu) { 55879f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, cpu)); 55889f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, cpu), NULL); 55894c16bd32STejun Heo } 55904c16bd32STejun Heo 55919f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, -1)); 55929f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, -1), NULL); 5593636b927eSTejun Heo 5594636b927eSTejun Heo rcu_read_unlock(); 55953af24433SOleg Nesterov } 55963af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 55973af24433SOleg Nesterov 5598dcd989cbSTejun Heo /** 5599dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 5600dcd989cbSTejun Heo * @wq: target workqueue 5601dcd989cbSTejun Heo * @max_active: new max_active value. 5602dcd989cbSTejun Heo * 56035797b1c1STejun Heo * Set max_active of @wq to @max_active. See the alloc_workqueue() function 56045797b1c1STejun Heo * comment. 5605dcd989cbSTejun Heo * 5606dcd989cbSTejun Heo * CONTEXT: 5607dcd989cbSTejun Heo * Don't call from IRQ context. 5608dcd989cbSTejun Heo */ 5609dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 5610dcd989cbSTejun Heo { 56114cb1ef64STejun Heo /* max_active doesn't mean anything for BH workqueues */ 56124cb1ef64STejun Heo if (WARN_ON(wq->flags & WQ_BH)) 56134cb1ef64STejun Heo return; 56148719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 56153bc1e711STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED)) 56168719dceaSTejun Heo return; 56178719dceaSTejun Heo 5618f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 5619dcd989cbSTejun Heo 5620a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5621dcd989cbSTejun Heo 5622dcd989cbSTejun Heo wq->saved_max_active = max_active; 56235797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) 56245797b1c1STejun Heo wq->saved_min_active = min(wq->saved_min_active, max_active); 56255797b1c1STejun Heo 5626a045a272STejun Heo wq_adjust_max_active(wq); 5627dcd989cbSTejun Heo 5628a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5629dcd989cbSTejun Heo } 5630dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 5631dcd989cbSTejun Heo 5632dcd989cbSTejun Heo /** 563327d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 563427d4ee03SLukas Wunner * 563527d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 563627d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 563727d4ee03SLukas Wunner * 563827d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 563927d4ee03SLukas Wunner */ 564027d4ee03SLukas Wunner struct work_struct *current_work(void) 564127d4ee03SLukas Wunner { 564227d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 564327d4ee03SLukas Wunner 564427d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 564527d4ee03SLukas Wunner } 564627d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 564727d4ee03SLukas Wunner 564827d4ee03SLukas Wunner /** 5649e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 5650e6267616STejun Heo * 5651e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 5652e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 5653d185af30SYacine Belkadi * 5654d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 5655e6267616STejun Heo */ 5656e6267616STejun Heo bool current_is_workqueue_rescuer(void) 5657e6267616STejun Heo { 5658e6267616STejun Heo struct worker *worker = current_wq_worker(); 5659e6267616STejun Heo 56606a092dfdSLai Jiangshan return worker && worker->rescue_wq; 5661e6267616STejun Heo } 5662e6267616STejun Heo 5663e6267616STejun Heo /** 5664dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 5665dcd989cbSTejun Heo * @cpu: CPU in question 5666dcd989cbSTejun Heo * @wq: target workqueue 5667dcd989cbSTejun Heo * 5668dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 5669dcd989cbSTejun Heo * no synchronization around this function and the test result is 5670dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5671dcd989cbSTejun Heo * 5672d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 5673636b927eSTejun Heo * 5674636b927eSTejun Heo * With the exception of ordered workqueues, all workqueues have per-cpu 5675636b927eSTejun Heo * pool_workqueues, each with its own congested state. A workqueue being 5676636b927eSTejun Heo * congested on one CPU doesn't mean that the workqueue is contested on any 5677636b927eSTejun Heo * other CPUs. 5678d3251859STejun Heo * 5679d185af30SYacine Belkadi * Return: 5680dcd989cbSTejun Heo * %true if congested, %false otherwise. 5681dcd989cbSTejun Heo */ 5682d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 5683dcd989cbSTejun Heo { 56847fb98ea7STejun Heo struct pool_workqueue *pwq; 568576af4d93STejun Heo bool ret; 568676af4d93STejun Heo 568724acfb71SThomas Gleixner rcu_read_lock(); 568824acfb71SThomas Gleixner preempt_disable(); 56897fb98ea7STejun Heo 5690d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 5691d3251859STejun Heo cpu = smp_processor_id(); 5692d3251859STejun Heo 5693687a9aa5STejun Heo pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 5694f97a4a1aSLai Jiangshan ret = !list_empty(&pwq->inactive_works); 5695636b927eSTejun Heo 569624acfb71SThomas Gleixner preempt_enable(); 569724acfb71SThomas Gleixner rcu_read_unlock(); 569876af4d93STejun Heo 569976af4d93STejun Heo return ret; 5700dcd989cbSTejun Heo } 5701dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 5702dcd989cbSTejun Heo 5703dcd989cbSTejun Heo /** 5704dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 5705dcd989cbSTejun Heo * @work: the work to be tested 5706dcd989cbSTejun Heo * 5707dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 5708dcd989cbSTejun Heo * synchronization around this function and the test result is 5709dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5710dcd989cbSTejun Heo * 5711d185af30SYacine Belkadi * Return: 5712dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 5713dcd989cbSTejun Heo */ 5714dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 5715dcd989cbSTejun Heo { 5716fa1b54e6STejun Heo struct worker_pool *pool; 5717dcd989cbSTejun Heo unsigned long flags; 5718dcd989cbSTejun Heo unsigned int ret = 0; 5719dcd989cbSTejun Heo 5720dcd989cbSTejun Heo if (work_pending(work)) 5721dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 5722038366c5SLai Jiangshan 572324acfb71SThomas Gleixner rcu_read_lock(); 5724fa1b54e6STejun Heo pool = get_work_pool(work); 5725038366c5SLai Jiangshan if (pool) { 5726a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 5727c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 5728dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 5729a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 5730038366c5SLai Jiangshan } 573124acfb71SThomas Gleixner rcu_read_unlock(); 5732dcd989cbSTejun Heo 5733dcd989cbSTejun Heo return ret; 5734dcd989cbSTejun Heo } 5735dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 5736dcd989cbSTejun Heo 57373d1cb205STejun Heo /** 57383d1cb205STejun Heo * set_worker_desc - set description for the current work item 57393d1cb205STejun Heo * @fmt: printf-style format string 57403d1cb205STejun Heo * @...: arguments for the format string 57413d1cb205STejun Heo * 57423d1cb205STejun Heo * This function can be called by a running work function to describe what 57433d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 57443d1cb205STejun Heo * information will be printed out together to help debugging. The 57453d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 57463d1cb205STejun Heo */ 57473d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 57483d1cb205STejun Heo { 57493d1cb205STejun Heo struct worker *worker = current_wq_worker(); 57503d1cb205STejun Heo va_list args; 57513d1cb205STejun Heo 57523d1cb205STejun Heo if (worker) { 57533d1cb205STejun Heo va_start(args, fmt); 57543d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 57553d1cb205STejun Heo va_end(args); 57563d1cb205STejun Heo } 57573d1cb205STejun Heo } 57585c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 57593d1cb205STejun Heo 57603d1cb205STejun Heo /** 57613d1cb205STejun Heo * print_worker_info - print out worker information and description 57623d1cb205STejun Heo * @log_lvl: the log level to use when printing 57633d1cb205STejun Heo * @task: target task 57643d1cb205STejun Heo * 57653d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 57663d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 57673d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 57683d1cb205STejun Heo * 57693d1cb205STejun Heo * This function can be safely called on any task as long as the 57703d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 57713d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 57723d1cb205STejun Heo */ 57733d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 57743d1cb205STejun Heo { 57753d1cb205STejun Heo work_func_t *fn = NULL; 57763d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 57773d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 57783d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 57793d1cb205STejun Heo struct workqueue_struct *wq = NULL; 57803d1cb205STejun Heo struct worker *worker; 57813d1cb205STejun Heo 57823d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 57833d1cb205STejun Heo return; 57843d1cb205STejun Heo 57853d1cb205STejun Heo /* 57863d1cb205STejun Heo * This function is called without any synchronization and @task 57873d1cb205STejun Heo * could be in any state. Be careful with dereferences. 57883d1cb205STejun Heo */ 5789e700591aSPetr Mladek worker = kthread_probe_data(task); 57903d1cb205STejun Heo 57913d1cb205STejun Heo /* 57928bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 57938bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 57943d1cb205STejun Heo */ 5795fe557319SChristoph Hellwig copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); 5796fe557319SChristoph Hellwig copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); 5797fe557319SChristoph Hellwig copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); 5798fe557319SChristoph Hellwig copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); 5799fe557319SChristoph Hellwig copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); 58003d1cb205STejun Heo 58013d1cb205STejun Heo if (fn || name[0] || desc[0]) { 5802d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn); 58038bf89593STejun Heo if (strcmp(name, desc)) 58043d1cb205STejun Heo pr_cont(" (%s)", desc); 58053d1cb205STejun Heo pr_cont("\n"); 58063d1cb205STejun Heo } 58073d1cb205STejun Heo } 58083d1cb205STejun Heo 58093494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 58103494fc30STejun Heo { 58113494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 58123494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 58133494fc30STejun Heo pr_cont(" node=%d", pool->node); 58144cb1ef64STejun Heo pr_cont(" flags=0x%x", pool->flags); 58154cb1ef64STejun Heo if (pool->flags & POOL_BH) 58164cb1ef64STejun Heo pr_cont(" bh%s", 58174cb1ef64STejun Heo pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); 58184cb1ef64STejun Heo else 58194cb1ef64STejun Heo pr_cont(" nice=%d", pool->attrs->nice); 58204cb1ef64STejun Heo } 58214cb1ef64STejun Heo 58224cb1ef64STejun Heo static void pr_cont_worker_id(struct worker *worker) 58234cb1ef64STejun Heo { 58244cb1ef64STejun Heo struct worker_pool *pool = worker->pool; 58254cb1ef64STejun Heo 58264cb1ef64STejun Heo if (pool->flags & WQ_BH) 58274cb1ef64STejun Heo pr_cont("bh%s", 58284cb1ef64STejun Heo pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); 58294cb1ef64STejun Heo else 58304cb1ef64STejun Heo pr_cont("%d%s", task_pid_nr(worker->task), 58314cb1ef64STejun Heo worker->rescue_wq ? "(RESCUER)" : ""); 58323494fc30STejun Heo } 58333494fc30STejun Heo 5834c76feb0dSPaul E. McKenney struct pr_cont_work_struct { 5835c76feb0dSPaul E. McKenney bool comma; 5836c76feb0dSPaul E. McKenney work_func_t func; 5837c76feb0dSPaul E. McKenney long ctr; 5838c76feb0dSPaul E. McKenney }; 5839c76feb0dSPaul E. McKenney 5840c76feb0dSPaul E. McKenney static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) 5841c76feb0dSPaul E. McKenney { 5842c76feb0dSPaul E. McKenney if (!pcwsp->ctr) 5843c76feb0dSPaul E. McKenney goto out_record; 5844c76feb0dSPaul E. McKenney if (func == pcwsp->func) { 5845c76feb0dSPaul E. McKenney pcwsp->ctr++; 5846c76feb0dSPaul E. McKenney return; 5847c76feb0dSPaul E. McKenney } 5848c76feb0dSPaul E. McKenney if (pcwsp->ctr == 1) 5849c76feb0dSPaul E. McKenney pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); 5850c76feb0dSPaul E. McKenney else 5851c76feb0dSPaul E. McKenney pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); 5852c76feb0dSPaul E. McKenney pcwsp->ctr = 0; 5853c76feb0dSPaul E. McKenney out_record: 5854c76feb0dSPaul E. McKenney if ((long)func == -1L) 5855c76feb0dSPaul E. McKenney return; 5856c76feb0dSPaul E. McKenney pcwsp->comma = comma; 5857c76feb0dSPaul E. McKenney pcwsp->func = func; 5858c76feb0dSPaul E. McKenney pcwsp->ctr = 1; 5859c76feb0dSPaul E. McKenney } 5860c76feb0dSPaul E. McKenney 5861c76feb0dSPaul E. McKenney static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) 58623494fc30STejun Heo { 58633494fc30STejun Heo if (work->func == wq_barrier_func) { 58643494fc30STejun Heo struct wq_barrier *barr; 58653494fc30STejun Heo 58663494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 58673494fc30STejun Heo 5868c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 58693494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 58703494fc30STejun Heo task_pid_nr(barr->task)); 58713494fc30STejun Heo } else { 5872c76feb0dSPaul E. McKenney if (!comma) 5873c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 5874c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, work->func, pcwsp); 58753494fc30STejun Heo } 58763494fc30STejun Heo } 58773494fc30STejun Heo 58783494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 58793494fc30STejun Heo { 5880c76feb0dSPaul E. McKenney struct pr_cont_work_struct pcws = { .ctr = 0, }; 58813494fc30STejun Heo struct worker_pool *pool = pwq->pool; 58823494fc30STejun Heo struct work_struct *work; 58833494fc30STejun Heo struct worker *worker; 58843494fc30STejun Heo bool has_in_flight = false, has_pending = false; 58853494fc30STejun Heo int bkt; 58863494fc30STejun Heo 58873494fc30STejun Heo pr_info(" pwq %d:", pool->id); 58883494fc30STejun Heo pr_cont_pool_info(pool); 58893494fc30STejun Heo 5890a045a272STejun Heo pr_cont(" active=%d refcnt=%d%s\n", 5891a045a272STejun Heo pwq->nr_active, pwq->refcnt, 58923494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 58933494fc30STejun Heo 58943494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 58953494fc30STejun Heo if (worker->current_pwq == pwq) { 58963494fc30STejun Heo has_in_flight = true; 58973494fc30STejun Heo break; 58983494fc30STejun Heo } 58993494fc30STejun Heo } 59003494fc30STejun Heo if (has_in_flight) { 59013494fc30STejun Heo bool comma = false; 59023494fc30STejun Heo 59033494fc30STejun Heo pr_info(" in-flight:"); 59043494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 59053494fc30STejun Heo if (worker->current_pwq != pwq) 59063494fc30STejun Heo continue; 59073494fc30STejun Heo 59084cb1ef64STejun Heo pr_cont(" %s", comma ? "," : ""); 59094cb1ef64STejun Heo pr_cont_worker_id(worker); 59104cb1ef64STejun Heo pr_cont(":%ps", worker->current_func); 59113494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 5912c76feb0dSPaul E. McKenney pr_cont_work(false, work, &pcws); 5913c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 59143494fc30STejun Heo comma = true; 59153494fc30STejun Heo } 59163494fc30STejun Heo pr_cont("\n"); 59173494fc30STejun Heo } 59183494fc30STejun Heo 59193494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 59203494fc30STejun Heo if (get_work_pwq(work) == pwq) { 59213494fc30STejun Heo has_pending = true; 59223494fc30STejun Heo break; 59233494fc30STejun Heo } 59243494fc30STejun Heo } 59253494fc30STejun Heo if (has_pending) { 59263494fc30STejun Heo bool comma = false; 59273494fc30STejun Heo 59283494fc30STejun Heo pr_info(" pending:"); 59293494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 59303494fc30STejun Heo if (get_work_pwq(work) != pwq) 59313494fc30STejun Heo continue; 59323494fc30STejun Heo 5933c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 59343494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 59353494fc30STejun Heo } 5936c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 59373494fc30STejun Heo pr_cont("\n"); 59383494fc30STejun Heo } 59393494fc30STejun Heo 5940f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 59413494fc30STejun Heo bool comma = false; 59423494fc30STejun Heo 5943f97a4a1aSLai Jiangshan pr_info(" inactive:"); 5944f97a4a1aSLai Jiangshan list_for_each_entry(work, &pwq->inactive_works, entry) { 5945c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 59463494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 59473494fc30STejun Heo } 5948c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 59493494fc30STejun Heo pr_cont("\n"); 59503494fc30STejun Heo } 59513494fc30STejun Heo } 59523494fc30STejun Heo 59533494fc30STejun Heo /** 595455df0933SImran Khan * show_one_workqueue - dump state of specified workqueue 595555df0933SImran Khan * @wq: workqueue whose state will be printed 59563494fc30STejun Heo */ 595755df0933SImran Khan void show_one_workqueue(struct workqueue_struct *wq) 59583494fc30STejun Heo { 59593494fc30STejun Heo struct pool_workqueue *pwq; 59603494fc30STejun Heo bool idle = true; 596155df0933SImran Khan unsigned long flags; 59623494fc30STejun Heo 59633494fc30STejun Heo for_each_pwq(pwq, wq) { 5964afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 59653494fc30STejun Heo idle = false; 59663494fc30STejun Heo break; 59673494fc30STejun Heo } 59683494fc30STejun Heo } 596955df0933SImran Khan if (idle) /* Nothing to print for idle workqueue */ 597055df0933SImran Khan return; 59713494fc30STejun Heo 59723494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 59733494fc30STejun Heo 59743494fc30STejun Heo for_each_pwq(pwq, wq) { 5975a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pwq->pool->lock, flags); 5976afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 597757116ce1SJohan Hovold /* 597857116ce1SJohan Hovold * Defer printing to avoid deadlocks in console 597957116ce1SJohan Hovold * drivers that queue work while holding locks 598057116ce1SJohan Hovold * also taken in their write paths. 598157116ce1SJohan Hovold */ 598257116ce1SJohan Hovold printk_deferred_enter(); 59833494fc30STejun Heo show_pwq(pwq); 598457116ce1SJohan Hovold printk_deferred_exit(); 598557116ce1SJohan Hovold } 5986a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 598762635ea8SSergey Senozhatsky /* 598862635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 598955df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 599062635ea8SSergey Senozhatsky * hard lockup. 599162635ea8SSergey Senozhatsky */ 599262635ea8SSergey Senozhatsky touch_nmi_watchdog(); 59933494fc30STejun Heo } 599455df0933SImran Khan 59953494fc30STejun Heo } 59963494fc30STejun Heo 599755df0933SImran Khan /** 599855df0933SImran Khan * show_one_worker_pool - dump state of specified worker pool 599955df0933SImran Khan * @pool: worker pool whose state will be printed 600055df0933SImran Khan */ 600155df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool) 600255df0933SImran Khan { 60033494fc30STejun Heo struct worker *worker; 60043494fc30STejun Heo bool first = true; 600555df0933SImran Khan unsigned long flags; 6006335a42ebSPetr Mladek unsigned long hung = 0; 60073494fc30STejun Heo 6008a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 60093494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 60103494fc30STejun Heo goto next_pool; 6011335a42ebSPetr Mladek 6012335a42ebSPetr Mladek /* How long the first pending work is waiting for a worker. */ 6013335a42ebSPetr Mladek if (!list_empty(&pool->worklist)) 6014335a42ebSPetr Mladek hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; 6015335a42ebSPetr Mladek 601657116ce1SJohan Hovold /* 601757116ce1SJohan Hovold * Defer printing to avoid deadlocks in console drivers that 601857116ce1SJohan Hovold * queue work while holding locks also taken in their write 601957116ce1SJohan Hovold * paths. 602057116ce1SJohan Hovold */ 602157116ce1SJohan Hovold printk_deferred_enter(); 60223494fc30STejun Heo pr_info("pool %d:", pool->id); 60233494fc30STejun Heo pr_cont_pool_info(pool); 6024335a42ebSPetr Mladek pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); 60253494fc30STejun Heo if (pool->manager) 60263494fc30STejun Heo pr_cont(" manager: %d", 60273494fc30STejun Heo task_pid_nr(pool->manager->task)); 60283494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 60294cb1ef64STejun Heo pr_cont(" %s", first ? "idle: " : ""); 60304cb1ef64STejun Heo pr_cont_worker_id(worker); 60313494fc30STejun Heo first = false; 60323494fc30STejun Heo } 60333494fc30STejun Heo pr_cont("\n"); 603457116ce1SJohan Hovold printk_deferred_exit(); 60353494fc30STejun Heo next_pool: 6036a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 603762635ea8SSergey Senozhatsky /* 603862635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 603955df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 604062635ea8SSergey Senozhatsky * hard lockup. 604162635ea8SSergey Senozhatsky */ 604262635ea8SSergey Senozhatsky touch_nmi_watchdog(); 604355df0933SImran Khan 60443494fc30STejun Heo } 60453494fc30STejun Heo 604655df0933SImran Khan /** 604755df0933SImran Khan * show_all_workqueues - dump workqueue state 604855df0933SImran Khan * 6049704bc669SJungseung Lee * Called from a sysrq handler and prints out all busy workqueues and pools. 605055df0933SImran Khan */ 605155df0933SImran Khan void show_all_workqueues(void) 605255df0933SImran Khan { 605355df0933SImran Khan struct workqueue_struct *wq; 605455df0933SImran Khan struct worker_pool *pool; 605555df0933SImran Khan int pi; 605655df0933SImran Khan 605755df0933SImran Khan rcu_read_lock(); 605855df0933SImran Khan 605955df0933SImran Khan pr_info("Showing busy workqueues and worker pools:\n"); 606055df0933SImran Khan 606155df0933SImran Khan list_for_each_entry_rcu(wq, &workqueues, list) 606255df0933SImran Khan show_one_workqueue(wq); 606355df0933SImran Khan 606455df0933SImran Khan for_each_pool(pool, pi) 606555df0933SImran Khan show_one_worker_pool(pool); 606655df0933SImran Khan 606724acfb71SThomas Gleixner rcu_read_unlock(); 60683494fc30STejun Heo } 60693494fc30STejun Heo 6070704bc669SJungseung Lee /** 6071704bc669SJungseung Lee * show_freezable_workqueues - dump freezable workqueue state 6072704bc669SJungseung Lee * 6073704bc669SJungseung Lee * Called from try_to_freeze_tasks() and prints out all freezable workqueues 6074704bc669SJungseung Lee * still busy. 6075704bc669SJungseung Lee */ 6076704bc669SJungseung Lee void show_freezable_workqueues(void) 6077704bc669SJungseung Lee { 6078704bc669SJungseung Lee struct workqueue_struct *wq; 6079704bc669SJungseung Lee 6080704bc669SJungseung Lee rcu_read_lock(); 6081704bc669SJungseung Lee 6082704bc669SJungseung Lee pr_info("Showing freezable workqueues that are still busy:\n"); 6083704bc669SJungseung Lee 6084704bc669SJungseung Lee list_for_each_entry_rcu(wq, &workqueues, list) { 6085704bc669SJungseung Lee if (!(wq->flags & WQ_FREEZABLE)) 6086704bc669SJungseung Lee continue; 6087704bc669SJungseung Lee show_one_workqueue(wq); 6088704bc669SJungseung Lee } 6089704bc669SJungseung Lee 6090704bc669SJungseung Lee rcu_read_unlock(); 6091704bc669SJungseung Lee } 6092704bc669SJungseung Lee 60936b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 60946b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 60956b59808bSTejun Heo { 60966b59808bSTejun Heo int off; 60976b59808bSTejun Heo 60986b59808bSTejun Heo /* always show the actual comm */ 60996b59808bSTejun Heo off = strscpy(buf, task->comm, size); 61006b59808bSTejun Heo if (off < 0) 61016b59808bSTejun Heo return; 61026b59808bSTejun Heo 6103197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 61046b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 61056b59808bSTejun Heo 6106197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 6107197f6accSTejun Heo struct worker *worker = kthread_data(task); 6108197f6accSTejun Heo struct worker_pool *pool = worker->pool; 61096b59808bSTejun Heo 61106b59808bSTejun Heo if (pool) { 6111a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 61126b59808bSTejun Heo /* 6113197f6accSTejun Heo * ->desc tracks information (wq name or 6114197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 6115197f6accSTejun Heo * current, prepend '+', otherwise '-'. 61166b59808bSTejun Heo */ 61176b59808bSTejun Heo if (worker->desc[0] != '\0') { 61186b59808bSTejun Heo if (worker->current_work) 61196b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 61206b59808bSTejun Heo worker->desc); 61216b59808bSTejun Heo else 61226b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 61236b59808bSTejun Heo worker->desc); 61246b59808bSTejun Heo } 6125a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 61266b59808bSTejun Heo } 6127197f6accSTejun Heo } 61286b59808bSTejun Heo 61296b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 61306b59808bSTejun Heo } 61316b59808bSTejun Heo 613266448bc2SMathieu Malaterre #ifdef CONFIG_SMP 613366448bc2SMathieu Malaterre 6134db7bccf4STejun Heo /* 6135db7bccf4STejun Heo * CPU hotplug. 6136db7bccf4STejun Heo * 6137e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 6138112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 6139706026c2STejun Heo * pool which make migrating pending and scheduled works very 6140e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 614194cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 6142e22bee78STejun Heo * blocked draining impractical. 6143e22bee78STejun Heo * 614424647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 6145628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 6146628c78e7STejun Heo * cpu comes back online. 6147db7bccf4STejun Heo */ 6148db7bccf4STejun Heo 6149e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 6150db7bccf4STejun Heo { 61514ce62e9eSTejun Heo struct worker_pool *pool; 6152db7bccf4STejun Heo struct worker *worker; 6153db7bccf4STejun Heo 6154f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 61551258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 6156a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 6157e22bee78STejun Heo 6158f2d5a0eeSTejun Heo /* 615992f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 616094cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 616111b45b0bSLai Jiangshan * must be on the cpu. After this, they may become diasporas. 6162b4ac9384SLai Jiangshan * And the preemption disabled section in their sched callbacks 6163b4ac9384SLai Jiangshan * are guaranteed to see WORKER_UNBOUND since the code here 6164b4ac9384SLai Jiangshan * is on the same cpu. 6165f2d5a0eeSTejun Heo */ 6166da028469SLai Jiangshan for_each_pool_worker(worker, pool) 6167403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 6168db7bccf4STejun Heo 616924647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 6170f2d5a0eeSTejun Heo 6171e22bee78STejun Heo /* 6172989442d7SLai Jiangshan * The handling of nr_running in sched callbacks are disabled 6173989442d7SLai Jiangshan * now. Zap nr_running. After this, nr_running stays zero and 6174989442d7SLai Jiangshan * need_more_worker() and keep_working() are always true as 6175989442d7SLai Jiangshan * long as the worklist is not empty. This pool now behaves as 6176989442d7SLai Jiangshan * an unbound (in terms of concurrency management) pool which 6177eb283428SLai Jiangshan * are served by workers tied to the pool. 6178e22bee78STejun Heo */ 6179bc35f7efSLai Jiangshan pool->nr_running = 0; 6180eb283428SLai Jiangshan 6181eb283428SLai Jiangshan /* 6182eb283428SLai Jiangshan * With concurrency management just turned off, a busy 6183eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 6184eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 6185eb283428SLai Jiangshan */ 61860219a352STejun Heo kick_pool(pool); 6187989442d7SLai Jiangshan 6188a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 6189989442d7SLai Jiangshan 6190793777bcSValentin Schneider for_each_pool_worker(worker, pool) 6191793777bcSValentin Schneider unbind_worker(worker); 6192989442d7SLai Jiangshan 6193989442d7SLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 6194eb283428SLai Jiangshan } 6195db7bccf4STejun Heo } 6196db7bccf4STejun Heo 6197bd7c089eSTejun Heo /** 6198bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 6199bd7c089eSTejun Heo * @pool: pool of interest 6200bd7c089eSTejun Heo * 6201a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 6202bd7c089eSTejun Heo */ 6203bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 6204bd7c089eSTejun Heo { 6205a9ab775bSTejun Heo struct worker *worker; 6206bd7c089eSTejun Heo 62071258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 6208bd7c089eSTejun Heo 6209bd7c089eSTejun Heo /* 6210a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 6211a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 6212402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 6213a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 6214a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 6215bd7c089eSTejun Heo */ 6216c63a2e52SValentin Schneider for_each_pool_worker(worker, pool) { 6217c63a2e52SValentin Schneider kthread_set_per_cpu(worker->task, pool->cpu); 6218c63a2e52SValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 62199546b29eSTejun Heo pool_allowed_cpus(pool)) < 0); 6220c63a2e52SValentin Schneider } 6221a9ab775bSTejun Heo 6222a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 6223f7c17d26SWanpeng Li 62243de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 6225a9ab775bSTejun Heo 6226da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 6227a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 6228a9ab775bSTejun Heo 6229a9ab775bSTejun Heo /* 6230a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 6231a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 6232a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 6233a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 6234a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 6235a9ab775bSTejun Heo * concurrency management. Note that when or whether 6236a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 6237a9ab775bSTejun Heo * 6238c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 6239a9ab775bSTejun Heo * tested without holding any lock in 62406d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may 6241a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 6242a9ab775bSTejun Heo * management operations. 6243bd7c089eSTejun Heo */ 6244a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 6245a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 6246a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 6247c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 6248bd7c089eSTejun Heo } 6249a9ab775bSTejun Heo 6250a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 6251bd7c089eSTejun Heo } 6252bd7c089eSTejun Heo 62537dbc725eSTejun Heo /** 62547dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 62557dbc725eSTejun Heo * @pool: unbound pool of interest 62567dbc725eSTejun Heo * @cpu: the CPU which is coming up 62577dbc725eSTejun Heo * 62587dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 62597dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 62607dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 62617dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 62627dbc725eSTejun Heo */ 62637dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 62647dbc725eSTejun Heo { 62657dbc725eSTejun Heo static cpumask_t cpumask; 62667dbc725eSTejun Heo struct worker *worker; 62677dbc725eSTejun Heo 62681258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 62697dbc725eSTejun Heo 62707dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 62717dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 62727dbc725eSTejun Heo return; 62737dbc725eSTejun Heo 62747dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 62757dbc725eSTejun Heo 62767dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 6277da028469SLai Jiangshan for_each_pool_worker(worker, pool) 6278d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 62797dbc725eSTejun Heo } 62807dbc725eSTejun Heo 62817ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 62821da177e4SLinus Torvalds { 62834ce62e9eSTejun Heo struct worker_pool *pool; 62841da177e4SLinus Torvalds 6285f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 62863ce63377STejun Heo if (pool->nr_workers) 62873ce63377STejun Heo continue; 6288051e1850SLai Jiangshan if (!create_worker(pool)) 62897ee681b2SThomas Gleixner return -ENOMEM; 62903af24433SOleg Nesterov } 62917ee681b2SThomas Gleixner return 0; 62927ee681b2SThomas Gleixner } 62931da177e4SLinus Torvalds 62947ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 62957ee681b2SThomas Gleixner { 62967ee681b2SThomas Gleixner struct worker_pool *pool; 62977ee681b2SThomas Gleixner struct workqueue_struct *wq; 62987ee681b2SThomas Gleixner int pi; 62997ee681b2SThomas Gleixner 630068e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 63017dbc725eSTejun Heo 63027dbc725eSTejun Heo for_each_pool(pool, pi) { 63034cb1ef64STejun Heo /* BH pools aren't affected by hotplug */ 63044cb1ef64STejun Heo if (pool->flags & POOL_BH) 63054cb1ef64STejun Heo continue; 630694cf58bbSTejun Heo 63074cb1ef64STejun Heo mutex_lock(&wq_pool_attach_mutex); 6308f05b558dSLai Jiangshan if (pool->cpu == cpu) 630994cf58bbSTejun Heo rebind_workers(pool); 6310f05b558dSLai Jiangshan else if (pool->cpu < 0) 63117dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 63121258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 631394cf58bbSTejun Heo } 63147dbc725eSTejun Heo 6315fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 63164cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 631784193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 631884193c07STejun Heo 631984193c07STejun Heo if (attrs) { 632084193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 63214cbfd3deSTejun Heo int tcpu; 63224cbfd3deSTejun Heo 632384193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 6324fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, true); 63255797b1c1STejun Heo 63265797b1c1STejun Heo mutex_lock(&wq->mutex); 63275797b1c1STejun Heo wq_update_node_max_active(wq, -1); 63285797b1c1STejun Heo mutex_unlock(&wq->mutex); 63294cbfd3deSTejun Heo } 63304cbfd3deSTejun Heo } 63314c16bd32STejun Heo 633268e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 63337ee681b2SThomas Gleixner return 0; 633465758202STejun Heo } 633565758202STejun Heo 63367ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 633765758202STejun Heo { 63384c16bd32STejun Heo struct workqueue_struct *wq; 63398db25e78STejun Heo 63404c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 6341e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 6342e8b3f8dbSLai Jiangshan return -1; 6343e8b3f8dbSLai Jiangshan 6344e8b3f8dbSLai Jiangshan unbind_workers(cpu); 63454c16bd32STejun Heo 6346fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 63474c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 63484cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 634984193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 635084193c07STejun Heo 635184193c07STejun Heo if (attrs) { 635284193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 63534cbfd3deSTejun Heo int tcpu; 63544cbfd3deSTejun Heo 635584193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 6356fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, false); 63575797b1c1STejun Heo 63585797b1c1STejun Heo mutex_lock(&wq->mutex); 63595797b1c1STejun Heo wq_update_node_max_active(wq, cpu); 63605797b1c1STejun Heo mutex_unlock(&wq->mutex); 63614cbfd3deSTejun Heo } 63624cbfd3deSTejun Heo } 63634c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 63644c16bd32STejun Heo 63657ee681b2SThomas Gleixner return 0; 636665758202STejun Heo } 636765758202STejun Heo 63682d3854a3SRusty Russell struct work_for_cpu { 6369ed48ece2STejun Heo struct work_struct work; 63702d3854a3SRusty Russell long (*fn)(void *); 63712d3854a3SRusty Russell void *arg; 63722d3854a3SRusty Russell long ret; 63732d3854a3SRusty Russell }; 63742d3854a3SRusty Russell 6375ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 63762d3854a3SRusty Russell { 6377ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 6378ed48ece2STejun Heo 63792d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 63802d3854a3SRusty Russell } 63812d3854a3SRusty Russell 63822d3854a3SRusty Russell /** 6383265f3ed0SFrederic Weisbecker * work_on_cpu_key - run a function in thread context on a particular cpu 63842d3854a3SRusty Russell * @cpu: the cpu to run on 63852d3854a3SRusty Russell * @fn: the function to run 63862d3854a3SRusty Russell * @arg: the function arg 6387265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 63882d3854a3SRusty Russell * 638931ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 63906b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 6391d185af30SYacine Belkadi * 6392d185af30SYacine Belkadi * Return: The value @fn returns. 63932d3854a3SRusty Russell */ 6394265f3ed0SFrederic Weisbecker long work_on_cpu_key(int cpu, long (*fn)(void *), 6395265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 63962d3854a3SRusty Russell { 6397ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 63982d3854a3SRusty Russell 6399265f3ed0SFrederic Weisbecker INIT_WORK_ONSTACK_KEY(&wfc.work, work_for_cpu_fn, key); 6400ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 640112997d1aSBjorn Helgaas flush_work(&wfc.work); 6402440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 64032d3854a3SRusty Russell return wfc.ret; 64042d3854a3SRusty Russell } 6405265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_key); 64060e8d6a93SThomas Gleixner 64070e8d6a93SThomas Gleixner /** 6408265f3ed0SFrederic Weisbecker * work_on_cpu_safe_key - run a function in thread context on a particular cpu 64090e8d6a93SThomas Gleixner * @cpu: the cpu to run on 64100e8d6a93SThomas Gleixner * @fn: the function to run 64110e8d6a93SThomas Gleixner * @arg: the function argument 6412265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 64130e8d6a93SThomas Gleixner * 64140e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 64150e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 64160e8d6a93SThomas Gleixner * 64170e8d6a93SThomas Gleixner * Return: The value @fn returns. 64180e8d6a93SThomas Gleixner */ 6419265f3ed0SFrederic Weisbecker long work_on_cpu_safe_key(int cpu, long (*fn)(void *), 6420265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 64210e8d6a93SThomas Gleixner { 64220e8d6a93SThomas Gleixner long ret = -ENODEV; 64230e8d6a93SThomas Gleixner 6424ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 64250e8d6a93SThomas Gleixner if (cpu_online(cpu)) 6426265f3ed0SFrederic Weisbecker ret = work_on_cpu_key(cpu, fn, arg, key); 6427ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 64280e8d6a93SThomas Gleixner return ret; 64290e8d6a93SThomas Gleixner } 6430265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_safe_key); 64312d3854a3SRusty Russell #endif /* CONFIG_SMP */ 64322d3854a3SRusty Russell 6433a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 6434e7577c50SRusty Russell 6435a0a1a5fdSTejun Heo /** 6436a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 6437a0a1a5fdSTejun Heo * 643858a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 6439f97a4a1aSLai Jiangshan * workqueues will queue new works to their inactive_works list instead of 6440706026c2STejun Heo * pool->worklist. 6441a0a1a5fdSTejun Heo * 6442a0a1a5fdSTejun Heo * CONTEXT: 6443a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 6444a0a1a5fdSTejun Heo */ 6445a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 6446a0a1a5fdSTejun Heo { 644724b8a847STejun Heo struct workqueue_struct *wq; 6448a0a1a5fdSTejun Heo 644968e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6450a0a1a5fdSTejun Heo 64516183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 6452a0a1a5fdSTejun Heo workqueue_freezing = true; 6453a0a1a5fdSTejun Heo 645424b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6455a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 6456a045a272STejun Heo wq_adjust_max_active(wq); 6457a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 6458a1056305STejun Heo } 64595bcab335STejun Heo 646068e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6461a0a1a5fdSTejun Heo } 6462a0a1a5fdSTejun Heo 6463a0a1a5fdSTejun Heo /** 646458a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 6465a0a1a5fdSTejun Heo * 6466a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 6467a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 6468a0a1a5fdSTejun Heo * 6469a0a1a5fdSTejun Heo * CONTEXT: 647068e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 6471a0a1a5fdSTejun Heo * 6472d185af30SYacine Belkadi * Return: 647358a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 647458a69cb4STejun Heo * is complete. 6475a0a1a5fdSTejun Heo */ 6476a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 6477a0a1a5fdSTejun Heo { 6478a0a1a5fdSTejun Heo bool busy = false; 647924b8a847STejun Heo struct workqueue_struct *wq; 648024b8a847STejun Heo struct pool_workqueue *pwq; 6481a0a1a5fdSTejun Heo 648268e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6483a0a1a5fdSTejun Heo 64846183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 6485a0a1a5fdSTejun Heo 648624b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 648724b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 648824b8a847STejun Heo continue; 6489a0a1a5fdSTejun Heo /* 6490a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 6491a0a1a5fdSTejun Heo * to peek without lock. 6492a0a1a5fdSTejun Heo */ 649324acfb71SThomas Gleixner rcu_read_lock(); 649424b8a847STejun Heo for_each_pwq(pwq, wq) { 64956183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 6496112202d9STejun Heo if (pwq->nr_active) { 6497a0a1a5fdSTejun Heo busy = true; 649824acfb71SThomas Gleixner rcu_read_unlock(); 6499a0a1a5fdSTejun Heo goto out_unlock; 6500a0a1a5fdSTejun Heo } 6501a0a1a5fdSTejun Heo } 650224acfb71SThomas Gleixner rcu_read_unlock(); 6503a0a1a5fdSTejun Heo } 6504a0a1a5fdSTejun Heo out_unlock: 650568e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6506a0a1a5fdSTejun Heo return busy; 6507a0a1a5fdSTejun Heo } 6508a0a1a5fdSTejun Heo 6509a0a1a5fdSTejun Heo /** 6510a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 6511a0a1a5fdSTejun Heo * 6512a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 6513706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 6514a0a1a5fdSTejun Heo * 6515a0a1a5fdSTejun Heo * CONTEXT: 6516a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 6517a0a1a5fdSTejun Heo */ 6518a0a1a5fdSTejun Heo void thaw_workqueues(void) 6519a0a1a5fdSTejun Heo { 652024b8a847STejun Heo struct workqueue_struct *wq; 6521a0a1a5fdSTejun Heo 652268e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6523a0a1a5fdSTejun Heo 6524a0a1a5fdSTejun Heo if (!workqueue_freezing) 6525a0a1a5fdSTejun Heo goto out_unlock; 6526a0a1a5fdSTejun Heo 652774b414eaSLai Jiangshan workqueue_freezing = false; 652824b8a847STejun Heo 652924b8a847STejun Heo /* restore max_active and repopulate worklist */ 653024b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6531a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 6532a045a272STejun Heo wq_adjust_max_active(wq); 6533a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 653424b8a847STejun Heo } 653524b8a847STejun Heo 6536a0a1a5fdSTejun Heo out_unlock: 653768e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6538a0a1a5fdSTejun Heo } 6539a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 6540a0a1a5fdSTejun Heo 654199c621efSLai Jiangshan static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) 6542042f7df1SLai Jiangshan { 6543042f7df1SLai Jiangshan LIST_HEAD(ctxs); 6544042f7df1SLai Jiangshan int ret = 0; 6545042f7df1SLai Jiangshan struct workqueue_struct *wq; 6546042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 6547042f7df1SLai Jiangshan 6548042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6549042f7df1SLai Jiangshan 6550042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 65518eb17dc1SWaiman Long if (!(wq->flags & WQ_UNBOUND) || (wq->flags & __WQ_DESTROYING)) 6552042f7df1SLai Jiangshan continue; 6553042f7df1SLai Jiangshan 655499c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); 655584193c07STejun Heo if (IS_ERR(ctx)) { 655684193c07STejun Heo ret = PTR_ERR(ctx); 6557042f7df1SLai Jiangshan break; 6558042f7df1SLai Jiangshan } 6559042f7df1SLai Jiangshan 6560042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 6561042f7df1SLai Jiangshan } 6562042f7df1SLai Jiangshan 6563042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 6564042f7df1SLai Jiangshan if (!ret) 6565042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 6566042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 6567042f7df1SLai Jiangshan } 6568042f7df1SLai Jiangshan 656999c621efSLai Jiangshan if (!ret) { 657099c621efSLai Jiangshan mutex_lock(&wq_pool_attach_mutex); 657199c621efSLai Jiangshan cpumask_copy(wq_unbound_cpumask, unbound_cpumask); 657299c621efSLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 657399c621efSLai Jiangshan } 6574042f7df1SLai Jiangshan return ret; 6575042f7df1SLai Jiangshan } 6576042f7df1SLai Jiangshan 6577042f7df1SLai Jiangshan /** 6578fe28f631SWaiman Long * workqueue_unbound_exclude_cpumask - Exclude given CPUs from unbound cpumask 6579fe28f631SWaiman Long * @exclude_cpumask: the cpumask to be excluded from wq_unbound_cpumask 6580fe28f631SWaiman Long * 6581fe28f631SWaiman Long * This function can be called from cpuset code to provide a set of isolated 6582fe28f631SWaiman Long * CPUs that should be excluded from wq_unbound_cpumask. The caller must hold 6583fe28f631SWaiman Long * either cpus_read_lock or cpus_write_lock. 6584fe28f631SWaiman Long */ 6585fe28f631SWaiman Long int workqueue_unbound_exclude_cpumask(cpumask_var_t exclude_cpumask) 6586fe28f631SWaiman Long { 6587fe28f631SWaiman Long cpumask_var_t cpumask; 6588fe28f631SWaiman Long int ret = 0; 6589fe28f631SWaiman Long 6590fe28f631SWaiman Long if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 6591fe28f631SWaiman Long return -ENOMEM; 6592fe28f631SWaiman Long 6593fe28f631SWaiman Long lockdep_assert_cpus_held(); 6594fe28f631SWaiman Long mutex_lock(&wq_pool_mutex); 6595fe28f631SWaiman Long 6596fe28f631SWaiman Long /* Save the current isolated cpumask & export it via sysfs */ 6597fe28f631SWaiman Long cpumask_copy(wq_isolated_cpumask, exclude_cpumask); 6598fe28f631SWaiman Long 6599fe28f631SWaiman Long /* 6600fe28f631SWaiman Long * If the operation fails, it will fall back to 6601fe28f631SWaiman Long * wq_requested_unbound_cpumask which is initially set to 6602fe28f631SWaiman Long * (HK_TYPE_WQ ∩ HK_TYPE_DOMAIN) house keeping mask and rewritten 6603fe28f631SWaiman Long * by any subsequent write to workqueue/cpumask sysfs file. 6604fe28f631SWaiman Long */ 6605fe28f631SWaiman Long if (!cpumask_andnot(cpumask, wq_requested_unbound_cpumask, exclude_cpumask)) 6606fe28f631SWaiman Long cpumask_copy(cpumask, wq_requested_unbound_cpumask); 6607fe28f631SWaiman Long if (!cpumask_equal(cpumask, wq_unbound_cpumask)) 6608fe28f631SWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 6609fe28f631SWaiman Long 6610fe28f631SWaiman Long mutex_unlock(&wq_pool_mutex); 6611fe28f631SWaiman Long free_cpumask_var(cpumask); 6612fe28f631SWaiman Long return ret; 6613fe28f631SWaiman Long } 6614fe28f631SWaiman Long 661563c5484eSTejun Heo static int parse_affn_scope(const char *val) 661663c5484eSTejun Heo { 661763c5484eSTejun Heo int i; 661863c5484eSTejun Heo 661963c5484eSTejun Heo for (i = 0; i < ARRAY_SIZE(wq_affn_names); i++) { 662063c5484eSTejun Heo if (!strncasecmp(val, wq_affn_names[i], strlen(wq_affn_names[i]))) 662163c5484eSTejun Heo return i; 662263c5484eSTejun Heo } 662363c5484eSTejun Heo return -EINVAL; 662463c5484eSTejun Heo } 662563c5484eSTejun Heo 662663c5484eSTejun Heo static int wq_affn_dfl_set(const char *val, const struct kernel_param *kp) 662763c5484eSTejun Heo { 6628523a301eSTejun Heo struct workqueue_struct *wq; 6629523a301eSTejun Heo int affn, cpu; 663063c5484eSTejun Heo 663163c5484eSTejun Heo affn = parse_affn_scope(val); 663263c5484eSTejun Heo if (affn < 0) 663363c5484eSTejun Heo return affn; 6634523a301eSTejun Heo if (affn == WQ_AFFN_DFL) 6635523a301eSTejun Heo return -EINVAL; 6636523a301eSTejun Heo 6637523a301eSTejun Heo cpus_read_lock(); 6638523a301eSTejun Heo mutex_lock(&wq_pool_mutex); 663963c5484eSTejun Heo 664063c5484eSTejun Heo wq_affn_dfl = affn; 6641523a301eSTejun Heo 6642523a301eSTejun Heo list_for_each_entry(wq, &workqueues, list) { 6643523a301eSTejun Heo for_each_online_cpu(cpu) { 6644523a301eSTejun Heo wq_update_pod(wq, cpu, cpu, true); 6645523a301eSTejun Heo } 6646523a301eSTejun Heo } 6647523a301eSTejun Heo 6648523a301eSTejun Heo mutex_unlock(&wq_pool_mutex); 6649523a301eSTejun Heo cpus_read_unlock(); 6650523a301eSTejun Heo 665163c5484eSTejun Heo return 0; 665263c5484eSTejun Heo } 665363c5484eSTejun Heo 665463c5484eSTejun Heo static int wq_affn_dfl_get(char *buffer, const struct kernel_param *kp) 665563c5484eSTejun Heo { 665663c5484eSTejun Heo return scnprintf(buffer, PAGE_SIZE, "%s\n", wq_affn_names[wq_affn_dfl]); 665763c5484eSTejun Heo } 665863c5484eSTejun Heo 665963c5484eSTejun Heo static const struct kernel_param_ops wq_affn_dfl_ops = { 666063c5484eSTejun Heo .set = wq_affn_dfl_set, 666163c5484eSTejun Heo .get = wq_affn_dfl_get, 666263c5484eSTejun Heo }; 666363c5484eSTejun Heo 666463c5484eSTejun Heo module_param_cb(default_affinity_scope, &wq_affn_dfl_ops, NULL, 0644); 666563c5484eSTejun Heo 66666ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 66676ba94429SFrederic Weisbecker /* 66686ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 66696ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 66706ba94429SFrederic Weisbecker * following attributes. 66716ba94429SFrederic Weisbecker * 66726ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 66736ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 66746ba94429SFrederic Weisbecker * 66756ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 66766ba94429SFrederic Weisbecker * 66776ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 66786ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 667963c5484eSTejun Heo * affinity_scope RW str : worker CPU affinity scope (cache, numa, none) 66808639ecebSTejun Heo * affinity_strict RW bool : worker CPU affinity is strict 66816ba94429SFrederic Weisbecker */ 66826ba94429SFrederic Weisbecker struct wq_device { 66836ba94429SFrederic Weisbecker struct workqueue_struct *wq; 66846ba94429SFrederic Weisbecker struct device dev; 66856ba94429SFrederic Weisbecker }; 66866ba94429SFrederic Weisbecker 66876ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 66886ba94429SFrederic Weisbecker { 66896ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 66906ba94429SFrederic Weisbecker 66916ba94429SFrederic Weisbecker return wq_dev->wq; 66926ba94429SFrederic Weisbecker } 66936ba94429SFrederic Weisbecker 66946ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 66956ba94429SFrederic Weisbecker char *buf) 66966ba94429SFrederic Weisbecker { 66976ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 66986ba94429SFrederic Weisbecker 66996ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 67006ba94429SFrederic Weisbecker } 67016ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 67026ba94429SFrederic Weisbecker 67036ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 67046ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 67056ba94429SFrederic Weisbecker { 67066ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 67076ba94429SFrederic Weisbecker 67086ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 67096ba94429SFrederic Weisbecker } 67106ba94429SFrederic Weisbecker 67116ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 67126ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 67136ba94429SFrederic Weisbecker size_t count) 67146ba94429SFrederic Weisbecker { 67156ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 67166ba94429SFrederic Weisbecker int val; 67176ba94429SFrederic Weisbecker 67186ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 67196ba94429SFrederic Weisbecker return -EINVAL; 67206ba94429SFrederic Weisbecker 67216ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 67226ba94429SFrederic Weisbecker return count; 67236ba94429SFrederic Weisbecker } 67246ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 67256ba94429SFrederic Weisbecker 67266ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 67276ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 67286ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 67296ba94429SFrederic Weisbecker NULL, 67306ba94429SFrederic Weisbecker }; 67316ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 67326ba94429SFrederic Weisbecker 673349277a5bSWaiman Long static void apply_wqattrs_lock(void) 673449277a5bSWaiman Long { 673549277a5bSWaiman Long /* CPUs should stay stable across pwq creations and installations */ 673649277a5bSWaiman Long cpus_read_lock(); 673749277a5bSWaiman Long mutex_lock(&wq_pool_mutex); 673849277a5bSWaiman Long } 673949277a5bSWaiman Long 674049277a5bSWaiman Long static void apply_wqattrs_unlock(void) 674149277a5bSWaiman Long { 674249277a5bSWaiman Long mutex_unlock(&wq_pool_mutex); 674349277a5bSWaiman Long cpus_read_unlock(); 674449277a5bSWaiman Long } 674549277a5bSWaiman Long 67466ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 67476ba94429SFrederic Weisbecker char *buf) 67486ba94429SFrederic Weisbecker { 67496ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 67506ba94429SFrederic Weisbecker int written; 67516ba94429SFrederic Weisbecker 67526ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 67536ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 67546ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 67556ba94429SFrederic Weisbecker 67566ba94429SFrederic Weisbecker return written; 67576ba94429SFrederic Weisbecker } 67586ba94429SFrederic Weisbecker 67596ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 67606ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 67616ba94429SFrederic Weisbecker { 67626ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 67636ba94429SFrederic Weisbecker 6764899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6765899a94feSLai Jiangshan 6766be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs(); 67676ba94429SFrederic Weisbecker if (!attrs) 67686ba94429SFrederic Weisbecker return NULL; 67696ba94429SFrederic Weisbecker 67706ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 67716ba94429SFrederic Weisbecker return attrs; 67726ba94429SFrederic Weisbecker } 67736ba94429SFrederic Weisbecker 67746ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 67756ba94429SFrederic Weisbecker const char *buf, size_t count) 67766ba94429SFrederic Weisbecker { 67776ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 67786ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6779d4d3e257SLai Jiangshan int ret = -ENOMEM; 6780d4d3e257SLai Jiangshan 6781d4d3e257SLai Jiangshan apply_wqattrs_lock(); 67826ba94429SFrederic Weisbecker 67836ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 67846ba94429SFrederic Weisbecker if (!attrs) 6785d4d3e257SLai Jiangshan goto out_unlock; 67866ba94429SFrederic Weisbecker 67876ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 67886ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 6789d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 67906ba94429SFrederic Weisbecker else 67916ba94429SFrederic Weisbecker ret = -EINVAL; 67926ba94429SFrederic Weisbecker 6793d4d3e257SLai Jiangshan out_unlock: 6794d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 67956ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 67966ba94429SFrederic Weisbecker return ret ?: count; 67976ba94429SFrederic Weisbecker } 67986ba94429SFrederic Weisbecker 67996ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 68006ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 68016ba94429SFrederic Weisbecker { 68026ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 68036ba94429SFrederic Weisbecker int written; 68046ba94429SFrederic Weisbecker 68056ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 68066ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 68076ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 68086ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 68096ba94429SFrederic Weisbecker return written; 68106ba94429SFrederic Weisbecker } 68116ba94429SFrederic Weisbecker 68126ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 68136ba94429SFrederic Weisbecker struct device_attribute *attr, 68146ba94429SFrederic Weisbecker const char *buf, size_t count) 68156ba94429SFrederic Weisbecker { 68166ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 68176ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6818d4d3e257SLai Jiangshan int ret = -ENOMEM; 6819d4d3e257SLai Jiangshan 6820d4d3e257SLai Jiangshan apply_wqattrs_lock(); 68216ba94429SFrederic Weisbecker 68226ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 68236ba94429SFrederic Weisbecker if (!attrs) 6824d4d3e257SLai Jiangshan goto out_unlock; 68256ba94429SFrederic Weisbecker 68266ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 68276ba94429SFrederic Weisbecker if (!ret) 6828d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 68296ba94429SFrederic Weisbecker 6830d4d3e257SLai Jiangshan out_unlock: 6831d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 68326ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 68336ba94429SFrederic Weisbecker return ret ?: count; 68346ba94429SFrederic Weisbecker } 68356ba94429SFrederic Weisbecker 683663c5484eSTejun Heo static ssize_t wq_affn_scope_show(struct device *dev, 683763c5484eSTejun Heo struct device_attribute *attr, char *buf) 683863c5484eSTejun Heo { 683963c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 684063c5484eSTejun Heo int written; 684163c5484eSTejun Heo 684263c5484eSTejun Heo mutex_lock(&wq->mutex); 6843523a301eSTejun Heo if (wq->unbound_attrs->affn_scope == WQ_AFFN_DFL) 6844523a301eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s (%s)\n", 6845523a301eSTejun Heo wq_affn_names[WQ_AFFN_DFL], 6846523a301eSTejun Heo wq_affn_names[wq_affn_dfl]); 6847523a301eSTejun Heo else 684863c5484eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s\n", 684963c5484eSTejun Heo wq_affn_names[wq->unbound_attrs->affn_scope]); 685063c5484eSTejun Heo mutex_unlock(&wq->mutex); 685163c5484eSTejun Heo 685263c5484eSTejun Heo return written; 685363c5484eSTejun Heo } 685463c5484eSTejun Heo 685563c5484eSTejun Heo static ssize_t wq_affn_scope_store(struct device *dev, 685663c5484eSTejun Heo struct device_attribute *attr, 685763c5484eSTejun Heo const char *buf, size_t count) 685863c5484eSTejun Heo { 685963c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 686063c5484eSTejun Heo struct workqueue_attrs *attrs; 686163c5484eSTejun Heo int affn, ret = -ENOMEM; 686263c5484eSTejun Heo 686363c5484eSTejun Heo affn = parse_affn_scope(buf); 686463c5484eSTejun Heo if (affn < 0) 686563c5484eSTejun Heo return affn; 686663c5484eSTejun Heo 686763c5484eSTejun Heo apply_wqattrs_lock(); 686863c5484eSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 686963c5484eSTejun Heo if (attrs) { 687063c5484eSTejun Heo attrs->affn_scope = affn; 687163c5484eSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 687263c5484eSTejun Heo } 687363c5484eSTejun Heo apply_wqattrs_unlock(); 687463c5484eSTejun Heo free_workqueue_attrs(attrs); 687563c5484eSTejun Heo return ret ?: count; 687663c5484eSTejun Heo } 687763c5484eSTejun Heo 68788639ecebSTejun Heo static ssize_t wq_affinity_strict_show(struct device *dev, 68798639ecebSTejun Heo struct device_attribute *attr, char *buf) 68808639ecebSTejun Heo { 68818639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 68828639ecebSTejun Heo 68838639ecebSTejun Heo return scnprintf(buf, PAGE_SIZE, "%d\n", 68848639ecebSTejun Heo wq->unbound_attrs->affn_strict); 68858639ecebSTejun Heo } 68868639ecebSTejun Heo 68878639ecebSTejun Heo static ssize_t wq_affinity_strict_store(struct device *dev, 68888639ecebSTejun Heo struct device_attribute *attr, 68898639ecebSTejun Heo const char *buf, size_t count) 68908639ecebSTejun Heo { 68918639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 68928639ecebSTejun Heo struct workqueue_attrs *attrs; 68938639ecebSTejun Heo int v, ret = -ENOMEM; 68948639ecebSTejun Heo 68958639ecebSTejun Heo if (sscanf(buf, "%d", &v) != 1) 68968639ecebSTejun Heo return -EINVAL; 68978639ecebSTejun Heo 68988639ecebSTejun Heo apply_wqattrs_lock(); 68998639ecebSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 69008639ecebSTejun Heo if (attrs) { 69018639ecebSTejun Heo attrs->affn_strict = (bool)v; 69028639ecebSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 69038639ecebSTejun Heo } 69048639ecebSTejun Heo apply_wqattrs_unlock(); 69058639ecebSTejun Heo free_workqueue_attrs(attrs); 69068639ecebSTejun Heo return ret ?: count; 69078639ecebSTejun Heo } 69088639ecebSTejun Heo 69096ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 69106ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 69116ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 691263c5484eSTejun Heo __ATTR(affinity_scope, 0644, wq_affn_scope_show, wq_affn_scope_store), 69138639ecebSTejun Heo __ATTR(affinity_strict, 0644, wq_affinity_strict_show, wq_affinity_strict_store), 69146ba94429SFrederic Weisbecker __ATTR_NULL, 69156ba94429SFrederic Weisbecker }; 69166ba94429SFrederic Weisbecker 69174f19b8e0STejun Heo static struct bus_type wq_subsys = { 69186ba94429SFrederic Weisbecker .name = "workqueue", 69196ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 69206ba94429SFrederic Weisbecker }; 69216ba94429SFrederic Weisbecker 692249277a5bSWaiman Long /** 692349277a5bSWaiman Long * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 692449277a5bSWaiman Long * @cpumask: the cpumask to set 692549277a5bSWaiman Long * 692649277a5bSWaiman Long * The low-level workqueues cpumask is a global cpumask that limits 692749277a5bSWaiman Long * the affinity of all unbound workqueues. This function check the @cpumask 692849277a5bSWaiman Long * and apply it to all unbound workqueues and updates all pwqs of them. 692949277a5bSWaiman Long * 693049277a5bSWaiman Long * Return: 0 - Success 693149277a5bSWaiman Long * -EINVAL - Invalid @cpumask 693249277a5bSWaiman Long * -ENOMEM - Failed to allocate memory for attrs or pwqs. 693349277a5bSWaiman Long */ 693449277a5bSWaiman Long static int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 693549277a5bSWaiman Long { 693649277a5bSWaiman Long int ret = -EINVAL; 693749277a5bSWaiman Long 693849277a5bSWaiman Long /* 693949277a5bSWaiman Long * Not excluding isolated cpus on purpose. 694049277a5bSWaiman Long * If the user wishes to include them, we allow that. 694149277a5bSWaiman Long */ 694249277a5bSWaiman Long cpumask_and(cpumask, cpumask, cpu_possible_mask); 694349277a5bSWaiman Long if (!cpumask_empty(cpumask)) { 694449277a5bSWaiman Long apply_wqattrs_lock(); 694549277a5bSWaiman Long cpumask_copy(wq_requested_unbound_cpumask, cpumask); 694649277a5bSWaiman Long if (cpumask_equal(cpumask, wq_unbound_cpumask)) { 694749277a5bSWaiman Long ret = 0; 694849277a5bSWaiman Long goto out_unlock; 694949277a5bSWaiman Long } 695049277a5bSWaiman Long 695149277a5bSWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 695249277a5bSWaiman Long 695349277a5bSWaiman Long out_unlock: 695449277a5bSWaiman Long apply_wqattrs_unlock(); 695549277a5bSWaiman Long } 695649277a5bSWaiman Long 695749277a5bSWaiman Long return ret; 695849277a5bSWaiman Long } 695949277a5bSWaiman Long 6960fe28f631SWaiman Long static ssize_t __wq_cpumask_show(struct device *dev, 6961fe28f631SWaiman Long struct device_attribute *attr, char *buf, cpumask_var_t mask) 6962b05a7928SFrederic Weisbecker { 6963b05a7928SFrederic Weisbecker int written; 6964b05a7928SFrederic Weisbecker 6965042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 6966fe28f631SWaiman Long written = scnprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask)); 6967042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6968b05a7928SFrederic Weisbecker 6969b05a7928SFrederic Weisbecker return written; 6970b05a7928SFrederic Weisbecker } 6971b05a7928SFrederic Weisbecker 6972fe28f631SWaiman Long static ssize_t wq_unbound_cpumask_show(struct device *dev, 6973fe28f631SWaiman Long struct device_attribute *attr, char *buf) 6974fe28f631SWaiman Long { 6975fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_unbound_cpumask); 6976fe28f631SWaiman Long } 6977fe28f631SWaiman Long 6978fe28f631SWaiman Long static ssize_t wq_requested_cpumask_show(struct device *dev, 6979fe28f631SWaiman Long struct device_attribute *attr, char *buf) 6980fe28f631SWaiman Long { 6981fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_requested_unbound_cpumask); 6982fe28f631SWaiman Long } 6983fe28f631SWaiman Long 6984fe28f631SWaiman Long static ssize_t wq_isolated_cpumask_show(struct device *dev, 6985fe28f631SWaiman Long struct device_attribute *attr, char *buf) 6986fe28f631SWaiman Long { 6987fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_isolated_cpumask); 6988fe28f631SWaiman Long } 6989fe28f631SWaiman Long 6990042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 6991042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 6992042f7df1SLai Jiangshan { 6993042f7df1SLai Jiangshan cpumask_var_t cpumask; 6994042f7df1SLai Jiangshan int ret; 6995042f7df1SLai Jiangshan 6996042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 6997042f7df1SLai Jiangshan return -ENOMEM; 6998042f7df1SLai Jiangshan 6999042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 7000042f7df1SLai Jiangshan if (!ret) 7001042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 7002042f7df1SLai Jiangshan 7003042f7df1SLai Jiangshan free_cpumask_var(cpumask); 7004042f7df1SLai Jiangshan return ret ? ret : count; 7005042f7df1SLai Jiangshan } 7006042f7df1SLai Jiangshan 7007fe28f631SWaiman Long static struct device_attribute wq_sysfs_cpumask_attrs[] = { 7008042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 7009fe28f631SWaiman Long wq_unbound_cpumask_store), 7010fe28f631SWaiman Long __ATTR(cpumask_requested, 0444, wq_requested_cpumask_show, NULL), 7011fe28f631SWaiman Long __ATTR(cpumask_isolated, 0444, wq_isolated_cpumask_show, NULL), 7012fe28f631SWaiman Long __ATTR_NULL, 7013fe28f631SWaiman Long }; 7014b05a7928SFrederic Weisbecker 70156ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 70166ba94429SFrederic Weisbecker { 7017686f6697SGreg Kroah-Hartman struct device *dev_root; 7018b05a7928SFrederic Weisbecker int err; 7019b05a7928SFrederic Weisbecker 7020b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 7021b05a7928SFrederic Weisbecker if (err) 7022b05a7928SFrederic Weisbecker return err; 7023b05a7928SFrederic Weisbecker 7024686f6697SGreg Kroah-Hartman dev_root = bus_get_dev_root(&wq_subsys); 7025686f6697SGreg Kroah-Hartman if (dev_root) { 7026fe28f631SWaiman Long struct device_attribute *attr; 7027fe28f631SWaiman Long 7028fe28f631SWaiman Long for (attr = wq_sysfs_cpumask_attrs; attr->attr.name; attr++) { 7029fe28f631SWaiman Long err = device_create_file(dev_root, attr); 7030fe28f631SWaiman Long if (err) 7031fe28f631SWaiman Long break; 7032fe28f631SWaiman Long } 7033686f6697SGreg Kroah-Hartman put_device(dev_root); 7034686f6697SGreg Kroah-Hartman } 7035686f6697SGreg Kroah-Hartman return err; 70366ba94429SFrederic Weisbecker } 70376ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 70386ba94429SFrederic Weisbecker 70396ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 70406ba94429SFrederic Weisbecker { 70416ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 70426ba94429SFrederic Weisbecker 70436ba94429SFrederic Weisbecker kfree(wq_dev); 70446ba94429SFrederic Weisbecker } 70456ba94429SFrederic Weisbecker 70466ba94429SFrederic Weisbecker /** 70476ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 70486ba94429SFrederic Weisbecker * @wq: the workqueue to register 70496ba94429SFrederic Weisbecker * 70506ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 70516ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 70526ba94429SFrederic Weisbecker * which is the preferred method. 70536ba94429SFrederic Weisbecker * 70546ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 70556ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 70566ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 70576ba94429SFrederic Weisbecker * attributes. 70586ba94429SFrederic Weisbecker * 70596ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 70606ba94429SFrederic Weisbecker */ 70616ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 70626ba94429SFrederic Weisbecker { 70636ba94429SFrederic Weisbecker struct wq_device *wq_dev; 70646ba94429SFrederic Weisbecker int ret; 70656ba94429SFrederic Weisbecker 70666ba94429SFrederic Weisbecker /* 70674c065dbcSWaiman Long * Adjusting max_active breaks ordering guarantee. Disallow exposing 70684c065dbcSWaiman Long * ordered workqueues. 70696ba94429SFrederic Weisbecker */ 70703bc1e711STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED)) 70716ba94429SFrederic Weisbecker return -EINVAL; 70726ba94429SFrederic Weisbecker 70736ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 70746ba94429SFrederic Weisbecker if (!wq_dev) 70756ba94429SFrederic Weisbecker return -ENOMEM; 70766ba94429SFrederic Weisbecker 70776ba94429SFrederic Weisbecker wq_dev->wq = wq; 70786ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 70796ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 708023217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 70816ba94429SFrederic Weisbecker 70826ba94429SFrederic Weisbecker /* 70836ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 70846ba94429SFrederic Weisbecker * everything is ready. 70856ba94429SFrederic Weisbecker */ 70866ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 70876ba94429SFrederic Weisbecker 70886ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 70896ba94429SFrederic Weisbecker if (ret) { 7090537f4146SArvind Yadav put_device(&wq_dev->dev); 70916ba94429SFrederic Weisbecker wq->wq_dev = NULL; 70926ba94429SFrederic Weisbecker return ret; 70936ba94429SFrederic Weisbecker } 70946ba94429SFrederic Weisbecker 70956ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 70966ba94429SFrederic Weisbecker struct device_attribute *attr; 70976ba94429SFrederic Weisbecker 70986ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 70996ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 71006ba94429SFrederic Weisbecker if (ret) { 71016ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 71026ba94429SFrederic Weisbecker wq->wq_dev = NULL; 71036ba94429SFrederic Weisbecker return ret; 71046ba94429SFrederic Weisbecker } 71056ba94429SFrederic Weisbecker } 71066ba94429SFrederic Weisbecker } 71076ba94429SFrederic Weisbecker 71086ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 71096ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 71106ba94429SFrederic Weisbecker return 0; 71116ba94429SFrederic Weisbecker } 71126ba94429SFrederic Weisbecker 71136ba94429SFrederic Weisbecker /** 71146ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 71156ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 71166ba94429SFrederic Weisbecker * 71176ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 71186ba94429SFrederic Weisbecker */ 71196ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 71206ba94429SFrederic Weisbecker { 71216ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 71226ba94429SFrederic Weisbecker 71236ba94429SFrederic Weisbecker if (!wq->wq_dev) 71246ba94429SFrederic Weisbecker return; 71256ba94429SFrederic Weisbecker 71266ba94429SFrederic Weisbecker wq->wq_dev = NULL; 71276ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 71286ba94429SFrederic Weisbecker } 71296ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 71306ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 71316ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 71326ba94429SFrederic Weisbecker 713382607adcSTejun Heo /* 713482607adcSTejun Heo * Workqueue watchdog. 713582607adcSTejun Heo * 713682607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 713782607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 713882607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 713982607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 714082607adcSTejun Heo * largely opaque. 714182607adcSTejun Heo * 714282607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 714382607adcSTejun Heo * state if some pools failed to make forward progress for a while where 714482607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 714582607adcSTejun Heo * 714682607adcSTejun Heo * This mechanism is controlled through the kernel parameter 714782607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 714882607adcSTejun Heo * corresponding sysfs parameter file. 714982607adcSTejun Heo */ 715082607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 715182607adcSTejun Heo 715282607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 71535cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 715482607adcSTejun Heo 715582607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 715682607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 715782607adcSTejun Heo 7158cd2440d6SPetr Mladek /* 7159cd2440d6SPetr Mladek * Show workers that might prevent the processing of pending work items. 7160cd2440d6SPetr Mladek * The only candidates are CPU-bound workers in the running state. 7161cd2440d6SPetr Mladek * Pending work items should be handled by another idle worker 7162cd2440d6SPetr Mladek * in all other situations. 7163cd2440d6SPetr Mladek */ 7164cd2440d6SPetr Mladek static void show_cpu_pool_hog(struct worker_pool *pool) 7165cd2440d6SPetr Mladek { 7166cd2440d6SPetr Mladek struct worker *worker; 7167cd2440d6SPetr Mladek unsigned long flags; 7168cd2440d6SPetr Mladek int bkt; 7169cd2440d6SPetr Mladek 7170cd2440d6SPetr Mladek raw_spin_lock_irqsave(&pool->lock, flags); 7171cd2440d6SPetr Mladek 7172cd2440d6SPetr Mladek hash_for_each(pool->busy_hash, bkt, worker, hentry) { 7173cd2440d6SPetr Mladek if (task_is_running(worker->task)) { 7174cd2440d6SPetr Mladek /* 7175cd2440d6SPetr Mladek * Defer printing to avoid deadlocks in console 7176cd2440d6SPetr Mladek * drivers that queue work while holding locks 7177cd2440d6SPetr Mladek * also taken in their write paths. 7178cd2440d6SPetr Mladek */ 7179cd2440d6SPetr Mladek printk_deferred_enter(); 7180cd2440d6SPetr Mladek 7181cd2440d6SPetr Mladek pr_info("pool %d:\n", pool->id); 7182cd2440d6SPetr Mladek sched_show_task(worker->task); 7183cd2440d6SPetr Mladek 7184cd2440d6SPetr Mladek printk_deferred_exit(); 7185cd2440d6SPetr Mladek } 7186cd2440d6SPetr Mladek } 7187cd2440d6SPetr Mladek 7188cd2440d6SPetr Mladek raw_spin_unlock_irqrestore(&pool->lock, flags); 7189cd2440d6SPetr Mladek } 7190cd2440d6SPetr Mladek 7191cd2440d6SPetr Mladek static void show_cpu_pools_hogs(void) 7192cd2440d6SPetr Mladek { 7193cd2440d6SPetr Mladek struct worker_pool *pool; 7194cd2440d6SPetr Mladek int pi; 7195cd2440d6SPetr Mladek 7196cd2440d6SPetr Mladek pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); 7197cd2440d6SPetr Mladek 7198cd2440d6SPetr Mladek rcu_read_lock(); 7199cd2440d6SPetr Mladek 7200cd2440d6SPetr Mladek for_each_pool(pool, pi) { 7201cd2440d6SPetr Mladek if (pool->cpu_stall) 7202cd2440d6SPetr Mladek show_cpu_pool_hog(pool); 7203cd2440d6SPetr Mladek 7204cd2440d6SPetr Mladek } 7205cd2440d6SPetr Mladek 7206cd2440d6SPetr Mladek rcu_read_unlock(); 7207cd2440d6SPetr Mladek } 7208cd2440d6SPetr Mladek 720982607adcSTejun Heo static void wq_watchdog_reset_touched(void) 721082607adcSTejun Heo { 721182607adcSTejun Heo int cpu; 721282607adcSTejun Heo 721382607adcSTejun Heo wq_watchdog_touched = jiffies; 721482607adcSTejun Heo for_each_possible_cpu(cpu) 721582607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 721682607adcSTejun Heo } 721782607adcSTejun Heo 72185cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 721982607adcSTejun Heo { 722082607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 722182607adcSTejun Heo bool lockup_detected = false; 7222cd2440d6SPetr Mladek bool cpu_pool_stall = false; 7223940d71c6SSergey Senozhatsky unsigned long now = jiffies; 722482607adcSTejun Heo struct worker_pool *pool; 722582607adcSTejun Heo int pi; 722682607adcSTejun Heo 722782607adcSTejun Heo if (!thresh) 722882607adcSTejun Heo return; 722982607adcSTejun Heo 723082607adcSTejun Heo rcu_read_lock(); 723182607adcSTejun Heo 723282607adcSTejun Heo for_each_pool(pool, pi) { 723382607adcSTejun Heo unsigned long pool_ts, touched, ts; 723482607adcSTejun Heo 7235cd2440d6SPetr Mladek pool->cpu_stall = false; 723682607adcSTejun Heo if (list_empty(&pool->worklist)) 723782607adcSTejun Heo continue; 723882607adcSTejun Heo 7239940d71c6SSergey Senozhatsky /* 7240940d71c6SSergey Senozhatsky * If a virtual machine is stopped by the host it can look to 7241940d71c6SSergey Senozhatsky * the watchdog like a stall. 7242940d71c6SSergey Senozhatsky */ 7243940d71c6SSergey Senozhatsky kvm_check_and_clear_guest_paused(); 7244940d71c6SSergey Senozhatsky 724582607adcSTejun Heo /* get the latest of pool and touched timestamps */ 724689e28ce6SWang Qing if (pool->cpu >= 0) 724789e28ce6SWang Qing touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); 724889e28ce6SWang Qing else 724982607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 725089e28ce6SWang Qing pool_ts = READ_ONCE(pool->watchdog_ts); 725182607adcSTejun Heo 725282607adcSTejun Heo if (time_after(pool_ts, touched)) 725382607adcSTejun Heo ts = pool_ts; 725482607adcSTejun Heo else 725582607adcSTejun Heo ts = touched; 725682607adcSTejun Heo 725782607adcSTejun Heo /* did we stall? */ 7258940d71c6SSergey Senozhatsky if (time_after(now, ts + thresh)) { 725982607adcSTejun Heo lockup_detected = true; 72604cb1ef64STejun Heo if (pool->cpu >= 0 && !(pool->flags & POOL_BH)) { 7261cd2440d6SPetr Mladek pool->cpu_stall = true; 7262cd2440d6SPetr Mladek cpu_pool_stall = true; 7263cd2440d6SPetr Mladek } 726482607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 726582607adcSTejun Heo pr_cont_pool_info(pool); 726682607adcSTejun Heo pr_cont(" stuck for %us!\n", 7267940d71c6SSergey Senozhatsky jiffies_to_msecs(now - pool_ts) / 1000); 726882607adcSTejun Heo } 7269cd2440d6SPetr Mladek 7270cd2440d6SPetr Mladek 727182607adcSTejun Heo } 727282607adcSTejun Heo 727382607adcSTejun Heo rcu_read_unlock(); 727482607adcSTejun Heo 727582607adcSTejun Heo if (lockup_detected) 727655df0933SImran Khan show_all_workqueues(); 727782607adcSTejun Heo 7278cd2440d6SPetr Mladek if (cpu_pool_stall) 7279cd2440d6SPetr Mladek show_cpu_pools_hogs(); 7280cd2440d6SPetr Mladek 728182607adcSTejun Heo wq_watchdog_reset_touched(); 728282607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 728382607adcSTejun Heo } 728482607adcSTejun Heo 7285cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 728682607adcSTejun Heo { 728782607adcSTejun Heo if (cpu >= 0) 728882607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 728989e28ce6SWang Qing 729082607adcSTejun Heo wq_watchdog_touched = jiffies; 729182607adcSTejun Heo } 729282607adcSTejun Heo 729382607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 729482607adcSTejun Heo { 729582607adcSTejun Heo wq_watchdog_thresh = 0; 729682607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 729782607adcSTejun Heo 729882607adcSTejun Heo if (thresh) { 729982607adcSTejun Heo wq_watchdog_thresh = thresh; 730082607adcSTejun Heo wq_watchdog_reset_touched(); 730182607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 730282607adcSTejun Heo } 730382607adcSTejun Heo } 730482607adcSTejun Heo 730582607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 730682607adcSTejun Heo const struct kernel_param *kp) 730782607adcSTejun Heo { 730882607adcSTejun Heo unsigned long thresh; 730982607adcSTejun Heo int ret; 731082607adcSTejun Heo 731182607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 731282607adcSTejun Heo if (ret) 731382607adcSTejun Heo return ret; 731482607adcSTejun Heo 731582607adcSTejun Heo if (system_wq) 731682607adcSTejun Heo wq_watchdog_set_thresh(thresh); 731782607adcSTejun Heo else 731882607adcSTejun Heo wq_watchdog_thresh = thresh; 731982607adcSTejun Heo 732082607adcSTejun Heo return 0; 732182607adcSTejun Heo } 732282607adcSTejun Heo 732382607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 732482607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 732582607adcSTejun Heo .get = param_get_ulong, 732682607adcSTejun Heo }; 732782607adcSTejun Heo 732882607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 732982607adcSTejun Heo 0644); 733082607adcSTejun Heo 733182607adcSTejun Heo static void wq_watchdog_init(void) 733282607adcSTejun Heo { 73335cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 733482607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 733582607adcSTejun Heo } 733682607adcSTejun Heo 733782607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 733882607adcSTejun Heo 733982607adcSTejun Heo static inline void wq_watchdog_init(void) { } 734082607adcSTejun Heo 734182607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 734282607adcSTejun Heo 73434a6c5607STejun Heo static void __init restrict_unbound_cpumask(const char *name, const struct cpumask *mask) 73444a6c5607STejun Heo { 73454a6c5607STejun Heo if (!cpumask_intersects(wq_unbound_cpumask, mask)) { 73464a6c5607STejun Heo pr_warn("workqueue: Restricting unbound_cpumask (%*pb) with %s (%*pb) leaves no CPU, ignoring\n", 73474a6c5607STejun Heo cpumask_pr_args(wq_unbound_cpumask), name, cpumask_pr_args(mask)); 73484a6c5607STejun Heo return; 73494a6c5607STejun Heo } 73504a6c5607STejun Heo 73514a6c5607STejun Heo cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, mask); 73524a6c5607STejun Heo } 73534a6c5607STejun Heo 73542fcdb1b4STejun Heo static void __init init_cpu_worker_pool(struct worker_pool *pool, int cpu, int nice) 73552fcdb1b4STejun Heo { 73562fcdb1b4STejun Heo BUG_ON(init_worker_pool(pool)); 73572fcdb1b4STejun Heo pool->cpu = cpu; 73582fcdb1b4STejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 73592fcdb1b4STejun Heo cpumask_copy(pool->attrs->__pod_cpumask, cpumask_of(cpu)); 73602fcdb1b4STejun Heo pool->attrs->nice = nice; 73612fcdb1b4STejun Heo pool->attrs->affn_strict = true; 73622fcdb1b4STejun Heo pool->node = cpu_to_node(cpu); 73632fcdb1b4STejun Heo 73642fcdb1b4STejun Heo /* alloc pool ID */ 73652fcdb1b4STejun Heo mutex_lock(&wq_pool_mutex); 73662fcdb1b4STejun Heo BUG_ON(worker_pool_assign_id(pool)); 73672fcdb1b4STejun Heo mutex_unlock(&wq_pool_mutex); 73682fcdb1b4STejun Heo } 73692fcdb1b4STejun Heo 73703347fa09STejun Heo /** 73713347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 73723347fa09STejun Heo * 73732930155bSTejun Heo * This is the first step of three-staged workqueue subsystem initialization and 73742930155bSTejun Heo * invoked as soon as the bare basics - memory allocation, cpumasks and idr are 73752930155bSTejun Heo * up. It sets up all the data structures and system workqueues and allows early 73762930155bSTejun Heo * boot code to create workqueues and queue/cancel work items. Actual work item 73772930155bSTejun Heo * execution starts only after kthreads can be created and scheduled right 73782930155bSTejun Heo * before early initcalls. 73793347fa09STejun Heo */ 73802333e829SYu Chen void __init workqueue_init_early(void) 73811da177e4SLinus Torvalds { 738284193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 73837a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 73847a4e344cSTejun Heo int i, cpu; 7385c34056a3STejun Heo 738610cdb157SLai Jiangshan BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 7387e904e6c2STejun Heo 7388b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 7389fe28f631SWaiman Long BUG_ON(!alloc_cpumask_var(&wq_requested_unbound_cpumask, GFP_KERNEL)); 7390fe28f631SWaiman Long BUG_ON(!zalloc_cpumask_var(&wq_isolated_cpumask, GFP_KERNEL)); 7391b05a7928SFrederic Weisbecker 73924a6c5607STejun Heo cpumask_copy(wq_unbound_cpumask, cpu_possible_mask); 73934a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_WQ", housekeeping_cpumask(HK_TYPE_WQ)); 73944a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_DOMAIN", housekeeping_cpumask(HK_TYPE_DOMAIN)); 7395ace3c549Stiozhang if (!cpumask_empty(&wq_cmdline_cpumask)) 73964a6c5607STejun Heo restrict_unbound_cpumask("workqueue.unbound_cpus", &wq_cmdline_cpumask); 7397ace3c549Stiozhang 7398fe28f631SWaiman Long cpumask_copy(wq_requested_unbound_cpumask, wq_unbound_cpumask); 73998b03ae3cSTejun Heo 74008b03ae3cSTejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 7401e22bee78STejun Heo 74022930155bSTejun Heo wq_update_pod_attrs_buf = alloc_workqueue_attrs(); 74032930155bSTejun Heo BUG_ON(!wq_update_pod_attrs_buf); 74042930155bSTejun Heo 74057bd20b6bSMarcelo Tosatti /* 74067bd20b6bSMarcelo Tosatti * If nohz_full is enabled, set power efficient workqueue as unbound. 74077bd20b6bSMarcelo Tosatti * This allows workqueue items to be moved to HK CPUs. 74087bd20b6bSMarcelo Tosatti */ 74097bd20b6bSMarcelo Tosatti if (housekeeping_enabled(HK_TYPE_TICK)) 74107bd20b6bSMarcelo Tosatti wq_power_efficient = true; 74117bd20b6bSMarcelo Tosatti 741284193c07STejun Heo /* initialize WQ_AFFN_SYSTEM pods */ 741384193c07STejun Heo pt->pod_cpus = kcalloc(1, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 741484193c07STejun Heo pt->pod_node = kcalloc(1, sizeof(pt->pod_node[0]), GFP_KERNEL); 741584193c07STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 741684193c07STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node || !pt->cpu_pod); 741784193c07STejun Heo 741884193c07STejun Heo BUG_ON(!zalloc_cpumask_var_node(&pt->pod_cpus[0], GFP_KERNEL, NUMA_NO_NODE)); 741984193c07STejun Heo 742084193c07STejun Heo pt->nr_pods = 1; 742184193c07STejun Heo cpumask_copy(pt->pod_cpus[0], cpu_possible_mask); 742284193c07STejun Heo pt->pod_node[0] = NUMA_NO_NODE; 742384193c07STejun Heo pt->cpu_pod[0] = 0; 742484193c07STejun Heo 74254cb1ef64STejun Heo /* initialize BH and CPU pools */ 74261da177e4SLinus Torvalds for_each_possible_cpu(cpu) { 74271da177e4SLinus Torvalds struct worker_pool *pool; 74281da177e4SLinus Torvalds 74291da177e4SLinus Torvalds i = 0; 74304cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) { 74314cb1ef64STejun Heo init_cpu_worker_pool(pool, cpu, std_nice[i++]); 74324cb1ef64STejun Heo pool->flags |= POOL_BH; 74334cb1ef64STejun Heo } 74344cb1ef64STejun Heo 74354cb1ef64STejun Heo i = 0; 74362fcdb1b4STejun Heo for_each_cpu_worker_pool(pool, cpu) 74372fcdb1b4STejun Heo init_cpu_worker_pool(pool, cpu, std_nice[i++]); 74381da177e4SLinus Torvalds } 74391da177e4SLinus Torvalds 74408a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 744129c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 744229c91e99STejun Heo struct workqueue_attrs *attrs; 744329c91e99STejun Heo 7444be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 744529c91e99STejun Heo attrs->nice = std_nice[i]; 744629c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 74478a2b7538STejun Heo 74488a2b7538STejun Heo /* 74498a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 74508a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 74518a2b7538STejun Heo */ 7452be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 74538a2b7538STejun Heo attrs->nice = std_nice[i]; 7454af73f5c9STejun Heo attrs->ordered = true; 74558a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 745629c91e99STejun Heo } 745729c91e99STejun Heo 7458d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 74591aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 7460d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 7461f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 7462636b927eSTejun Heo WQ_MAX_ACTIVE); 746324d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 746424d51addSTejun Heo WQ_FREEZABLE, 0); 74650668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 74660668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 74678318d6a6SAudra Mitchell system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_pwr_efficient", 74680668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 74690668106cSViresh Kumar 0); 74704cb1ef64STejun Heo system_bh_wq = alloc_workqueue("events_bh", WQ_BH, 0); 74714cb1ef64STejun Heo system_bh_highpri_wq = alloc_workqueue("events_bh_highpri", 74724cb1ef64STejun Heo WQ_BH | WQ_HIGHPRI, 0); 74731aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 74740668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 74750668106cSViresh Kumar !system_power_efficient_wq || 74764cb1ef64STejun Heo !system_freezable_power_efficient_wq || 74774cb1ef64STejun Heo !system_bh_wq || !system_bh_highpri_wq); 74783347fa09STejun Heo } 74793347fa09STejun Heo 7480aa6fde93STejun Heo static void __init wq_cpu_intensive_thresh_init(void) 7481aa6fde93STejun Heo { 7482aa6fde93STejun Heo unsigned long thresh; 7483aa6fde93STejun Heo unsigned long bogo; 7484aa6fde93STejun Heo 7485dd64c873SZqiang pwq_release_worker = kthread_create_worker(0, "pool_workqueue_release"); 7486dd64c873SZqiang BUG_ON(IS_ERR(pwq_release_worker)); 7487dd64c873SZqiang 7488aa6fde93STejun Heo /* if the user set it to a specific value, keep it */ 7489aa6fde93STejun Heo if (wq_cpu_intensive_thresh_us != ULONG_MAX) 7490aa6fde93STejun Heo return; 7491aa6fde93STejun Heo 7492aa6fde93STejun Heo /* 7493aa6fde93STejun Heo * The default of 10ms is derived from the fact that most modern (as of 7494aa6fde93STejun Heo * 2023) processors can do a lot in 10ms and that it's just below what 7495aa6fde93STejun Heo * most consider human-perceivable. However, the kernel also runs on a 7496aa6fde93STejun Heo * lot slower CPUs including microcontrollers where the threshold is way 7497aa6fde93STejun Heo * too low. 7498aa6fde93STejun Heo * 7499aa6fde93STejun Heo * Let's scale up the threshold upto 1 second if BogoMips is below 4000. 7500aa6fde93STejun Heo * This is by no means accurate but it doesn't have to be. The mechanism 7501aa6fde93STejun Heo * is still useful even when the threshold is fully scaled up. Also, as 7502aa6fde93STejun Heo * the reports would usually be applicable to everyone, some machines 7503aa6fde93STejun Heo * operating on longer thresholds won't significantly diminish their 7504aa6fde93STejun Heo * usefulness. 7505aa6fde93STejun Heo */ 7506aa6fde93STejun Heo thresh = 10 * USEC_PER_MSEC; 7507aa6fde93STejun Heo 7508aa6fde93STejun Heo /* see init/calibrate.c for lpj -> BogoMIPS calculation */ 7509aa6fde93STejun Heo bogo = max_t(unsigned long, loops_per_jiffy / 500000 * HZ, 1); 7510aa6fde93STejun Heo if (bogo < 4000) 7511aa6fde93STejun Heo thresh = min_t(unsigned long, thresh * 4000 / bogo, USEC_PER_SEC); 7512aa6fde93STejun Heo 7513aa6fde93STejun Heo pr_debug("wq_cpu_intensive_thresh: lpj=%lu BogoMIPS=%lu thresh_us=%lu\n", 7514aa6fde93STejun Heo loops_per_jiffy, bogo, thresh); 7515aa6fde93STejun Heo 7516aa6fde93STejun Heo wq_cpu_intensive_thresh_us = thresh; 7517aa6fde93STejun Heo } 7518aa6fde93STejun Heo 75193347fa09STejun Heo /** 75203347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 75213347fa09STejun Heo * 75222930155bSTejun Heo * This is the second step of three-staged workqueue subsystem initialization 75232930155bSTejun Heo * and invoked as soon as kthreads can be created and scheduled. Workqueues have 75242930155bSTejun Heo * been created and work items queued on them, but there are no kworkers 75252930155bSTejun Heo * executing the work items yet. Populate the worker pools with the initial 75262930155bSTejun Heo * workers and enable future kworker creations. 75273347fa09STejun Heo */ 75282333e829SYu Chen void __init workqueue_init(void) 75293347fa09STejun Heo { 75302186d9f9STejun Heo struct workqueue_struct *wq; 75313347fa09STejun Heo struct worker_pool *pool; 75323347fa09STejun Heo int cpu, bkt; 75333347fa09STejun Heo 7534aa6fde93STejun Heo wq_cpu_intensive_thresh_init(); 7535aa6fde93STejun Heo 75362186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 75372186d9f9STejun Heo 75382930155bSTejun Heo /* 75392930155bSTejun Heo * Per-cpu pools created earlier could be missing node hint. Fix them 75402930155bSTejun Heo * up. Also, create a rescuer for workqueues that requested it. 75412930155bSTejun Heo */ 75422186d9f9STejun Heo for_each_possible_cpu(cpu) { 75434cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) 75442186d9f9STejun Heo pool->node = cpu_to_node(cpu); 75454cb1ef64STejun Heo for_each_cpu_worker_pool(pool, cpu) 75464cb1ef64STejun Heo pool->node = cpu_to_node(cpu); 75472186d9f9STejun Heo } 75482186d9f9STejun Heo 754940c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 755040c17f75STejun Heo WARN(init_rescuer(wq), 755140c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 755240c17f75STejun Heo wq->name); 755340c17f75STejun Heo } 75542186d9f9STejun Heo 75552186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 75562186d9f9STejun Heo 75574cb1ef64STejun Heo /* 75584cb1ef64STejun Heo * Create the initial workers. A BH pool has one pseudo worker that 75594cb1ef64STejun Heo * represents the shared BH execution context and thus doesn't get 75604cb1ef64STejun Heo * affected by hotplug events. Create the BH pseudo workers for all 75614cb1ef64STejun Heo * possible CPUs here. 75624cb1ef64STejun Heo */ 75634cb1ef64STejun Heo for_each_possible_cpu(cpu) 75644cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) 75654cb1ef64STejun Heo BUG_ON(!create_worker(pool)); 75664cb1ef64STejun Heo 75673347fa09STejun Heo for_each_online_cpu(cpu) { 75683347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 75693347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 75703347fa09STejun Heo BUG_ON(!create_worker(pool)); 75713347fa09STejun Heo } 75723347fa09STejun Heo } 75733347fa09STejun Heo 75743347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 75753347fa09STejun Heo BUG_ON(!create_worker(pool)); 75763347fa09STejun Heo 75773347fa09STejun Heo wq_online = true; 757882607adcSTejun Heo wq_watchdog_init(); 75791da177e4SLinus Torvalds } 7580c4f135d6STetsuo Handa 7581025e1684STejun Heo /* 7582025e1684STejun Heo * Initialize @pt by first initializing @pt->cpu_pod[] with pod IDs according to 7583025e1684STejun Heo * @cpu_shares_pod(). Each subset of CPUs that share a pod is assigned a unique 7584025e1684STejun Heo * and consecutive pod ID. The rest of @pt is initialized accordingly. 7585025e1684STejun Heo */ 7586025e1684STejun Heo static void __init init_pod_type(struct wq_pod_type *pt, 7587025e1684STejun Heo bool (*cpus_share_pod)(int, int)) 7588025e1684STejun Heo { 7589025e1684STejun Heo int cur, pre, cpu, pod; 7590025e1684STejun Heo 7591025e1684STejun Heo pt->nr_pods = 0; 7592025e1684STejun Heo 7593025e1684STejun Heo /* init @pt->cpu_pod[] according to @cpus_share_pod() */ 7594025e1684STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 7595025e1684STejun Heo BUG_ON(!pt->cpu_pod); 7596025e1684STejun Heo 7597025e1684STejun Heo for_each_possible_cpu(cur) { 7598025e1684STejun Heo for_each_possible_cpu(pre) { 7599025e1684STejun Heo if (pre >= cur) { 7600025e1684STejun Heo pt->cpu_pod[cur] = pt->nr_pods++; 7601025e1684STejun Heo break; 7602025e1684STejun Heo } 7603025e1684STejun Heo if (cpus_share_pod(cur, pre)) { 7604025e1684STejun Heo pt->cpu_pod[cur] = pt->cpu_pod[pre]; 7605025e1684STejun Heo break; 7606025e1684STejun Heo } 7607025e1684STejun Heo } 7608025e1684STejun Heo } 7609025e1684STejun Heo 7610025e1684STejun Heo /* init the rest to match @pt->cpu_pod[] */ 7611025e1684STejun Heo pt->pod_cpus = kcalloc(pt->nr_pods, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 7612025e1684STejun Heo pt->pod_node = kcalloc(pt->nr_pods, sizeof(pt->pod_node[0]), GFP_KERNEL); 7613025e1684STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node); 7614025e1684STejun Heo 7615025e1684STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) 7616025e1684STejun Heo BUG_ON(!zalloc_cpumask_var(&pt->pod_cpus[pod], GFP_KERNEL)); 7617025e1684STejun Heo 7618025e1684STejun Heo for_each_possible_cpu(cpu) { 7619025e1684STejun Heo cpumask_set_cpu(cpu, pt->pod_cpus[pt->cpu_pod[cpu]]); 7620025e1684STejun Heo pt->pod_node[pt->cpu_pod[cpu]] = cpu_to_node(cpu); 7621025e1684STejun Heo } 7622025e1684STejun Heo } 7623025e1684STejun Heo 762463c5484eSTejun Heo static bool __init cpus_dont_share(int cpu0, int cpu1) 762563c5484eSTejun Heo { 762663c5484eSTejun Heo return false; 762763c5484eSTejun Heo } 762863c5484eSTejun Heo 762963c5484eSTejun Heo static bool __init cpus_share_smt(int cpu0, int cpu1) 763063c5484eSTejun Heo { 763163c5484eSTejun Heo #ifdef CONFIG_SCHED_SMT 763263c5484eSTejun Heo return cpumask_test_cpu(cpu0, cpu_smt_mask(cpu1)); 763363c5484eSTejun Heo #else 763463c5484eSTejun Heo return false; 763563c5484eSTejun Heo #endif 763663c5484eSTejun Heo } 763763c5484eSTejun Heo 7638025e1684STejun Heo static bool __init cpus_share_numa(int cpu0, int cpu1) 7639025e1684STejun Heo { 7640025e1684STejun Heo return cpu_to_node(cpu0) == cpu_to_node(cpu1); 7641025e1684STejun Heo } 7642025e1684STejun Heo 76432930155bSTejun Heo /** 76442930155bSTejun Heo * workqueue_init_topology - initialize CPU pods for unbound workqueues 76452930155bSTejun Heo * 764696068b60SWang Jinchao * This is the third step of three-staged workqueue subsystem initialization and 76472930155bSTejun Heo * invoked after SMP and topology information are fully initialized. It 76482930155bSTejun Heo * initializes the unbound CPU pods accordingly. 76492930155bSTejun Heo */ 76502930155bSTejun Heo void __init workqueue_init_topology(void) 7651a86feae6STejun Heo { 76522930155bSTejun Heo struct workqueue_struct *wq; 7653025e1684STejun Heo int cpu; 7654a86feae6STejun Heo 765563c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CPU], cpus_dont_share); 765663c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_SMT], cpus_share_smt); 765763c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CACHE], cpus_share_cache); 7658025e1684STejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_NUMA], cpus_share_numa); 7659a86feae6STejun Heo 7660c5f8cd6cSTejun Heo wq_topo_initialized = true; 7661c5f8cd6cSTejun Heo 76622930155bSTejun Heo mutex_lock(&wq_pool_mutex); 7663a86feae6STejun Heo 7664a86feae6STejun Heo /* 76652930155bSTejun Heo * Workqueues allocated earlier would have all CPUs sharing the default 76662930155bSTejun Heo * worker pool. Explicitly call wq_update_pod() on all workqueue and CPU 76672930155bSTejun Heo * combinations to apply per-pod sharing. 76682930155bSTejun Heo */ 76692930155bSTejun Heo list_for_each_entry(wq, &workqueues, list) { 76705797b1c1STejun Heo for_each_online_cpu(cpu) 76712930155bSTejun Heo wq_update_pod(wq, cpu, cpu, true); 76725797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) { 76735797b1c1STejun Heo mutex_lock(&wq->mutex); 76745797b1c1STejun Heo wq_update_node_max_active(wq, -1); 76755797b1c1STejun Heo mutex_unlock(&wq->mutex); 76762930155bSTejun Heo } 76772930155bSTejun Heo } 76782930155bSTejun Heo 76792930155bSTejun Heo mutex_unlock(&wq_pool_mutex); 7680a86feae6STejun Heo } 7681a86feae6STejun Heo 768220bdedafSTetsuo Handa void __warn_flushing_systemwide_wq(void) 768320bdedafSTetsuo Handa { 768420bdedafSTetsuo Handa pr_warn("WARNING: Flushing system-wide workqueues will be prohibited in near future.\n"); 768520bdedafSTetsuo Handa dump_stack(); 768620bdedafSTetsuo Handa } 7687c4f135d6STetsuo Handa EXPORT_SYMBOL(__warn_flushing_systemwide_wq); 7688ace3c549Stiozhang 7689ace3c549Stiozhang static int __init workqueue_unbound_cpus_setup(char *str) 7690ace3c549Stiozhang { 7691ace3c549Stiozhang if (cpulist_parse(str, &wq_cmdline_cpumask) < 0) { 7692ace3c549Stiozhang cpumask_clear(&wq_cmdline_cpumask); 7693ace3c549Stiozhang pr_warn("workqueue.unbound_cpus: incorrect CPU range, using default\n"); 7694ace3c549Stiozhang } 7695ace3c549Stiozhang 7696ace3c549Stiozhang return 1; 7697ace3c549Stiozhang } 7698ace3c549Stiozhang __setup("workqueue.unbound_cpus=", workqueue_unbound_cpus_setup); 7699