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> 321da177e4SLinus Torvalds #include <linux/signal.h> 331da177e4SLinus Torvalds #include <linux/completion.h> 341da177e4SLinus Torvalds #include <linux/workqueue.h> 351da177e4SLinus Torvalds #include <linux/slab.h> 361da177e4SLinus Torvalds #include <linux/cpu.h> 371da177e4SLinus Torvalds #include <linux/notifier.h> 381da177e4SLinus Torvalds #include <linux/kthread.h> 391fa44ecaSJames Bottomley #include <linux/hardirq.h> 4046934023SChristoph Lameter #include <linux/mempolicy.h> 41341a5958SRafael J. Wysocki #include <linux/freezer.h> 42d5abe669SPeter Zijlstra #include <linux/debug_locks.h> 434e6045f1SJohannes Berg #include <linux/lockdep.h> 44c34056a3STejun Heo #include <linux/idr.h> 4529c91e99STejun Heo #include <linux/jhash.h> 4642f8570fSSasha Levin #include <linux/hashtable.h> 4776af4d93STejun Heo #include <linux/rculist.h> 48bce90380STejun Heo #include <linux/nodemask.h> 494c16bd32STejun Heo #include <linux/moduleparam.h> 503d1cb205STejun Heo #include <linux/uaccess.h> 51c98a9805STal Shorer #include <linux/sched/isolation.h> 52cd2440d6SPetr Mladek #include <linux/sched/debug.h> 5362635ea8SSergey Senozhatsky #include <linux/nmi.h> 54940d71c6SSergey Senozhatsky #include <linux/kvm_para.h> 55e22bee78STejun Heo 56ea138446STejun Heo #include "workqueue_internal.h" 571da177e4SLinus Torvalds 58c8e55f36STejun Heo enum { 59bc2ae0f5STejun Heo /* 6024647570STejun Heo * worker_pool flags 61bc2ae0f5STejun Heo * 6224647570STejun Heo * A bound pool is either associated or disassociated with its CPU. 63bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 64bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 65bc2ae0f5STejun Heo * is in effect. 66bc2ae0f5STejun Heo * 67bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 68bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 6924647570STejun Heo * be executing on any CPU. The pool behaves as an unbound one. 70bc2ae0f5STejun Heo * 71bc3a1afcSTejun Heo * Note that DISASSOCIATED should be flipped only while holding 721258fae7STejun Heo * wq_pool_attach_mutex to avoid changing binding state while 734736cbf7SLai Jiangshan * worker_attach_to_pool() is in progress. 74bc2ae0f5STejun Heo */ 75692b4825STejun Heo POOL_MANAGER_ACTIVE = 1 << 0, /* being managed */ 7624647570STejun Heo POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ 77db7bccf4STejun Heo 78c8e55f36STejun Heo /* worker flags */ 79c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 80c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 81e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 82fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 83f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 84a9ab775bSTejun Heo WORKER_REBOUND = 1 << 8, /* worker was rebound */ 85e22bee78STejun Heo 86a9ab775bSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE | 87a9ab775bSTejun Heo WORKER_UNBOUND | WORKER_REBOUND, 88db7bccf4STejun Heo 89e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 904ce62e9eSTejun Heo 9129c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ 92c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 93db7bccf4STejun Heo 94e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 95e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 96e22bee78STejun Heo 973233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 983233cdbdSTejun Heo /* call for help after 10ms 993233cdbdSTejun Heo (min two ticks) */ 100e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 101e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds /* 104e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 1058698a745SDongsheng Yang * all cpus. Give MIN_NICE. 106e22bee78STejun Heo */ 1078698a745SDongsheng Yang RESCUER_NICE_LEVEL = MIN_NICE, 1088698a745SDongsheng Yang HIGHPRI_NICE_LEVEL = MIN_NICE, 109ecf6881fSTejun Heo 110ecf6881fSTejun Heo WQ_NAME_LEN = 24, 111c8e55f36STejun Heo }; 112c8e55f36STejun Heo 1131da177e4SLinus Torvalds /* 1144690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1154690c4abSTejun Heo * 116e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 117e41e704bSTejun Heo * everyone else. 1184690c4abSTejun Heo * 119e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 120e22bee78STejun Heo * only be modified and accessed from the local cpu. 121e22bee78STejun Heo * 122d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1234690c4abSTejun Heo * 124d565ed63STejun Heo * X: During normal operation, modification requires pool->lock and should 125d565ed63STejun Heo * be done only from local cpu. Either disabling preemption on local 126d565ed63STejun Heo * cpu or grabbing pool->lock is enough for read access. If 127d565ed63STejun Heo * POOL_DISASSOCIATED is set, it's identical to L. 128e22bee78STejun Heo * 129*bdf8b9bfSTejun Heo * K: Only modified by worker while holding pool->lock. Can be safely read by 130*bdf8b9bfSTejun Heo * self, while holding pool->lock or from IRQ context if %current is the 131*bdf8b9bfSTejun Heo * kworker. 132*bdf8b9bfSTejun Heo * 133*bdf8b9bfSTejun Heo * S: Only modified by worker self. 134*bdf8b9bfSTejun Heo * 1351258fae7STejun Heo * A: wq_pool_attach_mutex protected. 136822d8405STejun Heo * 13768e13a67SLai Jiangshan * PL: wq_pool_mutex protected. 13876af4d93STejun Heo * 13924acfb71SThomas Gleixner * PR: wq_pool_mutex protected for writes. RCU protected for reads. 1405bcab335STejun Heo * 1415b95e1afSLai Jiangshan * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads. 1425b95e1afSLai Jiangshan * 1435b95e1afSLai Jiangshan * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or 14424acfb71SThomas Gleixner * RCU for reads. 1455b95e1afSLai Jiangshan * 1463c25a55dSLai Jiangshan * WQ: wq->mutex protected. 1473c25a55dSLai Jiangshan * 14824acfb71SThomas Gleixner * WR: wq->mutex protected for writes. RCU protected for reads. 1492e109a28STejun Heo * 1502e109a28STejun Heo * MD: wq_mayday_lock protected. 151cd2440d6SPetr Mladek * 152cd2440d6SPetr Mladek * WD: Used internally by the watchdog. 1534690c4abSTejun Heo */ 1544690c4abSTejun Heo 1552eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 156c34056a3STejun Heo 157bd7bdd43STejun Heo struct worker_pool { 158a9b8a985SSebastian Andrzej Siewior raw_spinlock_t lock; /* the pool lock */ 159d84ff051STejun Heo int cpu; /* I: the associated cpu */ 160f3f90ad4STejun Heo int node; /* I: the associated node ID */ 1619daf9e67STejun Heo int id; /* I: pool ID */ 16211ebea50STejun Heo unsigned int flags; /* X: flags */ 163bd7bdd43STejun Heo 16482607adcSTejun Heo unsigned long watchdog_ts; /* L: watchdog timestamp */ 165cd2440d6SPetr Mladek bool cpu_stall; /* WD: stalled cpu bound pool */ 16682607adcSTejun Heo 167bc35f7efSLai Jiangshan /* 168bc35f7efSLai Jiangshan * The counter is incremented in a process context on the associated CPU 169bc35f7efSLai Jiangshan * w/ preemption disabled, and decremented or reset in the same context 170bc35f7efSLai Jiangshan * but w/ pool->lock held. The readers grab pool->lock and are 171bc35f7efSLai Jiangshan * guaranteed to see if the counter reached zero. 172bc35f7efSLai Jiangshan */ 173bc35f7efSLai Jiangshan int nr_running; 17484f91c62SLai Jiangshan 175bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 176ea1abd61SLai Jiangshan 1775826cc8fSLai Jiangshan int nr_workers; /* L: total number of workers */ 1785826cc8fSLai Jiangshan int nr_idle; /* L: currently idle workers */ 179bd7bdd43STejun Heo 1802c1f1a91SLai Jiangshan struct list_head idle_list; /* L: list of idle workers */ 181bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 1823f959aa3SValentin Schneider struct work_struct idle_cull_work; /* L: worker idle cleanup */ 1833f959aa3SValentin Schneider 184bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 185bd7bdd43STejun Heo 186c5aa87bbSTejun Heo /* a workers is either on busy_hash or idle_list, or the manager */ 187c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 188c9e7cf27STejun Heo /* L: hash of busy workers */ 189c9e7cf27STejun Heo 1902607d7a6STejun Heo struct worker *manager; /* L: purely informational */ 19192f9c5c4SLai Jiangshan struct list_head workers; /* A: attached workers */ 192e02b9312SValentin Schneider struct list_head dying_workers; /* A: workers about to die */ 19360f5a4bcSLai Jiangshan struct completion *detach_completion; /* all workers detached */ 194e19e397aSTejun Heo 1957cda9aaeSLai Jiangshan struct ida worker_ida; /* worker IDs for task name */ 196e19e397aSTejun Heo 1977a4e344cSTejun Heo struct workqueue_attrs *attrs; /* I: worker attributes */ 19868e13a67SLai Jiangshan struct hlist_node hash_node; /* PL: unbound_pool_hash node */ 19968e13a67SLai Jiangshan int refcnt; /* PL: refcnt for unbound pools */ 2007a4e344cSTejun Heo 201e19e397aSTejun Heo /* 20224acfb71SThomas Gleixner * Destruction of pool is RCU protected to allow dereferences 20329c91e99STejun Heo * from get_work_pool(). 20429c91e99STejun Heo */ 20529c91e99STejun Heo struct rcu_head rcu; 20684f91c62SLai Jiangshan }; 2078b03ae3cSTejun Heo 2088b03ae3cSTejun Heo /* 209725e8ec5STejun Heo * Per-pool_workqueue statistics. These can be monitored using 210725e8ec5STejun Heo * tools/workqueue/wq_monitor.py. 211725e8ec5STejun Heo */ 212725e8ec5STejun Heo enum pool_workqueue_stats { 213725e8ec5STejun Heo PWQ_STAT_STARTED, /* work items started execution */ 214725e8ec5STejun Heo PWQ_STAT_COMPLETED, /* work items completed execution */ 215725e8ec5STejun Heo PWQ_STAT_CM_WAKEUP, /* concurrency-management worker wakeups */ 216725e8ec5STejun Heo PWQ_STAT_MAYDAY, /* maydays to rescuer */ 217725e8ec5STejun Heo PWQ_STAT_RESCUED, /* linked work items executed by rescuer */ 218725e8ec5STejun Heo 219725e8ec5STejun Heo PWQ_NR_STATS, 220725e8ec5STejun Heo }; 221725e8ec5STejun Heo 222725e8ec5STejun Heo /* 223112202d9STejun Heo * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS 224112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits 225112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the 226112202d9STejun Heo * number of flag bits. 2271da177e4SLinus Torvalds */ 228112202d9STejun Heo struct pool_workqueue { 229bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 2304690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 23173f53c4aSTejun Heo int work_color; /* L: current color */ 23273f53c4aSTejun Heo int flush_color; /* L: flushing color */ 2338864b4e5STejun Heo int refcnt; /* L: reference count */ 23473f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 23573f53c4aSTejun Heo /* L: nr of in_flight works */ 236018f3a13SLai Jiangshan 237018f3a13SLai Jiangshan /* 238018f3a13SLai Jiangshan * nr_active management and WORK_STRUCT_INACTIVE: 239018f3a13SLai Jiangshan * 240018f3a13SLai Jiangshan * When pwq->nr_active >= max_active, new work item is queued to 241018f3a13SLai Jiangshan * pwq->inactive_works instead of pool->worklist and marked with 242018f3a13SLai Jiangshan * WORK_STRUCT_INACTIVE. 243018f3a13SLai Jiangshan * 244018f3a13SLai Jiangshan * All work items marked with WORK_STRUCT_INACTIVE do not participate 245018f3a13SLai Jiangshan * in pwq->nr_active and all work items in pwq->inactive_works are 246018f3a13SLai Jiangshan * marked with WORK_STRUCT_INACTIVE. But not all WORK_STRUCT_INACTIVE 247018f3a13SLai Jiangshan * work items are in pwq->inactive_works. Some of them are ready to 248018f3a13SLai Jiangshan * run in pool->worklist or worker->scheduled. Those work itmes are 249018f3a13SLai Jiangshan * only struct wq_barrier which is used for flush_work() and should 250018f3a13SLai Jiangshan * not participate in pwq->nr_active. For non-barrier work item, it 251018f3a13SLai Jiangshan * is marked with WORK_STRUCT_INACTIVE iff it is in pwq->inactive_works. 252018f3a13SLai Jiangshan */ 2531e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 254a0a1a5fdSTejun Heo int max_active; /* L: max active works */ 255f97a4a1aSLai Jiangshan struct list_head inactive_works; /* L: inactive works */ 2563c25a55dSLai Jiangshan struct list_head pwqs_node; /* WR: node on wq->pwqs */ 2572e109a28STejun Heo struct list_head mayday_node; /* MD: node on wq->maydays */ 2588864b4e5STejun Heo 259725e8ec5STejun Heo u64 stats[PWQ_NR_STATS]; 260725e8ec5STejun Heo 2618864b4e5STejun Heo /* 2628864b4e5STejun Heo * Release of unbound pwq is punted to system_wq. See put_pwq() 2638864b4e5STejun Heo * and pwq_unbound_release_workfn() for details. pool_workqueue 26424acfb71SThomas Gleixner * itself is also RCU protected so that the first pwq can be 265b09f4fd3SLai Jiangshan * determined without grabbing wq->mutex. 2668864b4e5STejun Heo */ 2678864b4e5STejun Heo struct work_struct unbound_release_work; 2688864b4e5STejun Heo struct rcu_head rcu; 269e904e6c2STejun Heo } __aligned(1 << WORK_STRUCT_FLAG_BITS); 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds /* 27273f53c4aSTejun Heo * Structure used to wait for workqueue flush. 27373f53c4aSTejun Heo */ 27473f53c4aSTejun Heo struct wq_flusher { 2753c25a55dSLai Jiangshan struct list_head list; /* WQ: list of flushers */ 2763c25a55dSLai Jiangshan int flush_color; /* WQ: flush color waiting for */ 27773f53c4aSTejun Heo struct completion done; /* flush completion */ 27873f53c4aSTejun Heo }; 2791da177e4SLinus Torvalds 280226223abSTejun Heo struct wq_device; 281226223abSTejun Heo 28273f53c4aSTejun Heo /* 283c5aa87bbSTejun Heo * The externally visible workqueue. It relays the issued work items to 284c5aa87bbSTejun Heo * the appropriate worker_pool through its pool_workqueues. 2851da177e4SLinus Torvalds */ 2861da177e4SLinus Torvalds struct workqueue_struct { 2873c25a55dSLai Jiangshan struct list_head pwqs; /* WR: all pwqs of this wq */ 288e2dca7adSTejun Heo struct list_head list; /* PR: list of all workqueues */ 28973f53c4aSTejun Heo 2903c25a55dSLai Jiangshan struct mutex mutex; /* protects this wq */ 2913c25a55dSLai Jiangshan int work_color; /* WQ: current work color */ 2923c25a55dSLai Jiangshan int flush_color; /* WQ: current flush color */ 293112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 2943c25a55dSLai Jiangshan struct wq_flusher *first_flusher; /* WQ: first flusher */ 2953c25a55dSLai Jiangshan struct list_head flusher_queue; /* WQ: flush waiters */ 2963c25a55dSLai Jiangshan struct list_head flusher_overflow; /* WQ: flush overflow list */ 29773f53c4aSTejun Heo 2982e109a28STejun Heo struct list_head maydays; /* MD: pwqs requesting rescue */ 29930ae2fc0STejun Heo struct worker *rescuer; /* MD: rescue worker */ 300e22bee78STejun Heo 30187fc741eSLai Jiangshan int nr_drainers; /* WQ: drain in progress */ 302a357fc03SLai Jiangshan int saved_max_active; /* WQ: saved pwq max_active */ 303226223abSTejun Heo 3045b95e1afSLai Jiangshan struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */ 3055b95e1afSLai Jiangshan struct pool_workqueue *dfl_pwq; /* PW: only for unbound wqs */ 3066029a918STejun Heo 307226223abSTejun Heo #ifdef CONFIG_SYSFS 308226223abSTejun Heo struct wq_device *wq_dev; /* I: for sysfs interface */ 309226223abSTejun Heo #endif 3104e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 311669de8bdSBart Van Assche char *lock_name; 312669de8bdSBart Van Assche struct lock_class_key key; 3134e6045f1SJohannes Berg struct lockdep_map lockdep_map; 3144e6045f1SJohannes Berg #endif 315ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */ 3162728fd2fSTejun Heo 317e2dca7adSTejun Heo /* 31824acfb71SThomas Gleixner * Destruction of workqueue_struct is RCU protected to allow walking 31924acfb71SThomas Gleixner * the workqueues list without grabbing wq_pool_mutex. 320e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq. 321e2dca7adSTejun Heo */ 322e2dca7adSTejun Heo struct rcu_head rcu; 323e2dca7adSTejun Heo 3242728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */ 3252728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ 3262728fd2fSTejun Heo struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */ 3275b95e1afSLai Jiangshan struct pool_workqueue __rcu *numa_pwq_tbl[]; /* PWR: unbound pwqs indexed by node */ 3281da177e4SLinus Torvalds }; 3291da177e4SLinus Torvalds 330e904e6c2STejun Heo static struct kmem_cache *pwq_cache; 331e904e6c2STejun Heo 332bce90380STejun Heo static cpumask_var_t *wq_numa_possible_cpumask; 333bce90380STejun Heo /* possible CPUs of each node */ 334bce90380STejun Heo 335d55262c4STejun Heo static bool wq_disable_numa; 336d55262c4STejun Heo module_param_named(disable_numa, wq_disable_numa, bool, 0444); 337d55262c4STejun Heo 338cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 339552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 340cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 341cee22a15SViresh Kumar 342863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */ 3433347fa09STejun Heo 344bce90380STejun Heo static bool wq_numa_enabled; /* unbound NUMA affinity enabled */ 345bce90380STejun Heo 3464c16bd32STejun Heo /* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */ 3474c16bd32STejun Heo static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf; 3484c16bd32STejun Heo 34968e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 3501258fae7STejun Heo static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ 351a9b8a985SSebastian Andrzej Siewior static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 352d8bb65abSSebastian Andrzej Siewior /* wait for manager to go away */ 353d8bb65abSSebastian Andrzej Siewior static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait); 3545bcab335STejun Heo 355e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 35668e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 3577d19c5ceSTejun Heo 35899c621efSLai Jiangshan /* PL&A: allowable cpus for unbound wqs and work items */ 359ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 360ef557180SMike Galbraith 361ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 362ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 363b05a7928SFrederic Weisbecker 364f303fccbSTejun Heo /* 365f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 366f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 367f303fccbSTejun Heo * to uncover usages which depend on it. 368f303fccbSTejun Heo */ 369f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 370f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 371f303fccbSTejun Heo #else 372f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 373f303fccbSTejun Heo #endif 374f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 375f303fccbSTejun Heo 3767d19c5ceSTejun Heo /* the per-cpu worker pools */ 37725528213SPeter Zijlstra static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools); 3787d19c5ceSTejun Heo 37968e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 3807d19c5ceSTejun Heo 38168e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 38229c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 38329c91e99STejun Heo 384c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 38529c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 38629c91e99STejun Heo 3878a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 3888a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 3898a2b7538STejun Heo 390d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly; 391ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 392044c782cSValentin Ilie struct workqueue_struct *system_highpri_wq __read_mostly; 3931aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 394044c782cSValentin Ilie struct workqueue_struct *system_long_wq __read_mostly; 395d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 396044c782cSValentin Ilie struct workqueue_struct *system_unbound_wq __read_mostly; 397f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 398044c782cSValentin Ilie struct workqueue_struct *system_freezable_wq __read_mostly; 39924d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 4000668106cSViresh Kumar struct workqueue_struct *system_power_efficient_wq __read_mostly; 4010668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 4020668106cSViresh Kumar struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly; 4030668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 404d320c038STejun Heo 4057d19c5ceSTejun Heo static int worker_thread(void *__worker); 4066ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 407c29eb853STejun Heo static void show_pwq(struct pool_workqueue *pwq); 40855df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool); 4097d19c5ceSTejun Heo 41097bd2347STejun Heo #define CREATE_TRACE_POINTS 41197bd2347STejun Heo #include <trace/events/workqueue.h> 41297bd2347STejun Heo 41368e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 41424acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 415f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 41624acfb71SThomas Gleixner "RCU or wq_pool_mutex should be held") 4175bcab335STejun Heo 4185b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 41924acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 420f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 421f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 42224acfb71SThomas Gleixner "RCU, wq->mutex or wq_pool_mutex should be held") 4235b95e1afSLai Jiangshan 424f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 425f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 426f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 4277a62c2c8STejun Heo (pool)++) 4284ce62e9eSTejun Heo 42949e3cf44STejun Heo /** 43017116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 43117116969STejun Heo * @pool: iteration cursor 432611c92a0STejun Heo * @pi: integer used for iteration 433fa1b54e6STejun Heo * 43424acfb71SThomas Gleixner * This must be called either with wq_pool_mutex held or RCU read 43568e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 43668e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 437fa1b54e6STejun Heo * 438fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 439fa1b54e6STejun Heo * ignored. 44017116969STejun Heo */ 441611c92a0STejun Heo #define for_each_pool(pool, pi) \ 442611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 44368e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 444fa1b54e6STejun Heo else 44517116969STejun Heo 44617116969STejun Heo /** 447822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 448822d8405STejun Heo * @worker: iteration cursor 449822d8405STejun Heo * @pool: worker_pool to iterate workers of 450822d8405STejun Heo * 4511258fae7STejun Heo * This must be called with wq_pool_attach_mutex. 452822d8405STejun Heo * 453822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 454822d8405STejun Heo * ignored. 455822d8405STejun Heo */ 456da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 457da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 4581258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 459822d8405STejun Heo else 460822d8405STejun Heo 461822d8405STejun Heo /** 46249e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 46349e3cf44STejun Heo * @pwq: iteration cursor 46449e3cf44STejun Heo * @wq: the target workqueue 46576af4d93STejun Heo * 46624acfb71SThomas Gleixner * This must be called either with wq->mutex held or RCU read locked. 467794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 468794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 46976af4d93STejun Heo * 47076af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 47176af4d93STejun Heo * ignored. 47249e3cf44STejun Heo */ 47349e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 47449e9d1a9SSebastian Andrzej Siewior list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ 4755a644662SJoel Fernandes (Google) lockdep_is_held(&(wq->mutex))) 476f3421797STejun Heo 477dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 478dc186ad7SThomas Gleixner 479f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr; 480dc186ad7SThomas Gleixner 48199777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 48299777288SStanislaw Gruszka { 48399777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 48499777288SStanislaw Gruszka } 48599777288SStanislaw Gruszka 486b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 487b9fdac7fSDu, Changbin { 488b9fdac7fSDu, Changbin struct work_struct *work = addr; 489b9fdac7fSDu, Changbin 490b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 491b9fdac7fSDu, Changbin } 492b9fdac7fSDu, Changbin 493dc186ad7SThomas Gleixner /* 494dc186ad7SThomas Gleixner * fixup_init is called when: 495dc186ad7SThomas Gleixner * - an active object is initialized 496dc186ad7SThomas Gleixner */ 49702a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 498dc186ad7SThomas Gleixner { 499dc186ad7SThomas Gleixner struct work_struct *work = addr; 500dc186ad7SThomas Gleixner 501dc186ad7SThomas Gleixner switch (state) { 502dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 503dc186ad7SThomas Gleixner cancel_work_sync(work); 504dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 50502a982a6SDu, Changbin return true; 506dc186ad7SThomas Gleixner default: 50702a982a6SDu, Changbin return false; 508dc186ad7SThomas Gleixner } 509dc186ad7SThomas Gleixner } 510dc186ad7SThomas Gleixner 511dc186ad7SThomas Gleixner /* 512dc186ad7SThomas Gleixner * fixup_free is called when: 513dc186ad7SThomas Gleixner * - an active object is freed 514dc186ad7SThomas Gleixner */ 51502a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 516dc186ad7SThomas Gleixner { 517dc186ad7SThomas Gleixner struct work_struct *work = addr; 518dc186ad7SThomas Gleixner 519dc186ad7SThomas Gleixner switch (state) { 520dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 521dc186ad7SThomas Gleixner cancel_work_sync(work); 522dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 52302a982a6SDu, Changbin return true; 524dc186ad7SThomas Gleixner default: 52502a982a6SDu, Changbin return false; 526dc186ad7SThomas Gleixner } 527dc186ad7SThomas Gleixner } 528dc186ad7SThomas Gleixner 529f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr = { 530dc186ad7SThomas Gleixner .name = "work_struct", 53199777288SStanislaw Gruszka .debug_hint = work_debug_hint, 532b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 533dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 534dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 535dc186ad7SThomas Gleixner }; 536dc186ad7SThomas Gleixner 537dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 538dc186ad7SThomas Gleixner { 539dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 540dc186ad7SThomas Gleixner } 541dc186ad7SThomas Gleixner 542dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 543dc186ad7SThomas Gleixner { 544dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 545dc186ad7SThomas Gleixner } 546dc186ad7SThomas Gleixner 547dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 548dc186ad7SThomas Gleixner { 549dc186ad7SThomas Gleixner if (onstack) 550dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 551dc186ad7SThomas Gleixner else 552dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 553dc186ad7SThomas Gleixner } 554dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 555dc186ad7SThomas Gleixner 556dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 557dc186ad7SThomas Gleixner { 558dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 559dc186ad7SThomas Gleixner } 560dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 561dc186ad7SThomas Gleixner 562ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 563ea2e64f2SThomas Gleixner { 564ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 565ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 566ea2e64f2SThomas Gleixner } 567ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 568ea2e64f2SThomas Gleixner 569dc186ad7SThomas Gleixner #else 570dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 571dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 572dc186ad7SThomas Gleixner #endif 573dc186ad7SThomas Gleixner 5744e8b22bdSLi Bin /** 57567dc8325SCai Huoqing * worker_pool_assign_id - allocate ID and assign it to @pool 5764e8b22bdSLi Bin * @pool: the pool pointer of interest 5774e8b22bdSLi Bin * 5784e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 5794e8b22bdSLi Bin * successfully, -errno on failure. 5804e8b22bdSLi Bin */ 5819daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 5829daf9e67STejun Heo { 5839daf9e67STejun Heo int ret; 5849daf9e67STejun Heo 58568e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5865bcab335STejun Heo 5874e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 5884e8b22bdSLi Bin GFP_KERNEL); 589229641a6STejun Heo if (ret >= 0) { 590e68035fbSTejun Heo pool->id = ret; 591229641a6STejun Heo return 0; 592229641a6STejun Heo } 5939daf9e67STejun Heo return ret; 5949daf9e67STejun Heo } 5959daf9e67STejun Heo 59676af4d93STejun Heo /** 597df2d5ae4STejun Heo * unbound_pwq_by_node - return the unbound pool_workqueue for the given node 598df2d5ae4STejun Heo * @wq: the target workqueue 599df2d5ae4STejun Heo * @node: the node ID 600df2d5ae4STejun Heo * 60124acfb71SThomas Gleixner * This must be called with any of wq_pool_mutex, wq->mutex or RCU 6025b95e1afSLai Jiangshan * read locked. 603df2d5ae4STejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 604df2d5ae4STejun Heo * responsible for guaranteeing that the pwq stays online. 605d185af30SYacine Belkadi * 606d185af30SYacine Belkadi * Return: The unbound pool_workqueue for @node. 607df2d5ae4STejun Heo */ 608df2d5ae4STejun Heo static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq, 609df2d5ae4STejun Heo int node) 610df2d5ae4STejun Heo { 6115b95e1afSLai Jiangshan assert_rcu_or_wq_mutex_or_pool_mutex(wq); 612d6e022f1STejun Heo 613d6e022f1STejun Heo /* 614d6e022f1STejun Heo * XXX: @node can be NUMA_NO_NODE if CPU goes offline while a 615d6e022f1STejun Heo * delayed item is pending. The plan is to keep CPU -> NODE 616d6e022f1STejun Heo * mapping valid and stable across CPU on/offlines. Once that 617d6e022f1STejun Heo * happens, this workaround can be removed. 618d6e022f1STejun Heo */ 619d6e022f1STejun Heo if (unlikely(node == NUMA_NO_NODE)) 620d6e022f1STejun Heo return wq->dfl_pwq; 621d6e022f1STejun Heo 622df2d5ae4STejun Heo return rcu_dereference_raw(wq->numa_pwq_tbl[node]); 623df2d5ae4STejun Heo } 624df2d5ae4STejun Heo 62573f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 62673f53c4aSTejun Heo { 62773f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 62873f53c4aSTejun Heo } 62973f53c4aSTejun Heo 630c4560c2cSLai Jiangshan static int get_work_color(unsigned long work_data) 63173f53c4aSTejun Heo { 632c4560c2cSLai Jiangshan return (work_data >> WORK_STRUCT_COLOR_SHIFT) & 63373f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 63473f53c4aSTejun Heo } 63573f53c4aSTejun Heo 63673f53c4aSTejun Heo static int work_next_color(int color) 63773f53c4aSTejun Heo { 63873f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 639a848e3b6SOleg Nesterov } 640a848e3b6SOleg Nesterov 6414594bf15SDavid Howells /* 642112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 643112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 6447c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 6457a22ad75STejun Heo * 646112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 647112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 648bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 649bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 6507a22ad75STejun Heo * 651112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 6527c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 653112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 6547c3eed5cSTejun Heo * available only while the work item is queued. 655bbb68dfaSTejun Heo * 656bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 657bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 658bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 659bbb68dfaSTejun Heo * try to steal the PENDING bit. 6604594bf15SDavid Howells */ 6617a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 6627a22ad75STejun Heo unsigned long flags) 6637a22ad75STejun Heo { 6646183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 6657a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 6667a22ad75STejun Heo } 6677a22ad75STejun Heo 668112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 6694690c4abSTejun Heo unsigned long extra_flags) 670365970a1SDavid Howells { 671112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 672112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 673365970a1SDavid Howells } 674365970a1SDavid Howells 6754468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 6764468a00fSLai Jiangshan int pool_id) 6774468a00fSLai Jiangshan { 6784468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 6794468a00fSLai Jiangshan WORK_STRUCT_PENDING); 6804468a00fSLai Jiangshan } 6814468a00fSLai Jiangshan 6827c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 6837c3eed5cSTejun Heo int pool_id) 6844d707b9fSOleg Nesterov { 68523657bb1STejun Heo /* 68623657bb1STejun Heo * The following wmb is paired with the implied mb in 68723657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 68823657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 68923657bb1STejun Heo * owner. 69023657bb1STejun Heo */ 69123657bb1STejun Heo smp_wmb(); 6927c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 693346c09f8SRoman Pen /* 694346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 695346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 696346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 6978bdc6201SLiu Song * reordering can lead to a missed execution on attempt to queue 698346c09f8SRoman Pen * the same @work. E.g. consider this case: 699346c09f8SRoman Pen * 700346c09f8SRoman Pen * CPU#0 CPU#1 701346c09f8SRoman Pen * ---------------------------- -------------------------------- 702346c09f8SRoman Pen * 703346c09f8SRoman Pen * 1 STORE event_indicated 704346c09f8SRoman Pen * 2 queue_work_on() { 705346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 706346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 707346c09f8SRoman Pen * 5 set_work_data() # clear bit 708346c09f8SRoman Pen * 6 smp_mb() 709346c09f8SRoman Pen * 7 work->current_func() { 710346c09f8SRoman Pen * 8 LOAD event_indicated 711346c09f8SRoman Pen * } 712346c09f8SRoman Pen * 713346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 714346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 715346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 716346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 717346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 718346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 719346c09f8SRoman Pen * before actual STORE. 720346c09f8SRoman Pen */ 721346c09f8SRoman Pen smp_mb(); 7224d707b9fSOleg Nesterov } 7234d707b9fSOleg Nesterov 7247a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 725365970a1SDavid Howells { 7267c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 7277c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 7287a22ad75STejun Heo } 7297a22ad75STejun Heo 730112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 7317a22ad75STejun Heo { 732e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 7337a22ad75STejun Heo 734112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 735e120153dSTejun Heo return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); 736e120153dSTejun Heo else 737e120153dSTejun Heo return NULL; 7387a22ad75STejun Heo } 7397a22ad75STejun Heo 7407c3eed5cSTejun Heo /** 7417c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 7427c3eed5cSTejun Heo * @work: the work item of interest 7437c3eed5cSTejun Heo * 74468e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 74524acfb71SThomas Gleixner * access under RCU read lock. As such, this function should be 74624acfb71SThomas Gleixner * called under wq_pool_mutex or inside of a rcu_read_lock() region. 747fa1b54e6STejun Heo * 748fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 749fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 750fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 751fa1b54e6STejun Heo * returned pool is and stays online. 752d185af30SYacine Belkadi * 753d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 7547c3eed5cSTejun Heo */ 7557c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 7567a22ad75STejun Heo { 757e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 7587c3eed5cSTejun Heo int pool_id; 7597a22ad75STejun Heo 76068e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 761fa1b54e6STejun Heo 762112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 763112202d9STejun Heo return ((struct pool_workqueue *) 7647c3eed5cSTejun Heo (data & WORK_STRUCT_WQ_DATA_MASK))->pool; 7657a22ad75STejun Heo 7667c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 7677c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 7687a22ad75STejun Heo return NULL; 7697a22ad75STejun Heo 770fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 7717c3eed5cSTejun Heo } 7727c3eed5cSTejun Heo 7737c3eed5cSTejun Heo /** 7747c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 7757c3eed5cSTejun Heo * @work: the work item of interest 7767c3eed5cSTejun Heo * 777d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 7787c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 7797c3eed5cSTejun Heo */ 7807c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 7817c3eed5cSTejun Heo { 78254d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 7837c3eed5cSTejun Heo 784112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 785112202d9STejun Heo return ((struct pool_workqueue *) 78654d5b7d0SLai Jiangshan (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id; 78754d5b7d0SLai Jiangshan 78854d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 7897c3eed5cSTejun Heo } 7907c3eed5cSTejun Heo 791bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 792bbb68dfaSTejun Heo { 7937c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 794bbb68dfaSTejun Heo 7957c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 7967c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 797bbb68dfaSTejun Heo } 798bbb68dfaSTejun Heo 799bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 800bbb68dfaSTejun Heo { 801bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 802bbb68dfaSTejun Heo 803112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 804bbb68dfaSTejun Heo } 805bbb68dfaSTejun Heo 806e22bee78STejun Heo /* 8073270476aSTejun Heo * Policy functions. These define the policies on how the global worker 8083270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 809d565ed63STejun Heo * they're being called with pool->lock held. 810e22bee78STejun Heo */ 811e22bee78STejun Heo 81263d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool) 813649027d7STejun Heo { 814bc35f7efSLai Jiangshan return !pool->nr_running; 815649027d7STejun Heo } 816649027d7STejun Heo 817e22bee78STejun Heo /* 818e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 819e22bee78STejun Heo * running workers. 820974271c4STejun Heo * 821974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 822706026c2STejun Heo * function will always return %true for unbound pools as long as the 823974271c4STejun Heo * worklist isn't empty. 824e22bee78STejun Heo */ 82563d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 826e22bee78STejun Heo { 82763d95a91STejun Heo return !list_empty(&pool->worklist) && __need_more_worker(pool); 828e22bee78STejun Heo } 829e22bee78STejun Heo 830e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 83163d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 832e22bee78STejun Heo { 83363d95a91STejun Heo return pool->nr_idle; 834e22bee78STejun Heo } 835e22bee78STejun Heo 836e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 83763d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 838e22bee78STejun Heo { 839bc35f7efSLai Jiangshan return !list_empty(&pool->worklist) && (pool->nr_running <= 1); 840e22bee78STejun Heo } 841e22bee78STejun Heo 842e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 84363d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 844e22bee78STejun Heo { 84563d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 846e22bee78STejun Heo } 847e22bee78STejun Heo 848e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 84963d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 850e22bee78STejun Heo { 851692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 85263d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 85363d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 854e22bee78STejun Heo 855e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 856e22bee78STejun Heo } 857e22bee78STejun Heo 858e22bee78STejun Heo /* 859e22bee78STejun Heo * Wake up functions. 860e22bee78STejun Heo */ 861e22bee78STejun Heo 8622c1f1a91SLai Jiangshan /* Return the first idle worker. Called with pool->lock held. */ 8631037de36SLai Jiangshan static struct worker *first_idle_worker(struct worker_pool *pool) 8647e11629dSTejun Heo { 86563d95a91STejun Heo if (unlikely(list_empty(&pool->idle_list))) 8667e11629dSTejun Heo return NULL; 8677e11629dSTejun Heo 86863d95a91STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 8697e11629dSTejun Heo } 8707e11629dSTejun Heo 8717e11629dSTejun Heo /** 8727e11629dSTejun Heo * wake_up_worker - wake up an idle worker 87363d95a91STejun Heo * @pool: worker pool to wake worker from 8747e11629dSTejun Heo * 87563d95a91STejun Heo * Wake up the first idle worker of @pool. 8767e11629dSTejun Heo * 8777e11629dSTejun Heo * CONTEXT: 878a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 8797e11629dSTejun Heo */ 88063d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool) 8817e11629dSTejun Heo { 8821037de36SLai Jiangshan struct worker *worker = first_idle_worker(pool); 8837e11629dSTejun Heo 8847e11629dSTejun Heo if (likely(worker)) 8857e11629dSTejun Heo wake_up_process(worker->task); 8867e11629dSTejun Heo } 8877e11629dSTejun Heo 8884690c4abSTejun Heo /** 889c54d5046STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 890c54d5046STejun Heo * @worker: self 891c54d5046STejun Heo * @flags: flags to set 892c54d5046STejun Heo * 893c54d5046STejun Heo * Set @flags in @worker->flags and adjust nr_running accordingly. 894c54d5046STejun Heo * 895c54d5046STejun Heo * CONTEXT: 896c54d5046STejun Heo * raw_spin_lock_irq(pool->lock) 897c54d5046STejun Heo */ 898c54d5046STejun Heo static inline void worker_set_flags(struct worker *worker, unsigned int flags) 899c54d5046STejun Heo { 900c54d5046STejun Heo struct worker_pool *pool = worker->pool; 901c54d5046STejun Heo 902c54d5046STejun Heo WARN_ON_ONCE(worker->task != current); 903c54d5046STejun Heo 904c54d5046STejun Heo /* If transitioning into NOT_RUNNING, adjust nr_running. */ 905c54d5046STejun Heo if ((flags & WORKER_NOT_RUNNING) && 906c54d5046STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 907c54d5046STejun Heo pool->nr_running--; 908c54d5046STejun Heo } 909c54d5046STejun Heo 910c54d5046STejun Heo worker->flags |= flags; 911c54d5046STejun Heo } 912c54d5046STejun Heo 913c54d5046STejun Heo /** 914c54d5046STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 915c54d5046STejun Heo * @worker: self 916c54d5046STejun Heo * @flags: flags to clear 917c54d5046STejun Heo * 918c54d5046STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 919c54d5046STejun Heo * 920c54d5046STejun Heo * CONTEXT: 921c54d5046STejun Heo * raw_spin_lock_irq(pool->lock) 922c54d5046STejun Heo */ 923c54d5046STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 924c54d5046STejun Heo { 925c54d5046STejun Heo struct worker_pool *pool = worker->pool; 926c54d5046STejun Heo unsigned int oflags = worker->flags; 927c54d5046STejun Heo 928c54d5046STejun Heo WARN_ON_ONCE(worker->task != current); 929c54d5046STejun Heo 930c54d5046STejun Heo worker->flags &= ~flags; 931c54d5046STejun Heo 932c54d5046STejun Heo /* 933c54d5046STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 934c54d5046STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 935c54d5046STejun Heo * of multiple flags, not a single flag. 936c54d5046STejun Heo */ 937c54d5046STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 938c54d5046STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 939c54d5046STejun Heo pool->nr_running++; 940c54d5046STejun Heo } 941c54d5046STejun Heo 942c54d5046STejun Heo /** 9436d25be57SThomas Gleixner * wq_worker_running - a worker is running again 944e22bee78STejun Heo * @task: task waking up 945e22bee78STejun Heo * 9466d25be57SThomas Gleixner * This function is called when a worker returns from schedule() 947e22bee78STejun Heo */ 9486d25be57SThomas Gleixner void wq_worker_running(struct task_struct *task) 949e22bee78STejun Heo { 950e22bee78STejun Heo struct worker *worker = kthread_data(task); 951e22bee78STejun Heo 9526d25be57SThomas Gleixner if (!worker->sleeping) 9536d25be57SThomas Gleixner return; 95407edfeceSFrederic Weisbecker 95507edfeceSFrederic Weisbecker /* 95607edfeceSFrederic Weisbecker * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check 95707edfeceSFrederic Weisbecker * and the nr_running increment below, we may ruin the nr_running reset 95807edfeceSFrederic Weisbecker * and leave with an unexpected pool->nr_running == 1 on the newly unbound 95907edfeceSFrederic Weisbecker * pool. Protect against such race. 96007edfeceSFrederic Weisbecker */ 96107edfeceSFrederic Weisbecker preempt_disable(); 9626d25be57SThomas Gleixner if (!(worker->flags & WORKER_NOT_RUNNING)) 963bc35f7efSLai Jiangshan worker->pool->nr_running++; 96407edfeceSFrederic Weisbecker preempt_enable(); 9656d25be57SThomas Gleixner worker->sleeping = 0; 96636576000SJoonsoo Kim } 967e22bee78STejun Heo 968e22bee78STejun Heo /** 969e22bee78STejun Heo * wq_worker_sleeping - a worker is going to sleep 970e22bee78STejun Heo * @task: task going to sleep 971e22bee78STejun Heo * 9726d25be57SThomas Gleixner * This function is called from schedule() when a busy worker is 973ccf45156SLai Jiangshan * going to sleep. 974e22bee78STejun Heo */ 9756d25be57SThomas Gleixner void wq_worker_sleeping(struct task_struct *task) 976e22bee78STejun Heo { 977cc5bff38SLai Jiangshan struct worker *worker = kthread_data(task); 978111c225aSTejun Heo struct worker_pool *pool; 979e22bee78STejun Heo 980111c225aSTejun Heo /* 981111c225aSTejun Heo * Rescuers, which may not have all the fields set up like normal 982111c225aSTejun Heo * workers, also reach here, let's not access anything before 983111c225aSTejun Heo * checking NOT_RUNNING. 984111c225aSTejun Heo */ 9852d64672eSSteven Rostedt if (worker->flags & WORKER_NOT_RUNNING) 9866d25be57SThomas Gleixner return; 987e22bee78STejun Heo 988111c225aSTejun Heo pool = worker->pool; 989111c225aSTejun Heo 99062849a96SSebastian Andrzej Siewior /* Return if preempted before wq_worker_running() was reached */ 99162849a96SSebastian Andrzej Siewior if (worker->sleeping) 9926d25be57SThomas Gleixner return; 9936d25be57SThomas Gleixner 9946d25be57SThomas Gleixner worker->sleeping = 1; 995a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 996e22bee78STejun Heo 997e22bee78STejun Heo /* 99845c753f5SFrederic Weisbecker * Recheck in case unbind_workers() preempted us. We don't 99945c753f5SFrederic Weisbecker * want to decrement nr_running after the worker is unbound 100045c753f5SFrederic Weisbecker * and nr_running has been reset. 100145c753f5SFrederic Weisbecker */ 100245c753f5SFrederic Weisbecker if (worker->flags & WORKER_NOT_RUNNING) { 100345c753f5SFrederic Weisbecker raw_spin_unlock_irq(&pool->lock); 100445c753f5SFrederic Weisbecker return; 100545c753f5SFrederic Weisbecker } 100645c753f5SFrederic Weisbecker 1007bc35f7efSLai Jiangshan pool->nr_running--; 1008725e8ec5STejun Heo if (need_more_worker(pool)) { 1009725e8ec5STejun Heo worker->current_pwq->stats[PWQ_STAT_CM_WAKEUP]++; 1010cc5bff38SLai Jiangshan wake_up_worker(pool); 1011725e8ec5STejun Heo } 1012a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 1013e22bee78STejun Heo } 1014e22bee78STejun Heo 1015e22bee78STejun Heo /** 10161b69ac6bSJohannes Weiner * wq_worker_last_func - retrieve worker's last work function 10178194fe94SBart Van Assche * @task: Task to retrieve last work function of. 10181b69ac6bSJohannes Weiner * 10191b69ac6bSJohannes Weiner * Determine the last function a worker executed. This is called from 10201b69ac6bSJohannes Weiner * the scheduler to get a worker's last known identity. 10211b69ac6bSJohannes Weiner * 10221b69ac6bSJohannes Weiner * CONTEXT: 1023a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(rq->lock) 10241b69ac6bSJohannes Weiner * 10254b047002SJohannes Weiner * This function is called during schedule() when a kworker is going 10264b047002SJohannes Weiner * to sleep. It's used by psi to identify aggregation workers during 10274b047002SJohannes Weiner * dequeuing, to allow periodic aggregation to shut-off when that 10284b047002SJohannes Weiner * worker is the last task in the system or cgroup to go to sleep. 10294b047002SJohannes Weiner * 10304b047002SJohannes Weiner * As this function doesn't involve any workqueue-related locking, it 10314b047002SJohannes Weiner * only returns stable values when called from inside the scheduler's 10324b047002SJohannes Weiner * queuing and dequeuing paths, when @task, which must be a kworker, 10334b047002SJohannes Weiner * is guaranteed to not be processing any works. 10344b047002SJohannes Weiner * 10351b69ac6bSJohannes Weiner * Return: 10361b69ac6bSJohannes Weiner * The last work function %current executed as a worker, NULL if it 10371b69ac6bSJohannes Weiner * hasn't executed any work yet. 10381b69ac6bSJohannes Weiner */ 10391b69ac6bSJohannes Weiner work_func_t wq_worker_last_func(struct task_struct *task) 10401b69ac6bSJohannes Weiner { 10411b69ac6bSJohannes Weiner struct worker *worker = kthread_data(task); 10421b69ac6bSJohannes Weiner 10431b69ac6bSJohannes Weiner return worker->last_func; 10441b69ac6bSJohannes Weiner } 10451b69ac6bSJohannes Weiner 10461b69ac6bSJohannes Weiner /** 10478cca0eeaSTejun Heo * find_worker_executing_work - find worker which is executing a work 1048c9e7cf27STejun Heo * @pool: pool of interest 10498cca0eeaSTejun Heo * @work: work to find worker for 10508cca0eeaSTejun Heo * 1051c9e7cf27STejun Heo * Find a worker which is executing @work on @pool by searching 1052c9e7cf27STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 1053a2c1c57bSTejun Heo * to match, its current execution should match the address of @work and 1054a2c1c57bSTejun Heo * its work function. This is to avoid unwanted dependency between 1055a2c1c57bSTejun Heo * unrelated work executions through a work item being recycled while still 1056a2c1c57bSTejun Heo * being executed. 1057a2c1c57bSTejun Heo * 1058a2c1c57bSTejun Heo * This is a bit tricky. A work item may be freed once its execution 1059a2c1c57bSTejun Heo * starts and nothing prevents the freed area from being recycled for 1060a2c1c57bSTejun Heo * another work item. If the same work item address ends up being reused 1061a2c1c57bSTejun Heo * before the original execution finishes, workqueue will identify the 1062a2c1c57bSTejun Heo * recycled work item as currently executing and make it wait until the 1063a2c1c57bSTejun Heo * current execution finishes, introducing an unwanted dependency. 1064a2c1c57bSTejun Heo * 1065c5aa87bbSTejun Heo * This function checks the work item address and work function to avoid 1066c5aa87bbSTejun Heo * false positives. Note that this isn't complete as one may construct a 1067c5aa87bbSTejun Heo * work function which can introduce dependency onto itself through a 1068c5aa87bbSTejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 1069c5aa87bbSTejun Heo * foot that badly, there's only so much we can do, and if such deadlock 1070c5aa87bbSTejun Heo * actually occurs, it should be easy to locate the culprit work function. 10718cca0eeaSTejun Heo * 10728cca0eeaSTejun Heo * CONTEXT: 1073a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 10748cca0eeaSTejun Heo * 1075d185af30SYacine Belkadi * Return: 1076d185af30SYacine Belkadi * Pointer to worker which is executing @work if found, %NULL 10778cca0eeaSTejun Heo * otherwise. 10788cca0eeaSTejun Heo */ 1079c9e7cf27STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 10808cca0eeaSTejun Heo struct work_struct *work) 10818cca0eeaSTejun Heo { 108242f8570fSSasha Levin struct worker *worker; 108342f8570fSSasha Levin 1084b67bfe0dSSasha Levin hash_for_each_possible(pool->busy_hash, worker, hentry, 1085a2c1c57bSTejun Heo (unsigned long)work) 1086a2c1c57bSTejun Heo if (worker->current_work == work && 1087a2c1c57bSTejun Heo worker->current_func == work->func) 108842f8570fSSasha Levin return worker; 108942f8570fSSasha Levin 109042f8570fSSasha Levin return NULL; 10918cca0eeaSTejun Heo } 10928cca0eeaSTejun Heo 10938cca0eeaSTejun Heo /** 1094bf4ede01STejun Heo * move_linked_works - move linked works to a list 1095bf4ede01STejun Heo * @work: start of series of works to be scheduled 1096bf4ede01STejun Heo * @head: target list to append @work to 1097402dd89dSShailendra Verma * @nextp: out parameter for nested worklist walking 1098bf4ede01STejun Heo * 1099bf4ede01STejun Heo * Schedule linked works starting from @work to @head. Work series to 1100bf4ede01STejun Heo * be scheduled starts at @work and includes any consecutive work with 1101bf4ede01STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 1102bf4ede01STejun Heo * 1103bf4ede01STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 1104bf4ede01STejun Heo * the last scheduled work. This allows move_linked_works() to be 1105bf4ede01STejun Heo * nested inside outer list_for_each_entry_safe(). 1106bf4ede01STejun Heo * 1107bf4ede01STejun Heo * CONTEXT: 1108a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1109bf4ede01STejun Heo */ 1110bf4ede01STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1111bf4ede01STejun Heo struct work_struct **nextp) 1112bf4ede01STejun Heo { 1113bf4ede01STejun Heo struct work_struct *n; 1114bf4ede01STejun Heo 1115bf4ede01STejun Heo /* 1116bf4ede01STejun Heo * Linked worklist will always end before the end of the list, 1117bf4ede01STejun Heo * use NULL for list head. 1118bf4ede01STejun Heo */ 1119bf4ede01STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1120bf4ede01STejun Heo list_move_tail(&work->entry, head); 1121bf4ede01STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1122bf4ede01STejun Heo break; 1123bf4ede01STejun Heo } 1124bf4ede01STejun Heo 1125bf4ede01STejun Heo /* 1126bf4ede01STejun Heo * If we're already inside safe list traversal and have moved 1127bf4ede01STejun Heo * multiple works to the scheduled queue, the next position 1128bf4ede01STejun Heo * needs to be updated. 1129bf4ede01STejun Heo */ 1130bf4ede01STejun Heo if (nextp) 1131bf4ede01STejun Heo *nextp = n; 1132bf4ede01STejun Heo } 1133bf4ede01STejun Heo 11348864b4e5STejun Heo /** 11358864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 11368864b4e5STejun Heo * @pwq: pool_workqueue to get 11378864b4e5STejun Heo * 11388864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 11398864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 11408864b4e5STejun Heo */ 11418864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 11428864b4e5STejun Heo { 11438864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 11448864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 11458864b4e5STejun Heo pwq->refcnt++; 11468864b4e5STejun Heo } 11478864b4e5STejun Heo 11488864b4e5STejun Heo /** 11498864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 11508864b4e5STejun Heo * @pwq: pool_workqueue to put 11518864b4e5STejun Heo * 11528864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 11538864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 11548864b4e5STejun Heo */ 11558864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 11568864b4e5STejun Heo { 11578864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 11588864b4e5STejun Heo if (likely(--pwq->refcnt)) 11598864b4e5STejun Heo return; 11608864b4e5STejun Heo if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND))) 11618864b4e5STejun Heo return; 11628864b4e5STejun Heo /* 11638864b4e5STejun Heo * @pwq can't be released under pool->lock, bounce to 11648864b4e5STejun Heo * pwq_unbound_release_workfn(). This never recurses on the same 11658864b4e5STejun Heo * pool->lock as this path is taken only for unbound workqueues and 11668864b4e5STejun Heo * the release work item is scheduled on a per-cpu workqueue. To 11678864b4e5STejun Heo * avoid lockdep warning, unbound pool->locks are given lockdep 11688864b4e5STejun Heo * subclass of 1 in get_unbound_pool(). 11698864b4e5STejun Heo */ 11708864b4e5STejun Heo schedule_work(&pwq->unbound_release_work); 11718864b4e5STejun Heo } 11728864b4e5STejun Heo 1173dce90d47STejun Heo /** 1174dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1175dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1176dce90d47STejun Heo * 1177dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1178dce90d47STejun Heo */ 1179dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1180dce90d47STejun Heo { 1181dce90d47STejun Heo if (pwq) { 1182dce90d47STejun Heo /* 118324acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the 1184dce90d47STejun Heo * following lock operations are safe. 1185dce90d47STejun Heo */ 1186a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 1187dce90d47STejun Heo put_pwq(pwq); 1188a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 1189dce90d47STejun Heo } 1190dce90d47STejun Heo } 1191dce90d47STejun Heo 1192f97a4a1aSLai Jiangshan static void pwq_activate_inactive_work(struct work_struct *work) 1193bf4ede01STejun Heo { 1194112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1195bf4ede01STejun Heo 1196bf4ede01STejun Heo trace_workqueue_activate_work(work); 119782607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 119882607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1199112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 1200f97a4a1aSLai Jiangshan __clear_bit(WORK_STRUCT_INACTIVE_BIT, work_data_bits(work)); 1201112202d9STejun Heo pwq->nr_active++; 1202bf4ede01STejun Heo } 1203bf4ede01STejun Heo 1204f97a4a1aSLai Jiangshan static void pwq_activate_first_inactive(struct pool_workqueue *pwq) 12053aa62497SLai Jiangshan { 1206f97a4a1aSLai Jiangshan struct work_struct *work = list_first_entry(&pwq->inactive_works, 12073aa62497SLai Jiangshan struct work_struct, entry); 12083aa62497SLai Jiangshan 1209f97a4a1aSLai Jiangshan pwq_activate_inactive_work(work); 12103aa62497SLai Jiangshan } 12113aa62497SLai Jiangshan 1212bf4ede01STejun Heo /** 1213112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1214112202d9STejun Heo * @pwq: pwq of interest 1215c4560c2cSLai Jiangshan * @work_data: work_data of work which left the queue 1216bf4ede01STejun Heo * 1217bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1218112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1219bf4ede01STejun Heo * 1220bf4ede01STejun Heo * CONTEXT: 1221a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1222bf4ede01STejun Heo */ 1223c4560c2cSLai Jiangshan static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) 1224bf4ede01STejun Heo { 1225c4560c2cSLai Jiangshan int color = get_work_color(work_data); 1226c4560c2cSLai Jiangshan 1227018f3a13SLai Jiangshan if (!(work_data & WORK_STRUCT_INACTIVE)) { 1228112202d9STejun Heo pwq->nr_active--; 1229f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 1230f97a4a1aSLai Jiangshan /* one down, submit an inactive one */ 1231112202d9STejun Heo if (pwq->nr_active < pwq->max_active) 1232f97a4a1aSLai Jiangshan pwq_activate_first_inactive(pwq); 1233bf4ede01STejun Heo } 1234018f3a13SLai Jiangshan } 1235018f3a13SLai Jiangshan 1236018f3a13SLai Jiangshan pwq->nr_in_flight[color]--; 1237bf4ede01STejun Heo 1238bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 1239112202d9STejun Heo if (likely(pwq->flush_color != color)) 12408864b4e5STejun Heo goto out_put; 1241bf4ede01STejun Heo 1242bf4ede01STejun Heo /* are there still in-flight works? */ 1243112202d9STejun Heo if (pwq->nr_in_flight[color]) 12448864b4e5STejun Heo goto out_put; 1245bf4ede01STejun Heo 1246112202d9STejun Heo /* this pwq is done, clear flush_color */ 1247112202d9STejun Heo pwq->flush_color = -1; 1248bf4ede01STejun Heo 1249bf4ede01STejun Heo /* 1250112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 1251bf4ede01STejun Heo * will handle the rest. 1252bf4ede01STejun Heo */ 1253112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1254112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 12558864b4e5STejun Heo out_put: 12568864b4e5STejun Heo put_pwq(pwq); 1257bf4ede01STejun Heo } 1258bf4ede01STejun Heo 125936e227d2STejun Heo /** 1260bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 126136e227d2STejun Heo * @work: work item to steal 126236e227d2STejun Heo * @is_dwork: @work is a delayed_work 1263bbb68dfaSTejun Heo * @flags: place to store irq state 126436e227d2STejun Heo * 126536e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 1266d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 126736e227d2STejun Heo * 1268d185af30SYacine Belkadi * Return: 12693eb6b31bSMauro Carvalho Chehab * 12703eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 127136e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 127236e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 127336e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1274bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1275bbb68dfaSTejun Heo * for arbitrarily long 12763eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 127736e227d2STejun Heo * 1278d185af30SYacine Belkadi * Note: 1279bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1280e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 1281e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 1282e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 1283bbb68dfaSTejun Heo * 1284bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1285bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1286bbb68dfaSTejun Heo * 1287e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 1288bf4ede01STejun Heo */ 1289bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1290bbb68dfaSTejun Heo unsigned long *flags) 1291bf4ede01STejun Heo { 1292d565ed63STejun Heo struct worker_pool *pool; 1293112202d9STejun Heo struct pool_workqueue *pwq; 1294bf4ede01STejun Heo 1295bbb68dfaSTejun Heo local_irq_save(*flags); 1296bbb68dfaSTejun Heo 129736e227d2STejun Heo /* try to steal the timer if it exists */ 129836e227d2STejun Heo if (is_dwork) { 129936e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 130036e227d2STejun Heo 1301e0aecdd8STejun Heo /* 1302e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 1303e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 1304e0aecdd8STejun Heo * running on the local CPU. 1305e0aecdd8STejun Heo */ 130636e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 130736e227d2STejun Heo return 1; 130836e227d2STejun Heo } 130936e227d2STejun Heo 131036e227d2STejun Heo /* try to claim PENDING the normal way */ 1311bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1312bf4ede01STejun Heo return 0; 1313bf4ede01STejun Heo 131424acfb71SThomas Gleixner rcu_read_lock(); 1315bf4ede01STejun Heo /* 1316bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1317bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1318bf4ede01STejun Heo */ 1319d565ed63STejun Heo pool = get_work_pool(work); 1320d565ed63STejun Heo if (!pool) 1321bbb68dfaSTejun Heo goto fail; 1322bf4ede01STejun Heo 1323a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pool->lock); 1324bf4ede01STejun Heo /* 1325112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 1326112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 1327112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 1328112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 1329112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 13300b3dae68SLai Jiangshan * item is currently queued on that pool. 1331bf4ede01STejun Heo */ 1332112202d9STejun Heo pwq = get_work_pwq(work); 1333112202d9STejun Heo if (pwq && pwq->pool == pool) { 1334bf4ede01STejun Heo debug_work_deactivate(work); 13353aa62497SLai Jiangshan 13363aa62497SLai Jiangshan /* 1337018f3a13SLai Jiangshan * A cancelable inactive work item must be in the 1338018f3a13SLai Jiangshan * pwq->inactive_works since a queued barrier can't be 1339018f3a13SLai Jiangshan * canceled (see the comments in insert_wq_barrier()). 1340018f3a13SLai Jiangshan * 1341f97a4a1aSLai Jiangshan * An inactive work item cannot be grabbed directly because 1342d812796eSLai Jiangshan * it might have linked barrier work items which, if left 1343f97a4a1aSLai Jiangshan * on the inactive_works list, will confuse pwq->nr_active 134416062836STejun Heo * management later on and cause stall. Make sure the work 134516062836STejun Heo * item is activated before grabbing. 13463aa62497SLai Jiangshan */ 1347f97a4a1aSLai Jiangshan if (*work_data_bits(work) & WORK_STRUCT_INACTIVE) 1348f97a4a1aSLai Jiangshan pwq_activate_inactive_work(work); 13493aa62497SLai Jiangshan 1350bf4ede01STejun Heo list_del_init(&work->entry); 1351c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, *work_data_bits(work)); 135236e227d2STejun Heo 1353112202d9STejun Heo /* work->data points to pwq iff queued, point to pool */ 13544468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 13554468a00fSLai Jiangshan 1356a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 135724acfb71SThomas Gleixner rcu_read_unlock(); 135836e227d2STejun Heo return 1; 1359bf4ede01STejun Heo } 1360a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 1361bbb68dfaSTejun Heo fail: 136224acfb71SThomas Gleixner rcu_read_unlock(); 1363bbb68dfaSTejun Heo local_irq_restore(*flags); 1364bbb68dfaSTejun Heo if (work_is_canceling(work)) 1365bbb68dfaSTejun Heo return -ENOENT; 1366bbb68dfaSTejun Heo cpu_relax(); 136736e227d2STejun Heo return -EAGAIN; 1368bf4ede01STejun Heo } 1369bf4ede01STejun Heo 1370bf4ede01STejun Heo /** 1371706026c2STejun Heo * insert_work - insert a work into a pool 1372112202d9STejun Heo * @pwq: pwq @work belongs to 13734690c4abSTejun Heo * @work: work to insert 13744690c4abSTejun Heo * @head: insertion point 13754690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 13764690c4abSTejun Heo * 1377112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 1378706026c2STejun Heo * work_struct flags. 13794690c4abSTejun Heo * 13804690c4abSTejun Heo * CONTEXT: 1381a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1382365970a1SDavid Howells */ 1383112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 1384112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 1385b89deed3SOleg Nesterov { 1386112202d9STejun Heo struct worker_pool *pool = pwq->pool; 1387e1d8aa9fSFrederic Weisbecker 1388e89a85d6SWalter Wu /* record the work call stack in order to print it in KASAN reports */ 1389f70da745SMarco Elver kasan_record_aux_stack_noalloc(work); 1390e89a85d6SWalter Wu 13914690c4abSTejun Heo /* we own @work, set data and link */ 1392112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 13931a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 13948864b4e5STejun Heo get_pwq(pwq); 1395e22bee78STejun Heo 139663d95a91STejun Heo if (__need_more_worker(pool)) 139763d95a91STejun Heo wake_up_worker(pool); 1398b89deed3SOleg Nesterov } 1399b89deed3SOleg Nesterov 1400c8efcc25STejun Heo /* 1401c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 14028d03ecfeSTejun Heo * same workqueue. 1403c8efcc25STejun Heo */ 1404c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1405c8efcc25STejun Heo { 1406c8efcc25STejun Heo struct worker *worker; 1407c8efcc25STejun Heo 14088d03ecfeSTejun Heo worker = current_wq_worker(); 1409c8efcc25STejun Heo /* 1410bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If 14118d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 1412c8efcc25STejun Heo */ 1413112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 1414c8efcc25STejun Heo } 1415c8efcc25STejun Heo 1416ef557180SMike Galbraith /* 1417ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 1418ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 1419ef557180SMike Galbraith * avoid perturbing sensitive tasks. 1420ef557180SMike Galbraith */ 1421ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 1422ef557180SMike Galbraith { 1423ef557180SMike Galbraith int new_cpu; 1424ef557180SMike Galbraith 1425f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 1426ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 1427ef557180SMike Galbraith return cpu; 1428a8ec5880SAmmar Faizi } else { 1429a8ec5880SAmmar Faizi pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); 1430f303fccbSTejun Heo } 1431f303fccbSTejun Heo 1432ef557180SMike Galbraith if (cpumask_empty(wq_unbound_cpumask)) 1433ef557180SMike Galbraith return cpu; 1434ef557180SMike Galbraith 1435ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 1436ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 1437ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 1438ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 1439ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 1440ef557180SMike Galbraith return cpu; 1441ef557180SMike Galbraith } 1442ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 1443ef557180SMike Galbraith 1444ef557180SMike Galbraith return new_cpu; 1445ef557180SMike Galbraith } 1446ef557180SMike Galbraith 1447d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 14481da177e4SLinus Torvalds struct work_struct *work) 14491da177e4SLinus Torvalds { 1450112202d9STejun Heo struct pool_workqueue *pwq; 1451c9178087STejun Heo struct worker_pool *last_pool; 14521e19ffc6STejun Heo struct list_head *worklist; 14538a2e8e5dSTejun Heo unsigned int work_flags; 1454b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 14558930cabaSTejun Heo 14568930cabaSTejun Heo /* 14578930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 14588930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 14598930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 14608930cabaSTejun Heo * happen with IRQ disabled. 14618930cabaSTejun Heo */ 14628e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 14631da177e4SLinus Torvalds 14641e19ffc6STejun Heo 146533e3f0a3SRichard Clark /* 146633e3f0a3SRichard Clark * For a draining wq, only works from the same workqueue are 146733e3f0a3SRichard Clark * allowed. The __WQ_DESTROYING helps to spot the issue that 146833e3f0a3SRichard Clark * queues a new work item to a wq after destroy_workqueue(wq). 146933e3f0a3SRichard Clark */ 147033e3f0a3SRichard Clark if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && 147133e3f0a3SRichard Clark WARN_ON_ONCE(!is_chained_work(wq)))) 1472e41e704bSTejun Heo return; 147324acfb71SThomas Gleixner rcu_read_lock(); 14749e8cd2f5STejun Heo retry: 1475aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */ 1476aa202f1fSHillf Danton if (wq->flags & WQ_UNBOUND) { 1477df2d5ae4STejun Heo if (req_cpu == WORK_CPU_UNBOUND) 1478ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 1479df2d5ae4STejun Heo pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu)); 1480aa202f1fSHillf Danton } else { 1481aa202f1fSHillf Danton if (req_cpu == WORK_CPU_UNBOUND) 1482aa202f1fSHillf Danton cpu = raw_smp_processor_id(); 1483aa202f1fSHillf Danton pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 1484aa202f1fSHillf Danton } 1485f3421797STejun Heo 148618aa9effSTejun Heo /* 1487c9178087STejun Heo * If @work was previously on a different pool, it might still be 1488c9178087STejun Heo * running there, in which case the work needs to be queued on that 1489c9178087STejun Heo * pool to guarantee non-reentrancy. 149018aa9effSTejun Heo */ 1491c9e7cf27STejun Heo last_pool = get_work_pool(work); 1492112202d9STejun Heo if (last_pool && last_pool != pwq->pool) { 149318aa9effSTejun Heo struct worker *worker; 149418aa9effSTejun Heo 1495a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&last_pool->lock); 149618aa9effSTejun Heo 1497c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 149818aa9effSTejun Heo 1499112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 1500c9178087STejun Heo pwq = worker->current_pwq; 15018594fadeSLai Jiangshan } else { 150218aa9effSTejun Heo /* meh... not running there, queue here */ 1503a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&last_pool->lock); 1504a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pwq->pool->lock); 150518aa9effSTejun Heo } 15068930cabaSTejun Heo } else { 1507a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pwq->pool->lock); 15088930cabaSTejun Heo } 1509502ca9d8STejun Heo 15109e8cd2f5STejun Heo /* 15119e8cd2f5STejun Heo * pwq is determined and locked. For unbound pools, we could have 15129e8cd2f5STejun Heo * raced with pwq release and it could already be dead. If its 15139e8cd2f5STejun Heo * refcnt is zero, repeat pwq selection. Note that pwqs never die 1514df2d5ae4STejun Heo * without another pwq replacing it in the numa_pwq_tbl or while 1515df2d5ae4STejun Heo * work items are executing on it, so the retrying is guaranteed to 15169e8cd2f5STejun Heo * make forward-progress. 15179e8cd2f5STejun Heo */ 15189e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 15199e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 1520a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pwq->pool->lock); 15219e8cd2f5STejun Heo cpu_relax(); 15229e8cd2f5STejun Heo goto retry; 15239e8cd2f5STejun Heo } 15249e8cd2f5STejun Heo /* oops */ 15259e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 15269e8cd2f5STejun Heo wq->name, cpu); 15279e8cd2f5STejun Heo } 15289e8cd2f5STejun Heo 1529112202d9STejun Heo /* pwq determined, queue */ 1530112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 1531502ca9d8STejun Heo 153224acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry))) 153324acfb71SThomas Gleixner goto out; 15341e19ffc6STejun Heo 1535112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 1536112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 15371e19ffc6STejun Heo 1538112202d9STejun Heo if (likely(pwq->nr_active < pwq->max_active)) { 1539cdadf009STejun Heo trace_workqueue_activate_work(work); 1540112202d9STejun Heo pwq->nr_active++; 1541112202d9STejun Heo worklist = &pwq->pool->worklist; 154282607adcSTejun Heo if (list_empty(worklist)) 154382607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 15448a2e8e5dSTejun Heo } else { 1545f97a4a1aSLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 1546f97a4a1aSLai Jiangshan worklist = &pwq->inactive_works; 15478a2e8e5dSTejun Heo } 15481e19ffc6STejun Heo 15490687c66bSZqiang debug_work_activate(work); 1550112202d9STejun Heo insert_work(pwq, work, worklist, work_flags); 15511e19ffc6STejun Heo 155224acfb71SThomas Gleixner out: 1553a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pwq->pool->lock); 155424acfb71SThomas Gleixner rcu_read_unlock(); 15551da177e4SLinus Torvalds } 15561da177e4SLinus Torvalds 15570fcb78c2SRolf Eike Beer /** 1558c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1559c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1560c1a220e7SZhang Rui * @wq: workqueue to use 1561c1a220e7SZhang Rui * @work: work to queue 1562c1a220e7SZhang Rui * 1563c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1564443378f0SPaul E. McKenney * can't go away. Callers that fail to ensure that the specified 1565443378f0SPaul E. McKenney * CPU cannot go away will execute on a randomly chosen CPU. 1566854f5cc5SPaul E. McKenney * But note well that callers specifying a CPU that never has been 1567854f5cc5SPaul E. McKenney * online will get a splat. 1568d185af30SYacine Belkadi * 1569d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 1570c1a220e7SZhang Rui */ 1571d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1572d4283e93STejun Heo struct work_struct *work) 1573c1a220e7SZhang Rui { 1574d4283e93STejun Heo bool ret = false; 15758930cabaSTejun Heo unsigned long flags; 15768930cabaSTejun Heo 15778930cabaSTejun Heo local_irq_save(flags); 1578c1a220e7SZhang Rui 157922df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 15804690c4abSTejun Heo __queue_work(cpu, wq, work); 1581d4283e93STejun Heo ret = true; 1582c1a220e7SZhang Rui } 15838930cabaSTejun Heo 15848930cabaSTejun Heo local_irq_restore(flags); 1585c1a220e7SZhang Rui return ret; 1586c1a220e7SZhang Rui } 1587ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 1588c1a220e7SZhang Rui 15898204e0c1SAlexander Duyck /** 15908204e0c1SAlexander Duyck * workqueue_select_cpu_near - Select a CPU based on NUMA node 15918204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from 15928204e0c1SAlexander Duyck * 15938204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given 15948204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return 15958204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any 15968204e0c1SAlexander Duyck * available CPU if we need to schedule this work. 15978204e0c1SAlexander Duyck */ 15988204e0c1SAlexander Duyck static int workqueue_select_cpu_near(int node) 15998204e0c1SAlexander Duyck { 16008204e0c1SAlexander Duyck int cpu; 16018204e0c1SAlexander Duyck 16028204e0c1SAlexander Duyck /* No point in doing this if NUMA isn't enabled for workqueues */ 16038204e0c1SAlexander Duyck if (!wq_numa_enabled) 16048204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 16058204e0c1SAlexander Duyck 16068204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */ 16078204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) 16088204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 16098204e0c1SAlexander Duyck 16108204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */ 16118204e0c1SAlexander Duyck cpu = raw_smp_processor_id(); 16128204e0c1SAlexander Duyck if (node == cpu_to_node(cpu)) 16138204e0c1SAlexander Duyck return cpu; 16148204e0c1SAlexander Duyck 16158204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */ 16168204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 16178204e0c1SAlexander Duyck 16188204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */ 16198204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; 16208204e0c1SAlexander Duyck } 16218204e0c1SAlexander Duyck 16228204e0c1SAlexander Duyck /** 16238204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node 16248204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for 16258204e0c1SAlexander Duyck * @wq: workqueue to use 16268204e0c1SAlexander Duyck * @work: work to queue 16278204e0c1SAlexander Duyck * 16288204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic 16298204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given 16308204e0c1SAlexander Duyck * NUMA node. 16318204e0c1SAlexander Duyck * 16328204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto 16338204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is 16348204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior. 16358204e0c1SAlexander Duyck * 16368204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the 16378204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we 16388204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU. 16398204e0c1SAlexander Duyck * 16408204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise. 16418204e0c1SAlexander Duyck */ 16428204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq, 16438204e0c1SAlexander Duyck struct work_struct *work) 16448204e0c1SAlexander Duyck { 16458204e0c1SAlexander Duyck unsigned long flags; 16468204e0c1SAlexander Duyck bool ret = false; 16478204e0c1SAlexander Duyck 16488204e0c1SAlexander Duyck /* 16498204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues. 16508204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given 16518204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node. 16528204e0c1SAlexander Duyck * 16538204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in 16548204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for 16558204e0c1SAlexander Duyck * some round robin type logic. 16568204e0c1SAlexander Duyck */ 16578204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 16588204e0c1SAlexander Duyck 16598204e0c1SAlexander Duyck local_irq_save(flags); 16608204e0c1SAlexander Duyck 16618204e0c1SAlexander Duyck if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 16628204e0c1SAlexander Duyck int cpu = workqueue_select_cpu_near(node); 16638204e0c1SAlexander Duyck 16648204e0c1SAlexander Duyck __queue_work(cpu, wq, work); 16658204e0c1SAlexander Duyck ret = true; 16668204e0c1SAlexander Duyck } 16678204e0c1SAlexander Duyck 16688204e0c1SAlexander Duyck local_irq_restore(flags); 16698204e0c1SAlexander Duyck return ret; 16708204e0c1SAlexander Duyck } 16718204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node); 16728204e0c1SAlexander Duyck 16738c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 16741da177e4SLinus Torvalds { 16758c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 16761da177e4SLinus Torvalds 1677e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 167860c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 16791da177e4SLinus Torvalds } 16801438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 16811da177e4SLinus Torvalds 16827beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 168352bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 16841da177e4SLinus Torvalds { 16857beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 16867beb2edfSTejun Heo struct work_struct *work = &dwork->work; 16871da177e4SLinus Torvalds 1688637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 16894b243563SSami Tolvanen WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 1690fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 1691fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 16927beb2edfSTejun Heo 16938852aac2STejun Heo /* 16948852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 16958852aac2STejun Heo * both optimization and correctness. The earliest @timer can 16968852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 16978852aac2STejun Heo * on that there's no such delay when @delay is 0. 16988852aac2STejun Heo */ 16998852aac2STejun Heo if (!delay) { 17008852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 17018852aac2STejun Heo return; 17028852aac2STejun Heo } 17038852aac2STejun Heo 170460c057bcSLai Jiangshan dwork->wq = wq; 17051265057fSTejun Heo dwork->cpu = cpu; 17067beb2edfSTejun Heo timer->expires = jiffies + delay; 17077beb2edfSTejun Heo 1708041bd12eSTejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 17097beb2edfSTejun Heo add_timer_on(timer, cpu); 1710041bd12eSTejun Heo else 1711041bd12eSTejun Heo add_timer(timer); 17127beb2edfSTejun Heo } 17131da177e4SLinus Torvalds 17140fcb78c2SRolf Eike Beer /** 17150fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 17160fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 17170fcb78c2SRolf Eike Beer * @wq: workqueue to use 1718af9997e4SRandy Dunlap * @dwork: work to queue 17190fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 17200fcb78c2SRolf Eike Beer * 1721d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 1722715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 1723715f1300STejun Heo * execution. 17240fcb78c2SRolf Eike Beer */ 1725d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 172652bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 17277a6bc1cdSVenkatesh Pallipadi { 172852bad64dSDavid Howells struct work_struct *work = &dwork->work; 1729d4283e93STejun Heo bool ret = false; 17308930cabaSTejun Heo unsigned long flags; 17318930cabaSTejun Heo 17328930cabaSTejun Heo /* read the comment in __queue_work() */ 17338930cabaSTejun Heo local_irq_save(flags); 17347a6bc1cdSVenkatesh Pallipadi 173522df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 17367beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 1737d4283e93STejun Heo ret = true; 17387a6bc1cdSVenkatesh Pallipadi } 17398930cabaSTejun Heo 17408930cabaSTejun Heo local_irq_restore(flags); 17417a6bc1cdSVenkatesh Pallipadi return ret; 17427a6bc1cdSVenkatesh Pallipadi } 1743ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 17441da177e4SLinus Torvalds 1745c8e55f36STejun Heo /** 17468376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 17478376fe22STejun Heo * @cpu: CPU number to execute work on 17488376fe22STejun Heo * @wq: workqueue to use 17498376fe22STejun Heo * @dwork: work to queue 17508376fe22STejun Heo * @delay: number of jiffies to wait before queueing 17518376fe22STejun Heo * 17528376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 17538376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 17548376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 17558376fe22STejun Heo * current state. 17568376fe22STejun Heo * 1757d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 17588376fe22STejun Heo * pending and its timer was modified. 17598376fe22STejun Heo * 1760e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 17618376fe22STejun Heo * See try_to_grab_pending() for details. 17628376fe22STejun Heo */ 17638376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 17648376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 17658376fe22STejun Heo { 17668376fe22STejun Heo unsigned long flags; 17678376fe22STejun Heo int ret; 17688376fe22STejun Heo 17698376fe22STejun Heo do { 17708376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 17718376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 17728376fe22STejun Heo 17738376fe22STejun Heo if (likely(ret >= 0)) { 17748376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 17758376fe22STejun Heo local_irq_restore(flags); 17768376fe22STejun Heo } 17778376fe22STejun Heo 17788376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 17798376fe22STejun Heo return ret; 17808376fe22STejun Heo } 17818376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 17828376fe22STejun Heo 178305f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 178405f0fe6bSTejun Heo { 178505f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 178605f0fe6bSTejun Heo 178705f0fe6bSTejun Heo /* read the comment in __queue_work() */ 178805f0fe6bSTejun Heo local_irq_disable(); 178905f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 179005f0fe6bSTejun Heo local_irq_enable(); 179105f0fe6bSTejun Heo } 179205f0fe6bSTejun Heo 179305f0fe6bSTejun Heo /** 179405f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 179505f0fe6bSTejun Heo * @wq: workqueue to use 179605f0fe6bSTejun Heo * @rwork: work to queue 179705f0fe6bSTejun Heo * 179805f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 179905f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 1800bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the 180105f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 180205f0fe6bSTejun Heo */ 180305f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 180405f0fe6bSTejun Heo { 180505f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 180605f0fe6bSTejun Heo 180705f0fe6bSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 180805f0fe6bSTejun Heo rwork->wq = wq; 1809a7e30c0eSUladzislau Rezki call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); 181005f0fe6bSTejun Heo return true; 181105f0fe6bSTejun Heo } 181205f0fe6bSTejun Heo 181305f0fe6bSTejun Heo return false; 181405f0fe6bSTejun Heo } 181505f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 181605f0fe6bSTejun Heo 18178376fe22STejun Heo /** 1818c8e55f36STejun Heo * worker_enter_idle - enter idle state 1819c8e55f36STejun Heo * @worker: worker which is entering idle state 1820c8e55f36STejun Heo * 1821c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 1822c8e55f36STejun Heo * necessary. 1823c8e55f36STejun Heo * 1824c8e55f36STejun Heo * LOCKING: 1825a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1826c8e55f36STejun Heo */ 1827c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 18281da177e4SLinus Torvalds { 1829bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1830c8e55f36STejun Heo 18316183c009STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 18326183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 18336183c009STejun Heo (worker->hentry.next || worker->hentry.pprev))) 18346183c009STejun Heo return; 1835c8e55f36STejun Heo 1836051e1850SLai Jiangshan /* can't use worker_set_flags(), also called from create_worker() */ 1837cb444766STejun Heo worker->flags |= WORKER_IDLE; 1838bd7bdd43STejun Heo pool->nr_idle++; 1839e22bee78STejun Heo worker->last_active = jiffies; 1840c8e55f36STejun Heo 1841c8e55f36STejun Heo /* idle_list is LIFO */ 1842bd7bdd43STejun Heo list_add(&worker->entry, &pool->idle_list); 1843db7bccf4STejun Heo 184463d95a91STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1845628c78e7STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1846cb444766STejun Heo 1847989442d7SLai Jiangshan /* Sanity check nr_running. */ 1848bc35f7efSLai Jiangshan WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); 1849c8e55f36STejun Heo } 1850c8e55f36STejun Heo 1851c8e55f36STejun Heo /** 1852c8e55f36STejun Heo * worker_leave_idle - leave idle state 1853c8e55f36STejun Heo * @worker: worker which is leaving idle state 1854c8e55f36STejun Heo * 1855c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 1856c8e55f36STejun Heo * 1857c8e55f36STejun Heo * LOCKING: 1858a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1859c8e55f36STejun Heo */ 1860c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 1861c8e55f36STejun Heo { 1862bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1863c8e55f36STejun Heo 18646183c009STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 18656183c009STejun Heo return; 1866d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1867bd7bdd43STejun Heo pool->nr_idle--; 1868c8e55f36STejun Heo list_del_init(&worker->entry); 1869c8e55f36STejun Heo } 1870c8e55f36STejun Heo 1871f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 1872c34056a3STejun Heo { 1873c34056a3STejun Heo struct worker *worker; 1874c34056a3STejun Heo 1875f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 1876c8e55f36STejun Heo if (worker) { 1877c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 1878affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 1879da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 1880e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 1881e22bee78STejun Heo worker->flags = WORKER_PREP; 1882c8e55f36STejun Heo } 1883c34056a3STejun Heo return worker; 1884c34056a3STejun Heo } 1885c34056a3STejun Heo 1886c34056a3STejun Heo /** 18874736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 18884736cbf7SLai Jiangshan * @worker: worker to be attached 18894736cbf7SLai Jiangshan * @pool: the target pool 18904736cbf7SLai Jiangshan * 18914736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 18924736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 18934736cbf7SLai Jiangshan * cpu-[un]hotplugs. 18944736cbf7SLai Jiangshan */ 18954736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 18964736cbf7SLai Jiangshan struct worker_pool *pool) 18974736cbf7SLai Jiangshan { 18981258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 18994736cbf7SLai Jiangshan 19004736cbf7SLai Jiangshan /* 19011258fae7STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains 19021258fae7STejun Heo * stable across this function. See the comments above the flag 19031258fae7STejun Heo * definition for details. 19044736cbf7SLai Jiangshan */ 19054736cbf7SLai Jiangshan if (pool->flags & POOL_DISASSOCIATED) 19064736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 19075c25b5ffSPeter Zijlstra else 19085c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu); 19094736cbf7SLai Jiangshan 1910640f17c8SPeter Zijlstra if (worker->rescue_wq) 1911640f17c8SPeter Zijlstra set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask); 1912640f17c8SPeter Zijlstra 19134736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 1914a2d812a2STejun Heo worker->pool = pool; 19154736cbf7SLai Jiangshan 19161258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 19174736cbf7SLai Jiangshan } 19184736cbf7SLai Jiangshan 19194736cbf7SLai Jiangshan /** 192060f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 192160f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 192260f5a4bcSLai Jiangshan * 19234736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 19244736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 19254736cbf7SLai Jiangshan * other reference to the pool. 192660f5a4bcSLai Jiangshan */ 1927a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 192860f5a4bcSLai Jiangshan { 1929a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 193060f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 193160f5a4bcSLai Jiangshan 19321258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 1933a2d812a2STejun Heo 19345c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, -1); 1935da028469SLai Jiangshan list_del(&worker->node); 1936a2d812a2STejun Heo worker->pool = NULL; 1937a2d812a2STejun Heo 1938e02b9312SValentin Schneider if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) 193960f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 19401258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 194160f5a4bcSLai Jiangshan 1942b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 1943b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 1944b62c0751SLai Jiangshan 194560f5a4bcSLai Jiangshan if (detach_completion) 194660f5a4bcSLai Jiangshan complete(detach_completion); 194760f5a4bcSLai Jiangshan } 194860f5a4bcSLai Jiangshan 194960f5a4bcSLai Jiangshan /** 1950c34056a3STejun Heo * create_worker - create a new workqueue worker 195163d95a91STejun Heo * @pool: pool the new worker will belong to 1952c34056a3STejun Heo * 1953051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 1954c34056a3STejun Heo * 1955c34056a3STejun Heo * CONTEXT: 1956c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 1957c34056a3STejun Heo * 1958d185af30SYacine Belkadi * Return: 1959c34056a3STejun Heo * Pointer to the newly created worker. 1960c34056a3STejun Heo */ 1961bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 1962c34056a3STejun Heo { 1963e441b56fSZhen Lei struct worker *worker; 1964e441b56fSZhen Lei int id; 1965e3c916a4STejun Heo char id_buf[16]; 1966c34056a3STejun Heo 19677cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 1968e441b56fSZhen Lei id = ida_alloc(&pool->worker_ida, GFP_KERNEL); 19693f0ea0b8SPetr Mladek if (id < 0) { 19703f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", 19713f0ea0b8SPetr Mladek ERR_PTR(id)); 1972e441b56fSZhen Lei return NULL; 19733f0ea0b8SPetr Mladek } 1974c34056a3STejun Heo 1975f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 19763f0ea0b8SPetr Mladek if (!worker) { 19773f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker\n"); 1978c34056a3STejun Heo goto fail; 19793f0ea0b8SPetr Mladek } 1980c34056a3STejun Heo 1981c34056a3STejun Heo worker->id = id; 1982c34056a3STejun Heo 198329c91e99STejun Heo if (pool->cpu >= 0) 1984e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 1985e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 1986f3421797STejun Heo else 1987e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 1988e3c916a4STejun Heo 1989f3f90ad4STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, pool->node, 1990e3c916a4STejun Heo "kworker/%s", id_buf); 19913f0ea0b8SPetr Mladek if (IS_ERR(worker->task)) { 199260f54038SPetr Mladek if (PTR_ERR(worker->task) == -EINTR) { 199360f54038SPetr Mladek pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", 199460f54038SPetr Mladek id_buf); 199560f54038SPetr Mladek } else { 19963f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to create a worker thread: %pe", 19973f0ea0b8SPetr Mladek worker->task); 199860f54038SPetr Mladek } 1999c34056a3STejun Heo goto fail; 20003f0ea0b8SPetr Mladek } 2001c34056a3STejun Heo 200291151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 200325834c73SPeter Zijlstra kthread_bind_mask(worker->task, pool->attrs->cpumask); 200491151228SOleg Nesterov 2005da028469SLai Jiangshan /* successful, attach the worker to the pool */ 20064736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 2007822d8405STejun Heo 2008051e1850SLai Jiangshan /* start the newly created worker */ 2009a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2010051e1850SLai Jiangshan worker->pool->nr_workers++; 2011051e1850SLai Jiangshan worker_enter_idle(worker); 2012051e1850SLai Jiangshan wake_up_process(worker->task); 2013a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2014051e1850SLai Jiangshan 2015c34056a3STejun Heo return worker; 2016822d8405STejun Heo 2017c34056a3STejun Heo fail: 2018e441b56fSZhen Lei ida_free(&pool->worker_ida, id); 2019c34056a3STejun Heo kfree(worker); 2020c34056a3STejun Heo return NULL; 2021c34056a3STejun Heo } 2022c34056a3STejun Heo 2023793777bcSValentin Schneider static void unbind_worker(struct worker *worker) 2024793777bcSValentin Schneider { 2025793777bcSValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2026793777bcSValentin Schneider 2027793777bcSValentin Schneider kthread_set_per_cpu(worker->task, -1); 2028793777bcSValentin Schneider if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) 2029793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); 2030793777bcSValentin Schneider else 2031793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); 2032793777bcSValentin Schneider } 2033793777bcSValentin Schneider 2034e02b9312SValentin Schneider static void wake_dying_workers(struct list_head *cull_list) 2035e02b9312SValentin Schneider { 2036e02b9312SValentin Schneider struct worker *worker, *tmp; 2037e02b9312SValentin Schneider 2038e02b9312SValentin Schneider list_for_each_entry_safe(worker, tmp, cull_list, entry) { 2039e02b9312SValentin Schneider list_del_init(&worker->entry); 2040e02b9312SValentin Schneider unbind_worker(worker); 2041e02b9312SValentin Schneider /* 2042e02b9312SValentin Schneider * If the worker was somehow already running, then it had to be 2043e02b9312SValentin Schneider * in pool->idle_list when set_worker_dying() happened or we 2044e02b9312SValentin Schneider * wouldn't have gotten here. 2045c34056a3STejun Heo * 2046e02b9312SValentin Schneider * Thus, the worker must either have observed the WORKER_DIE 2047e02b9312SValentin Schneider * flag, or have set its state to TASK_IDLE. Either way, the 2048e02b9312SValentin Schneider * below will be observed by the worker and is safe to do 2049e02b9312SValentin Schneider * outside of pool->lock. 2050e02b9312SValentin Schneider */ 2051e02b9312SValentin Schneider wake_up_process(worker->task); 2052e02b9312SValentin Schneider } 2053e02b9312SValentin Schneider } 2054e02b9312SValentin Schneider 2055e02b9312SValentin Schneider /** 2056e02b9312SValentin Schneider * set_worker_dying - Tag a worker for destruction 2057e02b9312SValentin Schneider * @worker: worker to be destroyed 2058e02b9312SValentin Schneider * @list: transfer worker away from its pool->idle_list and into list 2059e02b9312SValentin Schneider * 2060e02b9312SValentin Schneider * Tag @worker for destruction and adjust @pool stats accordingly. The worker 2061e02b9312SValentin Schneider * should be idle. 2062c8e55f36STejun Heo * 2063c8e55f36STejun Heo * CONTEXT: 2064a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2065c34056a3STejun Heo */ 2066e02b9312SValentin Schneider static void set_worker_dying(struct worker *worker, struct list_head *list) 2067c34056a3STejun Heo { 2068bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2069c34056a3STejun Heo 2070cd549687STejun Heo lockdep_assert_held(&pool->lock); 2071e02b9312SValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2072cd549687STejun Heo 2073c34056a3STejun Heo /* sanity check frenzy */ 20746183c009STejun Heo if (WARN_ON(worker->current_work) || 207573eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 207673eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 20776183c009STejun Heo return; 2078c34056a3STejun Heo 2079bd7bdd43STejun Heo pool->nr_workers--; 2080bd7bdd43STejun Heo pool->nr_idle--; 2081c8e55f36STejun Heo 2082cb444766STejun Heo worker->flags |= WORKER_DIE; 2083e02b9312SValentin Schneider 2084e02b9312SValentin Schneider list_move(&worker->entry, list); 2085e02b9312SValentin Schneider list_move(&worker->node, &pool->dying_workers); 2086c34056a3STejun Heo } 2087c34056a3STejun Heo 20883f959aa3SValentin Schneider /** 20893f959aa3SValentin Schneider * idle_worker_timeout - check if some idle workers can now be deleted. 20903f959aa3SValentin Schneider * @t: The pool's idle_timer that just expired 20913f959aa3SValentin Schneider * 20923f959aa3SValentin Schneider * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in 20933f959aa3SValentin Schneider * worker_leave_idle(), as a worker flicking between idle and active while its 20943f959aa3SValentin Schneider * pool is at the too_many_workers() tipping point would cause too much timer 20953f959aa3SValentin Schneider * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let 20963f959aa3SValentin Schneider * it expire and re-evaluate things from there. 20973f959aa3SValentin Schneider */ 209832a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 2099e22bee78STejun Heo { 210032a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 21013f959aa3SValentin Schneider bool do_cull = false; 21023f959aa3SValentin Schneider 21033f959aa3SValentin Schneider if (work_pending(&pool->idle_cull_work)) 21043f959aa3SValentin Schneider return; 21053f959aa3SValentin Schneider 21063f959aa3SValentin Schneider raw_spin_lock_irq(&pool->lock); 21073f959aa3SValentin Schneider 21083f959aa3SValentin Schneider if (too_many_workers(pool)) { 21093f959aa3SValentin Schneider struct worker *worker; 21103f959aa3SValentin Schneider unsigned long expires; 21113f959aa3SValentin Schneider 21123f959aa3SValentin Schneider /* idle_list is kept in LIFO order, check the last one */ 21133f959aa3SValentin Schneider worker = list_entry(pool->idle_list.prev, struct worker, entry); 21143f959aa3SValentin Schneider expires = worker->last_active + IDLE_WORKER_TIMEOUT; 21153f959aa3SValentin Schneider do_cull = !time_before(jiffies, expires); 21163f959aa3SValentin Schneider 21173f959aa3SValentin Schneider if (!do_cull) 21183f959aa3SValentin Schneider mod_timer(&pool->idle_timer, expires); 21193f959aa3SValentin Schneider } 21203f959aa3SValentin Schneider raw_spin_unlock_irq(&pool->lock); 21213f959aa3SValentin Schneider 21223f959aa3SValentin Schneider if (do_cull) 21233f959aa3SValentin Schneider queue_work(system_unbound_wq, &pool->idle_cull_work); 21243f959aa3SValentin Schneider } 21253f959aa3SValentin Schneider 21263f959aa3SValentin Schneider /** 21273f959aa3SValentin Schneider * idle_cull_fn - cull workers that have been idle for too long. 21283f959aa3SValentin Schneider * @work: the pool's work for handling these idle workers 21293f959aa3SValentin Schneider * 21303f959aa3SValentin Schneider * This goes through a pool's idle workers and gets rid of those that have been 21313f959aa3SValentin Schneider * idle for at least IDLE_WORKER_TIMEOUT seconds. 2132e02b9312SValentin Schneider * 2133e02b9312SValentin Schneider * We don't want to disturb isolated CPUs because of a pcpu kworker being 2134e02b9312SValentin Schneider * culled, so this also resets worker affinity. This requires a sleepable 2135e02b9312SValentin Schneider * context, hence the split between timer callback and work item. 21363f959aa3SValentin Schneider */ 21373f959aa3SValentin Schneider static void idle_cull_fn(struct work_struct *work) 21383f959aa3SValentin Schneider { 21393f959aa3SValentin Schneider struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); 2140e02b9312SValentin Schneider struct list_head cull_list; 2141e22bee78STejun Heo 2142e02b9312SValentin Schneider INIT_LIST_HEAD(&cull_list); 2143e02b9312SValentin Schneider /* 2144e02b9312SValentin Schneider * Grabbing wq_pool_attach_mutex here ensures an already-running worker 2145e02b9312SValentin Schneider * cannot proceed beyong worker_detach_from_pool() in its self-destruct 2146e02b9312SValentin Schneider * path. This is required as a previously-preempted worker could run after 2147e02b9312SValentin Schneider * set_worker_dying() has happened but before wake_dying_workers() did. 2148e02b9312SValentin Schneider */ 2149e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 2150a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2151e22bee78STejun Heo 21523347fc9fSLai Jiangshan while (too_many_workers(pool)) { 2153e22bee78STejun Heo struct worker *worker; 2154e22bee78STejun Heo unsigned long expires; 2155e22bee78STejun Heo 215663d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 2157e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 2158e22bee78STejun Heo 21593347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 216063d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 21613347fc9fSLai Jiangshan break; 2162e22bee78STejun Heo } 21633347fc9fSLai Jiangshan 2164e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 2165e22bee78STejun Heo } 2166e22bee78STejun Heo 2167a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2168e02b9312SValentin Schneider wake_dying_workers(&cull_list); 2169e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 2170e22bee78STejun Heo } 2171e22bee78STejun Heo 2172493a1724STejun Heo static void send_mayday(struct work_struct *work) 2173e22bee78STejun Heo { 2174112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2175112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 2176493a1724STejun Heo 21772e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 2178e22bee78STejun Heo 2179493008a8STejun Heo if (!wq->rescuer) 2180493a1724STejun Heo return; 2181e22bee78STejun Heo 2182e22bee78STejun Heo /* mayday mayday mayday */ 2183493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 218477668c8bSLai Jiangshan /* 218577668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 218677668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 218777668c8bSLai Jiangshan * rescuer is done with it. 218877668c8bSLai Jiangshan */ 218977668c8bSLai Jiangshan get_pwq(pwq); 2190493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2191e22bee78STejun Heo wake_up_process(wq->rescuer->task); 2192725e8ec5STejun Heo pwq->stats[PWQ_STAT_MAYDAY]++; 2193493a1724STejun Heo } 2194e22bee78STejun Heo } 2195e22bee78STejun Heo 219632a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 2197e22bee78STejun Heo { 219832a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 2199e22bee78STejun Heo struct work_struct *work; 2200e22bee78STejun Heo 2201a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2202a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ 2203e22bee78STejun Heo 220463d95a91STejun Heo if (need_to_create_worker(pool)) { 2205e22bee78STejun Heo /* 2206e22bee78STejun Heo * We've been trying to create a new worker but 2207e22bee78STejun Heo * haven't been successful. We might be hitting an 2208e22bee78STejun Heo * allocation deadlock. Send distress signals to 2209e22bee78STejun Heo * rescuers. 2210e22bee78STejun Heo */ 221163d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 2212e22bee78STejun Heo send_mayday(work); 2213e22bee78STejun Heo } 2214e22bee78STejun Heo 2215a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2216a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2217e22bee78STejun Heo 221863d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 2219e22bee78STejun Heo } 2220e22bee78STejun Heo 2221e22bee78STejun Heo /** 2222e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 222363d95a91STejun Heo * @pool: pool to create a new worker for 2224e22bee78STejun Heo * 222563d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 2226e22bee78STejun Heo * have at least one idle worker on return from this function. If 2227e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 222863d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 2229e22bee78STejun Heo * possible allocation deadlock. 2230e22bee78STejun Heo * 2231c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 2232c5aa87bbSTejun Heo * may_start_working() %true. 2233e22bee78STejun Heo * 2234e22bee78STejun Heo * LOCKING: 2235a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2236e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 2237e22bee78STejun Heo * manager. 2238e22bee78STejun Heo */ 223929187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 2240d565ed63STejun Heo __releases(&pool->lock) 2241d565ed63STejun Heo __acquires(&pool->lock) 2242e22bee78STejun Heo { 2243e22bee78STejun Heo restart: 2244a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 22459f9c2364STejun Heo 2246e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 224763d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 2248e22bee78STejun Heo 2249e22bee78STejun Heo while (true) { 2250051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 2251e22bee78STejun Heo break; 2252e22bee78STejun Heo 2253e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 22549f9c2364STejun Heo 225563d95a91STejun Heo if (!need_to_create_worker(pool)) 2256e22bee78STejun Heo break; 2257e22bee78STejun Heo } 2258e22bee78STejun Heo 225963d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 2260a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2261051e1850SLai Jiangshan /* 2262051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 2263051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 2264051e1850SLai Jiangshan * already become busy. 2265051e1850SLai Jiangshan */ 226663d95a91STejun Heo if (need_to_create_worker(pool)) 2267e22bee78STejun Heo goto restart; 2268e22bee78STejun Heo } 2269e22bee78STejun Heo 2270e22bee78STejun Heo /** 2271e22bee78STejun Heo * manage_workers - manage worker pool 2272e22bee78STejun Heo * @worker: self 2273e22bee78STejun Heo * 2274706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 2275e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 2276706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 2277e22bee78STejun Heo * 2278e22bee78STejun Heo * The caller can safely start processing works on false return. On 2279e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 2280e22bee78STejun Heo * and may_start_working() is true. 2281e22bee78STejun Heo * 2282e22bee78STejun Heo * CONTEXT: 2283a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2284e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 2285e22bee78STejun Heo * 2286d185af30SYacine Belkadi * Return: 228729187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 228829187a9eSTejun Heo * start processing works, %true if management function was performed and 228929187a9eSTejun Heo * the conditions that the caller verified before calling the function may 229029187a9eSTejun Heo * no longer be true. 2291e22bee78STejun Heo */ 2292e22bee78STejun Heo static bool manage_workers(struct worker *worker) 2293e22bee78STejun Heo { 229463d95a91STejun Heo struct worker_pool *pool = worker->pool; 2295e22bee78STejun Heo 2296692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 229729187a9eSTejun Heo return false; 2298692b4825STejun Heo 2299692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 23002607d7a6STejun Heo pool->manager = worker; 2301e22bee78STejun Heo 230229187a9eSTejun Heo maybe_create_worker(pool); 2303e22bee78STejun Heo 23042607d7a6STejun Heo pool->manager = NULL; 2305692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 2306d8bb65abSSebastian Andrzej Siewior rcuwait_wake_up(&manager_wait); 230729187a9eSTejun Heo return true; 2308e22bee78STejun Heo } 2309e22bee78STejun Heo 2310a62428c0STejun Heo /** 2311a62428c0STejun Heo * process_one_work - process single work 2312c34056a3STejun Heo * @worker: self 2313a62428c0STejun Heo * @work: work to process 2314a62428c0STejun Heo * 2315a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2316a62428c0STejun Heo * process a single work including synchronization against and 2317a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2318a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2319a62428c0STejun Heo * call this function to process a work. 2320a62428c0STejun Heo * 2321a62428c0STejun Heo * CONTEXT: 2322a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which is released and regrabbed. 2323a62428c0STejun Heo */ 2324c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 2325d565ed63STejun Heo __releases(&pool->lock) 2326d565ed63STejun Heo __acquires(&pool->lock) 23271da177e4SLinus Torvalds { 2328112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2329bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2330112202d9STejun Heo bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE; 2331c4560c2cSLai Jiangshan unsigned long work_data; 23327e11629dSTejun Heo struct worker *collision; 23334e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 23344e6045f1SJohannes Berg /* 2335a62428c0STejun Heo * It is permissible to free the struct work_struct from 2336a62428c0STejun Heo * inside the function that is called from it, this we need to 2337a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2338a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2339a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 23404e6045f1SJohannes Berg */ 23414d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 23424d82a1deSPeter Zijlstra 23434d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 23444e6045f1SJohannes Berg #endif 2345807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 234685327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 2347ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 234825511a47STejun Heo 23497e11629dSTejun Heo /* 23507e11629dSTejun Heo * A single work shouldn't be executed concurrently by 23517e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 23527e11629dSTejun Heo * already processing the work. If so, defer the work to the 23537e11629dSTejun Heo * currently executing one. 23547e11629dSTejun Heo */ 2355c9e7cf27STejun Heo collision = find_worker_executing_work(pool, work); 23567e11629dSTejun Heo if (unlikely(collision)) { 23577e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 23587e11629dSTejun Heo return; 23597e11629dSTejun Heo } 23601da177e4SLinus Torvalds 23618930cabaSTejun Heo /* claim and dequeue */ 23621da177e4SLinus Torvalds debug_work_deactivate(work); 2363c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 2364c34056a3STejun Heo worker->current_work = work; 2365a2c1c57bSTejun Heo worker->current_func = work->func; 2366112202d9STejun Heo worker->current_pwq = pwq; 2367c4560c2cSLai Jiangshan work_data = *work_data_bits(work); 2368d812796eSLai Jiangshan worker->current_color = get_work_color(work_data); 23697a22ad75STejun Heo 23708bf89593STejun Heo /* 23718bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 23728bf89593STejun Heo * overridden through set_worker_desc(). 23738bf89593STejun Heo */ 23748bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 23758bf89593STejun Heo 2376a62428c0STejun Heo list_del_init(&work->entry); 2377a62428c0STejun Heo 2378649027d7STejun Heo /* 2379228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 2380228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 2381228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 2382228f1d00SLai Jiangshan * execution of the pending work items. 2383fb0e7bebSTejun Heo */ 2384fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2385228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 2386fb0e7bebSTejun Heo 2387974271c4STejun Heo /* 2388a489a03eSLai Jiangshan * Wake up another worker if necessary. The condition is always 2389a489a03eSLai Jiangshan * false for normal per-cpu workers since nr_running would always 2390a489a03eSLai Jiangshan * be >= 1 at this point. This is used to chain execution of the 2391a489a03eSLai Jiangshan * pending work items for WORKER_NOT_RUNNING workers such as the 2392228f1d00SLai Jiangshan * UNBOUND and CPU_INTENSIVE ones. 2393974271c4STejun Heo */ 2394a489a03eSLai Jiangshan if (need_more_worker(pool)) 239563d95a91STejun Heo wake_up_worker(pool); 2396974271c4STejun Heo 23978930cabaSTejun Heo /* 23987c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 2399d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 240023657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 240123657bb1STejun Heo * disabled. 24028930cabaSTejun Heo */ 24037c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 24041da177e4SLinus Torvalds 2405a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2406365970a1SDavid Howells 2407a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 24083295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2409e6f3faa7SPeter Zijlstra /* 2410f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 2411f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 2412e6f3faa7SPeter Zijlstra * 2413e6f3faa7SPeter Zijlstra * However, that would result in: 2414e6f3faa7SPeter Zijlstra * 2415e6f3faa7SPeter Zijlstra * A(W1) 2416e6f3faa7SPeter Zijlstra * WFC(C) 2417e6f3faa7SPeter Zijlstra * A(W1) 2418e6f3faa7SPeter Zijlstra * C(C) 2419e6f3faa7SPeter Zijlstra * 2420e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 2421e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 2422e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 2423f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 2424e6f3faa7SPeter Zijlstra * these locks. 2425e6f3faa7SPeter Zijlstra * 2426e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 2427e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 2428e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 2429e6f3faa7SPeter Zijlstra */ 2430f52be570SPeter Zijlstra lockdep_invariant_state(true); 2431725e8ec5STejun Heo pwq->stats[PWQ_STAT_STARTED]++; 2432e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 2433a2c1c57bSTejun Heo worker->current_func(work); 2434e36c886aSArjan van de Ven /* 2435e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2436e36c886aSArjan van de Ven * point will only record its address. 2437e36c886aSArjan van de Ven */ 24381c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func); 2439725e8ec5STejun Heo pwq->stats[PWQ_STAT_COMPLETED]++; 24403295f0efSIngo Molnar lock_map_release(&lockdep_map); 2441112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 24421da177e4SLinus Torvalds 2443d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2444044c782cSValentin Ilie pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n" 2445d75f773cSSakari Ailus " last function: %ps\n", 2446a2c1c57bSTejun Heo current->comm, preempt_count(), task_pid_nr(current), 2447a2c1c57bSTejun Heo worker->current_func); 2448d5abe669SPeter Zijlstra debug_show_held_locks(current); 2449d5abe669SPeter Zijlstra dump_stack(); 2450d5abe669SPeter Zijlstra } 2451d5abe669SPeter Zijlstra 2452b22ce278STejun Heo /* 2453025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION 2454b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 2455b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 2456b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 2457789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 2458789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 2459b22ce278STejun Heo */ 2460a7e6425eSPaul E. McKenney cond_resched(); 2461b22ce278STejun Heo 2462a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2463a62428c0STejun Heo 2464fb0e7bebSTejun Heo /* clear cpu intensive status */ 2465fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2466fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2467fb0e7bebSTejun Heo 24681b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 24691b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 24701b69ac6bSJohannes Weiner 2471a62428c0STejun Heo /* we're done with it, release */ 247242f8570fSSasha Levin hash_del(&worker->hentry); 2473c34056a3STejun Heo worker->current_work = NULL; 2474a2c1c57bSTejun Heo worker->current_func = NULL; 2475112202d9STejun Heo worker->current_pwq = NULL; 2476d812796eSLai Jiangshan worker->current_color = INT_MAX; 2477c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, work_data); 24781da177e4SLinus Torvalds } 24791da177e4SLinus Torvalds 2480affee4b2STejun Heo /** 2481affee4b2STejun Heo * process_scheduled_works - process scheduled works 2482affee4b2STejun Heo * @worker: self 2483affee4b2STejun Heo * 2484affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2485affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2486affee4b2STejun Heo * fetches a work from the top and executes it. 2487affee4b2STejun Heo * 2488affee4b2STejun Heo * CONTEXT: 2489a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2490affee4b2STejun Heo * multiple times. 2491affee4b2STejun Heo */ 2492affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 24931da177e4SLinus Torvalds { 2494affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2495affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2496a62428c0STejun Heo struct work_struct, entry); 2497c34056a3STejun Heo process_one_work(worker, work); 2498a62428c0STejun Heo } 24991da177e4SLinus Torvalds } 25001da177e4SLinus Torvalds 2501197f6accSTejun Heo static void set_pf_worker(bool val) 2502197f6accSTejun Heo { 2503197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 2504197f6accSTejun Heo if (val) 2505197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 2506197f6accSTejun Heo else 2507197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 2508197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 2509197f6accSTejun Heo } 2510197f6accSTejun Heo 25114690c4abSTejun Heo /** 25124690c4abSTejun Heo * worker_thread - the worker thread function 2513c34056a3STejun Heo * @__worker: self 25144690c4abSTejun Heo * 2515c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 2516c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 2517c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 2518c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 2519c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 2520d185af30SYacine Belkadi * 2521d185af30SYacine Belkadi * Return: 0 25224690c4abSTejun Heo */ 2523c34056a3STejun Heo static int worker_thread(void *__worker) 25241da177e4SLinus Torvalds { 2525c34056a3STejun Heo struct worker *worker = __worker; 2526bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 25271da177e4SLinus Torvalds 2528e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2529197f6accSTejun Heo set_pf_worker(true); 2530c8e55f36STejun Heo woke_up: 2531a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2532affee4b2STejun Heo 2533a9ab775bSTejun Heo /* am I supposed to die? */ 2534a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 2535a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2536197f6accSTejun Heo set_pf_worker(false); 253760f5a4bcSLai Jiangshan 253860f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 2539e441b56fSZhen Lei ida_free(&pool->worker_ida, worker->id); 2540a2d812a2STejun Heo worker_detach_from_pool(worker); 2541e02b9312SValentin Schneider WARN_ON_ONCE(!list_empty(&worker->entry)); 254260f5a4bcSLai Jiangshan kfree(worker); 2543c8e55f36STejun Heo return 0; 2544c8e55f36STejun Heo } 2545c8e55f36STejun Heo 2546c8e55f36STejun Heo worker_leave_idle(worker); 2547db7bccf4STejun Heo recheck: 2548e22bee78STejun Heo /* no more worker necessary? */ 254963d95a91STejun Heo if (!need_more_worker(pool)) 2550e22bee78STejun Heo goto sleep; 2551e22bee78STejun Heo 2552e22bee78STejun Heo /* do we need to manage? */ 255363d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2554e22bee78STejun Heo goto recheck; 2555e22bee78STejun Heo 2556c8e55f36STejun Heo /* 2557c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2558c8e55f36STejun Heo * preparing to process a work or actually processing it. 2559c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2560c8e55f36STejun Heo */ 25616183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 2562c8e55f36STejun Heo 2563e22bee78STejun Heo /* 2564a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 2565a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 2566a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 2567a9ab775bSTejun Heo * management if applicable and concurrency management is restored 2568a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 2569e22bee78STejun Heo */ 2570a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 2571e22bee78STejun Heo 2572e22bee78STejun Heo do { 2573affee4b2STejun Heo struct work_struct *work = 2574bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2575affee4b2STejun Heo struct work_struct, entry); 2576affee4b2STejun Heo 257782607adcSTejun Heo pool->watchdog_ts = jiffies; 257882607adcSTejun Heo 2579c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2580affee4b2STejun Heo /* optimization path, not strictly necessary */ 2581affee4b2STejun Heo process_one_work(worker, work); 2582affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2583affee4b2STejun Heo process_scheduled_works(worker); 2584affee4b2STejun Heo } else { 2585c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2586affee4b2STejun Heo process_scheduled_works(worker); 2587affee4b2STejun Heo } 258863d95a91STejun Heo } while (keep_working(pool)); 2589affee4b2STejun Heo 2590228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 2591d313dd85STejun Heo sleep: 2592c8e55f36STejun Heo /* 2593d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 2594d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 2595d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 2596d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 2597d565ed63STejun Heo * event. 2598c8e55f36STejun Heo */ 2599c8e55f36STejun Heo worker_enter_idle(worker); 2600c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 2601a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 26021da177e4SLinus Torvalds schedule(); 2603c8e55f36STejun Heo goto woke_up; 26041da177e4SLinus Torvalds } 26051da177e4SLinus Torvalds 2606e22bee78STejun Heo /** 2607e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2608111c225aSTejun Heo * @__rescuer: self 2609e22bee78STejun Heo * 2610e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2611493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 2612e22bee78STejun Heo * 2613706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 2614e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2615e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2616e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2617e22bee78STejun Heo * the problem rescuer solves. 2618e22bee78STejun Heo * 2619706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 2620706026c2STejun Heo * workqueues which have works queued on the pool and let them process 2621e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2622e22bee78STejun Heo * 2623e22bee78STejun Heo * This should happen rarely. 2624d185af30SYacine Belkadi * 2625d185af30SYacine Belkadi * Return: 0 2626e22bee78STejun Heo */ 2627111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 2628e22bee78STejun Heo { 2629111c225aSTejun Heo struct worker *rescuer = __rescuer; 2630111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 2631e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 26324d595b86SLai Jiangshan bool should_stop; 2633e22bee78STejun Heo 2634e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2635111c225aSTejun Heo 2636111c225aSTejun Heo /* 2637111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 2638111c225aSTejun Heo * doesn't participate in concurrency management. 2639111c225aSTejun Heo */ 2640197f6accSTejun Heo set_pf_worker(true); 2641e22bee78STejun Heo repeat: 2642c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 26431da177e4SLinus Torvalds 26444d595b86SLai Jiangshan /* 26454d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 26464d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 26474d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 26484d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 26494d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 26504d595b86SLai Jiangshan * list is always empty on exit. 26514d595b86SLai Jiangshan */ 26524d595b86SLai Jiangshan should_stop = kthread_should_stop(); 26531da177e4SLinus Torvalds 2654493a1724STejun Heo /* see whether any pwq is asking for help */ 2655a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 2656493a1724STejun Heo 2657493a1724STejun Heo while (!list_empty(&wq->maydays)) { 2658493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 2659493a1724STejun Heo struct pool_workqueue, mayday_node); 2660112202d9STejun Heo struct worker_pool *pool = pwq->pool; 2661e22bee78STejun Heo struct work_struct *work, *n; 266282607adcSTejun Heo bool first = true; 2663e22bee78STejun Heo 2664e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2665493a1724STejun Heo list_del_init(&pwq->mayday_node); 2666493a1724STejun Heo 2667a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 2668e22bee78STejun Heo 266951697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 267051697d39SLai Jiangshan 2671a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2672e22bee78STejun Heo 2673e22bee78STejun Heo /* 2674e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2675e22bee78STejun Heo * process'em. 2676e22bee78STejun Heo */ 26770479c8c5STejun Heo WARN_ON_ONCE(!list_empty(scheduled)); 267882607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 267982607adcSTejun Heo if (get_work_pwq(work) == pwq) { 268082607adcSTejun Heo if (first) 268182607adcSTejun Heo pool->watchdog_ts = jiffies; 2682e22bee78STejun Heo move_linked_works(work, scheduled, &n); 2683725e8ec5STejun Heo pwq->stats[PWQ_STAT_RESCUED]++; 268482607adcSTejun Heo } 268582607adcSTejun Heo first = false; 268682607adcSTejun Heo } 2687e22bee78STejun Heo 2688008847f6SNeilBrown if (!list_empty(scheduled)) { 2689e22bee78STejun Heo process_scheduled_works(rescuer); 26907576958aSTejun Heo 26917576958aSTejun Heo /* 2692008847f6SNeilBrown * The above execution of rescued work items could 2693008847f6SNeilBrown * have created more to rescue through 2694f97a4a1aSLai Jiangshan * pwq_activate_first_inactive() or chained 2695008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 2696008847f6SNeilBrown * that such back-to-back work items, which may be 2697008847f6SNeilBrown * being used to relieve memory pressure, don't 2698008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 2699008847f6SNeilBrown */ 27004f3f4cf3SLai Jiangshan if (pwq->nr_active && need_to_create_worker(pool)) { 2701a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); 2702e66b39afSTejun Heo /* 2703e66b39afSTejun Heo * Queue iff we aren't racing destruction 2704e66b39afSTejun Heo * and somebody else hasn't queued it already. 2705e66b39afSTejun Heo */ 2706e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) { 2707008847f6SNeilBrown get_pwq(pwq); 2708e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2709e66b39afSTejun Heo } 2710a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2711008847f6SNeilBrown } 2712008847f6SNeilBrown } 2713008847f6SNeilBrown 2714008847f6SNeilBrown /* 271577668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 271613b1d625SLai Jiangshan * go away while we're still attached to it. 271777668c8bSLai Jiangshan */ 271877668c8bSLai Jiangshan put_pwq(pwq); 271977668c8bSLai Jiangshan 272077668c8bSLai Jiangshan /* 2721d8ca83e6SLai Jiangshan * Leave this pool. If need_more_worker() is %true, notify a 27227576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 27237576958aSTejun Heo * and stalling the execution. 27247576958aSTejun Heo */ 2725d8ca83e6SLai Jiangshan if (need_more_worker(pool)) 272663d95a91STejun Heo wake_up_worker(pool); 27277576958aSTejun Heo 2728a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 272913b1d625SLai Jiangshan 2730a2d812a2STejun Heo worker_detach_from_pool(rescuer); 273113b1d625SLai Jiangshan 2732a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 27331da177e4SLinus Torvalds } 27341da177e4SLinus Torvalds 2735a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 2736493a1724STejun Heo 27374d595b86SLai Jiangshan if (should_stop) { 27384d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 2739197f6accSTejun Heo set_pf_worker(false); 27404d595b86SLai Jiangshan return 0; 27414d595b86SLai Jiangshan } 27424d595b86SLai Jiangshan 2743111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 2744111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 2745e22bee78STejun Heo schedule(); 2746e22bee78STejun Heo goto repeat; 27471da177e4SLinus Torvalds } 27481da177e4SLinus Torvalds 2749fca839c0STejun Heo /** 2750fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 2751fca839c0STejun Heo * @target_wq: workqueue being flushed 2752fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 2753fca839c0STejun Heo * 2754fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 2755fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 2756fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 2757fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 2758fca839c0STejun Heo * a deadlock. 2759fca839c0STejun Heo */ 2760fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 2761fca839c0STejun Heo struct work_struct *target_work) 2762fca839c0STejun Heo { 2763fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 2764fca839c0STejun Heo struct worker *worker; 2765fca839c0STejun Heo 2766fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 2767fca839c0STejun Heo return; 2768fca839c0STejun Heo 2769fca839c0STejun Heo worker = current_wq_worker(); 2770fca839c0STejun Heo 2771fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 2772d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", 2773fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 277423d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 277523d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 2776d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", 2777fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 2778fca839c0STejun Heo target_wq->name, target_func); 2779fca839c0STejun Heo } 2780fca839c0STejun Heo 2781fc2e4d70SOleg Nesterov struct wq_barrier { 2782fc2e4d70SOleg Nesterov struct work_struct work; 2783fc2e4d70SOleg Nesterov struct completion done; 27842607d7a6STejun Heo struct task_struct *task; /* purely informational */ 2785fc2e4d70SOleg Nesterov }; 2786fc2e4d70SOleg Nesterov 2787fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2788fc2e4d70SOleg Nesterov { 2789fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2790fc2e4d70SOleg Nesterov complete(&barr->done); 2791fc2e4d70SOleg Nesterov } 2792fc2e4d70SOleg Nesterov 27934690c4abSTejun Heo /** 27944690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 2795112202d9STejun Heo * @pwq: pwq to insert barrier into 27964690c4abSTejun Heo * @barr: wq_barrier to insert 2797affee4b2STejun Heo * @target: target work to attach @barr to 2798affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 27994690c4abSTejun Heo * 2800affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2801affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2802affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2803affee4b2STejun Heo * cpu. 2804affee4b2STejun Heo * 2805affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2806affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2807affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2808affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2809affee4b2STejun Heo * after a work with LINKED flag set. 2810affee4b2STejun Heo * 2811affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2812112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 28134690c4abSTejun Heo * 28144690c4abSTejun Heo * CONTEXT: 2815a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 28164690c4abSTejun Heo */ 2817112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 2818affee4b2STejun Heo struct wq_barrier *barr, 2819affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2820fc2e4d70SOleg Nesterov { 2821d812796eSLai Jiangshan unsigned int work_flags = 0; 2822d812796eSLai Jiangshan unsigned int work_color; 2823affee4b2STejun Heo struct list_head *head; 2824affee4b2STejun Heo 2825dc186ad7SThomas Gleixner /* 2826d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 2827dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2828dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2829dc186ad7SThomas Gleixner * might deadlock. 2830dc186ad7SThomas Gleixner */ 2831ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 283222df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 283352fa5bc5SBoqun Feng 2834fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 2835fd1a5b04SByungchul Park 28362607d7a6STejun Heo barr->task = current; 283783c22520SOleg Nesterov 2838018f3a13SLai Jiangshan /* The barrier work item does not participate in pwq->nr_active. */ 2839018f3a13SLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 2840018f3a13SLai Jiangshan 2841affee4b2STejun Heo /* 2842affee4b2STejun Heo * If @target is currently being executed, schedule the 2843affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2844affee4b2STejun Heo */ 2845d812796eSLai Jiangshan if (worker) { 2846affee4b2STejun Heo head = worker->scheduled.next; 2847d812796eSLai Jiangshan work_color = worker->current_color; 2848d812796eSLai Jiangshan } else { 2849affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2850affee4b2STejun Heo 2851affee4b2STejun Heo head = target->entry.next; 2852affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2853d21cece0SLai Jiangshan work_flags |= *bits & WORK_STRUCT_LINKED; 2854d812796eSLai Jiangshan work_color = get_work_color(*bits); 2855affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2856affee4b2STejun Heo } 2857affee4b2STejun Heo 2858d812796eSLai Jiangshan pwq->nr_in_flight[work_color]++; 2859d812796eSLai Jiangshan work_flags |= work_color_to_flags(work_color); 2860d812796eSLai Jiangshan 2861dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2862d21cece0SLai Jiangshan insert_work(pwq, &barr->work, head, work_flags); 2863fc2e4d70SOleg Nesterov } 2864fc2e4d70SOleg Nesterov 286573f53c4aSTejun Heo /** 2866112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 286773f53c4aSTejun Heo * @wq: workqueue being flushed 286873f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 286973f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 287073f53c4aSTejun Heo * 2871112202d9STejun Heo * Prepare pwqs for workqueue flushing. 287273f53c4aSTejun Heo * 2873112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 2874112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 2875112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 2876112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 2877112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 287873f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 287973f53c4aSTejun Heo * 288073f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 288173f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 288273f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 288373f53c4aSTejun Heo * is returned. 288473f53c4aSTejun Heo * 2885112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 288673f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 288773f53c4aSTejun Heo * advanced to @work_color. 288873f53c4aSTejun Heo * 288973f53c4aSTejun Heo * CONTEXT: 28903c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 289173f53c4aSTejun Heo * 2892d185af30SYacine Belkadi * Return: 289373f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 289473f53c4aSTejun Heo * otherwise. 289573f53c4aSTejun Heo */ 2896112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 289773f53c4aSTejun Heo int flush_color, int work_color) 28981da177e4SLinus Torvalds { 289973f53c4aSTejun Heo bool wait = false; 290049e3cf44STejun Heo struct pool_workqueue *pwq; 29011da177e4SLinus Torvalds 290273f53c4aSTejun Heo if (flush_color >= 0) { 29036183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 2904112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 2905dc186ad7SThomas Gleixner } 290614441960SOleg Nesterov 290749e3cf44STejun Heo for_each_pwq(pwq, wq) { 2908112202d9STejun Heo struct worker_pool *pool = pwq->pool; 29091da177e4SLinus Torvalds 2910a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 291173f53c4aSTejun Heo 291273f53c4aSTejun Heo if (flush_color >= 0) { 29136183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 291473f53c4aSTejun Heo 2915112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 2916112202d9STejun Heo pwq->flush_color = flush_color; 2917112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 291873f53c4aSTejun Heo wait = true; 29191da177e4SLinus Torvalds } 292073f53c4aSTejun Heo } 292173f53c4aSTejun Heo 292273f53c4aSTejun Heo if (work_color >= 0) { 29236183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 2924112202d9STejun Heo pwq->work_color = work_color; 292573f53c4aSTejun Heo } 292673f53c4aSTejun Heo 2927a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 29281da177e4SLinus Torvalds } 29291da177e4SLinus Torvalds 2930112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 293173f53c4aSTejun Heo complete(&wq->first_flusher->done); 293273f53c4aSTejun Heo 293373f53c4aSTejun Heo return wait; 293483c22520SOleg Nesterov } 29351da177e4SLinus Torvalds 29360fcb78c2SRolf Eike Beer /** 2937c4f135d6STetsuo Handa * __flush_workqueue - ensure that any scheduled work has run to completion. 29380fcb78c2SRolf Eike Beer * @wq: workqueue to flush 29391da177e4SLinus Torvalds * 2940c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 2941c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 29421da177e4SLinus Torvalds */ 2943c4f135d6STetsuo Handa void __flush_workqueue(struct workqueue_struct *wq) 29441da177e4SLinus Torvalds { 294573f53c4aSTejun Heo struct wq_flusher this_flusher = { 294673f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 294773f53c4aSTejun Heo .flush_color = -1, 2948fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 294973f53c4aSTejun Heo }; 295073f53c4aSTejun Heo int next_color; 2951b1f4ec17SOleg Nesterov 29523347fa09STejun Heo if (WARN_ON(!wq_online)) 29533347fa09STejun Heo return; 29543347fa09STejun Heo 295587915adcSJohannes Berg lock_map_acquire(&wq->lockdep_map); 295687915adcSJohannes Berg lock_map_release(&wq->lockdep_map); 295787915adcSJohannes Berg 29583c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 295973f53c4aSTejun Heo 296073f53c4aSTejun Heo /* 296173f53c4aSTejun Heo * Start-to-wait phase 296273f53c4aSTejun Heo */ 296373f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 296473f53c4aSTejun Heo 296573f53c4aSTejun Heo if (next_color != wq->flush_color) { 296673f53c4aSTejun Heo /* 296773f53c4aSTejun Heo * Color space is not full. The current work_color 296873f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 296973f53c4aSTejun Heo * by one. 297073f53c4aSTejun Heo */ 29716183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 297273f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 297373f53c4aSTejun Heo wq->work_color = next_color; 297473f53c4aSTejun Heo 297573f53c4aSTejun Heo if (!wq->first_flusher) { 297673f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 29776183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 297873f53c4aSTejun Heo 297973f53c4aSTejun Heo wq->first_flusher = &this_flusher; 298073f53c4aSTejun Heo 2981112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 298273f53c4aSTejun Heo wq->work_color)) { 298373f53c4aSTejun Heo /* nothing to flush, done */ 298473f53c4aSTejun Heo wq->flush_color = next_color; 298573f53c4aSTejun Heo wq->first_flusher = NULL; 298673f53c4aSTejun Heo goto out_unlock; 298773f53c4aSTejun Heo } 298873f53c4aSTejun Heo } else { 298973f53c4aSTejun Heo /* wait in queue */ 29906183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 299173f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 2992112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 299373f53c4aSTejun Heo } 299473f53c4aSTejun Heo } else { 299573f53c4aSTejun Heo /* 299673f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 299773f53c4aSTejun Heo * The next flush completion will assign us 299873f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 299973f53c4aSTejun Heo */ 300073f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 300173f53c4aSTejun Heo } 300273f53c4aSTejun Heo 3003fca839c0STejun Heo check_flush_dependency(wq, NULL); 3004fca839c0STejun Heo 30053c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 300673f53c4aSTejun Heo 300773f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 300873f53c4aSTejun Heo 300973f53c4aSTejun Heo /* 301073f53c4aSTejun Heo * Wake-up-and-cascade phase 301173f53c4aSTejun Heo * 301273f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 301373f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 301473f53c4aSTejun Heo */ 301500d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher) 301673f53c4aSTejun Heo return; 301773f53c4aSTejun Heo 30183c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 301973f53c4aSTejun Heo 30204ce48b37STejun Heo /* we might have raced, check again with mutex held */ 30214ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 30224ce48b37STejun Heo goto out_unlock; 30234ce48b37STejun Heo 302400d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL); 302573f53c4aSTejun Heo 30266183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 30276183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 302873f53c4aSTejun Heo 302973f53c4aSTejun Heo while (true) { 303073f53c4aSTejun Heo struct wq_flusher *next, *tmp; 303173f53c4aSTejun Heo 303273f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 303373f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 303473f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 303573f53c4aSTejun Heo break; 303673f53c4aSTejun Heo list_del_init(&next->list); 303773f53c4aSTejun Heo complete(&next->done); 303873f53c4aSTejun Heo } 303973f53c4aSTejun Heo 30406183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 304173f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 304273f53c4aSTejun Heo 304373f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 304473f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 304573f53c4aSTejun Heo 304673f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 304773f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 304873f53c4aSTejun Heo /* 304973f53c4aSTejun Heo * Assign the same color to all overflowed 305073f53c4aSTejun Heo * flushers, advance work_color and append to 305173f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 305273f53c4aSTejun Heo * phase for these overflowed flushers. 305373f53c4aSTejun Heo */ 305473f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 305573f53c4aSTejun Heo tmp->flush_color = wq->work_color; 305673f53c4aSTejun Heo 305773f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 305873f53c4aSTejun Heo 305973f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 306073f53c4aSTejun Heo &wq->flusher_queue); 3061112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 306273f53c4aSTejun Heo } 306373f53c4aSTejun Heo 306473f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 30656183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 306673f53c4aSTejun Heo break; 306773f53c4aSTejun Heo } 306873f53c4aSTejun Heo 306973f53c4aSTejun Heo /* 307073f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 3071112202d9STejun Heo * the new first flusher and arm pwqs. 307273f53c4aSTejun Heo */ 30736183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 30746183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 307573f53c4aSTejun Heo 307673f53c4aSTejun Heo list_del_init(&next->list); 307773f53c4aSTejun Heo wq->first_flusher = next; 307873f53c4aSTejun Heo 3079112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 308073f53c4aSTejun Heo break; 308173f53c4aSTejun Heo 308273f53c4aSTejun Heo /* 308373f53c4aSTejun Heo * Meh... this color is already done, clear first 308473f53c4aSTejun Heo * flusher and repeat cascading. 308573f53c4aSTejun Heo */ 308673f53c4aSTejun Heo wq->first_flusher = NULL; 308773f53c4aSTejun Heo } 308873f53c4aSTejun Heo 308973f53c4aSTejun Heo out_unlock: 30903c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 30911da177e4SLinus Torvalds } 3092c4f135d6STetsuo Handa EXPORT_SYMBOL(__flush_workqueue); 30931da177e4SLinus Torvalds 30949c5a2ba7STejun Heo /** 30959c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 30969c5a2ba7STejun Heo * @wq: workqueue to drain 30979c5a2ba7STejun Heo * 30989c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 30999c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 31009c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 3101b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 31029c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 31039c5a2ba7STejun Heo * takes too long. 31049c5a2ba7STejun Heo */ 31059c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 31069c5a2ba7STejun Heo { 31079c5a2ba7STejun Heo unsigned int flush_cnt = 0; 310849e3cf44STejun Heo struct pool_workqueue *pwq; 31099c5a2ba7STejun Heo 31109c5a2ba7STejun Heo /* 31119c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 31129c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 3113618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 31149c5a2ba7STejun Heo */ 311587fc741eSLai Jiangshan mutex_lock(&wq->mutex); 31169c5a2ba7STejun Heo if (!wq->nr_drainers++) 3117618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 311887fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 31199c5a2ba7STejun Heo reflush: 3120c4f135d6STetsuo Handa __flush_workqueue(wq); 31219c5a2ba7STejun Heo 3122b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 312376af4d93STejun Heo 312449e3cf44STejun Heo for_each_pwq(pwq, wq) { 3125fa2563e4SThomas Tuttle bool drained; 31269c5a2ba7STejun Heo 3127a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 3128f97a4a1aSLai Jiangshan drained = !pwq->nr_active && list_empty(&pwq->inactive_works); 3129a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 3130fa2563e4SThomas Tuttle 3131fa2563e4SThomas Tuttle if (drained) 31329c5a2ba7STejun Heo continue; 31339c5a2ba7STejun Heo 31349c5a2ba7STejun Heo if (++flush_cnt == 10 || 31359c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 3136e9ad2eb3SStephen Zhang pr_warn("workqueue %s: %s() isn't complete after %u tries\n", 3137e9ad2eb3SStephen Zhang wq->name, __func__, flush_cnt); 313876af4d93STejun Heo 3139b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 31409c5a2ba7STejun Heo goto reflush; 31419c5a2ba7STejun Heo } 31429c5a2ba7STejun Heo 31439c5a2ba7STejun Heo if (!--wq->nr_drainers) 3144618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 314587fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 31469c5a2ba7STejun Heo } 31479c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 31489c5a2ba7STejun Heo 3149d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 3150d6e89786SJohannes Berg bool from_cancel) 3151baf59022STejun Heo { 3152baf59022STejun Heo struct worker *worker = NULL; 3153c9e7cf27STejun Heo struct worker_pool *pool; 3154112202d9STejun Heo struct pool_workqueue *pwq; 3155baf59022STejun Heo 3156baf59022STejun Heo might_sleep(); 3157baf59022STejun Heo 315824acfb71SThomas Gleixner rcu_read_lock(); 3159fa1b54e6STejun Heo pool = get_work_pool(work); 3160fa1b54e6STejun Heo if (!pool) { 316124acfb71SThomas Gleixner rcu_read_unlock(); 3162fa1b54e6STejun Heo return false; 3163fa1b54e6STejun Heo } 3164fa1b54e6STejun Heo 3165a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 31660b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 3167112202d9STejun Heo pwq = get_work_pwq(work); 3168112202d9STejun Heo if (pwq) { 3169112202d9STejun Heo if (unlikely(pwq->pool != pool)) 3170baf59022STejun Heo goto already_gone; 3171606a5020STejun Heo } else { 3172c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 3173baf59022STejun Heo if (!worker) 3174baf59022STejun Heo goto already_gone; 3175112202d9STejun Heo pwq = worker->current_pwq; 3176606a5020STejun Heo } 3177baf59022STejun Heo 3178fca839c0STejun Heo check_flush_dependency(pwq->wq, work); 3179fca839c0STejun Heo 3180112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 3181a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3182baf59022STejun Heo 3183e159489bSTejun Heo /* 3184a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 3185a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 3186a1d14934SPeter Zijlstra * 3187a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 3188a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 3189a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 3190a1d14934SPeter Zijlstra * forward progress. 3191e159489bSTejun Heo */ 3192d6e89786SJohannes Berg if (!from_cancel && 3193d6e89786SJohannes Berg (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)) { 3194112202d9STejun Heo lock_map_acquire(&pwq->wq->lockdep_map); 3195112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 3196a1d14934SPeter Zijlstra } 319724acfb71SThomas Gleixner rcu_read_unlock(); 3198baf59022STejun Heo return true; 3199baf59022STejun Heo already_gone: 3200a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 320124acfb71SThomas Gleixner rcu_read_unlock(); 3202baf59022STejun Heo return false; 3203baf59022STejun Heo } 3204baf59022STejun Heo 3205d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 3206d6e89786SJohannes Berg { 3207d6e89786SJohannes Berg struct wq_barrier barr; 3208d6e89786SJohannes Berg 3209d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 3210d6e89786SJohannes Berg return false; 3211d6e89786SJohannes Berg 32124d43d395STetsuo Handa if (WARN_ON(!work->func)) 32134d43d395STetsuo Handa return false; 32144d43d395STetsuo Handa 321587915adcSJohannes Berg lock_map_acquire(&work->lockdep_map); 321687915adcSJohannes Berg lock_map_release(&work->lockdep_map); 321787915adcSJohannes Berg 3218d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 3219d6e89786SJohannes Berg wait_for_completion(&barr.done); 3220d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 3221d6e89786SJohannes Berg return true; 3222d6e89786SJohannes Berg } else { 3223d6e89786SJohannes Berg return false; 3224d6e89786SJohannes Berg } 3225d6e89786SJohannes Berg } 3226d6e89786SJohannes Berg 3227db700897SOleg Nesterov /** 3228401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 3229401a8d04STejun Heo * @work: the work to flush 3230db700897SOleg Nesterov * 3231606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 3232606a5020STejun Heo * on return if it hasn't been requeued since flush started. 3233401a8d04STejun Heo * 3234d185af30SYacine Belkadi * Return: 3235401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3236401a8d04STejun Heo * %false if it was already idle. 3237db700897SOleg Nesterov */ 3238401a8d04STejun Heo bool flush_work(struct work_struct *work) 3239db700897SOleg Nesterov { 3240d6e89786SJohannes Berg return __flush_work(work, false); 3241606a5020STejun Heo } 3242db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 3243db700897SOleg Nesterov 32448603e1b3STejun Heo struct cwt_wait { 3245ac6424b9SIngo Molnar wait_queue_entry_t wait; 32468603e1b3STejun Heo struct work_struct *work; 32478603e1b3STejun Heo }; 32488603e1b3STejun Heo 3249ac6424b9SIngo Molnar static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 32508603e1b3STejun Heo { 32518603e1b3STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 32528603e1b3STejun Heo 32538603e1b3STejun Heo if (cwait->work != key) 32548603e1b3STejun Heo return 0; 32558603e1b3STejun Heo return autoremove_wake_function(wait, mode, sync, key); 32568603e1b3STejun Heo } 32578603e1b3STejun Heo 325836e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 3259401a8d04STejun Heo { 32608603e1b3STejun Heo static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); 3261bbb68dfaSTejun Heo unsigned long flags; 32621f1f642eSOleg Nesterov int ret; 32631f1f642eSOleg Nesterov 32641f1f642eSOleg Nesterov do { 3265bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 3266bbb68dfaSTejun Heo /* 32678603e1b3STejun Heo * If someone else is already canceling, wait for it to 32688603e1b3STejun Heo * finish. flush_work() doesn't work for PREEMPT_NONE 32698603e1b3STejun Heo * because we may get scheduled between @work's completion 32708603e1b3STejun Heo * and the other canceling task resuming and clearing 32718603e1b3STejun Heo * CANCELING - flush_work() will return false immediately 32728603e1b3STejun Heo * as @work is no longer busy, try_to_grab_pending() will 32738603e1b3STejun Heo * return -ENOENT as @work is still being canceled and the 32748603e1b3STejun Heo * other canceling task won't be able to clear CANCELING as 32758603e1b3STejun Heo * we're hogging the CPU. 32768603e1b3STejun Heo * 32778603e1b3STejun Heo * Let's wait for completion using a waitqueue. As this 32788603e1b3STejun Heo * may lead to the thundering herd problem, use a custom 32798603e1b3STejun Heo * wake function which matches @work along with exclusive 32808603e1b3STejun Heo * wait and wakeup. 3281bbb68dfaSTejun Heo */ 32828603e1b3STejun Heo if (unlikely(ret == -ENOENT)) { 32838603e1b3STejun Heo struct cwt_wait cwait; 32848603e1b3STejun Heo 32858603e1b3STejun Heo init_wait(&cwait.wait); 32868603e1b3STejun Heo cwait.wait.func = cwt_wakefn; 32878603e1b3STejun Heo cwait.work = work; 32888603e1b3STejun Heo 32898603e1b3STejun Heo prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, 32908603e1b3STejun Heo TASK_UNINTERRUPTIBLE); 32918603e1b3STejun Heo if (work_is_canceling(work)) 32928603e1b3STejun Heo schedule(); 32938603e1b3STejun Heo finish_wait(&cancel_waitq, &cwait.wait); 32948603e1b3STejun Heo } 32951f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 32961f1f642eSOleg Nesterov 3297bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 3298bbb68dfaSTejun Heo mark_work_canceling(work); 3299bbb68dfaSTejun Heo local_irq_restore(flags); 3300bbb68dfaSTejun Heo 33013347fa09STejun Heo /* 33023347fa09STejun Heo * This allows canceling during early boot. We know that @work 33033347fa09STejun Heo * isn't executing. 33043347fa09STejun Heo */ 33053347fa09STejun Heo if (wq_online) 3306d6e89786SJohannes Berg __flush_work(work, true); 33073347fa09STejun Heo 33087a22ad75STejun Heo clear_work_data(work); 33098603e1b3STejun Heo 33108603e1b3STejun Heo /* 33118603e1b3STejun Heo * Paired with prepare_to_wait() above so that either 33128603e1b3STejun Heo * waitqueue_active() is visible here or !work_is_canceling() is 33138603e1b3STejun Heo * visible there. 33148603e1b3STejun Heo */ 33158603e1b3STejun Heo smp_mb(); 33168603e1b3STejun Heo if (waitqueue_active(&cancel_waitq)) 33178603e1b3STejun Heo __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); 33188603e1b3STejun Heo 33191f1f642eSOleg Nesterov return ret; 33201f1f642eSOleg Nesterov } 33211f1f642eSOleg Nesterov 33226e84d644SOleg Nesterov /** 3323401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 3324401a8d04STejun Heo * @work: the work to cancel 33256e84d644SOleg Nesterov * 3326401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 3327401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 3328401a8d04STejun Heo * another workqueue. On return from this function, @work is 3329401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 33301f1f642eSOleg Nesterov * 3331401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 3332401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 33336e84d644SOleg Nesterov * 3334401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 33356e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 3336401a8d04STejun Heo * 3337d185af30SYacine Belkadi * Return: 3338401a8d04STejun Heo * %true if @work was pending, %false otherwise. 33396e84d644SOleg Nesterov */ 3340401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 33416e84d644SOleg Nesterov { 334236e227d2STejun Heo return __cancel_work_timer(work, false); 3343b89deed3SOleg Nesterov } 334428e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 3345b89deed3SOleg Nesterov 33466e84d644SOleg Nesterov /** 3347401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 3348401a8d04STejun Heo * @dwork: the delayed work to flush 33496e84d644SOleg Nesterov * 3350401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 3351401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 3352401a8d04STejun Heo * considers the last queueing instance of @dwork. 33531f1f642eSOleg Nesterov * 3354d185af30SYacine Belkadi * Return: 3355401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3356401a8d04STejun Heo * %false if it was already idle. 33576e84d644SOleg Nesterov */ 3358401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 3359401a8d04STejun Heo { 33608930cabaSTejun Heo local_irq_disable(); 3361401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 336260c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 33638930cabaSTejun Heo local_irq_enable(); 3364401a8d04STejun Heo return flush_work(&dwork->work); 3365401a8d04STejun Heo } 3366401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 3367401a8d04STejun Heo 336805f0fe6bSTejun Heo /** 336905f0fe6bSTejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 337005f0fe6bSTejun Heo * @rwork: the rcu work to flush 337105f0fe6bSTejun Heo * 337205f0fe6bSTejun Heo * Return: 337305f0fe6bSTejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 337405f0fe6bSTejun Heo * %false if it was already idle. 337505f0fe6bSTejun Heo */ 337605f0fe6bSTejun Heo bool flush_rcu_work(struct rcu_work *rwork) 337705f0fe6bSTejun Heo { 337805f0fe6bSTejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 337905f0fe6bSTejun Heo rcu_barrier(); 338005f0fe6bSTejun Heo flush_work(&rwork->work); 338105f0fe6bSTejun Heo return true; 338205f0fe6bSTejun Heo } else { 338305f0fe6bSTejun Heo return flush_work(&rwork->work); 338405f0fe6bSTejun Heo } 338505f0fe6bSTejun Heo } 338605f0fe6bSTejun Heo EXPORT_SYMBOL(flush_rcu_work); 338705f0fe6bSTejun Heo 3388f72b8792SJens Axboe static bool __cancel_work(struct work_struct *work, bool is_dwork) 3389f72b8792SJens Axboe { 3390f72b8792SJens Axboe unsigned long flags; 3391f72b8792SJens Axboe int ret; 3392f72b8792SJens Axboe 3393f72b8792SJens Axboe do { 3394f72b8792SJens Axboe ret = try_to_grab_pending(work, is_dwork, &flags); 3395f72b8792SJens Axboe } while (unlikely(ret == -EAGAIN)); 3396f72b8792SJens Axboe 3397f72b8792SJens Axboe if (unlikely(ret < 0)) 3398f72b8792SJens Axboe return false; 3399f72b8792SJens Axboe 3400f72b8792SJens Axboe set_work_pool_and_clear_pending(work, get_work_pool_id(work)); 3401f72b8792SJens Axboe local_irq_restore(flags); 3402f72b8792SJens Axboe return ret; 3403f72b8792SJens Axboe } 3404f72b8792SJens Axboe 340573b4b532SAndrey Grodzovsky /* 340673b4b532SAndrey Grodzovsky * See cancel_delayed_work() 340773b4b532SAndrey Grodzovsky */ 340873b4b532SAndrey Grodzovsky bool cancel_work(struct work_struct *work) 340973b4b532SAndrey Grodzovsky { 341073b4b532SAndrey Grodzovsky return __cancel_work(work, false); 341173b4b532SAndrey Grodzovsky } 341273b4b532SAndrey Grodzovsky EXPORT_SYMBOL(cancel_work); 341373b4b532SAndrey Grodzovsky 3414401a8d04STejun Heo /** 341557b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 341657b30ae7STejun Heo * @dwork: delayed_work to cancel 341709383498STejun Heo * 3418d185af30SYacine Belkadi * Kill off a pending delayed_work. 3419d185af30SYacine Belkadi * 3420d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 3421d185af30SYacine Belkadi * pending. 3422d185af30SYacine Belkadi * 3423d185af30SYacine Belkadi * Note: 3424d185af30SYacine Belkadi * The work callback function may still be running on return, unless 3425d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 3426d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 342709383498STejun Heo * 342857b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 342909383498STejun Heo */ 343057b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 343109383498STejun Heo { 3432f72b8792SJens Axboe return __cancel_work(&dwork->work, true); 343309383498STejun Heo } 343457b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 343509383498STejun Heo 343609383498STejun Heo /** 3437401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 3438401a8d04STejun Heo * @dwork: the delayed work cancel 3439401a8d04STejun Heo * 3440401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 3441401a8d04STejun Heo * 3442d185af30SYacine Belkadi * Return: 3443401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 3444401a8d04STejun Heo */ 3445401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 34466e84d644SOleg Nesterov { 344736e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 34486e84d644SOleg Nesterov } 3449f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 34501da177e4SLinus Torvalds 34510fcb78c2SRolf Eike Beer /** 345231ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 3453b6136773SAndrew Morton * @func: the function to call 3454b6136773SAndrew Morton * 345531ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 345631ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 3457b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 345831ddd871STejun Heo * 3459d185af30SYacine Belkadi * Return: 346031ddd871STejun Heo * 0 on success, -errno on failure. 3461b6136773SAndrew Morton */ 346265f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 346315316ba8SChristoph Lameter { 346415316ba8SChristoph Lameter int cpu; 346538f51568SNamhyung Kim struct work_struct __percpu *works; 346615316ba8SChristoph Lameter 3467b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 3468b6136773SAndrew Morton if (!works) 346915316ba8SChristoph Lameter return -ENOMEM; 3470b6136773SAndrew Morton 3471ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 347293981800STejun Heo 347315316ba8SChristoph Lameter for_each_online_cpu(cpu) { 34749bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 34759bfb1839SIngo Molnar 34769bfb1839SIngo Molnar INIT_WORK(work, func); 34778de6d308SOleg Nesterov schedule_work_on(cpu, work); 347815316ba8SChristoph Lameter } 347993981800STejun Heo 348093981800STejun Heo for_each_online_cpu(cpu) 34818616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 348293981800STejun Heo 3483ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 3484b6136773SAndrew Morton free_percpu(works); 348515316ba8SChristoph Lameter return 0; 348615316ba8SChristoph Lameter } 348715316ba8SChristoph Lameter 3488eef6a7d5SAlan Stern /** 34891fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 34901fa44ecaSJames Bottomley * @fn: the function to execute 34911fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 34921fa44ecaSJames Bottomley * be available when the work executes) 34931fa44ecaSJames Bottomley * 34941fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 34951fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 34961fa44ecaSJames Bottomley * 3497d185af30SYacine Belkadi * Return: 0 - function was executed 34981fa44ecaSJames Bottomley * 1 - function was scheduled for execution 34991fa44ecaSJames Bottomley */ 350065f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 35011fa44ecaSJames Bottomley { 35021fa44ecaSJames Bottomley if (!in_interrupt()) { 350365f27f38SDavid Howells fn(&ew->work); 35041fa44ecaSJames Bottomley return 0; 35051fa44ecaSJames Bottomley } 35061fa44ecaSJames Bottomley 350765f27f38SDavid Howells INIT_WORK(&ew->work, fn); 35081fa44ecaSJames Bottomley schedule_work(&ew->work); 35091fa44ecaSJames Bottomley 35101fa44ecaSJames Bottomley return 1; 35111fa44ecaSJames Bottomley } 35121fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 35131fa44ecaSJames Bottomley 35147a4e344cSTejun Heo /** 35157a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 35167a4e344cSTejun Heo * @attrs: workqueue_attrs to free 35177a4e344cSTejun Heo * 35187a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 35197a4e344cSTejun Heo */ 3520513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs) 35217a4e344cSTejun Heo { 35227a4e344cSTejun Heo if (attrs) { 35237a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 35247a4e344cSTejun Heo kfree(attrs); 35257a4e344cSTejun Heo } 35267a4e344cSTejun Heo } 35277a4e344cSTejun Heo 35287a4e344cSTejun Heo /** 35297a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 35307a4e344cSTejun Heo * 35317a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 3532d185af30SYacine Belkadi * return it. 3533d185af30SYacine Belkadi * 3534d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 35357a4e344cSTejun Heo */ 3536513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void) 35377a4e344cSTejun Heo { 35387a4e344cSTejun Heo struct workqueue_attrs *attrs; 35397a4e344cSTejun Heo 3540be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 35417a4e344cSTejun Heo if (!attrs) 35427a4e344cSTejun Heo goto fail; 3543be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) 35447a4e344cSTejun Heo goto fail; 35457a4e344cSTejun Heo 354613e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 35477a4e344cSTejun Heo return attrs; 35487a4e344cSTejun Heo fail: 35497a4e344cSTejun Heo free_workqueue_attrs(attrs); 35507a4e344cSTejun Heo return NULL; 35517a4e344cSTejun Heo } 35527a4e344cSTejun Heo 355329c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 355429c91e99STejun Heo const struct workqueue_attrs *from) 355529c91e99STejun Heo { 355629c91e99STejun Heo to->nice = from->nice; 355729c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 35582865a8fbSShaohua Li /* 35592865a8fbSShaohua Li * Unlike hash and equality test, this function doesn't ignore 35602865a8fbSShaohua Li * ->no_numa as it is used for both pool and wq attrs. Instead, 35612865a8fbSShaohua Li * get_unbound_pool() explicitly clears ->no_numa after copying. 35622865a8fbSShaohua Li */ 35632865a8fbSShaohua Li to->no_numa = from->no_numa; 356429c91e99STejun Heo } 356529c91e99STejun Heo 356629c91e99STejun Heo /* hash value of the content of @attr */ 356729c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 356829c91e99STejun Heo { 356929c91e99STejun Heo u32 hash = 0; 357029c91e99STejun Heo 357129c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 357213e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 357313e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 357429c91e99STejun Heo return hash; 357529c91e99STejun Heo } 357629c91e99STejun Heo 357729c91e99STejun Heo /* content equality test */ 357829c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 357929c91e99STejun Heo const struct workqueue_attrs *b) 358029c91e99STejun Heo { 358129c91e99STejun Heo if (a->nice != b->nice) 358229c91e99STejun Heo return false; 358329c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 358429c91e99STejun Heo return false; 358529c91e99STejun Heo return true; 358629c91e99STejun Heo } 358729c91e99STejun Heo 35887a4e344cSTejun Heo /** 35897a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 35907a4e344cSTejun Heo * @pool: worker_pool to initialize 35917a4e344cSTejun Heo * 3592402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 3593d185af30SYacine Belkadi * 3594d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 359529c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 359629c91e99STejun Heo * on @pool safely to release it. 35977a4e344cSTejun Heo */ 35987a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 35994e1a1f9aSTejun Heo { 3600a9b8a985SSebastian Andrzej Siewior raw_spin_lock_init(&pool->lock); 360129c91e99STejun Heo pool->id = -1; 360229c91e99STejun Heo pool->cpu = -1; 3603f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 36044e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 360582607adcSTejun Heo pool->watchdog_ts = jiffies; 36064e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 36074e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 36084e1a1f9aSTejun Heo hash_init(pool->busy_hash); 36094e1a1f9aSTejun Heo 361032a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 36113f959aa3SValentin Schneider INIT_WORK(&pool->idle_cull_work, idle_cull_fn); 36124e1a1f9aSTejun Heo 361332a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 36144e1a1f9aSTejun Heo 3615da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 3616e02b9312SValentin Schneider INIT_LIST_HEAD(&pool->dying_workers); 36177a4e344cSTejun Heo 36187cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 361929c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 362029c91e99STejun Heo pool->refcnt = 1; 362129c91e99STejun Heo 362229c91e99STejun Heo /* shouldn't fail above this point */ 3623be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs(); 36247a4e344cSTejun Heo if (!pool->attrs) 36257a4e344cSTejun Heo return -ENOMEM; 36267a4e344cSTejun Heo return 0; 36274e1a1f9aSTejun Heo } 36284e1a1f9aSTejun Heo 3629669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP 3630669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 3631669de8bdSBart Van Assche { 3632669de8bdSBart Van Assche char *lock_name; 3633669de8bdSBart Van Assche 3634669de8bdSBart Van Assche lockdep_register_key(&wq->key); 3635669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); 3636669de8bdSBart Van Assche if (!lock_name) 3637669de8bdSBart Van Assche lock_name = wq->name; 363869a106c0SQian Cai 363969a106c0SQian Cai wq->lock_name = lock_name; 3640669de8bdSBart Van Assche lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); 3641669de8bdSBart Van Assche } 3642669de8bdSBart Van Assche 3643669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 3644669de8bdSBart Van Assche { 3645669de8bdSBart Van Assche lockdep_unregister_key(&wq->key); 3646669de8bdSBart Van Assche } 3647669de8bdSBart Van Assche 3648669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 3649669de8bdSBart Van Assche { 3650669de8bdSBart Van Assche if (wq->lock_name != wq->name) 3651669de8bdSBart Van Assche kfree(wq->lock_name); 3652669de8bdSBart Van Assche } 3653669de8bdSBart Van Assche #else 3654669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 3655669de8bdSBart Van Assche { 3656669de8bdSBart Van Assche } 3657669de8bdSBart Van Assche 3658669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 3659669de8bdSBart Van Assche { 3660669de8bdSBart Van Assche } 3661669de8bdSBart Van Assche 3662669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 3663669de8bdSBart Van Assche { 3664669de8bdSBart Van Assche } 3665669de8bdSBart Van Assche #endif 3666669de8bdSBart Van Assche 3667e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 3668e2dca7adSTejun Heo { 3669e2dca7adSTejun Heo struct workqueue_struct *wq = 3670e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 3671e2dca7adSTejun Heo 3672669de8bdSBart Van Assche wq_free_lockdep(wq); 3673669de8bdSBart Van Assche 3674e2dca7adSTejun Heo if (!(wq->flags & WQ_UNBOUND)) 3675e2dca7adSTejun Heo free_percpu(wq->cpu_pwqs); 3676e2dca7adSTejun Heo else 3677e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 3678e2dca7adSTejun Heo 3679e2dca7adSTejun Heo kfree(wq); 3680e2dca7adSTejun Heo } 3681e2dca7adSTejun Heo 368229c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 368329c91e99STejun Heo { 368429c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 368529c91e99STejun Heo 36867cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 368729c91e99STejun Heo free_workqueue_attrs(pool->attrs); 368829c91e99STejun Heo kfree(pool); 368929c91e99STejun Heo } 369029c91e99STejun Heo 369129c91e99STejun Heo /** 369229c91e99STejun Heo * put_unbound_pool - put a worker_pool 369329c91e99STejun Heo * @pool: worker_pool to put 369429c91e99STejun Heo * 369524acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU 3696c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 3697c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 3698c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 3699a892caccSTejun Heo * 3700a892caccSTejun Heo * Should be called with wq_pool_mutex held. 370129c91e99STejun Heo */ 370229c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 370329c91e99STejun Heo { 370460f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 3705e02b9312SValentin Schneider struct list_head cull_list; 370629c91e99STejun Heo struct worker *worker; 370729c91e99STejun Heo 3708e02b9312SValentin Schneider INIT_LIST_HEAD(&cull_list); 3709e02b9312SValentin Schneider 3710a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3711a892caccSTejun Heo 3712a892caccSTejun Heo if (--pool->refcnt) 371329c91e99STejun Heo return; 371429c91e99STejun Heo 371529c91e99STejun Heo /* sanity checks */ 371661d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 3717a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 371829c91e99STejun Heo return; 371929c91e99STejun Heo 372029c91e99STejun Heo /* release id and unhash */ 372129c91e99STejun Heo if (pool->id >= 0) 372229c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 372329c91e99STejun Heo hash_del(&pool->hash_node); 372429c91e99STejun Heo 3725c5aa87bbSTejun Heo /* 3726692b4825STejun Heo * Become the manager and destroy all workers. This prevents 3727692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 3728692b4825STejun Heo * manager and @pool gets freed with the flag set. 37299ab03be4SValentin Schneider * 37309ab03be4SValentin Schneider * Having a concurrent manager is quite unlikely to happen as we can 37319ab03be4SValentin Schneider * only get here with 37329ab03be4SValentin Schneider * pwq->refcnt == pool->refcnt == 0 37339ab03be4SValentin Schneider * which implies no work queued to the pool, which implies no worker can 37349ab03be4SValentin Schneider * become the manager. However a worker could have taken the role of 37359ab03be4SValentin Schneider * manager before the refcnts dropped to 0, since maybe_create_worker() 37369ab03be4SValentin Schneider * drops pool->lock 3737c5aa87bbSTejun Heo */ 37389ab03be4SValentin Schneider while (true) { 37399ab03be4SValentin Schneider rcuwait_wait_event(&manager_wait, 37409ab03be4SValentin Schneider !(pool->flags & POOL_MANAGER_ACTIVE), 3741d8bb65abSSebastian Andrzej Siewior TASK_UNINTERRUPTIBLE); 3742e02b9312SValentin Schneider 3743e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 37449ab03be4SValentin Schneider raw_spin_lock_irq(&pool->lock); 37459ab03be4SValentin Schneider if (!(pool->flags & POOL_MANAGER_ACTIVE)) { 3746692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 37479ab03be4SValentin Schneider break; 37489ab03be4SValentin Schneider } 37499ab03be4SValentin Schneider raw_spin_unlock_irq(&pool->lock); 3750e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 37519ab03be4SValentin Schneider } 3752692b4825STejun Heo 37531037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 3754e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 375529c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 3756a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 375760f5a4bcSLai Jiangshan 3758e02b9312SValentin Schneider wake_dying_workers(&cull_list); 3759e02b9312SValentin Schneider 3760e02b9312SValentin Schneider if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) 376160f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 37621258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 376360f5a4bcSLai Jiangshan 376460f5a4bcSLai Jiangshan if (pool->detach_completion) 376560f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 376660f5a4bcSLai Jiangshan 376729c91e99STejun Heo /* shut down the timers */ 376829c91e99STejun Heo del_timer_sync(&pool->idle_timer); 37693f959aa3SValentin Schneider cancel_work_sync(&pool->idle_cull_work); 377029c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 377129c91e99STejun Heo 377224acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */ 377325b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 377429c91e99STejun Heo } 377529c91e99STejun Heo 377629c91e99STejun Heo /** 377729c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 377829c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 377929c91e99STejun Heo * 378029c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 378129c91e99STejun Heo * reference count and return it. If there already is a matching 378229c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 3783d185af30SYacine Belkadi * create a new one. 3784a892caccSTejun Heo * 3785a892caccSTejun Heo * Should be called with wq_pool_mutex held. 3786d185af30SYacine Belkadi * 3787d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 3788d185af30SYacine Belkadi * On failure, %NULL. 378929c91e99STejun Heo */ 379029c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 379129c91e99STejun Heo { 379229c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 379329c91e99STejun Heo struct worker_pool *pool; 3794f3f90ad4STejun Heo int node; 3795e2273584SXunlei Pang int target_node = NUMA_NO_NODE; 379629c91e99STejun Heo 3797a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 379829c91e99STejun Heo 379929c91e99STejun Heo /* do we already have a matching pool? */ 380029c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 380129c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 380229c91e99STejun Heo pool->refcnt++; 38033fb1823cSLai Jiangshan return pool; 380429c91e99STejun Heo } 380529c91e99STejun Heo } 380629c91e99STejun Heo 3807e2273584SXunlei Pang /* if cpumask is contained inside a NUMA node, we belong to that node */ 3808e2273584SXunlei Pang if (wq_numa_enabled) { 3809e2273584SXunlei Pang for_each_node(node) { 3810e2273584SXunlei Pang if (cpumask_subset(attrs->cpumask, 3811e2273584SXunlei Pang wq_numa_possible_cpumask[node])) { 3812e2273584SXunlei Pang target_node = node; 3813e2273584SXunlei Pang break; 3814e2273584SXunlei Pang } 3815e2273584SXunlei Pang } 3816e2273584SXunlei Pang } 3817e2273584SXunlei Pang 381829c91e99STejun Heo /* nope, create a new one */ 3819e2273584SXunlei Pang pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, target_node); 382029c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 382129c91e99STejun Heo goto fail; 382229c91e99STejun Heo 38238864b4e5STejun Heo lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */ 382429c91e99STejun Heo copy_workqueue_attrs(pool->attrs, attrs); 3825e2273584SXunlei Pang pool->node = target_node; 382629c91e99STejun Heo 38272865a8fbSShaohua Li /* 38282865a8fbSShaohua Li * no_numa isn't a worker_pool attribute, always clear it. See 38292865a8fbSShaohua Li * 'struct workqueue_attrs' comments for detail. 38302865a8fbSShaohua Li */ 38312865a8fbSShaohua Li pool->attrs->no_numa = false; 38322865a8fbSShaohua Li 383329c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 383429c91e99STejun Heo goto fail; 383529c91e99STejun Heo 383629c91e99STejun Heo /* create and start the initial worker */ 38373347fa09STejun Heo if (wq_online && !create_worker(pool)) 383829c91e99STejun Heo goto fail; 383929c91e99STejun Heo 384029c91e99STejun Heo /* install */ 384129c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 38423fb1823cSLai Jiangshan 384329c91e99STejun Heo return pool; 384429c91e99STejun Heo fail: 384529c91e99STejun Heo if (pool) 384629c91e99STejun Heo put_unbound_pool(pool); 384729c91e99STejun Heo return NULL; 384829c91e99STejun Heo } 384929c91e99STejun Heo 38508864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 38518864b4e5STejun Heo { 38528864b4e5STejun Heo kmem_cache_free(pwq_cache, 38538864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 38548864b4e5STejun Heo } 38558864b4e5STejun Heo 38568864b4e5STejun Heo /* 38578864b4e5STejun Heo * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt 38588864b4e5STejun Heo * and needs to be destroyed. 38598864b4e5STejun Heo */ 38608864b4e5STejun Heo static void pwq_unbound_release_workfn(struct work_struct *work) 38618864b4e5STejun Heo { 38628864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 38638864b4e5STejun Heo unbound_release_work); 38648864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 38658864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 3866b42b0bddSYang Yingliang bool is_last = false; 38678864b4e5STejun Heo 3868b42b0bddSYang Yingliang /* 3869b42b0bddSYang Yingliang * when @pwq is not linked, it doesn't hold any reference to the 3870b42b0bddSYang Yingliang * @wq, and @wq is invalid to access. 3871b42b0bddSYang Yingliang */ 3872b42b0bddSYang Yingliang if (!list_empty(&pwq->pwqs_node)) { 38738864b4e5STejun Heo if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND))) 38748864b4e5STejun Heo return; 38758864b4e5STejun Heo 38763c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 38778864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 3878bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 38793c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 3880b42b0bddSYang Yingliang } 38818864b4e5STejun Heo 3882a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 38838864b4e5STejun Heo put_unbound_pool(pool); 3884a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 3885a892caccSTejun Heo 388625b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 38878864b4e5STejun Heo 38888864b4e5STejun Heo /* 38898864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 3890e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 38918864b4e5STejun Heo */ 3892669de8bdSBart Van Assche if (is_last) { 3893669de8bdSBart Van Assche wq_unregister_lockdep(wq); 389425b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 38956029a918STejun Heo } 3896669de8bdSBart Van Assche } 38978864b4e5STejun Heo 38980fbd95aaSTejun Heo /** 3899699ce097STejun Heo * pwq_adjust_max_active - update a pwq's max_active to the current setting 39000fbd95aaSTejun Heo * @pwq: target pool_workqueue 39010fbd95aaSTejun Heo * 3902699ce097STejun Heo * If @pwq isn't freezing, set @pwq->max_active to the associated 3903f97a4a1aSLai Jiangshan * workqueue's saved_max_active and activate inactive work items 3904699ce097STejun Heo * accordingly. If @pwq is freezing, clear @pwq->max_active to zero. 39050fbd95aaSTejun Heo */ 3906699ce097STejun Heo static void pwq_adjust_max_active(struct pool_workqueue *pwq) 39070fbd95aaSTejun Heo { 3908699ce097STejun Heo struct workqueue_struct *wq = pwq->wq; 3909699ce097STejun Heo bool freezable = wq->flags & WQ_FREEZABLE; 39103347fa09STejun Heo unsigned long flags; 3911699ce097STejun Heo 3912699ce097STejun Heo /* for @wq->saved_max_active */ 3913a357fc03SLai Jiangshan lockdep_assert_held(&wq->mutex); 3914699ce097STejun Heo 3915699ce097STejun Heo /* fast exit for non-freezable wqs */ 3916699ce097STejun Heo if (!freezable && pwq->max_active == wq->saved_max_active) 3917699ce097STejun Heo return; 3918699ce097STejun Heo 39193347fa09STejun Heo /* this function can be called during early boot w/ irq disabled */ 3920a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pwq->pool->lock, flags); 3921699ce097STejun Heo 392274b414eaSLai Jiangshan /* 392374b414eaSLai Jiangshan * During [un]freezing, the caller is responsible for ensuring that 392474b414eaSLai Jiangshan * this function is called at least once after @workqueue_freezing 392574b414eaSLai Jiangshan * is updated and visible. 392674b414eaSLai Jiangshan */ 392774b414eaSLai Jiangshan if (!freezable || !workqueue_freezing) { 392801341fbdSYunfeng Ye bool kick = false; 392901341fbdSYunfeng Ye 3930699ce097STejun Heo pwq->max_active = wq->saved_max_active; 39310fbd95aaSTejun Heo 3932f97a4a1aSLai Jiangshan while (!list_empty(&pwq->inactive_works) && 393301341fbdSYunfeng Ye pwq->nr_active < pwq->max_active) { 3934f97a4a1aSLai Jiangshan pwq_activate_first_inactive(pwq); 393501341fbdSYunfeng Ye kick = true; 393601341fbdSYunfeng Ye } 3937951a078aSLai Jiangshan 3938951a078aSLai Jiangshan /* 3939951a078aSLai Jiangshan * Need to kick a worker after thawed or an unbound wq's 394001341fbdSYunfeng Ye * max_active is bumped. In realtime scenarios, always kicking a 394101341fbdSYunfeng Ye * worker will cause interference on the isolated cpu cores, so 394201341fbdSYunfeng Ye * let's kick iff work items were activated. 3943951a078aSLai Jiangshan */ 394401341fbdSYunfeng Ye if (kick) 3945951a078aSLai Jiangshan wake_up_worker(pwq->pool); 3946699ce097STejun Heo } else { 3947699ce097STejun Heo pwq->max_active = 0; 3948699ce097STejun Heo } 3949699ce097STejun Heo 3950a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 39510fbd95aaSTejun Heo } 39520fbd95aaSTejun Heo 395367dc8325SCai Huoqing /* initialize newly allocated @pwq which is associated with @wq and @pool */ 3954f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 3955f147f29eSTejun Heo struct worker_pool *pool) 3956d2c1d404STejun Heo { 3957d2c1d404STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 3958d2c1d404STejun Heo 3959e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 3960e50aba9aSTejun Heo 3961d2c1d404STejun Heo pwq->pool = pool; 3962d2c1d404STejun Heo pwq->wq = wq; 3963d2c1d404STejun Heo pwq->flush_color = -1; 39648864b4e5STejun Heo pwq->refcnt = 1; 3965f97a4a1aSLai Jiangshan INIT_LIST_HEAD(&pwq->inactive_works); 39661befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 3967d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 39688864b4e5STejun Heo INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn); 3969f147f29eSTejun Heo } 3970d2c1d404STejun Heo 3971f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 39721befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 3973f147f29eSTejun Heo { 3974f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 3975f147f29eSTejun Heo 3976f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 397775ccf595STejun Heo 39781befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 39791befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 39801befcf30STejun Heo return; 39811befcf30STejun Heo 398229b1cb41SLai Jiangshan /* set the matching work_color */ 398375ccf595STejun Heo pwq->work_color = wq->work_color; 3984983ca25eSTejun Heo 3985983ca25eSTejun Heo /* sync max_active to the current setting */ 3986983ca25eSTejun Heo pwq_adjust_max_active(pwq); 3987983ca25eSTejun Heo 3988983ca25eSTejun Heo /* link in @pwq */ 39899e8cd2f5STejun Heo list_add_rcu(&pwq->pwqs_node, &wq->pwqs); 3990df2d5ae4STejun Heo } 39916029a918STejun Heo 3992f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 3993f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 3994f147f29eSTejun Heo const struct workqueue_attrs *attrs) 3995f147f29eSTejun Heo { 3996f147f29eSTejun Heo struct worker_pool *pool; 3997f147f29eSTejun Heo struct pool_workqueue *pwq; 3998f147f29eSTejun Heo 3999f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4000f147f29eSTejun Heo 4001f147f29eSTejun Heo pool = get_unbound_pool(attrs); 4002f147f29eSTejun Heo if (!pool) 4003f147f29eSTejun Heo return NULL; 4004f147f29eSTejun Heo 4005e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 4006f147f29eSTejun Heo if (!pwq) { 4007f147f29eSTejun Heo put_unbound_pool(pool); 4008f147f29eSTejun Heo return NULL; 4009f147f29eSTejun Heo } 4010f147f29eSTejun Heo 4011f147f29eSTejun Heo init_pwq(pwq, wq, pool); 4012f147f29eSTejun Heo return pwq; 4013d2c1d404STejun Heo } 4014d2c1d404STejun Heo 40154c16bd32STejun Heo /** 401630186c6fSGong Zhaogang * wq_calc_node_cpumask - calculate a wq_attrs' cpumask for the specified node 4017042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 40184c16bd32STejun Heo * @node: the target NUMA node 40194c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 40204c16bd32STejun Heo * @cpumask: outarg, the resulting cpumask 40214c16bd32STejun Heo * 40224c16bd32STejun Heo * Calculate the cpumask a workqueue with @attrs should use on @node. If 40234c16bd32STejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during 4024d185af30SYacine Belkadi * calculation. The result is stored in @cpumask. 40254c16bd32STejun Heo * 40264c16bd32STejun Heo * If NUMA affinity is not enabled, @attrs->cpumask is always used. If 40274c16bd32STejun Heo * enabled and @node has online CPUs requested by @attrs, the returned 40284c16bd32STejun Heo * cpumask is the intersection of the possible CPUs of @node and 40294c16bd32STejun Heo * @attrs->cpumask. 40304c16bd32STejun Heo * 40314c16bd32STejun Heo * The caller is responsible for ensuring that the cpumask of @node stays 40324c16bd32STejun Heo * stable. 4033d185af30SYacine Belkadi * 4034d185af30SYacine Belkadi * Return: %true if the resulting @cpumask is different from @attrs->cpumask, 4035d185af30SYacine Belkadi * %false if equal. 40364c16bd32STejun Heo */ 40374c16bd32STejun Heo static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node, 40384c16bd32STejun Heo int cpu_going_down, cpumask_t *cpumask) 40394c16bd32STejun Heo { 4040d55262c4STejun Heo if (!wq_numa_enabled || attrs->no_numa) 40414c16bd32STejun Heo goto use_dfl; 40424c16bd32STejun Heo 40434c16bd32STejun Heo /* does @node have any online CPUs @attrs wants? */ 40444c16bd32STejun Heo cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask); 40454c16bd32STejun Heo if (cpu_going_down >= 0) 40464c16bd32STejun Heo cpumask_clear_cpu(cpu_going_down, cpumask); 40474c16bd32STejun Heo 40484c16bd32STejun Heo if (cpumask_empty(cpumask)) 40494c16bd32STejun Heo goto use_dfl; 40504c16bd32STejun Heo 40514c16bd32STejun Heo /* yeap, return possible CPUs in @node that @attrs wants */ 40524c16bd32STejun Heo cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]); 40531ad0f0a7SMichael Bringmann 40541ad0f0a7SMichael Bringmann if (cpumask_empty(cpumask)) { 40551ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 40561ad0f0a7SMichael Bringmann "possible intersect\n"); 40571ad0f0a7SMichael Bringmann return false; 40581ad0f0a7SMichael Bringmann } 40591ad0f0a7SMichael Bringmann 40604c16bd32STejun Heo return !cpumask_equal(cpumask, attrs->cpumask); 40614c16bd32STejun Heo 40624c16bd32STejun Heo use_dfl: 40634c16bd32STejun Heo cpumask_copy(cpumask, attrs->cpumask); 40644c16bd32STejun Heo return false; 40654c16bd32STejun Heo } 40664c16bd32STejun Heo 40671befcf30STejun Heo /* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */ 40681befcf30STejun Heo static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq, 40691befcf30STejun Heo int node, 40701befcf30STejun Heo struct pool_workqueue *pwq) 40711befcf30STejun Heo { 40721befcf30STejun Heo struct pool_workqueue *old_pwq; 40731befcf30STejun Heo 40745b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 40751befcf30STejun Heo lockdep_assert_held(&wq->mutex); 40761befcf30STejun Heo 40771befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 40781befcf30STejun Heo link_pwq(pwq); 40791befcf30STejun Heo 40801befcf30STejun Heo old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]); 40811befcf30STejun Heo rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq); 40821befcf30STejun Heo return old_pwq; 40831befcf30STejun Heo } 40841befcf30STejun Heo 40852d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 40862d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 40872d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 40882d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 4089042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 40902d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 40912d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 40922d5f0764SLai Jiangshan }; 40932d5f0764SLai Jiangshan 40942d5f0764SLai Jiangshan /* free the resources after success or abort */ 40952d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 40962d5f0764SLai Jiangshan { 40972d5f0764SLai Jiangshan if (ctx) { 40982d5f0764SLai Jiangshan int node; 40992d5f0764SLai Jiangshan 41002d5f0764SLai Jiangshan for_each_node(node) 41012d5f0764SLai Jiangshan put_pwq_unlocked(ctx->pwq_tbl[node]); 41022d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 41032d5f0764SLai Jiangshan 41042d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 41052d5f0764SLai Jiangshan 41062d5f0764SLai Jiangshan kfree(ctx); 41072d5f0764SLai Jiangshan } 41082d5f0764SLai Jiangshan } 41092d5f0764SLai Jiangshan 41102d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 41112d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 41122d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 411399c621efSLai Jiangshan const struct workqueue_attrs *attrs, 411499c621efSLai Jiangshan const cpumask_var_t unbound_cpumask) 41152d5f0764SLai Jiangshan { 41162d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 41172d5f0764SLai Jiangshan struct workqueue_attrs *new_attrs, *tmp_attrs; 41182d5f0764SLai Jiangshan int node; 41192d5f0764SLai Jiangshan 41202d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 41212d5f0764SLai Jiangshan 4122acafe7e3SKees Cook ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_node_ids), GFP_KERNEL); 41232d5f0764SLai Jiangshan 4124be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs(); 4125be69d00dSThomas Gleixner tmp_attrs = alloc_workqueue_attrs(); 41262d5f0764SLai Jiangshan if (!ctx || !new_attrs || !tmp_attrs) 41272d5f0764SLai Jiangshan goto out_free; 41282d5f0764SLai Jiangshan 4129042f7df1SLai Jiangshan /* 413099c621efSLai Jiangshan * Calculate the attrs of the default pwq with unbound_cpumask 413199c621efSLai Jiangshan * which is wq_unbound_cpumask or to set to wq_unbound_cpumask. 4132042f7df1SLai Jiangshan * If the user configured cpumask doesn't overlap with the 4133042f7df1SLai Jiangshan * wq_unbound_cpumask, we fallback to the wq_unbound_cpumask. 4134042f7df1SLai Jiangshan */ 41352d5f0764SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 413699c621efSLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, unbound_cpumask); 4137042f7df1SLai Jiangshan if (unlikely(cpumask_empty(new_attrs->cpumask))) 413899c621efSLai Jiangshan cpumask_copy(new_attrs->cpumask, unbound_cpumask); 41392d5f0764SLai Jiangshan 41402d5f0764SLai Jiangshan /* 41412d5f0764SLai Jiangshan * We may create multiple pwqs with differing cpumasks. Make a 41422d5f0764SLai Jiangshan * copy of @new_attrs which will be modified and used to obtain 41432d5f0764SLai Jiangshan * pools. 41442d5f0764SLai Jiangshan */ 41452d5f0764SLai Jiangshan copy_workqueue_attrs(tmp_attrs, new_attrs); 41462d5f0764SLai Jiangshan 41472d5f0764SLai Jiangshan /* 41482d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 41492d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 41502d5f0764SLai Jiangshan * it even if we don't use it immediately. 41512d5f0764SLai Jiangshan */ 41522d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 41532d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 41542d5f0764SLai Jiangshan goto out_free; 41552d5f0764SLai Jiangshan 41562d5f0764SLai Jiangshan for_each_node(node) { 4157042f7df1SLai Jiangshan if (wq_calc_node_cpumask(new_attrs, node, -1, tmp_attrs->cpumask)) { 41582d5f0764SLai Jiangshan ctx->pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs); 41592d5f0764SLai Jiangshan if (!ctx->pwq_tbl[node]) 41602d5f0764SLai Jiangshan goto out_free; 41612d5f0764SLai Jiangshan } else { 41622d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 41632d5f0764SLai Jiangshan ctx->pwq_tbl[node] = ctx->dfl_pwq; 41642d5f0764SLai Jiangshan } 41652d5f0764SLai Jiangshan } 41662d5f0764SLai Jiangshan 4167042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 4168042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 4169042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 41702d5f0764SLai Jiangshan ctx->attrs = new_attrs; 4171042f7df1SLai Jiangshan 41722d5f0764SLai Jiangshan ctx->wq = wq; 41732d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 41742d5f0764SLai Jiangshan return ctx; 41752d5f0764SLai Jiangshan 41762d5f0764SLai Jiangshan out_free: 41772d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 41782d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 41792d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 41802d5f0764SLai Jiangshan return NULL; 41812d5f0764SLai Jiangshan } 41822d5f0764SLai Jiangshan 41832d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 41842d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 41852d5f0764SLai Jiangshan { 41862d5f0764SLai Jiangshan int node; 41872d5f0764SLai Jiangshan 41882d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 41892d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 41902d5f0764SLai Jiangshan 41912d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 41922d5f0764SLai Jiangshan 41932d5f0764SLai Jiangshan /* save the previous pwq and install the new one */ 41942d5f0764SLai Jiangshan for_each_node(node) 41952d5f0764SLai Jiangshan ctx->pwq_tbl[node] = numa_pwq_tbl_install(ctx->wq, node, 41962d5f0764SLai Jiangshan ctx->pwq_tbl[node]); 41972d5f0764SLai Jiangshan 41982d5f0764SLai Jiangshan /* @dfl_pwq might not have been used, ensure it's linked */ 41992d5f0764SLai Jiangshan link_pwq(ctx->dfl_pwq); 42002d5f0764SLai Jiangshan swap(ctx->wq->dfl_pwq, ctx->dfl_pwq); 42012d5f0764SLai Jiangshan 42022d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 42032d5f0764SLai Jiangshan } 42042d5f0764SLai Jiangshan 4205a0111cf6SLai Jiangshan static void apply_wqattrs_lock(void) 4206a0111cf6SLai Jiangshan { 4207a0111cf6SLai Jiangshan /* CPUs should stay stable across pwq creations and installations */ 4208ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 4209a0111cf6SLai Jiangshan mutex_lock(&wq_pool_mutex); 4210a0111cf6SLai Jiangshan } 4211a0111cf6SLai Jiangshan 4212a0111cf6SLai Jiangshan static void apply_wqattrs_unlock(void) 4213a0111cf6SLai Jiangshan { 4214a0111cf6SLai Jiangshan mutex_unlock(&wq_pool_mutex); 4215ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4216a0111cf6SLai Jiangshan } 4217a0111cf6SLai Jiangshan 4218a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 4219a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 4220a0111cf6SLai Jiangshan { 4221a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 4222a0111cf6SLai Jiangshan 4223a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 4224a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 4225a0111cf6SLai Jiangshan return -EINVAL; 4226a0111cf6SLai Jiangshan 4227a0111cf6SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 42280a94efb5STejun Heo if (!list_empty(&wq->pwqs)) { 42290a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 4230a0111cf6SLai Jiangshan return -EINVAL; 4231a0111cf6SLai Jiangshan 42320a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 42330a94efb5STejun Heo } 42340a94efb5STejun Heo 423599c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); 42366201171eSwanghaibin if (!ctx) 42376201171eSwanghaibin return -ENOMEM; 4238a0111cf6SLai Jiangshan 4239a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 4240a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 4241a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 4242a0111cf6SLai Jiangshan 42436201171eSwanghaibin return 0; 4244a0111cf6SLai Jiangshan } 4245a0111cf6SLai Jiangshan 42469e8cd2f5STejun Heo /** 42479e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 42489e8cd2f5STejun Heo * @wq: the target workqueue 42499e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 42509e8cd2f5STejun Heo * 42514c16bd32STejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA 42524c16bd32STejun Heo * machines, this function maps a separate pwq to each NUMA node with 42534c16bd32STejun Heo * possibles CPUs in @attrs->cpumask so that work items are affine to the 42544c16bd32STejun Heo * NUMA node it was issued on. Older pwqs are released as in-flight work 42554c16bd32STejun Heo * items finish. Note that a work item which repeatedly requeues itself 42564c16bd32STejun Heo * back-to-back will stay on its current pwq. 42579e8cd2f5STejun Heo * 4258d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 4259d185af30SYacine Belkadi * 4260ffd8bea8SSebastian Andrzej Siewior * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). 4261509b3204SDaniel Jordan * 4262d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 42639e8cd2f5STejun Heo */ 4264513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 42659e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 42669e8cd2f5STejun Heo { 4267a0111cf6SLai Jiangshan int ret; 42689e8cd2f5STejun Heo 4269509b3204SDaniel Jordan lockdep_assert_cpus_held(); 4270509b3204SDaniel Jordan 4271509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex); 4272a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 4273509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex); 42742d5f0764SLai Jiangshan 42752d5f0764SLai Jiangshan return ret; 42769e8cd2f5STejun Heo } 42779e8cd2f5STejun Heo 42784c16bd32STejun Heo /** 42794c16bd32STejun Heo * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug 42804c16bd32STejun Heo * @wq: the target workqueue 42814c16bd32STejun Heo * @cpu: the CPU coming up or going down 42824c16bd32STejun Heo * @online: whether @cpu is coming up or going down 42834c16bd32STejun Heo * 42844c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 42854c16bd32STejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of 42864c16bd32STejun Heo * @wq accordingly. 42874c16bd32STejun Heo * 42884c16bd32STejun Heo * If NUMA affinity can't be adjusted due to memory allocation failure, it 42894c16bd32STejun Heo * falls back to @wq->dfl_pwq which may not be optimal but is always 42904c16bd32STejun Heo * correct. 42914c16bd32STejun Heo * 42924c16bd32STejun Heo * Note that when the last allowed CPU of a NUMA node goes offline for a 42934c16bd32STejun Heo * workqueue with a cpumask spanning multiple nodes, the workers which were 42944c16bd32STejun Heo * already executing the work items for the workqueue will lose their CPU 42954c16bd32STejun Heo * affinity and may execute on any CPU. This is similar to how per-cpu 42964c16bd32STejun Heo * workqueues behave on CPU_DOWN. If a workqueue user wants strict 42974c16bd32STejun Heo * affinity, it's the user's responsibility to flush the work item from 42984c16bd32STejun Heo * CPU_DOWN_PREPARE. 42994c16bd32STejun Heo */ 43004c16bd32STejun Heo static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu, 43014c16bd32STejun Heo bool online) 43024c16bd32STejun Heo { 43034c16bd32STejun Heo int node = cpu_to_node(cpu); 43044c16bd32STejun Heo int cpu_off = online ? -1 : cpu; 43054c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 43064c16bd32STejun Heo struct workqueue_attrs *target_attrs; 43074c16bd32STejun Heo cpumask_t *cpumask; 43084c16bd32STejun Heo 43094c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 43104c16bd32STejun Heo 4311f7142ed4SLai Jiangshan if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND) || 4312f7142ed4SLai Jiangshan wq->unbound_attrs->no_numa) 43134c16bd32STejun Heo return; 43144c16bd32STejun Heo 43154c16bd32STejun Heo /* 43164c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 43174c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 43184c16bd32STejun Heo * CPU hotplug exclusion. 43194c16bd32STejun Heo */ 43204c16bd32STejun Heo target_attrs = wq_update_unbound_numa_attrs_buf; 43214c16bd32STejun Heo cpumask = target_attrs->cpumask; 43224c16bd32STejun Heo 43234c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 43244c16bd32STejun Heo pwq = unbound_pwq_by_node(wq, node); 43254c16bd32STejun Heo 43264c16bd32STejun Heo /* 43274c16bd32STejun Heo * Let's determine what needs to be done. If the target cpumask is 4328042f7df1SLai Jiangshan * different from the default pwq's, we need to compare it to @pwq's 4329042f7df1SLai Jiangshan * and create a new one if they don't match. If the target cpumask 4330042f7df1SLai Jiangshan * equals the default pwq's, the default pwq should be used. 43314c16bd32STejun Heo */ 4332042f7df1SLai Jiangshan if (wq_calc_node_cpumask(wq->dfl_pwq->pool->attrs, node, cpu_off, cpumask)) { 43334c16bd32STejun Heo if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask)) 4334f7142ed4SLai Jiangshan return; 43354c16bd32STejun Heo } else { 43364c16bd32STejun Heo goto use_dfl_pwq; 43374c16bd32STejun Heo } 43384c16bd32STejun Heo 43394c16bd32STejun Heo /* create a new pwq */ 43404c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 43414c16bd32STejun Heo if (!pwq) { 43422d916033SFabian Frederick pr_warn("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n", 43434c16bd32STejun Heo wq->name); 434477f300b1SDaeseok Youn goto use_dfl_pwq; 43454c16bd32STejun Heo } 43464c16bd32STejun Heo 4347f7142ed4SLai Jiangshan /* Install the new pwq. */ 43484c16bd32STejun Heo mutex_lock(&wq->mutex); 43494c16bd32STejun Heo old_pwq = numa_pwq_tbl_install(wq, node, pwq); 43504c16bd32STejun Heo goto out_unlock; 43514c16bd32STejun Heo 43524c16bd32STejun Heo use_dfl_pwq: 4353f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 4354a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq->dfl_pwq->pool->lock); 43554c16bd32STejun Heo get_pwq(wq->dfl_pwq); 4356a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq->dfl_pwq->pool->lock); 43574c16bd32STejun Heo old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq); 43584c16bd32STejun Heo out_unlock: 43594c16bd32STejun Heo mutex_unlock(&wq->mutex); 43604c16bd32STejun Heo put_pwq_unlocked(old_pwq); 43614c16bd32STejun Heo } 43624c16bd32STejun Heo 436330cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 43641da177e4SLinus Torvalds { 436549e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 43668a2b7538STejun Heo int cpu, ret; 4367e1d8aa9fSFrederic Weisbecker 436830cdf249STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 4369420c0ddbSTejun Heo wq->cpu_pwqs = alloc_percpu(struct pool_workqueue); 4370420c0ddbSTejun Heo if (!wq->cpu_pwqs) 437130cdf249STejun Heo return -ENOMEM; 437230cdf249STejun Heo 437330cdf249STejun Heo for_each_possible_cpu(cpu) { 43747fb98ea7STejun Heo struct pool_workqueue *pwq = 43757fb98ea7STejun Heo per_cpu_ptr(wq->cpu_pwqs, cpu); 43767a62c2c8STejun Heo struct worker_pool *cpu_pools = 4377f02ae73aSTejun Heo per_cpu(cpu_worker_pools, cpu); 437830cdf249STejun Heo 4379f147f29eSTejun Heo init_pwq(pwq, wq, &cpu_pools[highpri]); 4380f147f29eSTejun Heo 4381f147f29eSTejun Heo mutex_lock(&wq->mutex); 43821befcf30STejun Heo link_pwq(pwq); 4383f147f29eSTejun Heo mutex_unlock(&wq->mutex); 438430cdf249STejun Heo } 438530cdf249STejun Heo return 0; 4386509b3204SDaniel Jordan } 4387509b3204SDaniel Jordan 4388ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 4389509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) { 43908a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 43918a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 43928a2b7538STejun Heo WARN(!ret && (wq->pwqs.next != &wq->dfl_pwq->pwqs_node || 43938a2b7538STejun Heo wq->pwqs.prev != &wq->dfl_pwq->pwqs_node), 43948a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 43959e8cd2f5STejun Heo } else { 4396509b3204SDaniel Jordan ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 43979e8cd2f5STejun Heo } 4398ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4399509b3204SDaniel Jordan 4400509b3204SDaniel Jordan return ret; 44010f900049STejun Heo } 44020f900049STejun Heo 4403f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 4404f3421797STejun Heo const char *name) 4405b71ab8c2STejun Heo { 4406f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 4407f3421797STejun Heo 4408f3421797STejun Heo if (max_active < 1 || max_active > lim) 4409044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 4410f3421797STejun Heo max_active, name, 1, lim); 4411b71ab8c2STejun Heo 4412f3421797STejun Heo return clamp_val(max_active, 1, lim); 4413b71ab8c2STejun Heo } 4414b71ab8c2STejun Heo 4415983c7515STejun Heo /* 4416983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 4417983c7515STejun Heo * to guarantee forward progress. 4418983c7515STejun Heo */ 4419983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 4420983c7515STejun Heo { 4421983c7515STejun Heo struct worker *rescuer; 4422b92b36eaSDan Carpenter int ret; 4423983c7515STejun Heo 4424983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 4425983c7515STejun Heo return 0; 4426983c7515STejun Heo 4427983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 44284c0736a7SPetr Mladek if (!rescuer) { 44294c0736a7SPetr Mladek pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", 44304c0736a7SPetr Mladek wq->name); 4431983c7515STejun Heo return -ENOMEM; 44324c0736a7SPetr Mladek } 4433983c7515STejun Heo 4434983c7515STejun Heo rescuer->rescue_wq = wq; 4435983c7515STejun Heo rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", wq->name); 4436f187b697SSean Fu if (IS_ERR(rescuer->task)) { 4437b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task); 44384c0736a7SPetr Mladek pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", 44394c0736a7SPetr Mladek wq->name, ERR_PTR(ret)); 4440983c7515STejun Heo kfree(rescuer); 4441b92b36eaSDan Carpenter return ret; 4442983c7515STejun Heo } 4443983c7515STejun Heo 4444983c7515STejun Heo wq->rescuer = rescuer; 4445983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 4446983c7515STejun Heo wake_up_process(rescuer->task); 4447983c7515STejun Heo 4448983c7515STejun Heo return 0; 4449983c7515STejun Heo } 4450983c7515STejun Heo 4451a2775bbcSMathieu Malaterre __printf(1, 4) 4452669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt, 445397e37d7bSTejun Heo unsigned int flags, 4454669de8bdSBart Van Assche int max_active, ...) 44553af24433SOleg Nesterov { 4456df2d5ae4STejun Heo size_t tbl_size = 0; 4457ecf6881fSTejun Heo va_list args; 44583af24433SOleg Nesterov struct workqueue_struct *wq; 445949e3cf44STejun Heo struct pool_workqueue *pwq; 4460b196be89STejun Heo 44615c0338c6STejun Heo /* 44625c0338c6STejun Heo * Unbound && max_active == 1 used to imply ordered, which is no 44635c0338c6STejun Heo * longer the case on NUMA machines due to per-node pools. While 44645c0338c6STejun Heo * alloc_ordered_workqueue() is the right way to create an ordered 44655c0338c6STejun Heo * workqueue, keep the previous behavior to avoid subtle breakages 44665c0338c6STejun Heo * on NUMA. 44675c0338c6STejun Heo */ 44685c0338c6STejun Heo if ((flags & WQ_UNBOUND) && max_active == 1) 44695c0338c6STejun Heo flags |= __WQ_ORDERED; 44705c0338c6STejun Heo 4471cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 4472cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 4473cee22a15SViresh Kumar flags |= WQ_UNBOUND; 4474cee22a15SViresh Kumar 4475ecf6881fSTejun Heo /* allocate wq and format name */ 4476df2d5ae4STejun Heo if (flags & WQ_UNBOUND) 4477ddcb57e2SLai Jiangshan tbl_size = nr_node_ids * sizeof(wq->numa_pwq_tbl[0]); 4478df2d5ae4STejun Heo 4479df2d5ae4STejun Heo wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL); 4480b196be89STejun Heo if (!wq) 4481d2c1d404STejun Heo return NULL; 4482b196be89STejun Heo 44836029a918STejun Heo if (flags & WQ_UNBOUND) { 4484be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs(); 44856029a918STejun Heo if (!wq->unbound_attrs) 44866029a918STejun Heo goto err_free_wq; 44876029a918STejun Heo } 44886029a918STejun Heo 4489669de8bdSBart Van Assche va_start(args, max_active); 4490ecf6881fSTejun Heo vsnprintf(wq->name, sizeof(wq->name), fmt, args); 4491b196be89STejun Heo va_end(args); 44923af24433SOleg Nesterov 4493d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 4494b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 44953af24433SOleg Nesterov 4496b196be89STejun Heo /* init wq */ 449797e37d7bSTejun Heo wq->flags = flags; 4498a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 44993c25a55dSLai Jiangshan mutex_init(&wq->mutex); 4500112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 450130cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 450273f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 450373f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 4504493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 45053af24433SOleg Nesterov 4506669de8bdSBart Van Assche wq_init_lockdep(wq); 4507cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 45083af24433SOleg Nesterov 450930cdf249STejun Heo if (alloc_and_link_pwqs(wq) < 0) 451082efcab3SBart Van Assche goto err_unreg_lockdep; 45111537663fSTejun Heo 451240c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 4513d2c1d404STejun Heo goto err_destroy; 4514e22bee78STejun Heo 4515226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 4516226223abSTejun Heo goto err_destroy; 4517226223abSTejun Heo 45186af8bf3dSOleg Nesterov /* 451968e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 452068e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 452168e13a67SLai Jiangshan * list. 45226af8bf3dSOleg Nesterov */ 452368e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4524a0a1a5fdSTejun Heo 4525a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 452649e3cf44STejun Heo for_each_pwq(pwq, wq) 4527699ce097STejun Heo pwq_adjust_max_active(pwq); 4528a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4529a0a1a5fdSTejun Heo 4530e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 4531a0a1a5fdSTejun Heo 453268e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 45333af24433SOleg Nesterov 45343af24433SOleg Nesterov return wq; 4535d2c1d404STejun Heo 453682efcab3SBart Van Assche err_unreg_lockdep: 4537009bb421SBart Van Assche wq_unregister_lockdep(wq); 4538009bb421SBart Van Assche wq_free_lockdep(wq); 453982efcab3SBart Van Assche err_free_wq: 45406029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 45414690c4abSTejun Heo kfree(wq); 4542d2c1d404STejun Heo return NULL; 4543d2c1d404STejun Heo err_destroy: 4544d2c1d404STejun Heo destroy_workqueue(wq); 45454690c4abSTejun Heo return NULL; 45461da177e4SLinus Torvalds } 4547669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue); 45481da177e4SLinus Torvalds 4549c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq) 4550c29eb853STejun Heo { 4551c29eb853STejun Heo int i; 4552c29eb853STejun Heo 4553c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 4554c29eb853STejun Heo if (pwq->nr_in_flight[i]) 4555c29eb853STejun Heo return true; 4556c29eb853STejun Heo 4557c29eb853STejun Heo if ((pwq != pwq->wq->dfl_pwq) && (pwq->refcnt > 1)) 4558c29eb853STejun Heo return true; 4559f97a4a1aSLai Jiangshan if (pwq->nr_active || !list_empty(&pwq->inactive_works)) 4560c29eb853STejun Heo return true; 4561c29eb853STejun Heo 4562c29eb853STejun Heo return false; 4563c29eb853STejun Heo } 4564c29eb853STejun Heo 45653af24433SOleg Nesterov /** 45663af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 45673af24433SOleg Nesterov * @wq: target workqueue 45683af24433SOleg Nesterov * 45693af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 45703af24433SOleg Nesterov */ 45713af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 45723af24433SOleg Nesterov { 457349e3cf44STejun Heo struct pool_workqueue *pwq; 45744c16bd32STejun Heo int node; 45753af24433SOleg Nesterov 4576def98c84STejun Heo /* 4577def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't 4578def98c84STejun Heo * lead to sysfs name conflicts. 4579def98c84STejun Heo */ 4580def98c84STejun Heo workqueue_sysfs_unregister(wq); 4581def98c84STejun Heo 458233e3f0a3SRichard Clark /* mark the workqueue destruction is in progress */ 458333e3f0a3SRichard Clark mutex_lock(&wq->mutex); 458433e3f0a3SRichard Clark wq->flags |= __WQ_DESTROYING; 458533e3f0a3SRichard Clark mutex_unlock(&wq->mutex); 458633e3f0a3SRichard Clark 45879c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 45889c5a2ba7STejun Heo drain_workqueue(wq); 4589c8efcc25STejun Heo 4590def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ 4591def98c84STejun Heo if (wq->rescuer) { 4592def98c84STejun Heo struct worker *rescuer = wq->rescuer; 4593def98c84STejun Heo 4594def98c84STejun Heo /* this prevents new queueing */ 4595a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 4596def98c84STejun Heo wq->rescuer = NULL; 4597a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 4598def98c84STejun Heo 4599def98c84STejun Heo /* rescuer will empty maydays list before exiting */ 4600def98c84STejun Heo kthread_stop(rescuer->task); 46018efe1223STejun Heo kfree(rescuer); 4602def98c84STejun Heo } 4603def98c84STejun Heo 4604c29eb853STejun Heo /* 4605c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all 4606c29eb853STejun Heo * in-flight operations which may do put_pwq(). 4607c29eb853STejun Heo */ 4608c29eb853STejun Heo mutex_lock(&wq_pool_mutex); 4609b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 461049e3cf44STejun Heo for_each_pwq(pwq, wq) { 4611a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 4612c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) { 46131d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n", 4614e66b39afSTejun Heo __func__, wq->name); 4615c29eb853STejun Heo show_pwq(pwq); 4616a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 4617b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 4618c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 461955df0933SImran Khan show_one_workqueue(wq); 46206183c009STejun Heo return; 462176af4d93STejun Heo } 4622a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 462376af4d93STejun Heo } 4624b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 46256183c009STejun Heo 4626a0a1a5fdSTejun Heo /* 4627a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 4628a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 4629a0a1a5fdSTejun Heo */ 4630e2dca7adSTejun Heo list_del_rcu(&wq->list); 463168e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 46323af24433SOleg Nesterov 46338864b4e5STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 4634669de8bdSBart Van Assche wq_unregister_lockdep(wq); 463529c91e99STejun Heo /* 46368864b4e5STejun Heo * The base ref is never dropped on per-cpu pwqs. Directly 4637e2dca7adSTejun Heo * schedule RCU free. 463829c91e99STejun Heo */ 463925b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 46408864b4e5STejun Heo } else { 46418864b4e5STejun Heo /* 46428864b4e5STejun Heo * We're the sole accessor of @wq at this point. Directly 46434c16bd32STejun Heo * access numa_pwq_tbl[] and dfl_pwq to put the base refs. 46444c16bd32STejun Heo * @wq will be freed when the last pwq is released. 46458864b4e5STejun Heo */ 46464c16bd32STejun Heo for_each_node(node) { 46474c16bd32STejun Heo pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]); 46484c16bd32STejun Heo RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL); 46494c16bd32STejun Heo put_pwq_unlocked(pwq); 46504c16bd32STejun Heo } 46514c16bd32STejun Heo 46524c16bd32STejun Heo /* 46534c16bd32STejun Heo * Put dfl_pwq. @wq may be freed any time after dfl_pwq is 46544c16bd32STejun Heo * put. Don't access it afterwards. 46554c16bd32STejun Heo */ 46564c16bd32STejun Heo pwq = wq->dfl_pwq; 46574c16bd32STejun Heo wq->dfl_pwq = NULL; 4658dce90d47STejun Heo put_pwq_unlocked(pwq); 465929c91e99STejun Heo } 46603af24433SOleg Nesterov } 46613af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 46623af24433SOleg Nesterov 4663dcd989cbSTejun Heo /** 4664dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 4665dcd989cbSTejun Heo * @wq: target workqueue 4666dcd989cbSTejun Heo * @max_active: new max_active value. 4667dcd989cbSTejun Heo * 4668dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 4669dcd989cbSTejun Heo * 4670dcd989cbSTejun Heo * CONTEXT: 4671dcd989cbSTejun Heo * Don't call from IRQ context. 4672dcd989cbSTejun Heo */ 4673dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 4674dcd989cbSTejun Heo { 467549e3cf44STejun Heo struct pool_workqueue *pwq; 4676dcd989cbSTejun Heo 46778719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 46780a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 46798719dceaSTejun Heo return; 46808719dceaSTejun Heo 4681f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 4682dcd989cbSTejun Heo 4683a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4684dcd989cbSTejun Heo 46850a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 4686dcd989cbSTejun Heo wq->saved_max_active = max_active; 4687dcd989cbSTejun Heo 4688699ce097STejun Heo for_each_pwq(pwq, wq) 4689699ce097STejun Heo pwq_adjust_max_active(pwq); 4690dcd989cbSTejun Heo 4691a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4692dcd989cbSTejun Heo } 4693dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 4694dcd989cbSTejun Heo 4695dcd989cbSTejun Heo /** 469627d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 469727d4ee03SLukas Wunner * 469827d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 469927d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 470027d4ee03SLukas Wunner * 470127d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 470227d4ee03SLukas Wunner */ 470327d4ee03SLukas Wunner struct work_struct *current_work(void) 470427d4ee03SLukas Wunner { 470527d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 470627d4ee03SLukas Wunner 470727d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 470827d4ee03SLukas Wunner } 470927d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 471027d4ee03SLukas Wunner 471127d4ee03SLukas Wunner /** 4712e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 4713e6267616STejun Heo * 4714e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 4715e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 4716d185af30SYacine Belkadi * 4717d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 4718e6267616STejun Heo */ 4719e6267616STejun Heo bool current_is_workqueue_rescuer(void) 4720e6267616STejun Heo { 4721e6267616STejun Heo struct worker *worker = current_wq_worker(); 4722e6267616STejun Heo 47236a092dfdSLai Jiangshan return worker && worker->rescue_wq; 4724e6267616STejun Heo } 4725e6267616STejun Heo 4726e6267616STejun Heo /** 4727dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 4728dcd989cbSTejun Heo * @cpu: CPU in question 4729dcd989cbSTejun Heo * @wq: target workqueue 4730dcd989cbSTejun Heo * 4731dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 4732dcd989cbSTejun Heo * no synchronization around this function and the test result is 4733dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4734dcd989cbSTejun Heo * 4735d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 4736d3251859STejun Heo * Note that both per-cpu and unbound workqueues may be associated with 4737d3251859STejun Heo * multiple pool_workqueues which have separate congested states. A 4738d3251859STejun Heo * workqueue being congested on one CPU doesn't mean the workqueue is also 4739d3251859STejun Heo * contested on other CPUs / NUMA nodes. 4740d3251859STejun Heo * 4741d185af30SYacine Belkadi * Return: 4742dcd989cbSTejun Heo * %true if congested, %false otherwise. 4743dcd989cbSTejun Heo */ 4744d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 4745dcd989cbSTejun Heo { 47467fb98ea7STejun Heo struct pool_workqueue *pwq; 474776af4d93STejun Heo bool ret; 474876af4d93STejun Heo 474924acfb71SThomas Gleixner rcu_read_lock(); 475024acfb71SThomas Gleixner preempt_disable(); 47517fb98ea7STejun Heo 4752d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 4753d3251859STejun Heo cpu = smp_processor_id(); 4754d3251859STejun Heo 47557fb98ea7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 47567fb98ea7STejun Heo pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 47577fb98ea7STejun Heo else 4758df2d5ae4STejun Heo pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu)); 4759dcd989cbSTejun Heo 4760f97a4a1aSLai Jiangshan ret = !list_empty(&pwq->inactive_works); 476124acfb71SThomas Gleixner preempt_enable(); 476224acfb71SThomas Gleixner rcu_read_unlock(); 476376af4d93STejun Heo 476476af4d93STejun Heo return ret; 4765dcd989cbSTejun Heo } 4766dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 4767dcd989cbSTejun Heo 4768dcd989cbSTejun Heo /** 4769dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 4770dcd989cbSTejun Heo * @work: the work to be tested 4771dcd989cbSTejun Heo * 4772dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 4773dcd989cbSTejun Heo * synchronization around this function and the test result is 4774dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4775dcd989cbSTejun Heo * 4776d185af30SYacine Belkadi * Return: 4777dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 4778dcd989cbSTejun Heo */ 4779dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 4780dcd989cbSTejun Heo { 4781fa1b54e6STejun Heo struct worker_pool *pool; 4782dcd989cbSTejun Heo unsigned long flags; 4783dcd989cbSTejun Heo unsigned int ret = 0; 4784dcd989cbSTejun Heo 4785dcd989cbSTejun Heo if (work_pending(work)) 4786dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 4787038366c5SLai Jiangshan 478824acfb71SThomas Gleixner rcu_read_lock(); 4789fa1b54e6STejun Heo pool = get_work_pool(work); 4790038366c5SLai Jiangshan if (pool) { 4791a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 4792c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 4793dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 4794a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 4795038366c5SLai Jiangshan } 479624acfb71SThomas Gleixner rcu_read_unlock(); 4797dcd989cbSTejun Heo 4798dcd989cbSTejun Heo return ret; 4799dcd989cbSTejun Heo } 4800dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 4801dcd989cbSTejun Heo 48023d1cb205STejun Heo /** 48033d1cb205STejun Heo * set_worker_desc - set description for the current work item 48043d1cb205STejun Heo * @fmt: printf-style format string 48053d1cb205STejun Heo * @...: arguments for the format string 48063d1cb205STejun Heo * 48073d1cb205STejun Heo * This function can be called by a running work function to describe what 48083d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 48093d1cb205STejun Heo * information will be printed out together to help debugging. The 48103d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 48113d1cb205STejun Heo */ 48123d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 48133d1cb205STejun Heo { 48143d1cb205STejun Heo struct worker *worker = current_wq_worker(); 48153d1cb205STejun Heo va_list args; 48163d1cb205STejun Heo 48173d1cb205STejun Heo if (worker) { 48183d1cb205STejun Heo va_start(args, fmt); 48193d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 48203d1cb205STejun Heo va_end(args); 48213d1cb205STejun Heo } 48223d1cb205STejun Heo } 48235c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 48243d1cb205STejun Heo 48253d1cb205STejun Heo /** 48263d1cb205STejun Heo * print_worker_info - print out worker information and description 48273d1cb205STejun Heo * @log_lvl: the log level to use when printing 48283d1cb205STejun Heo * @task: target task 48293d1cb205STejun Heo * 48303d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 48313d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 48323d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 48333d1cb205STejun Heo * 48343d1cb205STejun Heo * This function can be safely called on any task as long as the 48353d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 48363d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 48373d1cb205STejun Heo */ 48383d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 48393d1cb205STejun Heo { 48403d1cb205STejun Heo work_func_t *fn = NULL; 48413d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 48423d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 48433d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 48443d1cb205STejun Heo struct workqueue_struct *wq = NULL; 48453d1cb205STejun Heo struct worker *worker; 48463d1cb205STejun Heo 48473d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 48483d1cb205STejun Heo return; 48493d1cb205STejun Heo 48503d1cb205STejun Heo /* 48513d1cb205STejun Heo * This function is called without any synchronization and @task 48523d1cb205STejun Heo * could be in any state. Be careful with dereferences. 48533d1cb205STejun Heo */ 4854e700591aSPetr Mladek worker = kthread_probe_data(task); 48553d1cb205STejun Heo 48563d1cb205STejun Heo /* 48578bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 48588bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 48593d1cb205STejun Heo */ 4860fe557319SChristoph Hellwig copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); 4861fe557319SChristoph Hellwig copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); 4862fe557319SChristoph Hellwig copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); 4863fe557319SChristoph Hellwig copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); 4864fe557319SChristoph Hellwig copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); 48653d1cb205STejun Heo 48663d1cb205STejun Heo if (fn || name[0] || desc[0]) { 4867d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn); 48688bf89593STejun Heo if (strcmp(name, desc)) 48693d1cb205STejun Heo pr_cont(" (%s)", desc); 48703d1cb205STejun Heo pr_cont("\n"); 48713d1cb205STejun Heo } 48723d1cb205STejun Heo } 48733d1cb205STejun Heo 48743494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 48753494fc30STejun Heo { 48763494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 48773494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 48783494fc30STejun Heo pr_cont(" node=%d", pool->node); 48793494fc30STejun Heo pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice); 48803494fc30STejun Heo } 48813494fc30STejun Heo 4882c76feb0dSPaul E. McKenney struct pr_cont_work_struct { 4883c76feb0dSPaul E. McKenney bool comma; 4884c76feb0dSPaul E. McKenney work_func_t func; 4885c76feb0dSPaul E. McKenney long ctr; 4886c76feb0dSPaul E. McKenney }; 4887c76feb0dSPaul E. McKenney 4888c76feb0dSPaul E. McKenney static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) 4889c76feb0dSPaul E. McKenney { 4890c76feb0dSPaul E. McKenney if (!pcwsp->ctr) 4891c76feb0dSPaul E. McKenney goto out_record; 4892c76feb0dSPaul E. McKenney if (func == pcwsp->func) { 4893c76feb0dSPaul E. McKenney pcwsp->ctr++; 4894c76feb0dSPaul E. McKenney return; 4895c76feb0dSPaul E. McKenney } 4896c76feb0dSPaul E. McKenney if (pcwsp->ctr == 1) 4897c76feb0dSPaul E. McKenney pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); 4898c76feb0dSPaul E. McKenney else 4899c76feb0dSPaul E. McKenney pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); 4900c76feb0dSPaul E. McKenney pcwsp->ctr = 0; 4901c76feb0dSPaul E. McKenney out_record: 4902c76feb0dSPaul E. McKenney if ((long)func == -1L) 4903c76feb0dSPaul E. McKenney return; 4904c76feb0dSPaul E. McKenney pcwsp->comma = comma; 4905c76feb0dSPaul E. McKenney pcwsp->func = func; 4906c76feb0dSPaul E. McKenney pcwsp->ctr = 1; 4907c76feb0dSPaul E. McKenney } 4908c76feb0dSPaul E. McKenney 4909c76feb0dSPaul E. McKenney static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) 49103494fc30STejun Heo { 49113494fc30STejun Heo if (work->func == wq_barrier_func) { 49123494fc30STejun Heo struct wq_barrier *barr; 49133494fc30STejun Heo 49143494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 49153494fc30STejun Heo 4916c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 49173494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 49183494fc30STejun Heo task_pid_nr(barr->task)); 49193494fc30STejun Heo } else { 4920c76feb0dSPaul E. McKenney if (!comma) 4921c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 4922c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, work->func, pcwsp); 49233494fc30STejun Heo } 49243494fc30STejun Heo } 49253494fc30STejun Heo 49263494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 49273494fc30STejun Heo { 4928c76feb0dSPaul E. McKenney struct pr_cont_work_struct pcws = { .ctr = 0, }; 49293494fc30STejun Heo struct worker_pool *pool = pwq->pool; 49303494fc30STejun Heo struct work_struct *work; 49313494fc30STejun Heo struct worker *worker; 49323494fc30STejun Heo bool has_in_flight = false, has_pending = false; 49333494fc30STejun Heo int bkt; 49343494fc30STejun Heo 49353494fc30STejun Heo pr_info(" pwq %d:", pool->id); 49363494fc30STejun Heo pr_cont_pool_info(pool); 49373494fc30STejun Heo 4938e66b39afSTejun Heo pr_cont(" active=%d/%d refcnt=%d%s\n", 4939e66b39afSTejun Heo pwq->nr_active, pwq->max_active, pwq->refcnt, 49403494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 49413494fc30STejun Heo 49423494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 49433494fc30STejun Heo if (worker->current_pwq == pwq) { 49443494fc30STejun Heo has_in_flight = true; 49453494fc30STejun Heo break; 49463494fc30STejun Heo } 49473494fc30STejun Heo } 49483494fc30STejun Heo if (has_in_flight) { 49493494fc30STejun Heo bool comma = false; 49503494fc30STejun Heo 49513494fc30STejun Heo pr_info(" in-flight:"); 49523494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 49533494fc30STejun Heo if (worker->current_pwq != pwq) 49543494fc30STejun Heo continue; 49553494fc30STejun Heo 4956d75f773cSSakari Ailus pr_cont("%s %d%s:%ps", comma ? "," : "", 49573494fc30STejun Heo task_pid_nr(worker->task), 495830ae2fc0STejun Heo worker->rescue_wq ? "(RESCUER)" : "", 49593494fc30STejun Heo worker->current_func); 49603494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 4961c76feb0dSPaul E. McKenney pr_cont_work(false, work, &pcws); 4962c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 49633494fc30STejun Heo comma = true; 49643494fc30STejun Heo } 49653494fc30STejun Heo pr_cont("\n"); 49663494fc30STejun Heo } 49673494fc30STejun Heo 49683494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 49693494fc30STejun Heo if (get_work_pwq(work) == pwq) { 49703494fc30STejun Heo has_pending = true; 49713494fc30STejun Heo break; 49723494fc30STejun Heo } 49733494fc30STejun Heo } 49743494fc30STejun Heo if (has_pending) { 49753494fc30STejun Heo bool comma = false; 49763494fc30STejun Heo 49773494fc30STejun Heo pr_info(" pending:"); 49783494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 49793494fc30STejun Heo if (get_work_pwq(work) != pwq) 49803494fc30STejun Heo continue; 49813494fc30STejun Heo 4982c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 49833494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 49843494fc30STejun Heo } 4985c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 49863494fc30STejun Heo pr_cont("\n"); 49873494fc30STejun Heo } 49883494fc30STejun Heo 4989f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 49903494fc30STejun Heo bool comma = false; 49913494fc30STejun Heo 4992f97a4a1aSLai Jiangshan pr_info(" inactive:"); 4993f97a4a1aSLai Jiangshan list_for_each_entry(work, &pwq->inactive_works, entry) { 4994c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 49953494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 49963494fc30STejun Heo } 4997c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 49983494fc30STejun Heo pr_cont("\n"); 49993494fc30STejun Heo } 50003494fc30STejun Heo } 50013494fc30STejun Heo 50023494fc30STejun Heo /** 500355df0933SImran Khan * show_one_workqueue - dump state of specified workqueue 500455df0933SImran Khan * @wq: workqueue whose state will be printed 50053494fc30STejun Heo */ 500655df0933SImran Khan void show_one_workqueue(struct workqueue_struct *wq) 50073494fc30STejun Heo { 50083494fc30STejun Heo struct pool_workqueue *pwq; 50093494fc30STejun Heo bool idle = true; 501055df0933SImran Khan unsigned long flags; 50113494fc30STejun Heo 50123494fc30STejun Heo for_each_pwq(pwq, wq) { 5013f97a4a1aSLai Jiangshan if (pwq->nr_active || !list_empty(&pwq->inactive_works)) { 50143494fc30STejun Heo idle = false; 50153494fc30STejun Heo break; 50163494fc30STejun Heo } 50173494fc30STejun Heo } 501855df0933SImran Khan if (idle) /* Nothing to print for idle workqueue */ 501955df0933SImran Khan return; 50203494fc30STejun Heo 50213494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 50223494fc30STejun Heo 50233494fc30STejun Heo for_each_pwq(pwq, wq) { 5024a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pwq->pool->lock, flags); 502557116ce1SJohan Hovold if (pwq->nr_active || !list_empty(&pwq->inactive_works)) { 502657116ce1SJohan Hovold /* 502757116ce1SJohan Hovold * Defer printing to avoid deadlocks in console 502857116ce1SJohan Hovold * drivers that queue work while holding locks 502957116ce1SJohan Hovold * also taken in their write paths. 503057116ce1SJohan Hovold */ 503157116ce1SJohan Hovold printk_deferred_enter(); 50323494fc30STejun Heo show_pwq(pwq); 503357116ce1SJohan Hovold printk_deferred_exit(); 503457116ce1SJohan Hovold } 5035a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 503662635ea8SSergey Senozhatsky /* 503762635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 503855df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 503962635ea8SSergey Senozhatsky * hard lockup. 504062635ea8SSergey Senozhatsky */ 504162635ea8SSergey Senozhatsky touch_nmi_watchdog(); 50423494fc30STejun Heo } 504355df0933SImran Khan 50443494fc30STejun Heo } 50453494fc30STejun Heo 504655df0933SImran Khan /** 504755df0933SImran Khan * show_one_worker_pool - dump state of specified worker pool 504855df0933SImran Khan * @pool: worker pool whose state will be printed 504955df0933SImran Khan */ 505055df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool) 505155df0933SImran Khan { 50523494fc30STejun Heo struct worker *worker; 50533494fc30STejun Heo bool first = true; 505455df0933SImran Khan unsigned long flags; 5055335a42ebSPetr Mladek unsigned long hung = 0; 50563494fc30STejun Heo 5057a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 50583494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 50593494fc30STejun Heo goto next_pool; 5060335a42ebSPetr Mladek 5061335a42ebSPetr Mladek /* How long the first pending work is waiting for a worker. */ 5062335a42ebSPetr Mladek if (!list_empty(&pool->worklist)) 5063335a42ebSPetr Mladek hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; 5064335a42ebSPetr Mladek 506557116ce1SJohan Hovold /* 506657116ce1SJohan Hovold * Defer printing to avoid deadlocks in console drivers that 506757116ce1SJohan Hovold * queue work while holding locks also taken in their write 506857116ce1SJohan Hovold * paths. 506957116ce1SJohan Hovold */ 507057116ce1SJohan Hovold printk_deferred_enter(); 50713494fc30STejun Heo pr_info("pool %d:", pool->id); 50723494fc30STejun Heo pr_cont_pool_info(pool); 5073335a42ebSPetr Mladek pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); 50743494fc30STejun Heo if (pool->manager) 50753494fc30STejun Heo pr_cont(" manager: %d", 50763494fc30STejun Heo task_pid_nr(pool->manager->task)); 50773494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 50783494fc30STejun Heo pr_cont(" %s%d", first ? "idle: " : "", 50793494fc30STejun Heo task_pid_nr(worker->task)); 50803494fc30STejun Heo first = false; 50813494fc30STejun Heo } 50823494fc30STejun Heo pr_cont("\n"); 508357116ce1SJohan Hovold printk_deferred_exit(); 50843494fc30STejun Heo next_pool: 5085a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 508662635ea8SSergey Senozhatsky /* 508762635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 508855df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 508962635ea8SSergey Senozhatsky * hard lockup. 509062635ea8SSergey Senozhatsky */ 509162635ea8SSergey Senozhatsky touch_nmi_watchdog(); 509255df0933SImran Khan 50933494fc30STejun Heo } 50943494fc30STejun Heo 509555df0933SImran Khan /** 509655df0933SImran Khan * show_all_workqueues - dump workqueue state 509755df0933SImran Khan * 5098704bc669SJungseung Lee * Called from a sysrq handler and prints out all busy workqueues and pools. 509955df0933SImran Khan */ 510055df0933SImran Khan void show_all_workqueues(void) 510155df0933SImran Khan { 510255df0933SImran Khan struct workqueue_struct *wq; 510355df0933SImran Khan struct worker_pool *pool; 510455df0933SImran Khan int pi; 510555df0933SImran Khan 510655df0933SImran Khan rcu_read_lock(); 510755df0933SImran Khan 510855df0933SImran Khan pr_info("Showing busy workqueues and worker pools:\n"); 510955df0933SImran Khan 511055df0933SImran Khan list_for_each_entry_rcu(wq, &workqueues, list) 511155df0933SImran Khan show_one_workqueue(wq); 511255df0933SImran Khan 511355df0933SImran Khan for_each_pool(pool, pi) 511455df0933SImran Khan show_one_worker_pool(pool); 511555df0933SImran Khan 511624acfb71SThomas Gleixner rcu_read_unlock(); 51173494fc30STejun Heo } 51183494fc30STejun Heo 5119704bc669SJungseung Lee /** 5120704bc669SJungseung Lee * show_freezable_workqueues - dump freezable workqueue state 5121704bc669SJungseung Lee * 5122704bc669SJungseung Lee * Called from try_to_freeze_tasks() and prints out all freezable workqueues 5123704bc669SJungseung Lee * still busy. 5124704bc669SJungseung Lee */ 5125704bc669SJungseung Lee void show_freezable_workqueues(void) 5126704bc669SJungseung Lee { 5127704bc669SJungseung Lee struct workqueue_struct *wq; 5128704bc669SJungseung Lee 5129704bc669SJungseung Lee rcu_read_lock(); 5130704bc669SJungseung Lee 5131704bc669SJungseung Lee pr_info("Showing freezable workqueues that are still busy:\n"); 5132704bc669SJungseung Lee 5133704bc669SJungseung Lee list_for_each_entry_rcu(wq, &workqueues, list) { 5134704bc669SJungseung Lee if (!(wq->flags & WQ_FREEZABLE)) 5135704bc669SJungseung Lee continue; 5136704bc669SJungseung Lee show_one_workqueue(wq); 5137704bc669SJungseung Lee } 5138704bc669SJungseung Lee 5139704bc669SJungseung Lee rcu_read_unlock(); 5140704bc669SJungseung Lee } 5141704bc669SJungseung Lee 51426b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 51436b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 51446b59808bSTejun Heo { 51456b59808bSTejun Heo int off; 51466b59808bSTejun Heo 51476b59808bSTejun Heo /* always show the actual comm */ 51486b59808bSTejun Heo off = strscpy(buf, task->comm, size); 51496b59808bSTejun Heo if (off < 0) 51506b59808bSTejun Heo return; 51516b59808bSTejun Heo 5152197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 51536b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 51546b59808bSTejun Heo 5155197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 5156197f6accSTejun Heo struct worker *worker = kthread_data(task); 5157197f6accSTejun Heo struct worker_pool *pool = worker->pool; 51586b59808bSTejun Heo 51596b59808bSTejun Heo if (pool) { 5160a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 51616b59808bSTejun Heo /* 5162197f6accSTejun Heo * ->desc tracks information (wq name or 5163197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 5164197f6accSTejun Heo * current, prepend '+', otherwise '-'. 51656b59808bSTejun Heo */ 51666b59808bSTejun Heo if (worker->desc[0] != '\0') { 51676b59808bSTejun Heo if (worker->current_work) 51686b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 51696b59808bSTejun Heo worker->desc); 51706b59808bSTejun Heo else 51716b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 51726b59808bSTejun Heo worker->desc); 51736b59808bSTejun Heo } 5174a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 51756b59808bSTejun Heo } 5176197f6accSTejun Heo } 51776b59808bSTejun Heo 51786b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 51796b59808bSTejun Heo } 51806b59808bSTejun Heo 518166448bc2SMathieu Malaterre #ifdef CONFIG_SMP 518266448bc2SMathieu Malaterre 5183db7bccf4STejun Heo /* 5184db7bccf4STejun Heo * CPU hotplug. 5185db7bccf4STejun Heo * 5186e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 5187112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 5188706026c2STejun Heo * pool which make migrating pending and scheduled works very 5189e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 519094cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 5191e22bee78STejun Heo * blocked draining impractical. 5192e22bee78STejun Heo * 519324647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 5194628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 5195628c78e7STejun Heo * cpu comes back online. 5196db7bccf4STejun Heo */ 5197db7bccf4STejun Heo 5198e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 5199db7bccf4STejun Heo { 52004ce62e9eSTejun Heo struct worker_pool *pool; 5201db7bccf4STejun Heo struct worker *worker; 5202db7bccf4STejun Heo 5203f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 52041258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 5205a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 5206e22bee78STejun Heo 5207f2d5a0eeSTejun Heo /* 520892f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 520994cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 521011b45b0bSLai Jiangshan * must be on the cpu. After this, they may become diasporas. 5211b4ac9384SLai Jiangshan * And the preemption disabled section in their sched callbacks 5212b4ac9384SLai Jiangshan * are guaranteed to see WORKER_UNBOUND since the code here 5213b4ac9384SLai Jiangshan * is on the same cpu. 5214f2d5a0eeSTejun Heo */ 5215da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5216403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 5217db7bccf4STejun Heo 521824647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 5219f2d5a0eeSTejun Heo 5220e22bee78STejun Heo /* 5221989442d7SLai Jiangshan * The handling of nr_running in sched callbacks are disabled 5222989442d7SLai Jiangshan * now. Zap nr_running. After this, nr_running stays zero and 5223989442d7SLai Jiangshan * need_more_worker() and keep_working() are always true as 5224989442d7SLai Jiangshan * long as the worklist is not empty. This pool now behaves as 5225989442d7SLai Jiangshan * an unbound (in terms of concurrency management) pool which 5226eb283428SLai Jiangshan * are served by workers tied to the pool. 5227e22bee78STejun Heo */ 5228bc35f7efSLai Jiangshan pool->nr_running = 0; 5229eb283428SLai Jiangshan 5230eb283428SLai Jiangshan /* 5231eb283428SLai Jiangshan * With concurrency management just turned off, a busy 5232eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 5233eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 5234eb283428SLai Jiangshan */ 5235eb283428SLai Jiangshan wake_up_worker(pool); 5236989442d7SLai Jiangshan 5237a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 5238989442d7SLai Jiangshan 5239793777bcSValentin Schneider for_each_pool_worker(worker, pool) 5240793777bcSValentin Schneider unbind_worker(worker); 5241989442d7SLai Jiangshan 5242989442d7SLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 5243eb283428SLai Jiangshan } 5244db7bccf4STejun Heo } 5245db7bccf4STejun Heo 5246bd7c089eSTejun Heo /** 5247bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 5248bd7c089eSTejun Heo * @pool: pool of interest 5249bd7c089eSTejun Heo * 5250a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 5251bd7c089eSTejun Heo */ 5252bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 5253bd7c089eSTejun Heo { 5254a9ab775bSTejun Heo struct worker *worker; 5255bd7c089eSTejun Heo 52561258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 5257bd7c089eSTejun Heo 5258bd7c089eSTejun Heo /* 5259a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 5260a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 5261402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 5262a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 5263a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 5264bd7c089eSTejun Heo */ 5265c63a2e52SValentin Schneider for_each_pool_worker(worker, pool) { 5266c63a2e52SValentin Schneider kthread_set_per_cpu(worker->task, pool->cpu); 5267c63a2e52SValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 5268c63a2e52SValentin Schneider pool->attrs->cpumask) < 0); 5269c63a2e52SValentin Schneider } 5270a9ab775bSTejun Heo 5271a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 5272f7c17d26SWanpeng Li 52733de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 5274a9ab775bSTejun Heo 5275da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 5276a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 5277a9ab775bSTejun Heo 5278a9ab775bSTejun Heo /* 5279a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 5280a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 5281a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 5282a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 5283a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 5284a9ab775bSTejun Heo * concurrency management. Note that when or whether 5285a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 5286a9ab775bSTejun Heo * 5287c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 5288a9ab775bSTejun Heo * tested without holding any lock in 52896d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may 5290a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 5291a9ab775bSTejun Heo * management operations. 5292bd7c089eSTejun Heo */ 5293a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 5294a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 5295a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 5296c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 5297bd7c089eSTejun Heo } 5298a9ab775bSTejun Heo 5299a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 5300bd7c089eSTejun Heo } 5301bd7c089eSTejun Heo 53027dbc725eSTejun Heo /** 53037dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 53047dbc725eSTejun Heo * @pool: unbound pool of interest 53057dbc725eSTejun Heo * @cpu: the CPU which is coming up 53067dbc725eSTejun Heo * 53077dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 53087dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 53097dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 53107dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 53117dbc725eSTejun Heo */ 53127dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 53137dbc725eSTejun Heo { 53147dbc725eSTejun Heo static cpumask_t cpumask; 53157dbc725eSTejun Heo struct worker *worker; 53167dbc725eSTejun Heo 53171258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 53187dbc725eSTejun Heo 53197dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 53207dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 53217dbc725eSTejun Heo return; 53227dbc725eSTejun Heo 53237dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 53247dbc725eSTejun Heo 53257dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 5326da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5327d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 53287dbc725eSTejun Heo } 53297dbc725eSTejun Heo 53307ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 53311da177e4SLinus Torvalds { 53324ce62e9eSTejun Heo struct worker_pool *pool; 53331da177e4SLinus Torvalds 5334f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 53353ce63377STejun Heo if (pool->nr_workers) 53363ce63377STejun Heo continue; 5337051e1850SLai Jiangshan if (!create_worker(pool)) 53387ee681b2SThomas Gleixner return -ENOMEM; 53393af24433SOleg Nesterov } 53407ee681b2SThomas Gleixner return 0; 53417ee681b2SThomas Gleixner } 53421da177e4SLinus Torvalds 53437ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 53447ee681b2SThomas Gleixner { 53457ee681b2SThomas Gleixner struct worker_pool *pool; 53467ee681b2SThomas Gleixner struct workqueue_struct *wq; 53477ee681b2SThomas Gleixner int pi; 53487ee681b2SThomas Gleixner 534968e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 53507dbc725eSTejun Heo 53517dbc725eSTejun Heo for_each_pool(pool, pi) { 53521258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 535394cf58bbSTejun Heo 5354f05b558dSLai Jiangshan if (pool->cpu == cpu) 535594cf58bbSTejun Heo rebind_workers(pool); 5356f05b558dSLai Jiangshan else if (pool->cpu < 0) 53577dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 535894cf58bbSTejun Heo 53591258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 536094cf58bbSTejun Heo } 53617dbc725eSTejun Heo 53624c16bd32STejun Heo /* update NUMA affinity of unbound workqueues */ 53634c16bd32STejun Heo list_for_each_entry(wq, &workqueues, list) 53644c16bd32STejun Heo wq_update_unbound_numa(wq, cpu, true); 53654c16bd32STejun Heo 536668e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 53677ee681b2SThomas Gleixner return 0; 536865758202STejun Heo } 536965758202STejun Heo 53707ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 537165758202STejun Heo { 53724c16bd32STejun Heo struct workqueue_struct *wq; 53738db25e78STejun Heo 53744c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 5375e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 5376e8b3f8dbSLai Jiangshan return -1; 5377e8b3f8dbSLai Jiangshan 5378e8b3f8dbSLai Jiangshan unbind_workers(cpu); 53794c16bd32STejun Heo 53804c16bd32STejun Heo /* update NUMA affinity of unbound workqueues */ 53814c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 53824c16bd32STejun Heo list_for_each_entry(wq, &workqueues, list) 53834c16bd32STejun Heo wq_update_unbound_numa(wq, cpu, false); 53844c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 53854c16bd32STejun Heo 53867ee681b2SThomas Gleixner return 0; 538765758202STejun Heo } 538865758202STejun Heo 53892d3854a3SRusty Russell struct work_for_cpu { 5390ed48ece2STejun Heo struct work_struct work; 53912d3854a3SRusty Russell long (*fn)(void *); 53922d3854a3SRusty Russell void *arg; 53932d3854a3SRusty Russell long ret; 53942d3854a3SRusty Russell }; 53952d3854a3SRusty Russell 5396ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 53972d3854a3SRusty Russell { 5398ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 5399ed48ece2STejun Heo 54002d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 54012d3854a3SRusty Russell } 54022d3854a3SRusty Russell 54032d3854a3SRusty Russell /** 540422aceb31SAnna-Maria Gleixner * work_on_cpu - run a function in thread context on a particular cpu 54052d3854a3SRusty Russell * @cpu: the cpu to run on 54062d3854a3SRusty Russell * @fn: the function to run 54072d3854a3SRusty Russell * @arg: the function arg 54082d3854a3SRusty Russell * 540931ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 54106b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 5411d185af30SYacine Belkadi * 5412d185af30SYacine Belkadi * Return: The value @fn returns. 54132d3854a3SRusty Russell */ 5414d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg) 54152d3854a3SRusty Russell { 5416ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 54172d3854a3SRusty Russell 5418ed48ece2STejun Heo INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn); 5419ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 542012997d1aSBjorn Helgaas flush_work(&wfc.work); 5421440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 54222d3854a3SRusty Russell return wfc.ret; 54232d3854a3SRusty Russell } 54242d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 54250e8d6a93SThomas Gleixner 54260e8d6a93SThomas Gleixner /** 54270e8d6a93SThomas Gleixner * work_on_cpu_safe - run a function in thread context on a particular cpu 54280e8d6a93SThomas Gleixner * @cpu: the cpu to run on 54290e8d6a93SThomas Gleixner * @fn: the function to run 54300e8d6a93SThomas Gleixner * @arg: the function argument 54310e8d6a93SThomas Gleixner * 54320e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 54330e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 54340e8d6a93SThomas Gleixner * 54350e8d6a93SThomas Gleixner * Return: The value @fn returns. 54360e8d6a93SThomas Gleixner */ 54370e8d6a93SThomas Gleixner long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg) 54380e8d6a93SThomas Gleixner { 54390e8d6a93SThomas Gleixner long ret = -ENODEV; 54400e8d6a93SThomas Gleixner 5441ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 54420e8d6a93SThomas Gleixner if (cpu_online(cpu)) 54430e8d6a93SThomas Gleixner ret = work_on_cpu(cpu, fn, arg); 5444ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 54450e8d6a93SThomas Gleixner return ret; 54460e8d6a93SThomas Gleixner } 54470e8d6a93SThomas Gleixner EXPORT_SYMBOL_GPL(work_on_cpu_safe); 54482d3854a3SRusty Russell #endif /* CONFIG_SMP */ 54492d3854a3SRusty Russell 5450a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 5451e7577c50SRusty Russell 5452a0a1a5fdSTejun Heo /** 5453a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 5454a0a1a5fdSTejun Heo * 545558a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 5456f97a4a1aSLai Jiangshan * workqueues will queue new works to their inactive_works list instead of 5457706026c2STejun Heo * pool->worklist. 5458a0a1a5fdSTejun Heo * 5459a0a1a5fdSTejun Heo * CONTEXT: 5460a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5461a0a1a5fdSTejun Heo */ 5462a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 5463a0a1a5fdSTejun Heo { 546424b8a847STejun Heo struct workqueue_struct *wq; 546524b8a847STejun Heo struct pool_workqueue *pwq; 5466a0a1a5fdSTejun Heo 546768e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5468a0a1a5fdSTejun Heo 54696183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 5470a0a1a5fdSTejun Heo workqueue_freezing = true; 5471a0a1a5fdSTejun Heo 547224b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 5473a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5474699ce097STejun Heo for_each_pwq(pwq, wq) 5475699ce097STejun Heo pwq_adjust_max_active(pwq); 5476a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5477a1056305STejun Heo } 54785bcab335STejun Heo 547968e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5480a0a1a5fdSTejun Heo } 5481a0a1a5fdSTejun Heo 5482a0a1a5fdSTejun Heo /** 548358a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 5484a0a1a5fdSTejun Heo * 5485a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 5486a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 5487a0a1a5fdSTejun Heo * 5488a0a1a5fdSTejun Heo * CONTEXT: 548968e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 5490a0a1a5fdSTejun Heo * 5491d185af30SYacine Belkadi * Return: 549258a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 549358a69cb4STejun Heo * is complete. 5494a0a1a5fdSTejun Heo */ 5495a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 5496a0a1a5fdSTejun Heo { 5497a0a1a5fdSTejun Heo bool busy = false; 549824b8a847STejun Heo struct workqueue_struct *wq; 549924b8a847STejun Heo struct pool_workqueue *pwq; 5500a0a1a5fdSTejun Heo 550168e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5502a0a1a5fdSTejun Heo 55036183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 5504a0a1a5fdSTejun Heo 550524b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 550624b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 550724b8a847STejun Heo continue; 5508a0a1a5fdSTejun Heo /* 5509a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 5510a0a1a5fdSTejun Heo * to peek without lock. 5511a0a1a5fdSTejun Heo */ 551224acfb71SThomas Gleixner rcu_read_lock(); 551324b8a847STejun Heo for_each_pwq(pwq, wq) { 55146183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 5515112202d9STejun Heo if (pwq->nr_active) { 5516a0a1a5fdSTejun Heo busy = true; 551724acfb71SThomas Gleixner rcu_read_unlock(); 5518a0a1a5fdSTejun Heo goto out_unlock; 5519a0a1a5fdSTejun Heo } 5520a0a1a5fdSTejun Heo } 552124acfb71SThomas Gleixner rcu_read_unlock(); 5522a0a1a5fdSTejun Heo } 5523a0a1a5fdSTejun Heo out_unlock: 552468e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5525a0a1a5fdSTejun Heo return busy; 5526a0a1a5fdSTejun Heo } 5527a0a1a5fdSTejun Heo 5528a0a1a5fdSTejun Heo /** 5529a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 5530a0a1a5fdSTejun Heo * 5531a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 5532706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 5533a0a1a5fdSTejun Heo * 5534a0a1a5fdSTejun Heo * CONTEXT: 5535a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5536a0a1a5fdSTejun Heo */ 5537a0a1a5fdSTejun Heo void thaw_workqueues(void) 5538a0a1a5fdSTejun Heo { 553924b8a847STejun Heo struct workqueue_struct *wq; 554024b8a847STejun Heo struct pool_workqueue *pwq; 5541a0a1a5fdSTejun Heo 554268e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5543a0a1a5fdSTejun Heo 5544a0a1a5fdSTejun Heo if (!workqueue_freezing) 5545a0a1a5fdSTejun Heo goto out_unlock; 5546a0a1a5fdSTejun Heo 554774b414eaSLai Jiangshan workqueue_freezing = false; 554824b8a847STejun Heo 554924b8a847STejun Heo /* restore max_active and repopulate worklist */ 555024b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 5551a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5552699ce097STejun Heo for_each_pwq(pwq, wq) 5553699ce097STejun Heo pwq_adjust_max_active(pwq); 5554a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 555524b8a847STejun Heo } 555624b8a847STejun Heo 5557a0a1a5fdSTejun Heo out_unlock: 555868e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5559a0a1a5fdSTejun Heo } 5560a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 5561a0a1a5fdSTejun Heo 556299c621efSLai Jiangshan static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) 5563042f7df1SLai Jiangshan { 5564042f7df1SLai Jiangshan LIST_HEAD(ctxs); 5565042f7df1SLai Jiangshan int ret = 0; 5566042f7df1SLai Jiangshan struct workqueue_struct *wq; 5567042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 5568042f7df1SLai Jiangshan 5569042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5570042f7df1SLai Jiangshan 5571042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 5572042f7df1SLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 5573042f7df1SLai Jiangshan continue; 5574042f7df1SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 5575042f7df1SLai Jiangshan if (wq->flags & __WQ_ORDERED) 5576042f7df1SLai Jiangshan continue; 5577042f7df1SLai Jiangshan 557899c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); 5579042f7df1SLai Jiangshan if (!ctx) { 5580042f7df1SLai Jiangshan ret = -ENOMEM; 5581042f7df1SLai Jiangshan break; 5582042f7df1SLai Jiangshan } 5583042f7df1SLai Jiangshan 5584042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 5585042f7df1SLai Jiangshan } 5586042f7df1SLai Jiangshan 5587042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 5588042f7df1SLai Jiangshan if (!ret) 5589042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 5590042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 5591042f7df1SLai Jiangshan } 5592042f7df1SLai Jiangshan 559399c621efSLai Jiangshan if (!ret) { 559499c621efSLai Jiangshan mutex_lock(&wq_pool_attach_mutex); 559599c621efSLai Jiangshan cpumask_copy(wq_unbound_cpumask, unbound_cpumask); 559699c621efSLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 559799c621efSLai Jiangshan } 5598042f7df1SLai Jiangshan return ret; 5599042f7df1SLai Jiangshan } 5600042f7df1SLai Jiangshan 5601042f7df1SLai Jiangshan /** 5602042f7df1SLai Jiangshan * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 5603042f7df1SLai Jiangshan * @cpumask: the cpumask to set 5604042f7df1SLai Jiangshan * 5605042f7df1SLai Jiangshan * The low-level workqueues cpumask is a global cpumask that limits 5606042f7df1SLai Jiangshan * the affinity of all unbound workqueues. This function check the @cpumask 5607042f7df1SLai Jiangshan * and apply it to all unbound workqueues and updates all pwqs of them. 5608042f7df1SLai Jiangshan * 560967dc8325SCai Huoqing * Return: 0 - Success 5610042f7df1SLai Jiangshan * -EINVAL - Invalid @cpumask 5611042f7df1SLai Jiangshan * -ENOMEM - Failed to allocate memory for attrs or pwqs. 5612042f7df1SLai Jiangshan */ 5613042f7df1SLai Jiangshan int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 5614042f7df1SLai Jiangshan { 5615042f7df1SLai Jiangshan int ret = -EINVAL; 5616042f7df1SLai Jiangshan 5617c98a9805STal Shorer /* 5618c98a9805STal Shorer * Not excluding isolated cpus on purpose. 5619c98a9805STal Shorer * If the user wishes to include them, we allow that. 5620c98a9805STal Shorer */ 5621042f7df1SLai Jiangshan cpumask_and(cpumask, cpumask, cpu_possible_mask); 5622042f7df1SLai Jiangshan if (!cpumask_empty(cpumask)) { 5623a0111cf6SLai Jiangshan apply_wqattrs_lock(); 5624d25302e4SMenglong Dong if (cpumask_equal(cpumask, wq_unbound_cpumask)) { 5625d25302e4SMenglong Dong ret = 0; 5626d25302e4SMenglong Dong goto out_unlock; 5627d25302e4SMenglong Dong } 5628d25302e4SMenglong Dong 562999c621efSLai Jiangshan ret = workqueue_apply_unbound_cpumask(cpumask); 5630042f7df1SLai Jiangshan 5631d25302e4SMenglong Dong out_unlock: 5632a0111cf6SLai Jiangshan apply_wqattrs_unlock(); 5633042f7df1SLai Jiangshan } 5634042f7df1SLai Jiangshan 5635042f7df1SLai Jiangshan return ret; 5636042f7df1SLai Jiangshan } 5637042f7df1SLai Jiangshan 56386ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 56396ba94429SFrederic Weisbecker /* 56406ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 56416ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 56426ba94429SFrederic Weisbecker * following attributes. 56436ba94429SFrederic Weisbecker * 56446ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 56456ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 56466ba94429SFrederic Weisbecker * 56476ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 56486ba94429SFrederic Weisbecker * 56499a19b463SWang Long * pool_ids RO int : the associated pool IDs for each node 56506ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 56516ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 56529a19b463SWang Long * numa RW bool : whether enable NUMA affinity 56536ba94429SFrederic Weisbecker */ 56546ba94429SFrederic Weisbecker struct wq_device { 56556ba94429SFrederic Weisbecker struct workqueue_struct *wq; 56566ba94429SFrederic Weisbecker struct device dev; 56576ba94429SFrederic Weisbecker }; 56586ba94429SFrederic Weisbecker 56596ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 56606ba94429SFrederic Weisbecker { 56616ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 56626ba94429SFrederic Weisbecker 56636ba94429SFrederic Weisbecker return wq_dev->wq; 56646ba94429SFrederic Weisbecker } 56656ba94429SFrederic Weisbecker 56666ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 56676ba94429SFrederic Weisbecker char *buf) 56686ba94429SFrederic Weisbecker { 56696ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 56706ba94429SFrederic Weisbecker 56716ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 56726ba94429SFrederic Weisbecker } 56736ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 56746ba94429SFrederic Weisbecker 56756ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 56766ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 56776ba94429SFrederic Weisbecker { 56786ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 56796ba94429SFrederic Weisbecker 56806ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 56816ba94429SFrederic Weisbecker } 56826ba94429SFrederic Weisbecker 56836ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 56846ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 56856ba94429SFrederic Weisbecker size_t count) 56866ba94429SFrederic Weisbecker { 56876ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 56886ba94429SFrederic Weisbecker int val; 56896ba94429SFrederic Weisbecker 56906ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 56916ba94429SFrederic Weisbecker return -EINVAL; 56926ba94429SFrederic Weisbecker 56936ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 56946ba94429SFrederic Weisbecker return count; 56956ba94429SFrederic Weisbecker } 56966ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 56976ba94429SFrederic Weisbecker 56986ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 56996ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 57006ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 57016ba94429SFrederic Weisbecker NULL, 57026ba94429SFrederic Weisbecker }; 57036ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 57046ba94429SFrederic Weisbecker 57056ba94429SFrederic Weisbecker static ssize_t wq_pool_ids_show(struct device *dev, 57066ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 57076ba94429SFrederic Weisbecker { 57086ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 57096ba94429SFrederic Weisbecker const char *delim = ""; 57106ba94429SFrederic Weisbecker int node, written = 0; 57116ba94429SFrederic Weisbecker 5712ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 571324acfb71SThomas Gleixner rcu_read_lock(); 57146ba94429SFrederic Weisbecker for_each_node(node) { 57156ba94429SFrederic Weisbecker written += scnprintf(buf + written, PAGE_SIZE - written, 57166ba94429SFrederic Weisbecker "%s%d:%d", delim, node, 57176ba94429SFrederic Weisbecker unbound_pwq_by_node(wq, node)->pool->id); 57186ba94429SFrederic Weisbecker delim = " "; 57196ba94429SFrederic Weisbecker } 57206ba94429SFrederic Weisbecker written += scnprintf(buf + written, PAGE_SIZE - written, "\n"); 572124acfb71SThomas Gleixner rcu_read_unlock(); 5722ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 57236ba94429SFrederic Weisbecker 57246ba94429SFrederic Weisbecker return written; 57256ba94429SFrederic Weisbecker } 57266ba94429SFrederic Weisbecker 57276ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 57286ba94429SFrederic Weisbecker char *buf) 57296ba94429SFrederic Weisbecker { 57306ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 57316ba94429SFrederic Weisbecker int written; 57326ba94429SFrederic Weisbecker 57336ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 57346ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 57356ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 57366ba94429SFrederic Weisbecker 57376ba94429SFrederic Weisbecker return written; 57386ba94429SFrederic Weisbecker } 57396ba94429SFrederic Weisbecker 57406ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 57416ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 57426ba94429SFrederic Weisbecker { 57436ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 57446ba94429SFrederic Weisbecker 5745899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5746899a94feSLai Jiangshan 5747be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs(); 57486ba94429SFrederic Weisbecker if (!attrs) 57496ba94429SFrederic Weisbecker return NULL; 57506ba94429SFrederic Weisbecker 57516ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 57526ba94429SFrederic Weisbecker return attrs; 57536ba94429SFrederic Weisbecker } 57546ba94429SFrederic Weisbecker 57556ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 57566ba94429SFrederic Weisbecker const char *buf, size_t count) 57576ba94429SFrederic Weisbecker { 57586ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 57596ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5760d4d3e257SLai Jiangshan int ret = -ENOMEM; 5761d4d3e257SLai Jiangshan 5762d4d3e257SLai Jiangshan apply_wqattrs_lock(); 57636ba94429SFrederic Weisbecker 57646ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 57656ba94429SFrederic Weisbecker if (!attrs) 5766d4d3e257SLai Jiangshan goto out_unlock; 57676ba94429SFrederic Weisbecker 57686ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 57696ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 5770d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 57716ba94429SFrederic Weisbecker else 57726ba94429SFrederic Weisbecker ret = -EINVAL; 57736ba94429SFrederic Weisbecker 5774d4d3e257SLai Jiangshan out_unlock: 5775d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 57766ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 57776ba94429SFrederic Weisbecker return ret ?: count; 57786ba94429SFrederic Weisbecker } 57796ba94429SFrederic Weisbecker 57806ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 57816ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 57826ba94429SFrederic Weisbecker { 57836ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 57846ba94429SFrederic Weisbecker int written; 57856ba94429SFrederic Weisbecker 57866ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 57876ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 57886ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 57896ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 57906ba94429SFrederic Weisbecker return written; 57916ba94429SFrederic Weisbecker } 57926ba94429SFrederic Weisbecker 57936ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 57946ba94429SFrederic Weisbecker struct device_attribute *attr, 57956ba94429SFrederic Weisbecker const char *buf, size_t count) 57966ba94429SFrederic Weisbecker { 57976ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 57986ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5799d4d3e257SLai Jiangshan int ret = -ENOMEM; 5800d4d3e257SLai Jiangshan 5801d4d3e257SLai Jiangshan apply_wqattrs_lock(); 58026ba94429SFrederic Weisbecker 58036ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 58046ba94429SFrederic Weisbecker if (!attrs) 5805d4d3e257SLai Jiangshan goto out_unlock; 58066ba94429SFrederic Weisbecker 58076ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 58086ba94429SFrederic Weisbecker if (!ret) 5809d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 58106ba94429SFrederic Weisbecker 5811d4d3e257SLai Jiangshan out_unlock: 5812d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 58136ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 58146ba94429SFrederic Weisbecker return ret ?: count; 58156ba94429SFrederic Weisbecker } 58166ba94429SFrederic Weisbecker 58176ba94429SFrederic Weisbecker static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr, 58186ba94429SFrederic Weisbecker char *buf) 58196ba94429SFrederic Weisbecker { 58206ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 58216ba94429SFrederic Weisbecker int written; 58226ba94429SFrederic Weisbecker 58236ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 58246ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", 58256ba94429SFrederic Weisbecker !wq->unbound_attrs->no_numa); 58266ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 58276ba94429SFrederic Weisbecker 58286ba94429SFrederic Weisbecker return written; 58296ba94429SFrederic Weisbecker } 58306ba94429SFrederic Weisbecker 58316ba94429SFrederic Weisbecker static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr, 58326ba94429SFrederic Weisbecker const char *buf, size_t count) 58336ba94429SFrederic Weisbecker { 58346ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 58356ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5836d4d3e257SLai Jiangshan int v, ret = -ENOMEM; 5837d4d3e257SLai Jiangshan 5838d4d3e257SLai Jiangshan apply_wqattrs_lock(); 58396ba94429SFrederic Weisbecker 58406ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 58416ba94429SFrederic Weisbecker if (!attrs) 5842d4d3e257SLai Jiangshan goto out_unlock; 58436ba94429SFrederic Weisbecker 58446ba94429SFrederic Weisbecker ret = -EINVAL; 58456ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &v) == 1) { 58466ba94429SFrederic Weisbecker attrs->no_numa = !v; 5847d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 58486ba94429SFrederic Weisbecker } 58496ba94429SFrederic Weisbecker 5850d4d3e257SLai Jiangshan out_unlock: 5851d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 58526ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 58536ba94429SFrederic Weisbecker return ret ?: count; 58546ba94429SFrederic Weisbecker } 58556ba94429SFrederic Weisbecker 58566ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 58576ba94429SFrederic Weisbecker __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL), 58586ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 58596ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 58606ba94429SFrederic Weisbecker __ATTR(numa, 0644, wq_numa_show, wq_numa_store), 58616ba94429SFrederic Weisbecker __ATTR_NULL, 58626ba94429SFrederic Weisbecker }; 58636ba94429SFrederic Weisbecker 58646ba94429SFrederic Weisbecker static struct bus_type wq_subsys = { 58656ba94429SFrederic Weisbecker .name = "workqueue", 58666ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 58676ba94429SFrederic Weisbecker }; 58686ba94429SFrederic Weisbecker 5869b05a7928SFrederic Weisbecker static ssize_t wq_unbound_cpumask_show(struct device *dev, 5870b05a7928SFrederic Weisbecker struct device_attribute *attr, char *buf) 5871b05a7928SFrederic Weisbecker { 5872b05a7928SFrederic Weisbecker int written; 5873b05a7928SFrederic Weisbecker 5874042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 5875b05a7928SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 5876b05a7928SFrederic Weisbecker cpumask_pr_args(wq_unbound_cpumask)); 5877042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5878b05a7928SFrederic Weisbecker 5879b05a7928SFrederic Weisbecker return written; 5880b05a7928SFrederic Weisbecker } 5881b05a7928SFrederic Weisbecker 5882042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 5883042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 5884042f7df1SLai Jiangshan { 5885042f7df1SLai Jiangshan cpumask_var_t cpumask; 5886042f7df1SLai Jiangshan int ret; 5887042f7df1SLai Jiangshan 5888042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 5889042f7df1SLai Jiangshan return -ENOMEM; 5890042f7df1SLai Jiangshan 5891042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 5892042f7df1SLai Jiangshan if (!ret) 5893042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 5894042f7df1SLai Jiangshan 5895042f7df1SLai Jiangshan free_cpumask_var(cpumask); 5896042f7df1SLai Jiangshan return ret ? ret : count; 5897042f7df1SLai Jiangshan } 5898042f7df1SLai Jiangshan 5899b05a7928SFrederic Weisbecker static struct device_attribute wq_sysfs_cpumask_attr = 5900042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 5901042f7df1SLai Jiangshan wq_unbound_cpumask_store); 5902b05a7928SFrederic Weisbecker 59036ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 59046ba94429SFrederic Weisbecker { 5905686f6697SGreg Kroah-Hartman struct device *dev_root; 5906b05a7928SFrederic Weisbecker int err; 5907b05a7928SFrederic Weisbecker 5908b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 5909b05a7928SFrederic Weisbecker if (err) 5910b05a7928SFrederic Weisbecker return err; 5911b05a7928SFrederic Weisbecker 5912686f6697SGreg Kroah-Hartman dev_root = bus_get_dev_root(&wq_subsys); 5913686f6697SGreg Kroah-Hartman if (dev_root) { 5914686f6697SGreg Kroah-Hartman err = device_create_file(dev_root, &wq_sysfs_cpumask_attr); 5915686f6697SGreg Kroah-Hartman put_device(dev_root); 5916686f6697SGreg Kroah-Hartman } 5917686f6697SGreg Kroah-Hartman return err; 59186ba94429SFrederic Weisbecker } 59196ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 59206ba94429SFrederic Weisbecker 59216ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 59226ba94429SFrederic Weisbecker { 59236ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 59246ba94429SFrederic Weisbecker 59256ba94429SFrederic Weisbecker kfree(wq_dev); 59266ba94429SFrederic Weisbecker } 59276ba94429SFrederic Weisbecker 59286ba94429SFrederic Weisbecker /** 59296ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 59306ba94429SFrederic Weisbecker * @wq: the workqueue to register 59316ba94429SFrederic Weisbecker * 59326ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 59336ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 59346ba94429SFrederic Weisbecker * which is the preferred method. 59356ba94429SFrederic Weisbecker * 59366ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 59376ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 59386ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 59396ba94429SFrederic Weisbecker * attributes. 59406ba94429SFrederic Weisbecker * 59416ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 59426ba94429SFrederic Weisbecker */ 59436ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 59446ba94429SFrederic Weisbecker { 59456ba94429SFrederic Weisbecker struct wq_device *wq_dev; 59466ba94429SFrederic Weisbecker int ret; 59476ba94429SFrederic Weisbecker 59486ba94429SFrederic Weisbecker /* 5949402dd89dSShailendra Verma * Adjusting max_active or creating new pwqs by applying 59506ba94429SFrederic Weisbecker * attributes breaks ordering guarantee. Disallow exposing ordered 59516ba94429SFrederic Weisbecker * workqueues. 59526ba94429SFrederic Weisbecker */ 59530a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 59546ba94429SFrederic Weisbecker return -EINVAL; 59556ba94429SFrederic Weisbecker 59566ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 59576ba94429SFrederic Weisbecker if (!wq_dev) 59586ba94429SFrederic Weisbecker return -ENOMEM; 59596ba94429SFrederic Weisbecker 59606ba94429SFrederic Weisbecker wq_dev->wq = wq; 59616ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 59626ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 596323217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 59646ba94429SFrederic Weisbecker 59656ba94429SFrederic Weisbecker /* 59666ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 59676ba94429SFrederic Weisbecker * everything is ready. 59686ba94429SFrederic Weisbecker */ 59696ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 59706ba94429SFrederic Weisbecker 59716ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 59726ba94429SFrederic Weisbecker if (ret) { 5973537f4146SArvind Yadav put_device(&wq_dev->dev); 59746ba94429SFrederic Weisbecker wq->wq_dev = NULL; 59756ba94429SFrederic Weisbecker return ret; 59766ba94429SFrederic Weisbecker } 59776ba94429SFrederic Weisbecker 59786ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 59796ba94429SFrederic Weisbecker struct device_attribute *attr; 59806ba94429SFrederic Weisbecker 59816ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 59826ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 59836ba94429SFrederic Weisbecker if (ret) { 59846ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 59856ba94429SFrederic Weisbecker wq->wq_dev = NULL; 59866ba94429SFrederic Weisbecker return ret; 59876ba94429SFrederic Weisbecker } 59886ba94429SFrederic Weisbecker } 59896ba94429SFrederic Weisbecker } 59906ba94429SFrederic Weisbecker 59916ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 59926ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 59936ba94429SFrederic Weisbecker return 0; 59946ba94429SFrederic Weisbecker } 59956ba94429SFrederic Weisbecker 59966ba94429SFrederic Weisbecker /** 59976ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 59986ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 59996ba94429SFrederic Weisbecker * 60006ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 60016ba94429SFrederic Weisbecker */ 60026ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 60036ba94429SFrederic Weisbecker { 60046ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 60056ba94429SFrederic Weisbecker 60066ba94429SFrederic Weisbecker if (!wq->wq_dev) 60076ba94429SFrederic Weisbecker return; 60086ba94429SFrederic Weisbecker 60096ba94429SFrederic Weisbecker wq->wq_dev = NULL; 60106ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 60116ba94429SFrederic Weisbecker } 60126ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 60136ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 60146ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 60156ba94429SFrederic Weisbecker 601682607adcSTejun Heo /* 601782607adcSTejun Heo * Workqueue watchdog. 601882607adcSTejun Heo * 601982607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 602082607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 602182607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 602282607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 602382607adcSTejun Heo * largely opaque. 602482607adcSTejun Heo * 602582607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 602682607adcSTejun Heo * state if some pools failed to make forward progress for a while where 602782607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 602882607adcSTejun Heo * 602982607adcSTejun Heo * This mechanism is controlled through the kernel parameter 603082607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 603182607adcSTejun Heo * corresponding sysfs parameter file. 603282607adcSTejun Heo */ 603382607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 603482607adcSTejun Heo 603582607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 60365cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 603782607adcSTejun Heo 603882607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 603982607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 604082607adcSTejun Heo 6041cd2440d6SPetr Mladek /* 6042cd2440d6SPetr Mladek * Show workers that might prevent the processing of pending work items. 6043cd2440d6SPetr Mladek * The only candidates are CPU-bound workers in the running state. 6044cd2440d6SPetr Mladek * Pending work items should be handled by another idle worker 6045cd2440d6SPetr Mladek * in all other situations. 6046cd2440d6SPetr Mladek */ 6047cd2440d6SPetr Mladek static void show_cpu_pool_hog(struct worker_pool *pool) 6048cd2440d6SPetr Mladek { 6049cd2440d6SPetr Mladek struct worker *worker; 6050cd2440d6SPetr Mladek unsigned long flags; 6051cd2440d6SPetr Mladek int bkt; 6052cd2440d6SPetr Mladek 6053cd2440d6SPetr Mladek raw_spin_lock_irqsave(&pool->lock, flags); 6054cd2440d6SPetr Mladek 6055cd2440d6SPetr Mladek hash_for_each(pool->busy_hash, bkt, worker, hentry) { 6056cd2440d6SPetr Mladek if (task_is_running(worker->task)) { 6057cd2440d6SPetr Mladek /* 6058cd2440d6SPetr Mladek * Defer printing to avoid deadlocks in console 6059cd2440d6SPetr Mladek * drivers that queue work while holding locks 6060cd2440d6SPetr Mladek * also taken in their write paths. 6061cd2440d6SPetr Mladek */ 6062cd2440d6SPetr Mladek printk_deferred_enter(); 6063cd2440d6SPetr Mladek 6064cd2440d6SPetr Mladek pr_info("pool %d:\n", pool->id); 6065cd2440d6SPetr Mladek sched_show_task(worker->task); 6066cd2440d6SPetr Mladek 6067cd2440d6SPetr Mladek printk_deferred_exit(); 6068cd2440d6SPetr Mladek } 6069cd2440d6SPetr Mladek } 6070cd2440d6SPetr Mladek 6071cd2440d6SPetr Mladek raw_spin_unlock_irqrestore(&pool->lock, flags); 6072cd2440d6SPetr Mladek } 6073cd2440d6SPetr Mladek 6074cd2440d6SPetr Mladek static void show_cpu_pools_hogs(void) 6075cd2440d6SPetr Mladek { 6076cd2440d6SPetr Mladek struct worker_pool *pool; 6077cd2440d6SPetr Mladek int pi; 6078cd2440d6SPetr Mladek 6079cd2440d6SPetr Mladek pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); 6080cd2440d6SPetr Mladek 6081cd2440d6SPetr Mladek rcu_read_lock(); 6082cd2440d6SPetr Mladek 6083cd2440d6SPetr Mladek for_each_pool(pool, pi) { 6084cd2440d6SPetr Mladek if (pool->cpu_stall) 6085cd2440d6SPetr Mladek show_cpu_pool_hog(pool); 6086cd2440d6SPetr Mladek 6087cd2440d6SPetr Mladek } 6088cd2440d6SPetr Mladek 6089cd2440d6SPetr Mladek rcu_read_unlock(); 6090cd2440d6SPetr Mladek } 6091cd2440d6SPetr Mladek 609282607adcSTejun Heo static void wq_watchdog_reset_touched(void) 609382607adcSTejun Heo { 609482607adcSTejun Heo int cpu; 609582607adcSTejun Heo 609682607adcSTejun Heo wq_watchdog_touched = jiffies; 609782607adcSTejun Heo for_each_possible_cpu(cpu) 609882607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 609982607adcSTejun Heo } 610082607adcSTejun Heo 61015cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 610282607adcSTejun Heo { 610382607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 610482607adcSTejun Heo bool lockup_detected = false; 6105cd2440d6SPetr Mladek bool cpu_pool_stall = false; 6106940d71c6SSergey Senozhatsky unsigned long now = jiffies; 610782607adcSTejun Heo struct worker_pool *pool; 610882607adcSTejun Heo int pi; 610982607adcSTejun Heo 611082607adcSTejun Heo if (!thresh) 611182607adcSTejun Heo return; 611282607adcSTejun Heo 611382607adcSTejun Heo rcu_read_lock(); 611482607adcSTejun Heo 611582607adcSTejun Heo for_each_pool(pool, pi) { 611682607adcSTejun Heo unsigned long pool_ts, touched, ts; 611782607adcSTejun Heo 6118cd2440d6SPetr Mladek pool->cpu_stall = false; 611982607adcSTejun Heo if (list_empty(&pool->worklist)) 612082607adcSTejun Heo continue; 612182607adcSTejun Heo 6122940d71c6SSergey Senozhatsky /* 6123940d71c6SSergey Senozhatsky * If a virtual machine is stopped by the host it can look to 6124940d71c6SSergey Senozhatsky * the watchdog like a stall. 6125940d71c6SSergey Senozhatsky */ 6126940d71c6SSergey Senozhatsky kvm_check_and_clear_guest_paused(); 6127940d71c6SSergey Senozhatsky 612882607adcSTejun Heo /* get the latest of pool and touched timestamps */ 612989e28ce6SWang Qing if (pool->cpu >= 0) 613089e28ce6SWang Qing touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); 613189e28ce6SWang Qing else 613282607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 613389e28ce6SWang Qing pool_ts = READ_ONCE(pool->watchdog_ts); 613482607adcSTejun Heo 613582607adcSTejun Heo if (time_after(pool_ts, touched)) 613682607adcSTejun Heo ts = pool_ts; 613782607adcSTejun Heo else 613882607adcSTejun Heo ts = touched; 613982607adcSTejun Heo 614082607adcSTejun Heo /* did we stall? */ 6141940d71c6SSergey Senozhatsky if (time_after(now, ts + thresh)) { 614282607adcSTejun Heo lockup_detected = true; 6143cd2440d6SPetr Mladek if (pool->cpu >= 0) { 6144cd2440d6SPetr Mladek pool->cpu_stall = true; 6145cd2440d6SPetr Mladek cpu_pool_stall = true; 6146cd2440d6SPetr Mladek } 614782607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 614882607adcSTejun Heo pr_cont_pool_info(pool); 614982607adcSTejun Heo pr_cont(" stuck for %us!\n", 6150940d71c6SSergey Senozhatsky jiffies_to_msecs(now - pool_ts) / 1000); 615182607adcSTejun Heo } 6152cd2440d6SPetr Mladek 6153cd2440d6SPetr Mladek 615482607adcSTejun Heo } 615582607adcSTejun Heo 615682607adcSTejun Heo rcu_read_unlock(); 615782607adcSTejun Heo 615882607adcSTejun Heo if (lockup_detected) 615955df0933SImran Khan show_all_workqueues(); 616082607adcSTejun Heo 6161cd2440d6SPetr Mladek if (cpu_pool_stall) 6162cd2440d6SPetr Mladek show_cpu_pools_hogs(); 6163cd2440d6SPetr Mladek 616482607adcSTejun Heo wq_watchdog_reset_touched(); 616582607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 616682607adcSTejun Heo } 616782607adcSTejun Heo 6168cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 616982607adcSTejun Heo { 617082607adcSTejun Heo if (cpu >= 0) 617182607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 617289e28ce6SWang Qing 617382607adcSTejun Heo wq_watchdog_touched = jiffies; 617482607adcSTejun Heo } 617582607adcSTejun Heo 617682607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 617782607adcSTejun Heo { 617882607adcSTejun Heo wq_watchdog_thresh = 0; 617982607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 618082607adcSTejun Heo 618182607adcSTejun Heo if (thresh) { 618282607adcSTejun Heo wq_watchdog_thresh = thresh; 618382607adcSTejun Heo wq_watchdog_reset_touched(); 618482607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 618582607adcSTejun Heo } 618682607adcSTejun Heo } 618782607adcSTejun Heo 618882607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 618982607adcSTejun Heo const struct kernel_param *kp) 619082607adcSTejun Heo { 619182607adcSTejun Heo unsigned long thresh; 619282607adcSTejun Heo int ret; 619382607adcSTejun Heo 619482607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 619582607adcSTejun Heo if (ret) 619682607adcSTejun Heo return ret; 619782607adcSTejun Heo 619882607adcSTejun Heo if (system_wq) 619982607adcSTejun Heo wq_watchdog_set_thresh(thresh); 620082607adcSTejun Heo else 620182607adcSTejun Heo wq_watchdog_thresh = thresh; 620282607adcSTejun Heo 620382607adcSTejun Heo return 0; 620482607adcSTejun Heo } 620582607adcSTejun Heo 620682607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 620782607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 620882607adcSTejun Heo .get = param_get_ulong, 620982607adcSTejun Heo }; 621082607adcSTejun Heo 621182607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 621282607adcSTejun Heo 0644); 621382607adcSTejun Heo 621482607adcSTejun Heo static void wq_watchdog_init(void) 621582607adcSTejun Heo { 62165cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 621782607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 621882607adcSTejun Heo } 621982607adcSTejun Heo 622082607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 622182607adcSTejun Heo 622282607adcSTejun Heo static inline void wq_watchdog_init(void) { } 622382607adcSTejun Heo 622482607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 622582607adcSTejun Heo 6226bce90380STejun Heo static void __init wq_numa_init(void) 6227bce90380STejun Heo { 6228bce90380STejun Heo cpumask_var_t *tbl; 6229bce90380STejun Heo int node, cpu; 6230bce90380STejun Heo 6231bce90380STejun Heo if (num_possible_nodes() <= 1) 6232bce90380STejun Heo return; 6233bce90380STejun Heo 6234d55262c4STejun Heo if (wq_disable_numa) { 6235d55262c4STejun Heo pr_info("workqueue: NUMA affinity support disabled\n"); 6236d55262c4STejun Heo return; 6237d55262c4STejun Heo } 6238d55262c4STejun Heo 6239f728c4a9SZhen Lei for_each_possible_cpu(cpu) { 6240f728c4a9SZhen Lei if (WARN_ON(cpu_to_node(cpu) == NUMA_NO_NODE)) { 6241f728c4a9SZhen Lei pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu); 6242f728c4a9SZhen Lei return; 6243f728c4a9SZhen Lei } 6244f728c4a9SZhen Lei } 6245f728c4a9SZhen Lei 6246be69d00dSThomas Gleixner wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(); 62474c16bd32STejun Heo BUG_ON(!wq_update_unbound_numa_attrs_buf); 62484c16bd32STejun Heo 6249bce90380STejun Heo /* 6250bce90380STejun Heo * We want masks of possible CPUs of each node which isn't readily 6251bce90380STejun Heo * available. Build one from cpu_to_node() which should have been 6252bce90380STejun Heo * fully initialized by now. 6253bce90380STejun Heo */ 62546396bb22SKees Cook tbl = kcalloc(nr_node_ids, sizeof(tbl[0]), GFP_KERNEL); 6255bce90380STejun Heo BUG_ON(!tbl); 6256bce90380STejun Heo 6257bce90380STejun Heo for_each_node(node) 62585a6024f1SYasuaki Ishimatsu BUG_ON(!zalloc_cpumask_var_node(&tbl[node], GFP_KERNEL, 62591be0c25dSTejun Heo node_online(node) ? node : NUMA_NO_NODE)); 6260bce90380STejun Heo 6261bce90380STejun Heo for_each_possible_cpu(cpu) { 6262bce90380STejun Heo node = cpu_to_node(cpu); 6263bce90380STejun Heo cpumask_set_cpu(cpu, tbl[node]); 6264bce90380STejun Heo } 6265bce90380STejun Heo 6266bce90380STejun Heo wq_numa_possible_cpumask = tbl; 6267bce90380STejun Heo wq_numa_enabled = true; 6268bce90380STejun Heo } 6269bce90380STejun Heo 62703347fa09STejun Heo /** 62713347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 62723347fa09STejun Heo * 62733347fa09STejun Heo * This is the first half of two-staged workqueue subsystem initialization 62743347fa09STejun Heo * and invoked as soon as the bare basics - memory allocation, cpumasks and 62753347fa09STejun Heo * idr are up. It sets up all the data structures and system workqueues 62763347fa09STejun Heo * and allows early boot code to create workqueues and queue/cancel work 62773347fa09STejun Heo * items. Actual work item execution starts only after kthreads can be 62783347fa09STejun Heo * created and scheduled right before early initcalls. 62793347fa09STejun Heo */ 62802333e829SYu Chen void __init workqueue_init_early(void) 62811da177e4SLinus Torvalds { 62827a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 62837a4e344cSTejun Heo int i, cpu; 6284c34056a3STejun Heo 628510cdb157SLai Jiangshan BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 6286e904e6c2STejun Heo 6287b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 628804d4e665SFrederic Weisbecker cpumask_copy(wq_unbound_cpumask, housekeeping_cpumask(HK_TYPE_WQ)); 628904d4e665SFrederic Weisbecker cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, housekeeping_cpumask(HK_TYPE_DOMAIN)); 6290b05a7928SFrederic Weisbecker 6291e904e6c2STejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 6292e904e6c2STejun Heo 6293706026c2STejun Heo /* initialize CPU pools */ 629429c91e99STejun Heo for_each_possible_cpu(cpu) { 62954ce62e9eSTejun Heo struct worker_pool *pool; 62968b03ae3cSTejun Heo 62977a4e344cSTejun Heo i = 0; 6298f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 62997a4e344cSTejun Heo BUG_ON(init_worker_pool(pool)); 6300ec22ca5eSTejun Heo pool->cpu = cpu; 63017a4e344cSTejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 63027a4e344cSTejun Heo pool->attrs->nice = std_nice[i++]; 6303f3f90ad4STejun Heo pool->node = cpu_to_node(cpu); 63047a4e344cSTejun Heo 63059daf9e67STejun Heo /* alloc pool ID */ 630668e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 63079daf9e67STejun Heo BUG_ON(worker_pool_assign_id(pool)); 630868e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 63094ce62e9eSTejun Heo } 63108b03ae3cSTejun Heo } 63118b03ae3cSTejun Heo 63128a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 631329c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 631429c91e99STejun Heo struct workqueue_attrs *attrs; 631529c91e99STejun Heo 6316be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 631729c91e99STejun Heo attrs->nice = std_nice[i]; 631829c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 63198a2b7538STejun Heo 63208a2b7538STejun Heo /* 63218a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 63228a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 63238a2b7538STejun Heo * Turn off NUMA so that dfl_pwq is used for all nodes. 63248a2b7538STejun Heo */ 6325be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 63268a2b7538STejun Heo attrs->nice = std_nice[i]; 63278a2b7538STejun Heo attrs->no_numa = true; 63288a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 632929c91e99STejun Heo } 633029c91e99STejun Heo 6331d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 63321aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 6333d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 6334f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 6335f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 633624d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 633724d51addSTejun Heo WQ_FREEZABLE, 0); 63380668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 63390668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 63400668106cSViresh Kumar system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient", 63410668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 63420668106cSViresh Kumar 0); 63431aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 63440668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 63450668106cSViresh Kumar !system_power_efficient_wq || 63460668106cSViresh Kumar !system_freezable_power_efficient_wq); 63473347fa09STejun Heo } 63483347fa09STejun Heo 63493347fa09STejun Heo /** 63503347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 63513347fa09STejun Heo * 63523347fa09STejun Heo * This is the latter half of two-staged workqueue subsystem initialization 63533347fa09STejun Heo * and invoked as soon as kthreads can be created and scheduled. 63543347fa09STejun Heo * Workqueues have been created and work items queued on them, but there 63553347fa09STejun Heo * are no kworkers executing the work items yet. Populate the worker pools 63563347fa09STejun Heo * with the initial workers and enable future kworker creations. 63573347fa09STejun Heo */ 63582333e829SYu Chen void __init workqueue_init(void) 63593347fa09STejun Heo { 63602186d9f9STejun Heo struct workqueue_struct *wq; 63613347fa09STejun Heo struct worker_pool *pool; 63623347fa09STejun Heo int cpu, bkt; 63633347fa09STejun Heo 63642186d9f9STejun Heo /* 63652186d9f9STejun Heo * It'd be simpler to initialize NUMA in workqueue_init_early() but 63662186d9f9STejun Heo * CPU to node mapping may not be available that early on some 63672186d9f9STejun Heo * archs such as power and arm64. As per-cpu pools created 63682186d9f9STejun Heo * previously could be missing node hint and unbound pools NUMA 63692186d9f9STejun Heo * affinity, fix them up. 637040c17f75STejun Heo * 637140c17f75STejun Heo * Also, while iterating workqueues, create rescuers if requested. 63722186d9f9STejun Heo */ 63732186d9f9STejun Heo wq_numa_init(); 63742186d9f9STejun Heo 63752186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 63762186d9f9STejun Heo 63772186d9f9STejun Heo for_each_possible_cpu(cpu) { 63782186d9f9STejun Heo for_each_cpu_worker_pool(pool, cpu) { 63792186d9f9STejun Heo pool->node = cpu_to_node(cpu); 63802186d9f9STejun Heo } 63812186d9f9STejun Heo } 63822186d9f9STejun Heo 638340c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 63842186d9f9STejun Heo wq_update_unbound_numa(wq, smp_processor_id(), true); 638540c17f75STejun Heo WARN(init_rescuer(wq), 638640c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 638740c17f75STejun Heo wq->name); 638840c17f75STejun Heo } 63892186d9f9STejun Heo 63902186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 63912186d9f9STejun Heo 63923347fa09STejun Heo /* create the initial workers */ 63933347fa09STejun Heo for_each_online_cpu(cpu) { 63943347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 63953347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 63963347fa09STejun Heo BUG_ON(!create_worker(pool)); 63973347fa09STejun Heo } 63983347fa09STejun Heo } 63993347fa09STejun Heo 64003347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 64013347fa09STejun Heo BUG_ON(!create_worker(pool)); 64023347fa09STejun Heo 64033347fa09STejun Heo wq_online = true; 640482607adcSTejun Heo wq_watchdog_init(); 64051da177e4SLinus Torvalds } 6406c4f135d6STetsuo Handa 6407c4f135d6STetsuo Handa /* 6408c4f135d6STetsuo Handa * Despite the naming, this is a no-op function which is here only for avoiding 6409c4f135d6STetsuo Handa * link error. Since compile-time warning may fail to catch, we will need to 6410c4f135d6STetsuo Handa * emit run-time warning from __flush_workqueue(). 6411c4f135d6STetsuo Handa */ 6412c4f135d6STetsuo Handa void __warn_flushing_systemwide_wq(void) { } 6413c4f135d6STetsuo Handa EXPORT_SYMBOL(__warn_flushing_systemwide_wq); 6414