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> 55aa6fde93STejun Heo #include <linux/delay.h> 56e22bee78STejun Heo 57ea138446STejun Heo #include "workqueue_internal.h" 581da177e4SLinus Torvalds 59c8e55f36STejun Heo enum { 60bc2ae0f5STejun Heo /* 6124647570STejun Heo * worker_pool flags 62bc2ae0f5STejun Heo * 6324647570STejun Heo * A bound pool is either associated or disassociated with its CPU. 64bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 65bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 66bc2ae0f5STejun Heo * is in effect. 67bc2ae0f5STejun Heo * 68bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 69bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 7024647570STejun Heo * be executing on any CPU. The pool behaves as an unbound one. 71bc2ae0f5STejun Heo * 72bc3a1afcSTejun Heo * Note that DISASSOCIATED should be flipped only while holding 731258fae7STejun Heo * wq_pool_attach_mutex to avoid changing binding state while 744736cbf7SLai Jiangshan * worker_attach_to_pool() is in progress. 75bc2ae0f5STejun Heo */ 76692b4825STejun Heo POOL_MANAGER_ACTIVE = 1 << 0, /* being managed */ 7724647570STejun Heo POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ 78db7bccf4STejun Heo 79c8e55f36STejun Heo /* worker flags */ 80c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 81c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 82e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 83fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 84f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 85a9ab775bSTejun Heo WORKER_REBOUND = 1 << 8, /* worker was rebound */ 86e22bee78STejun Heo 87a9ab775bSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE | 88a9ab775bSTejun Heo WORKER_UNBOUND | WORKER_REBOUND, 89db7bccf4STejun Heo 90e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 914ce62e9eSTejun Heo 9229c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ 93c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 94db7bccf4STejun Heo 95e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 96e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 97e22bee78STejun Heo 983233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 993233cdbdSTejun Heo /* call for help after 10ms 1003233cdbdSTejun Heo (min two ticks) */ 101e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 102e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds /* 105e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 1068698a745SDongsheng Yang * all cpus. Give MIN_NICE. 107e22bee78STejun Heo */ 1088698a745SDongsheng Yang RESCUER_NICE_LEVEL = MIN_NICE, 1098698a745SDongsheng Yang HIGHPRI_NICE_LEVEL = MIN_NICE, 110ecf6881fSTejun Heo 111ecf6881fSTejun Heo WQ_NAME_LEN = 24, 112c8e55f36STejun Heo }; 113c8e55f36STejun Heo 1141da177e4SLinus Torvalds /* 1154690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1164690c4abSTejun Heo * 117e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 118e41e704bSTejun Heo * everyone else. 1194690c4abSTejun Heo * 120e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 121e22bee78STejun Heo * only be modified and accessed from the local cpu. 122e22bee78STejun Heo * 123d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1244690c4abSTejun Heo * 125bdf8b9bfSTejun Heo * K: Only modified by worker while holding pool->lock. Can be safely read by 126bdf8b9bfSTejun Heo * self, while holding pool->lock or from IRQ context if %current is the 127bdf8b9bfSTejun Heo * kworker. 128bdf8b9bfSTejun Heo * 129bdf8b9bfSTejun Heo * S: Only modified by worker self. 130bdf8b9bfSTejun Heo * 1311258fae7STejun Heo * A: wq_pool_attach_mutex protected. 132822d8405STejun Heo * 13368e13a67SLai Jiangshan * PL: wq_pool_mutex protected. 13476af4d93STejun Heo * 13524acfb71SThomas Gleixner * PR: wq_pool_mutex protected for writes. RCU protected for reads. 1365bcab335STejun Heo * 1375b95e1afSLai Jiangshan * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads. 1385b95e1afSLai Jiangshan * 1395b95e1afSLai Jiangshan * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or 14024acfb71SThomas Gleixner * RCU for reads. 1415b95e1afSLai Jiangshan * 1423c25a55dSLai Jiangshan * WQ: wq->mutex protected. 1433c25a55dSLai Jiangshan * 14424acfb71SThomas Gleixner * WR: wq->mutex protected for writes. RCU protected for reads. 1452e109a28STejun Heo * 1462e109a28STejun Heo * MD: wq_mayday_lock protected. 147cd2440d6SPetr Mladek * 148cd2440d6SPetr Mladek * WD: Used internally by the watchdog. 1494690c4abSTejun Heo */ 1504690c4abSTejun Heo 1512eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 152c34056a3STejun Heo 153bd7bdd43STejun Heo struct worker_pool { 154a9b8a985SSebastian Andrzej Siewior raw_spinlock_t lock; /* the pool lock */ 155d84ff051STejun Heo int cpu; /* I: the associated cpu */ 156f3f90ad4STejun Heo int node; /* I: the associated node ID */ 1579daf9e67STejun Heo int id; /* I: pool ID */ 158bc8b50c2STejun Heo unsigned int flags; /* L: flags */ 159bd7bdd43STejun Heo 16082607adcSTejun Heo unsigned long watchdog_ts; /* L: watchdog timestamp */ 161cd2440d6SPetr Mladek bool cpu_stall; /* WD: stalled cpu bound pool */ 16282607adcSTejun Heo 163bc35f7efSLai Jiangshan /* 164bc35f7efSLai Jiangshan * The counter is incremented in a process context on the associated CPU 165bc35f7efSLai Jiangshan * w/ preemption disabled, and decremented or reset in the same context 166bc35f7efSLai Jiangshan * but w/ pool->lock held. The readers grab pool->lock and are 167bc35f7efSLai Jiangshan * guaranteed to see if the counter reached zero. 168bc35f7efSLai Jiangshan */ 169bc35f7efSLai Jiangshan int nr_running; 17084f91c62SLai Jiangshan 171bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 172ea1abd61SLai Jiangshan 1735826cc8fSLai Jiangshan int nr_workers; /* L: total number of workers */ 1745826cc8fSLai Jiangshan int nr_idle; /* L: currently idle workers */ 175bd7bdd43STejun Heo 1762c1f1a91SLai Jiangshan struct list_head idle_list; /* L: list of idle workers */ 177bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 1783f959aa3SValentin Schneider struct work_struct idle_cull_work; /* L: worker idle cleanup */ 1793f959aa3SValentin Schneider 180bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 181bd7bdd43STejun Heo 182c5aa87bbSTejun Heo /* a workers is either on busy_hash or idle_list, or the manager */ 183c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 184c9e7cf27STejun Heo /* L: hash of busy workers */ 185c9e7cf27STejun Heo 1862607d7a6STejun Heo struct worker *manager; /* L: purely informational */ 18792f9c5c4SLai Jiangshan struct list_head workers; /* A: attached workers */ 188e02b9312SValentin Schneider struct list_head dying_workers; /* A: workers about to die */ 18960f5a4bcSLai Jiangshan struct completion *detach_completion; /* all workers detached */ 190e19e397aSTejun Heo 1917cda9aaeSLai Jiangshan struct ida worker_ida; /* worker IDs for task name */ 192e19e397aSTejun Heo 1937a4e344cSTejun Heo struct workqueue_attrs *attrs; /* I: worker attributes */ 19468e13a67SLai Jiangshan struct hlist_node hash_node; /* PL: unbound_pool_hash node */ 19568e13a67SLai Jiangshan int refcnt; /* PL: refcnt for unbound pools */ 1967a4e344cSTejun Heo 197e19e397aSTejun Heo /* 19824acfb71SThomas Gleixner * Destruction of pool is RCU protected to allow dereferences 19929c91e99STejun Heo * from get_work_pool(). 20029c91e99STejun Heo */ 20129c91e99STejun Heo struct rcu_head rcu; 20284f91c62SLai Jiangshan }; 2038b03ae3cSTejun Heo 2048b03ae3cSTejun Heo /* 205725e8ec5STejun Heo * Per-pool_workqueue statistics. These can be monitored using 206725e8ec5STejun Heo * tools/workqueue/wq_monitor.py. 207725e8ec5STejun Heo */ 208725e8ec5STejun Heo enum pool_workqueue_stats { 209725e8ec5STejun Heo PWQ_STAT_STARTED, /* work items started execution */ 210725e8ec5STejun Heo PWQ_STAT_COMPLETED, /* work items completed execution */ 2118a1dd1e5STejun Heo PWQ_STAT_CPU_TIME, /* total CPU time consumed */ 212616db877STejun Heo PWQ_STAT_CPU_INTENSIVE, /* wq_cpu_intensive_thresh_us violations */ 213725e8ec5STejun Heo PWQ_STAT_CM_WAKEUP, /* concurrency-management worker wakeups */ 214725e8ec5STejun Heo PWQ_STAT_MAYDAY, /* maydays to rescuer */ 215725e8ec5STejun Heo PWQ_STAT_RESCUED, /* linked work items executed by rescuer */ 216725e8ec5STejun Heo 217725e8ec5STejun Heo PWQ_NR_STATS, 218725e8ec5STejun Heo }; 219725e8ec5STejun Heo 220725e8ec5STejun Heo /* 221112202d9STejun Heo * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS 222112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits 223112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the 224112202d9STejun Heo * number of flag bits. 2251da177e4SLinus Torvalds */ 226112202d9STejun Heo struct pool_workqueue { 227bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 2284690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 22973f53c4aSTejun Heo int work_color; /* L: current color */ 23073f53c4aSTejun Heo int flush_color; /* L: flushing color */ 2318864b4e5STejun Heo int refcnt; /* L: reference count */ 23273f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 23373f53c4aSTejun Heo /* L: nr of in_flight works */ 234018f3a13SLai Jiangshan 235018f3a13SLai Jiangshan /* 236018f3a13SLai Jiangshan * nr_active management and WORK_STRUCT_INACTIVE: 237018f3a13SLai Jiangshan * 238018f3a13SLai Jiangshan * When pwq->nr_active >= max_active, new work item is queued to 239018f3a13SLai Jiangshan * pwq->inactive_works instead of pool->worklist and marked with 240018f3a13SLai Jiangshan * WORK_STRUCT_INACTIVE. 241018f3a13SLai Jiangshan * 242018f3a13SLai Jiangshan * All work items marked with WORK_STRUCT_INACTIVE do not participate 243018f3a13SLai Jiangshan * in pwq->nr_active and all work items in pwq->inactive_works are 244018f3a13SLai Jiangshan * marked with WORK_STRUCT_INACTIVE. But not all WORK_STRUCT_INACTIVE 245018f3a13SLai Jiangshan * work items are in pwq->inactive_works. Some of them are ready to 246018f3a13SLai Jiangshan * run in pool->worklist or worker->scheduled. Those work itmes are 247018f3a13SLai Jiangshan * only struct wq_barrier which is used for flush_work() and should 248018f3a13SLai Jiangshan * not participate in pwq->nr_active. For non-barrier work item, it 249018f3a13SLai Jiangshan * is marked with WORK_STRUCT_INACTIVE iff it is in pwq->inactive_works. 250018f3a13SLai Jiangshan */ 2511e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 252a0a1a5fdSTejun Heo int max_active; /* L: max active works */ 253f97a4a1aSLai Jiangshan struct list_head inactive_works; /* L: inactive works */ 2543c25a55dSLai Jiangshan struct list_head pwqs_node; /* WR: node on wq->pwqs */ 2552e109a28STejun Heo struct list_head mayday_node; /* MD: node on wq->maydays */ 2568864b4e5STejun Heo 257725e8ec5STejun Heo u64 stats[PWQ_NR_STATS]; 258725e8ec5STejun Heo 2598864b4e5STejun Heo /* 260967b494eSTejun Heo * Release of unbound pwq is punted to a kthread_worker. See put_pwq() 261687a9aa5STejun Heo * and pwq_release_workfn() for details. pool_workqueue itself is also 262687a9aa5STejun Heo * RCU protected so that the first pwq can be determined without 263967b494eSTejun Heo * grabbing wq->mutex. 2648864b4e5STejun Heo */ 265687a9aa5STejun Heo struct kthread_work release_work; 2668864b4e5STejun Heo struct rcu_head rcu; 267e904e6c2STejun Heo } __aligned(1 << WORK_STRUCT_FLAG_BITS); 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds /* 27073f53c4aSTejun Heo * Structure used to wait for workqueue flush. 27173f53c4aSTejun Heo */ 27273f53c4aSTejun Heo struct wq_flusher { 2733c25a55dSLai Jiangshan struct list_head list; /* WQ: list of flushers */ 2743c25a55dSLai Jiangshan int flush_color; /* WQ: flush color waiting for */ 27573f53c4aSTejun Heo struct completion done; /* flush completion */ 27673f53c4aSTejun Heo }; 2771da177e4SLinus Torvalds 278226223abSTejun Heo struct wq_device; 279226223abSTejun Heo 28073f53c4aSTejun Heo /* 281c5aa87bbSTejun Heo * The externally visible workqueue. It relays the issued work items to 282c5aa87bbSTejun Heo * the appropriate worker_pool through its pool_workqueues. 2831da177e4SLinus Torvalds */ 2841da177e4SLinus Torvalds struct workqueue_struct { 2853c25a55dSLai Jiangshan struct list_head pwqs; /* WR: all pwqs of this wq */ 286e2dca7adSTejun Heo struct list_head list; /* PR: list of all workqueues */ 28773f53c4aSTejun Heo 2883c25a55dSLai Jiangshan struct mutex mutex; /* protects this wq */ 2893c25a55dSLai Jiangshan int work_color; /* WQ: current work color */ 2903c25a55dSLai Jiangshan int flush_color; /* WQ: current flush color */ 291112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 2923c25a55dSLai Jiangshan struct wq_flusher *first_flusher; /* WQ: first flusher */ 2933c25a55dSLai Jiangshan struct list_head flusher_queue; /* WQ: flush waiters */ 2943c25a55dSLai Jiangshan struct list_head flusher_overflow; /* WQ: flush overflow list */ 29573f53c4aSTejun Heo 2962e109a28STejun Heo struct list_head maydays; /* MD: pwqs requesting rescue */ 29730ae2fc0STejun Heo struct worker *rescuer; /* MD: rescue worker */ 298e22bee78STejun Heo 29987fc741eSLai Jiangshan int nr_drainers; /* WQ: drain in progress */ 300a357fc03SLai Jiangshan int saved_max_active; /* WQ: saved pwq max_active */ 301226223abSTejun Heo 3025b95e1afSLai Jiangshan struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */ 3035b95e1afSLai Jiangshan struct pool_workqueue *dfl_pwq; /* PW: only for unbound wqs */ 3046029a918STejun Heo 305226223abSTejun Heo #ifdef CONFIG_SYSFS 306226223abSTejun Heo struct wq_device *wq_dev; /* I: for sysfs interface */ 307226223abSTejun Heo #endif 3084e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 309669de8bdSBart Van Assche char *lock_name; 310669de8bdSBart Van Assche struct lock_class_key key; 3114e6045f1SJohannes Berg struct lockdep_map lockdep_map; 3124e6045f1SJohannes Berg #endif 313ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */ 3142728fd2fSTejun Heo 315e2dca7adSTejun Heo /* 31624acfb71SThomas Gleixner * Destruction of workqueue_struct is RCU protected to allow walking 31724acfb71SThomas Gleixner * the workqueues list without grabbing wq_pool_mutex. 318e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq. 319e2dca7adSTejun Heo */ 320e2dca7adSTejun Heo struct rcu_head rcu; 321e2dca7adSTejun Heo 3222728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */ 3232728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ 324636b927eSTejun Heo struct pool_workqueue __percpu __rcu **cpu_pwq; /* I: per-cpu pwqs */ 3251da177e4SLinus Torvalds }; 3261da177e4SLinus Torvalds 327e904e6c2STejun Heo static struct kmem_cache *pwq_cache; 328e904e6c2STejun Heo 32984193c07STejun Heo /* 33084193c07STejun Heo * Each pod type describes how CPUs should be grouped for unbound workqueues. 33184193c07STejun Heo * See the comment above workqueue_attrs->affn_scope. 33284193c07STejun Heo */ 33384193c07STejun Heo struct wq_pod_type { 33484193c07STejun Heo int nr_pods; /* number of pods */ 33584193c07STejun Heo cpumask_var_t *pod_cpus; /* pod -> cpus */ 33684193c07STejun Heo int *pod_node; /* pod -> node */ 33784193c07STejun Heo int *cpu_pod; /* cpu -> pod */ 33884193c07STejun Heo }; 33984193c07STejun Heo 34084193c07STejun Heo static struct wq_pod_type wq_pod_types[WQ_AFFN_NR_TYPES]; 341bce90380STejun Heo 342616db877STejun Heo /* 343616db877STejun Heo * Per-cpu work items which run for longer than the following threshold are 344616db877STejun Heo * automatically considered CPU intensive and excluded from concurrency 345616db877STejun Heo * management to prevent them from noticeably delaying other per-cpu work items. 346aa6fde93STejun Heo * ULONG_MAX indicates that the user hasn't overridden it with a boot parameter. 347aa6fde93STejun Heo * The actual value is initialized in wq_cpu_intensive_thresh_init(). 348616db877STejun Heo */ 349aa6fde93STejun Heo static unsigned long wq_cpu_intensive_thresh_us = ULONG_MAX; 350616db877STejun Heo module_param_named(cpu_intensive_thresh_us, wq_cpu_intensive_thresh_us, ulong, 0644); 351616db877STejun Heo 352cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 353552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 354cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 355cee22a15SViresh Kumar 356863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */ 3573347fa09STejun Heo 358fef59c9cSTejun Heo /* buf for wq_update_unbound_pod_attrs(), protected by CPU hotplug exclusion */ 359fef59c9cSTejun Heo static struct workqueue_attrs *wq_update_pod_attrs_buf; 3600f36ee24STejun Heo static cpumask_var_t wq_update_pod_cpumask_buf; 3614c16bd32STejun Heo 36268e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 3631258fae7STejun Heo static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ 364a9b8a985SSebastian Andrzej Siewior static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 365d8bb65abSSebastian Andrzej Siewior /* wait for manager to go away */ 366d8bb65abSSebastian Andrzej Siewior static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait); 3675bcab335STejun Heo 368e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 36968e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 3707d19c5ceSTejun Heo 37199c621efSLai Jiangshan /* PL&A: allowable cpus for unbound wqs and work items */ 372ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 373ef557180SMike Galbraith 374ace3c549Stiozhang /* for further constrain wq_unbound_cpumask by cmdline parameter*/ 375ace3c549Stiozhang static struct cpumask wq_cmdline_cpumask __initdata; 376ace3c549Stiozhang 377ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 378ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 379b05a7928SFrederic Weisbecker 380f303fccbSTejun Heo /* 381f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 382f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 383f303fccbSTejun Heo * to uncover usages which depend on it. 384f303fccbSTejun Heo */ 385f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 386f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 387f303fccbSTejun Heo #else 388f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 389f303fccbSTejun Heo #endif 390f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 391f303fccbSTejun Heo 3927d19c5ceSTejun Heo /* the per-cpu worker pools */ 39325528213SPeter Zijlstra static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools); 3947d19c5ceSTejun Heo 39568e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 3967d19c5ceSTejun Heo 39768e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 39829c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 39929c91e99STejun Heo 400c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 40129c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 40229c91e99STejun Heo 4038a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 4048a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 4058a2b7538STejun Heo 406967b494eSTejun Heo /* 407967b494eSTejun Heo * I: kthread_worker to release pwq's. pwq release needs to be bounced to a 408967b494eSTejun Heo * process context while holding a pool lock. Bounce to a dedicated kthread 409967b494eSTejun Heo * worker to avoid A-A deadlocks. 410967b494eSTejun Heo */ 411967b494eSTejun Heo static struct kthread_worker *pwq_release_worker; 412967b494eSTejun Heo 413d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly; 414ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 415044c782cSValentin Ilie struct workqueue_struct *system_highpri_wq __read_mostly; 4161aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 417044c782cSValentin Ilie struct workqueue_struct *system_long_wq __read_mostly; 418d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 419044c782cSValentin Ilie struct workqueue_struct *system_unbound_wq __read_mostly; 420f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 421044c782cSValentin Ilie struct workqueue_struct *system_freezable_wq __read_mostly; 42224d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 4230668106cSViresh Kumar struct workqueue_struct *system_power_efficient_wq __read_mostly; 4240668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 4250668106cSViresh Kumar struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly; 4260668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 427d320c038STejun Heo 4287d19c5ceSTejun Heo static int worker_thread(void *__worker); 4296ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 430c29eb853STejun Heo static void show_pwq(struct pool_workqueue *pwq); 43155df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool); 4327d19c5ceSTejun Heo 43397bd2347STejun Heo #define CREATE_TRACE_POINTS 43497bd2347STejun Heo #include <trace/events/workqueue.h> 43597bd2347STejun Heo 43668e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 43724acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 438f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 43924acfb71SThomas Gleixner "RCU or wq_pool_mutex should be held") 4405bcab335STejun Heo 4415b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 44224acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 443f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 444f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 44524acfb71SThomas Gleixner "RCU, wq->mutex or wq_pool_mutex should be held") 4465b95e1afSLai Jiangshan 447f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 448f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 449f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 4507a62c2c8STejun Heo (pool)++) 4514ce62e9eSTejun Heo 45249e3cf44STejun Heo /** 45317116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 45417116969STejun Heo * @pool: iteration cursor 455611c92a0STejun Heo * @pi: integer used for iteration 456fa1b54e6STejun Heo * 45724acfb71SThomas Gleixner * This must be called either with wq_pool_mutex held or RCU read 45868e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 45968e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 460fa1b54e6STejun Heo * 461fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 462fa1b54e6STejun Heo * ignored. 46317116969STejun Heo */ 464611c92a0STejun Heo #define for_each_pool(pool, pi) \ 465611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 46668e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 467fa1b54e6STejun Heo else 46817116969STejun Heo 46917116969STejun Heo /** 470822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 471822d8405STejun Heo * @worker: iteration cursor 472822d8405STejun Heo * @pool: worker_pool to iterate workers of 473822d8405STejun Heo * 4741258fae7STejun Heo * This must be called with wq_pool_attach_mutex. 475822d8405STejun Heo * 476822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 477822d8405STejun Heo * ignored. 478822d8405STejun Heo */ 479da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 480da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 4811258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 482822d8405STejun Heo else 483822d8405STejun Heo 484822d8405STejun Heo /** 48549e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 48649e3cf44STejun Heo * @pwq: iteration cursor 48749e3cf44STejun Heo * @wq: the target workqueue 48876af4d93STejun Heo * 48924acfb71SThomas Gleixner * This must be called either with wq->mutex held or RCU read locked. 490794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 491794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 49276af4d93STejun Heo * 49376af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 49476af4d93STejun Heo * ignored. 49549e3cf44STejun Heo */ 49649e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 49749e9d1a9SSebastian Andrzej Siewior list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ 4985a644662SJoel Fernandes (Google) lockdep_is_held(&(wq->mutex))) 499f3421797STejun Heo 500dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 501dc186ad7SThomas Gleixner 502f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr; 503dc186ad7SThomas Gleixner 50499777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 50599777288SStanislaw Gruszka { 50699777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 50799777288SStanislaw Gruszka } 50899777288SStanislaw Gruszka 509b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 510b9fdac7fSDu, Changbin { 511b9fdac7fSDu, Changbin struct work_struct *work = addr; 512b9fdac7fSDu, Changbin 513b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 514b9fdac7fSDu, Changbin } 515b9fdac7fSDu, Changbin 516dc186ad7SThomas Gleixner /* 517dc186ad7SThomas Gleixner * fixup_init is called when: 518dc186ad7SThomas Gleixner * - an active object is initialized 519dc186ad7SThomas Gleixner */ 52002a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 521dc186ad7SThomas Gleixner { 522dc186ad7SThomas Gleixner struct work_struct *work = addr; 523dc186ad7SThomas Gleixner 524dc186ad7SThomas Gleixner switch (state) { 525dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 526dc186ad7SThomas Gleixner cancel_work_sync(work); 527dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 52802a982a6SDu, Changbin return true; 529dc186ad7SThomas Gleixner default: 53002a982a6SDu, Changbin return false; 531dc186ad7SThomas Gleixner } 532dc186ad7SThomas Gleixner } 533dc186ad7SThomas Gleixner 534dc186ad7SThomas Gleixner /* 535dc186ad7SThomas Gleixner * fixup_free is called when: 536dc186ad7SThomas Gleixner * - an active object is freed 537dc186ad7SThomas Gleixner */ 53802a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 539dc186ad7SThomas Gleixner { 540dc186ad7SThomas Gleixner struct work_struct *work = addr; 541dc186ad7SThomas Gleixner 542dc186ad7SThomas Gleixner switch (state) { 543dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 544dc186ad7SThomas Gleixner cancel_work_sync(work); 545dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 54602a982a6SDu, Changbin return true; 547dc186ad7SThomas Gleixner default: 54802a982a6SDu, Changbin return false; 549dc186ad7SThomas Gleixner } 550dc186ad7SThomas Gleixner } 551dc186ad7SThomas Gleixner 552f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr = { 553dc186ad7SThomas Gleixner .name = "work_struct", 55499777288SStanislaw Gruszka .debug_hint = work_debug_hint, 555b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 556dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 557dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 558dc186ad7SThomas Gleixner }; 559dc186ad7SThomas Gleixner 560dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 561dc186ad7SThomas Gleixner { 562dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 563dc186ad7SThomas Gleixner } 564dc186ad7SThomas Gleixner 565dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 566dc186ad7SThomas Gleixner { 567dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 568dc186ad7SThomas Gleixner } 569dc186ad7SThomas Gleixner 570dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 571dc186ad7SThomas Gleixner { 572dc186ad7SThomas Gleixner if (onstack) 573dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 574dc186ad7SThomas Gleixner else 575dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 576dc186ad7SThomas Gleixner } 577dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 578dc186ad7SThomas Gleixner 579dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 580dc186ad7SThomas Gleixner { 581dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 582dc186ad7SThomas Gleixner } 583dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 584dc186ad7SThomas Gleixner 585ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 586ea2e64f2SThomas Gleixner { 587ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 588ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 589ea2e64f2SThomas Gleixner } 590ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 591ea2e64f2SThomas Gleixner 592dc186ad7SThomas Gleixner #else 593dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 594dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 595dc186ad7SThomas Gleixner #endif 596dc186ad7SThomas Gleixner 5974e8b22bdSLi Bin /** 59867dc8325SCai Huoqing * worker_pool_assign_id - allocate ID and assign it to @pool 5994e8b22bdSLi Bin * @pool: the pool pointer of interest 6004e8b22bdSLi Bin * 6014e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 6024e8b22bdSLi Bin * successfully, -errno on failure. 6034e8b22bdSLi Bin */ 6049daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 6059daf9e67STejun Heo { 6069daf9e67STejun Heo int ret; 6079daf9e67STejun Heo 60868e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6095bcab335STejun Heo 6104e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 6114e8b22bdSLi Bin GFP_KERNEL); 612229641a6STejun Heo if (ret >= 0) { 613e68035fbSTejun Heo pool->id = ret; 614229641a6STejun Heo return 0; 615229641a6STejun Heo } 6169daf9e67STejun Heo return ret; 6179daf9e67STejun Heo } 6189daf9e67STejun Heo 61973f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 62073f53c4aSTejun Heo { 62173f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 62273f53c4aSTejun Heo } 62373f53c4aSTejun Heo 624c4560c2cSLai Jiangshan static int get_work_color(unsigned long work_data) 62573f53c4aSTejun Heo { 626c4560c2cSLai Jiangshan return (work_data >> WORK_STRUCT_COLOR_SHIFT) & 62773f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 62873f53c4aSTejun Heo } 62973f53c4aSTejun Heo 63073f53c4aSTejun Heo static int work_next_color(int color) 63173f53c4aSTejun Heo { 63273f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 633a848e3b6SOleg Nesterov } 634a848e3b6SOleg Nesterov 6354594bf15SDavid Howells /* 636112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 637112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 6387c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 6397a22ad75STejun Heo * 640112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 641112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 642bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 643bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 6447a22ad75STejun Heo * 645112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 6467c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 647112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 6487c3eed5cSTejun Heo * available only while the work item is queued. 649bbb68dfaSTejun Heo * 650bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 651bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 652bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 653bbb68dfaSTejun Heo * try to steal the PENDING bit. 6544594bf15SDavid Howells */ 6557a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 6567a22ad75STejun Heo unsigned long flags) 6577a22ad75STejun Heo { 6586183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 6597a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 6607a22ad75STejun Heo } 6617a22ad75STejun Heo 662112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 6634690c4abSTejun Heo unsigned long extra_flags) 664365970a1SDavid Howells { 665112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 666112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 667365970a1SDavid Howells } 668365970a1SDavid Howells 6694468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 6704468a00fSLai Jiangshan int pool_id) 6714468a00fSLai Jiangshan { 6724468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 6734468a00fSLai Jiangshan WORK_STRUCT_PENDING); 6744468a00fSLai Jiangshan } 6754468a00fSLai Jiangshan 6767c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 6777c3eed5cSTejun Heo int pool_id) 6784d707b9fSOleg Nesterov { 67923657bb1STejun Heo /* 68023657bb1STejun Heo * The following wmb is paired with the implied mb in 68123657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 68223657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 68323657bb1STejun Heo * owner. 68423657bb1STejun Heo */ 68523657bb1STejun Heo smp_wmb(); 6867c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 687346c09f8SRoman Pen /* 688346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 689346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 690346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 6918bdc6201SLiu Song * reordering can lead to a missed execution on attempt to queue 692346c09f8SRoman Pen * the same @work. E.g. consider this case: 693346c09f8SRoman Pen * 694346c09f8SRoman Pen * CPU#0 CPU#1 695346c09f8SRoman Pen * ---------------------------- -------------------------------- 696346c09f8SRoman Pen * 697346c09f8SRoman Pen * 1 STORE event_indicated 698346c09f8SRoman Pen * 2 queue_work_on() { 699346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 700346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 701346c09f8SRoman Pen * 5 set_work_data() # clear bit 702346c09f8SRoman Pen * 6 smp_mb() 703346c09f8SRoman Pen * 7 work->current_func() { 704346c09f8SRoman Pen * 8 LOAD event_indicated 705346c09f8SRoman Pen * } 706346c09f8SRoman Pen * 707346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 708346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 709346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 710346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 711346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 712346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 713346c09f8SRoman Pen * before actual STORE. 714346c09f8SRoman Pen */ 715346c09f8SRoman Pen smp_mb(); 7164d707b9fSOleg Nesterov } 7174d707b9fSOleg Nesterov 7187a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 719365970a1SDavid Howells { 7207c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 7217c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 7227a22ad75STejun Heo } 7237a22ad75STejun Heo 724afa4bb77SLinus Torvalds static inline struct pool_workqueue *work_struct_pwq(unsigned long data) 725afa4bb77SLinus Torvalds { 726afa4bb77SLinus Torvalds return (struct pool_workqueue *)(data & WORK_STRUCT_WQ_DATA_MASK); 727afa4bb77SLinus Torvalds } 728afa4bb77SLinus Torvalds 729112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 7307a22ad75STejun Heo { 731e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 7327a22ad75STejun Heo 733112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 734afa4bb77SLinus Torvalds return work_struct_pwq(data); 735e120153dSTejun Heo else 736e120153dSTejun Heo return NULL; 7377a22ad75STejun Heo } 7387a22ad75STejun Heo 7397c3eed5cSTejun Heo /** 7407c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 7417c3eed5cSTejun Heo * @work: the work item of interest 7427c3eed5cSTejun Heo * 74368e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 74424acfb71SThomas Gleixner * access under RCU read lock. As such, this function should be 74524acfb71SThomas Gleixner * called under wq_pool_mutex or inside of a rcu_read_lock() region. 746fa1b54e6STejun Heo * 747fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 748fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 749fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 750fa1b54e6STejun Heo * returned pool is and stays online. 751d185af30SYacine Belkadi * 752d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 7537c3eed5cSTejun Heo */ 7547c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 7557a22ad75STejun Heo { 756e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 7577c3eed5cSTejun Heo int pool_id; 7587a22ad75STejun Heo 75968e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 760fa1b54e6STejun Heo 761112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 762afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool; 7637a22ad75STejun Heo 7647c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 7657c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 7667a22ad75STejun Heo return NULL; 7677a22ad75STejun Heo 768fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 7697c3eed5cSTejun Heo } 7707c3eed5cSTejun Heo 7717c3eed5cSTejun Heo /** 7727c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 7737c3eed5cSTejun Heo * @work: the work item of interest 7747c3eed5cSTejun Heo * 775d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 7767c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 7777c3eed5cSTejun Heo */ 7787c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 7797c3eed5cSTejun Heo { 78054d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 7817c3eed5cSTejun Heo 782112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 783afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool->id; 78454d5b7d0SLai Jiangshan 78554d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 7867c3eed5cSTejun Heo } 7877c3eed5cSTejun Heo 788bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 789bbb68dfaSTejun Heo { 7907c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 791bbb68dfaSTejun Heo 7927c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 7937c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 794bbb68dfaSTejun Heo } 795bbb68dfaSTejun Heo 796bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 797bbb68dfaSTejun Heo { 798bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 799bbb68dfaSTejun Heo 800112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 801bbb68dfaSTejun Heo } 802bbb68dfaSTejun Heo 803e22bee78STejun Heo /* 8043270476aSTejun Heo * Policy functions. These define the policies on how the global worker 8053270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 806d565ed63STejun Heo * they're being called with pool->lock held. 807e22bee78STejun Heo */ 808e22bee78STejun Heo 80963d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool) 810649027d7STejun Heo { 811bc35f7efSLai Jiangshan return !pool->nr_running; 812649027d7STejun Heo } 813649027d7STejun Heo 814e22bee78STejun Heo /* 815e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 816e22bee78STejun Heo * running workers. 817974271c4STejun Heo * 818974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 819706026c2STejun Heo * function will always return %true for unbound pools as long as the 820974271c4STejun Heo * worklist isn't empty. 821e22bee78STejun Heo */ 82263d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 823e22bee78STejun Heo { 82463d95a91STejun Heo return !list_empty(&pool->worklist) && __need_more_worker(pool); 825e22bee78STejun Heo } 826e22bee78STejun Heo 827e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 82863d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 829e22bee78STejun Heo { 83063d95a91STejun Heo return pool->nr_idle; 831e22bee78STejun Heo } 832e22bee78STejun Heo 833e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 83463d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 835e22bee78STejun Heo { 836bc35f7efSLai Jiangshan return !list_empty(&pool->worklist) && (pool->nr_running <= 1); 837e22bee78STejun Heo } 838e22bee78STejun Heo 839e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 84063d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 841e22bee78STejun Heo { 84263d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 843e22bee78STejun Heo } 844e22bee78STejun Heo 845e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 84663d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 847e22bee78STejun Heo { 848692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 84963d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 85063d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 851e22bee78STejun Heo 852e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 853e22bee78STejun Heo } 854e22bee78STejun Heo 8554690c4abSTejun Heo /** 856e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 857cb444766STejun Heo * @worker: self 858d302f017STejun Heo * @flags: flags to set 859d302f017STejun Heo * 860228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 861d302f017STejun Heo */ 862228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 863d302f017STejun Heo { 864bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 865e22bee78STejun Heo 866bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 867cb444766STejun Heo 868228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 869e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 870e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 871bc35f7efSLai Jiangshan pool->nr_running--; 872e22bee78STejun Heo } 873e22bee78STejun Heo 874d302f017STejun Heo worker->flags |= flags; 875d302f017STejun Heo } 876d302f017STejun Heo 877d302f017STejun Heo /** 878e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 879cb444766STejun Heo * @worker: self 880d302f017STejun Heo * @flags: flags to clear 881d302f017STejun Heo * 882e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 883d302f017STejun Heo */ 884d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 885d302f017STejun Heo { 88663d95a91STejun Heo struct worker_pool *pool = worker->pool; 887e22bee78STejun Heo unsigned int oflags = worker->flags; 888e22bee78STejun Heo 889bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 890cb444766STejun Heo 891d302f017STejun Heo worker->flags &= ~flags; 892e22bee78STejun Heo 89342c025f3STejun Heo /* 89442c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 89542c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 89642c025f3STejun Heo * of multiple flags, not a single flag. 89742c025f3STejun Heo */ 898e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 899e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 900bc35f7efSLai Jiangshan pool->nr_running++; 901d302f017STejun Heo } 902d302f017STejun Heo 903797e8345STejun Heo /* Return the first idle worker. Called with pool->lock held. */ 904797e8345STejun Heo static struct worker *first_idle_worker(struct worker_pool *pool) 905797e8345STejun Heo { 906797e8345STejun Heo if (unlikely(list_empty(&pool->idle_list))) 907797e8345STejun Heo return NULL; 908797e8345STejun Heo 909797e8345STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 910797e8345STejun Heo } 911797e8345STejun Heo 912797e8345STejun Heo /** 913797e8345STejun Heo * worker_enter_idle - enter idle state 914797e8345STejun Heo * @worker: worker which is entering idle state 915797e8345STejun Heo * 916797e8345STejun Heo * @worker is entering idle state. Update stats and idle timer if 917797e8345STejun Heo * necessary. 918797e8345STejun Heo * 919797e8345STejun Heo * LOCKING: 920797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 921797e8345STejun Heo */ 922797e8345STejun Heo static void worker_enter_idle(struct worker *worker) 923797e8345STejun Heo { 924797e8345STejun Heo struct worker_pool *pool = worker->pool; 925797e8345STejun Heo 926797e8345STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 927797e8345STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 928797e8345STejun Heo (worker->hentry.next || worker->hentry.pprev))) 929797e8345STejun Heo return; 930797e8345STejun Heo 931797e8345STejun Heo /* can't use worker_set_flags(), also called from create_worker() */ 932797e8345STejun Heo worker->flags |= WORKER_IDLE; 933797e8345STejun Heo pool->nr_idle++; 934797e8345STejun Heo worker->last_active = jiffies; 935797e8345STejun Heo 936797e8345STejun Heo /* idle_list is LIFO */ 937797e8345STejun Heo list_add(&worker->entry, &pool->idle_list); 938797e8345STejun Heo 939797e8345STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 940797e8345STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 941797e8345STejun Heo 942797e8345STejun Heo /* Sanity check nr_running. */ 943797e8345STejun Heo WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); 944797e8345STejun Heo } 945797e8345STejun Heo 946797e8345STejun Heo /** 947797e8345STejun Heo * worker_leave_idle - leave idle state 948797e8345STejun Heo * @worker: worker which is leaving idle state 949797e8345STejun Heo * 950797e8345STejun Heo * @worker is leaving idle state. Update stats. 951797e8345STejun Heo * 952797e8345STejun Heo * LOCKING: 953797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 954797e8345STejun Heo */ 955797e8345STejun Heo static void worker_leave_idle(struct worker *worker) 956797e8345STejun Heo { 957797e8345STejun Heo struct worker_pool *pool = worker->pool; 958797e8345STejun Heo 959797e8345STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 960797e8345STejun Heo return; 961797e8345STejun Heo worker_clr_flags(worker, WORKER_IDLE); 962797e8345STejun Heo pool->nr_idle--; 963797e8345STejun Heo list_del_init(&worker->entry); 964797e8345STejun Heo } 965797e8345STejun Heo 966797e8345STejun Heo /** 967797e8345STejun Heo * find_worker_executing_work - find worker which is executing a work 968797e8345STejun Heo * @pool: pool of interest 969797e8345STejun Heo * @work: work to find worker for 970797e8345STejun Heo * 971797e8345STejun Heo * Find a worker which is executing @work on @pool by searching 972797e8345STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 973797e8345STejun Heo * to match, its current execution should match the address of @work and 974797e8345STejun Heo * its work function. This is to avoid unwanted dependency between 975797e8345STejun Heo * unrelated work executions through a work item being recycled while still 976797e8345STejun Heo * being executed. 977797e8345STejun Heo * 978797e8345STejun Heo * This is a bit tricky. A work item may be freed once its execution 979797e8345STejun Heo * starts and nothing prevents the freed area from being recycled for 980797e8345STejun Heo * another work item. If the same work item address ends up being reused 981797e8345STejun Heo * before the original execution finishes, workqueue will identify the 982797e8345STejun Heo * recycled work item as currently executing and make it wait until the 983797e8345STejun Heo * current execution finishes, introducing an unwanted dependency. 984797e8345STejun Heo * 985797e8345STejun Heo * This function checks the work item address and work function to avoid 986797e8345STejun Heo * false positives. Note that this isn't complete as one may construct a 987797e8345STejun Heo * work function which can introduce dependency onto itself through a 988797e8345STejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 989797e8345STejun Heo * foot that badly, there's only so much we can do, and if such deadlock 990797e8345STejun Heo * actually occurs, it should be easy to locate the culprit work function. 991797e8345STejun Heo * 992797e8345STejun Heo * CONTEXT: 993797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 994797e8345STejun Heo * 995797e8345STejun Heo * Return: 996797e8345STejun Heo * Pointer to worker which is executing @work if found, %NULL 997797e8345STejun Heo * otherwise. 998797e8345STejun Heo */ 999797e8345STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 1000797e8345STejun Heo struct work_struct *work) 1001797e8345STejun Heo { 1002797e8345STejun Heo struct worker *worker; 1003797e8345STejun Heo 1004797e8345STejun Heo hash_for_each_possible(pool->busy_hash, worker, hentry, 1005797e8345STejun Heo (unsigned long)work) 1006797e8345STejun Heo if (worker->current_work == work && 1007797e8345STejun Heo worker->current_func == work->func) 1008797e8345STejun Heo return worker; 1009797e8345STejun Heo 1010797e8345STejun Heo return NULL; 1011797e8345STejun Heo } 1012797e8345STejun Heo 1013797e8345STejun Heo /** 1014797e8345STejun Heo * move_linked_works - move linked works to a list 1015797e8345STejun Heo * @work: start of series of works to be scheduled 1016797e8345STejun Heo * @head: target list to append @work to 1017797e8345STejun Heo * @nextp: out parameter for nested worklist walking 1018797e8345STejun Heo * 1019797e8345STejun Heo * Schedule linked works starting from @work to @head. Work series to 1020797e8345STejun Heo * be scheduled starts at @work and includes any consecutive work with 1021797e8345STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 1022797e8345STejun Heo * 1023797e8345STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 1024797e8345STejun Heo * the last scheduled work. This allows move_linked_works() to be 1025797e8345STejun Heo * nested inside outer list_for_each_entry_safe(). 1026797e8345STejun Heo * 1027797e8345STejun Heo * CONTEXT: 1028797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1029797e8345STejun Heo */ 1030797e8345STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1031797e8345STejun Heo struct work_struct **nextp) 1032797e8345STejun Heo { 1033797e8345STejun Heo struct work_struct *n; 1034797e8345STejun Heo 1035797e8345STejun Heo /* 1036797e8345STejun Heo * Linked worklist will always end before the end of the list, 1037797e8345STejun Heo * use NULL for list head. 1038797e8345STejun Heo */ 1039797e8345STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1040797e8345STejun Heo list_move_tail(&work->entry, head); 1041797e8345STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1042797e8345STejun Heo break; 1043797e8345STejun Heo } 1044797e8345STejun Heo 1045797e8345STejun Heo /* 1046797e8345STejun Heo * If we're already inside safe list traversal and have moved 1047797e8345STejun Heo * multiple works to the scheduled queue, the next position 1048797e8345STejun Heo * needs to be updated. 1049797e8345STejun Heo */ 1050797e8345STejun Heo if (nextp) 1051797e8345STejun Heo *nextp = n; 1052797e8345STejun Heo } 1053797e8345STejun Heo 1054797e8345STejun Heo /** 1055797e8345STejun Heo * wake_up_worker - wake up an idle worker 1056797e8345STejun Heo * @pool: worker pool to wake worker from 1057797e8345STejun Heo * 1058797e8345STejun Heo * Wake up the first idle worker of @pool. 1059797e8345STejun Heo * 1060797e8345STejun Heo * CONTEXT: 1061797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1062797e8345STejun Heo */ 1063797e8345STejun Heo static void wake_up_worker(struct worker_pool *pool) 1064797e8345STejun Heo { 1065797e8345STejun Heo struct worker *worker = first_idle_worker(pool); 1066797e8345STejun Heo 1067797e8345STejun Heo if (likely(worker)) 1068797e8345STejun Heo wake_up_process(worker->task); 1069797e8345STejun Heo } 1070797e8345STejun Heo 107163638450STejun Heo #ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT 107263638450STejun Heo 107363638450STejun Heo /* 107463638450STejun Heo * Concurrency-managed per-cpu work items that hog CPU for longer than 107563638450STejun Heo * wq_cpu_intensive_thresh_us trigger the automatic CPU_INTENSIVE mechanism, 107663638450STejun Heo * which prevents them from stalling other concurrency-managed work items. If a 107763638450STejun Heo * work function keeps triggering this mechanism, it's likely that the work item 107863638450STejun Heo * should be using an unbound workqueue instead. 107963638450STejun Heo * 108063638450STejun Heo * wq_cpu_intensive_report() tracks work functions which trigger such conditions 108163638450STejun Heo * and report them so that they can be examined and converted to use unbound 108263638450STejun Heo * workqueues as appropriate. To avoid flooding the console, each violating work 108363638450STejun Heo * function is tracked and reported with exponential backoff. 108463638450STejun Heo */ 108563638450STejun Heo #define WCI_MAX_ENTS 128 108663638450STejun Heo 108763638450STejun Heo struct wci_ent { 108863638450STejun Heo work_func_t func; 108963638450STejun Heo atomic64_t cnt; 109063638450STejun Heo struct hlist_node hash_node; 109163638450STejun Heo }; 109263638450STejun Heo 109363638450STejun Heo static struct wci_ent wci_ents[WCI_MAX_ENTS]; 109463638450STejun Heo static int wci_nr_ents; 109563638450STejun Heo static DEFINE_RAW_SPINLOCK(wci_lock); 109663638450STejun Heo static DEFINE_HASHTABLE(wci_hash, ilog2(WCI_MAX_ENTS)); 109763638450STejun Heo 109863638450STejun Heo static struct wci_ent *wci_find_ent(work_func_t func) 109963638450STejun Heo { 110063638450STejun Heo struct wci_ent *ent; 110163638450STejun Heo 110263638450STejun Heo hash_for_each_possible_rcu(wci_hash, ent, hash_node, 110363638450STejun Heo (unsigned long)func) { 110463638450STejun Heo if (ent->func == func) 110563638450STejun Heo return ent; 110663638450STejun Heo } 110763638450STejun Heo return NULL; 110863638450STejun Heo } 110963638450STejun Heo 111063638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) 111163638450STejun Heo { 111263638450STejun Heo struct wci_ent *ent; 111363638450STejun Heo 111463638450STejun Heo restart: 111563638450STejun Heo ent = wci_find_ent(func); 111663638450STejun Heo if (ent) { 111763638450STejun Heo u64 cnt; 111863638450STejun Heo 111963638450STejun Heo /* 112063638450STejun Heo * Start reporting from the fourth time and back off 112163638450STejun Heo * exponentially. 112263638450STejun Heo */ 112363638450STejun Heo cnt = atomic64_inc_return_relaxed(&ent->cnt); 112463638450STejun Heo if (cnt >= 4 && is_power_of_2(cnt)) 112563638450STejun Heo printk_deferred(KERN_WARNING "workqueue: %ps hogged CPU for >%luus %llu times, consider switching to WQ_UNBOUND\n", 112663638450STejun Heo ent->func, wq_cpu_intensive_thresh_us, 112763638450STejun Heo atomic64_read(&ent->cnt)); 112863638450STejun Heo return; 112963638450STejun Heo } 113063638450STejun Heo 113163638450STejun Heo /* 113263638450STejun Heo * @func is a new violation. Allocate a new entry for it. If wcn_ents[] 113363638450STejun Heo * is exhausted, something went really wrong and we probably made enough 113463638450STejun Heo * noise already. 113563638450STejun Heo */ 113663638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) 113763638450STejun Heo return; 113863638450STejun Heo 113963638450STejun Heo raw_spin_lock(&wci_lock); 114063638450STejun Heo 114163638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) { 114263638450STejun Heo raw_spin_unlock(&wci_lock); 114363638450STejun Heo return; 114463638450STejun Heo } 114563638450STejun Heo 114663638450STejun Heo if (wci_find_ent(func)) { 114763638450STejun Heo raw_spin_unlock(&wci_lock); 114863638450STejun Heo goto restart; 114963638450STejun Heo } 115063638450STejun Heo 115163638450STejun Heo ent = &wci_ents[wci_nr_ents++]; 115263638450STejun Heo ent->func = func; 115363638450STejun Heo atomic64_set(&ent->cnt, 1); 115463638450STejun Heo hash_add_rcu(wci_hash, &ent->hash_node, (unsigned long)func); 115563638450STejun Heo 115663638450STejun Heo raw_spin_unlock(&wci_lock); 115763638450STejun Heo } 115863638450STejun Heo 115963638450STejun Heo #else /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 116063638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) {} 116163638450STejun Heo #endif /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 116263638450STejun Heo 1163c54d5046STejun Heo /** 11641da177e4SLinus Torvalds * wq_worker_running - a worker is running again 11651da177e4SLinus Torvalds * @task: task waking up 11661da177e4SLinus Torvalds * 11671da177e4SLinus Torvalds * This function is called when a worker returns from schedule() 11681da177e4SLinus Torvalds */ 11691da177e4SLinus Torvalds void wq_worker_running(struct task_struct *task) 11701da177e4SLinus Torvalds { 11711da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 11721da177e4SLinus Torvalds 1173c8f6219bSZqiang if (!READ_ONCE(worker->sleeping)) 11741da177e4SLinus Torvalds return; 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds /* 11771da177e4SLinus Torvalds * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check 11781da177e4SLinus Torvalds * and the nr_running increment below, we may ruin the nr_running reset 11791da177e4SLinus Torvalds * and leave with an unexpected pool->nr_running == 1 on the newly unbound 11801da177e4SLinus Torvalds * pool. Protect against such race. 11811da177e4SLinus Torvalds */ 11821da177e4SLinus Torvalds preempt_disable(); 11831da177e4SLinus Torvalds if (!(worker->flags & WORKER_NOT_RUNNING)) 11841da177e4SLinus Torvalds worker->pool->nr_running++; 11851da177e4SLinus Torvalds preempt_enable(); 1186616db877STejun Heo 1187616db877STejun Heo /* 1188616db877STejun Heo * CPU intensive auto-detection cares about how long a work item hogged 1189616db877STejun Heo * CPU without sleeping. Reset the starting timestamp on wakeup. 1190616db877STejun Heo */ 1191616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 1192616db877STejun Heo 1193c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 0); 11941da177e4SLinus Torvalds } 11951da177e4SLinus Torvalds 11961da177e4SLinus Torvalds /** 11971da177e4SLinus Torvalds * wq_worker_sleeping - a worker is going to sleep 11981da177e4SLinus Torvalds * @task: task going to sleep 11991da177e4SLinus Torvalds * 12001da177e4SLinus Torvalds * This function is called from schedule() when a busy worker is 12011da177e4SLinus Torvalds * going to sleep. 12021da177e4SLinus Torvalds */ 12031da177e4SLinus Torvalds void wq_worker_sleeping(struct task_struct *task) 12041da177e4SLinus Torvalds { 12051da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 12061da177e4SLinus Torvalds struct worker_pool *pool; 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds /* 12091da177e4SLinus Torvalds * Rescuers, which may not have all the fields set up like normal 12101da177e4SLinus Torvalds * workers, also reach here, let's not access anything before 12111da177e4SLinus Torvalds * checking NOT_RUNNING. 12121da177e4SLinus Torvalds */ 12131da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) 12141da177e4SLinus Torvalds return; 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds pool = worker->pool; 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds /* Return if preempted before wq_worker_running() was reached */ 1219c8f6219bSZqiang if (READ_ONCE(worker->sleeping)) 12201da177e4SLinus Torvalds return; 12211da177e4SLinus Torvalds 1222c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 1); 12231da177e4SLinus Torvalds raw_spin_lock_irq(&pool->lock); 12241da177e4SLinus Torvalds 12251da177e4SLinus Torvalds /* 12261da177e4SLinus Torvalds * Recheck in case unbind_workers() preempted us. We don't 12271da177e4SLinus Torvalds * want to decrement nr_running after the worker is unbound 12281da177e4SLinus Torvalds * and nr_running has been reset. 12291da177e4SLinus Torvalds */ 12301da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) { 12311da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 12321da177e4SLinus Torvalds return; 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds pool->nr_running--; 1236725e8ec5STejun Heo if (need_more_worker(pool)) { 1237725e8ec5STejun Heo worker->current_pwq->stats[PWQ_STAT_CM_WAKEUP]++; 12381da177e4SLinus Torvalds wake_up_worker(pool); 1239725e8ec5STejun Heo } 12401da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 12411da177e4SLinus Torvalds } 12421da177e4SLinus Torvalds 12431da177e4SLinus Torvalds /** 1244616db877STejun Heo * wq_worker_tick - a scheduler tick occurred while a kworker is running 1245616db877STejun Heo * @task: task currently running 1246616db877STejun Heo * 1247616db877STejun Heo * Called from scheduler_tick(). We're in the IRQ context and the current 1248616db877STejun Heo * worker's fields which follow the 'K' locking rule can be accessed safely. 1249616db877STejun Heo */ 1250616db877STejun Heo void wq_worker_tick(struct task_struct *task) 1251616db877STejun Heo { 1252616db877STejun Heo struct worker *worker = kthread_data(task); 1253616db877STejun Heo struct pool_workqueue *pwq = worker->current_pwq; 1254616db877STejun Heo struct worker_pool *pool = worker->pool; 1255616db877STejun Heo 1256616db877STejun Heo if (!pwq) 1257616db877STejun Heo return; 1258616db877STejun Heo 12598a1dd1e5STejun Heo pwq->stats[PWQ_STAT_CPU_TIME] += TICK_USEC; 12608a1dd1e5STejun Heo 126118c8ae81SZqiang if (!wq_cpu_intensive_thresh_us) 126218c8ae81SZqiang return; 126318c8ae81SZqiang 1264616db877STejun Heo /* 1265616db877STejun Heo * If the current worker is concurrency managed and hogged the CPU for 1266616db877STejun Heo * longer than wq_cpu_intensive_thresh_us, it's automatically marked 1267616db877STejun Heo * CPU_INTENSIVE to avoid stalling other concurrency-managed work items. 1268c8f6219bSZqiang * 1269c8f6219bSZqiang * Set @worker->sleeping means that @worker is in the process of 1270c8f6219bSZqiang * switching out voluntarily and won't be contributing to 1271c8f6219bSZqiang * @pool->nr_running until it wakes up. As wq_worker_sleeping() also 1272c8f6219bSZqiang * decrements ->nr_running, setting CPU_INTENSIVE here can lead to 1273c8f6219bSZqiang * double decrements. The task is releasing the CPU anyway. Let's skip. 1274c8f6219bSZqiang * We probably want to make this prettier in the future. 1275616db877STejun Heo */ 1276c8f6219bSZqiang if ((worker->flags & WORKER_NOT_RUNNING) || READ_ONCE(worker->sleeping) || 1277616db877STejun Heo worker->task->se.sum_exec_runtime - worker->current_at < 1278616db877STejun Heo wq_cpu_intensive_thresh_us * NSEC_PER_USEC) 1279616db877STejun Heo return; 1280616db877STejun Heo 1281616db877STejun Heo raw_spin_lock(&pool->lock); 1282616db877STejun Heo 1283616db877STejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE); 128463638450STejun Heo wq_cpu_intensive_report(worker->current_func); 1285616db877STejun Heo pwq->stats[PWQ_STAT_CPU_INTENSIVE]++; 1286616db877STejun Heo 1287616db877STejun Heo if (need_more_worker(pool)) { 1288616db877STejun Heo pwq->stats[PWQ_STAT_CM_WAKEUP]++; 1289616db877STejun Heo wake_up_worker(pool); 1290616db877STejun Heo } 1291616db877STejun Heo 1292616db877STejun Heo raw_spin_unlock(&pool->lock); 1293616db877STejun Heo } 1294616db877STejun Heo 1295616db877STejun Heo /** 12961da177e4SLinus Torvalds * wq_worker_last_func - retrieve worker's last work function 12971da177e4SLinus Torvalds * @task: Task to retrieve last work function of. 12981da177e4SLinus Torvalds * 12991da177e4SLinus Torvalds * Determine the last function a worker executed. This is called from 13001da177e4SLinus Torvalds * the scheduler to get a worker's last known identity. 13011da177e4SLinus Torvalds * 13021da177e4SLinus Torvalds * CONTEXT: 13039b41ea72SAndrew Morton * raw_spin_lock_irq(rq->lock) 13041da177e4SLinus Torvalds * 13051da177e4SLinus Torvalds * This function is called during schedule() when a kworker is going 1306f756d5e2SNathan Lynch * to sleep. It's used by psi to identify aggregation workers during 1307f756d5e2SNathan Lynch * dequeuing, to allow periodic aggregation to shut-off when that 13081da177e4SLinus Torvalds * worker is the last task in the system or cgroup to go to sleep. 13091da177e4SLinus Torvalds * 13101da177e4SLinus Torvalds * As this function doesn't involve any workqueue-related locking, it 13111da177e4SLinus Torvalds * only returns stable values when called from inside the scheduler's 13121da177e4SLinus Torvalds * queuing and dequeuing paths, when @task, which must be a kworker, 13131da177e4SLinus Torvalds * is guaranteed to not be processing any works. 1314365970a1SDavid Howells * 1315365970a1SDavid Howells * Return: 1316365970a1SDavid Howells * The last work function %current executed as a worker, NULL if it 1317365970a1SDavid Howells * hasn't executed any work yet. 1318365970a1SDavid Howells */ 1319365970a1SDavid Howells work_func_t wq_worker_last_func(struct task_struct *task) 1320365970a1SDavid Howells { 1321365970a1SDavid Howells struct worker *worker = kthread_data(task); 1322365970a1SDavid Howells 1323365970a1SDavid Howells return worker->last_func; 1324365970a1SDavid Howells } 1325365970a1SDavid Howells 1326d302f017STejun Heo /** 13278864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 13288864b4e5STejun Heo * @pwq: pool_workqueue to get 13298864b4e5STejun Heo * 13308864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 13318864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 13328864b4e5STejun Heo */ 13338864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 13348864b4e5STejun Heo { 13358864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 13368864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 13378864b4e5STejun Heo pwq->refcnt++; 13388864b4e5STejun Heo } 13398864b4e5STejun Heo 13408864b4e5STejun Heo /** 13418864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 13428864b4e5STejun Heo * @pwq: pool_workqueue to put 13438864b4e5STejun Heo * 13448864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 13458864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 13468864b4e5STejun Heo */ 13478864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 13488864b4e5STejun Heo { 13498864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 13508864b4e5STejun Heo if (likely(--pwq->refcnt)) 13518864b4e5STejun Heo return; 13528864b4e5STejun Heo /* 1353967b494eSTejun Heo * @pwq can't be released under pool->lock, bounce to a dedicated 1354967b494eSTejun Heo * kthread_worker to avoid A-A deadlocks. 13558864b4e5STejun Heo */ 1356687a9aa5STejun Heo kthread_queue_work(pwq_release_worker, &pwq->release_work); 13578864b4e5STejun Heo } 13588864b4e5STejun Heo 1359dce90d47STejun Heo /** 1360dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1361dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1362dce90d47STejun Heo * 1363dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1364dce90d47STejun Heo */ 1365dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1366dce90d47STejun Heo { 1367dce90d47STejun Heo if (pwq) { 1368dce90d47STejun Heo /* 136924acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the 1370dce90d47STejun Heo * following lock operations are safe. 1371dce90d47STejun Heo */ 1372a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 1373dce90d47STejun Heo put_pwq(pwq); 1374a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 1375dce90d47STejun Heo } 1376dce90d47STejun Heo } 1377dce90d47STejun Heo 1378f97a4a1aSLai Jiangshan static void pwq_activate_inactive_work(struct work_struct *work) 1379bf4ede01STejun Heo { 1380112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1381bf4ede01STejun Heo 1382bf4ede01STejun Heo trace_workqueue_activate_work(work); 138382607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 138482607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1385112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 1386f97a4a1aSLai Jiangshan __clear_bit(WORK_STRUCT_INACTIVE_BIT, work_data_bits(work)); 1387112202d9STejun Heo pwq->nr_active++; 1388bf4ede01STejun Heo } 1389bf4ede01STejun Heo 1390f97a4a1aSLai Jiangshan static void pwq_activate_first_inactive(struct pool_workqueue *pwq) 13913aa62497SLai Jiangshan { 1392f97a4a1aSLai Jiangshan struct work_struct *work = list_first_entry(&pwq->inactive_works, 13933aa62497SLai Jiangshan struct work_struct, entry); 13943aa62497SLai Jiangshan 1395f97a4a1aSLai Jiangshan pwq_activate_inactive_work(work); 13963aa62497SLai Jiangshan } 13973aa62497SLai Jiangshan 1398bf4ede01STejun Heo /** 1399112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1400112202d9STejun Heo * @pwq: pwq of interest 1401c4560c2cSLai Jiangshan * @work_data: work_data of work which left the queue 1402bf4ede01STejun Heo * 1403bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1404112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1405bf4ede01STejun Heo * 1406bf4ede01STejun Heo * CONTEXT: 1407a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1408bf4ede01STejun Heo */ 1409c4560c2cSLai Jiangshan static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) 1410bf4ede01STejun Heo { 1411c4560c2cSLai Jiangshan int color = get_work_color(work_data); 1412c4560c2cSLai Jiangshan 1413018f3a13SLai Jiangshan if (!(work_data & WORK_STRUCT_INACTIVE)) { 1414112202d9STejun Heo pwq->nr_active--; 1415f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 1416f97a4a1aSLai Jiangshan /* one down, submit an inactive one */ 1417112202d9STejun Heo if (pwq->nr_active < pwq->max_active) 1418f97a4a1aSLai Jiangshan pwq_activate_first_inactive(pwq); 1419bf4ede01STejun Heo } 1420018f3a13SLai Jiangshan } 1421018f3a13SLai Jiangshan 1422018f3a13SLai Jiangshan pwq->nr_in_flight[color]--; 1423bf4ede01STejun Heo 1424bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 1425112202d9STejun Heo if (likely(pwq->flush_color != color)) 14268864b4e5STejun Heo goto out_put; 1427bf4ede01STejun Heo 1428bf4ede01STejun Heo /* are there still in-flight works? */ 1429112202d9STejun Heo if (pwq->nr_in_flight[color]) 14308864b4e5STejun Heo goto out_put; 1431bf4ede01STejun Heo 1432112202d9STejun Heo /* this pwq is done, clear flush_color */ 1433112202d9STejun Heo pwq->flush_color = -1; 1434bf4ede01STejun Heo 1435bf4ede01STejun Heo /* 1436112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 1437bf4ede01STejun Heo * will handle the rest. 1438bf4ede01STejun Heo */ 1439112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1440112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 14418864b4e5STejun Heo out_put: 14428864b4e5STejun Heo put_pwq(pwq); 1443bf4ede01STejun Heo } 1444bf4ede01STejun Heo 144536e227d2STejun Heo /** 1446bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 144736e227d2STejun Heo * @work: work item to steal 144836e227d2STejun Heo * @is_dwork: @work is a delayed_work 1449bbb68dfaSTejun Heo * @flags: place to store irq state 145036e227d2STejun Heo * 145136e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 1452d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 145336e227d2STejun Heo * 1454d185af30SYacine Belkadi * Return: 14553eb6b31bSMauro Carvalho Chehab * 14563eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 145736e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 145836e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 145936e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1460bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1461bbb68dfaSTejun Heo * for arbitrarily long 14623eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 146336e227d2STejun Heo * 1464d185af30SYacine Belkadi * Note: 1465bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1466e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 1467e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 1468e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 1469bbb68dfaSTejun Heo * 1470bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1471bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1472bbb68dfaSTejun Heo * 1473e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 1474bf4ede01STejun Heo */ 1475bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1476bbb68dfaSTejun Heo unsigned long *flags) 1477bf4ede01STejun Heo { 1478d565ed63STejun Heo struct worker_pool *pool; 1479112202d9STejun Heo struct pool_workqueue *pwq; 1480bf4ede01STejun Heo 1481bbb68dfaSTejun Heo local_irq_save(*flags); 1482bbb68dfaSTejun Heo 148336e227d2STejun Heo /* try to steal the timer if it exists */ 148436e227d2STejun Heo if (is_dwork) { 148536e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 148636e227d2STejun Heo 1487e0aecdd8STejun Heo /* 1488e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 1489e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 1490e0aecdd8STejun Heo * running on the local CPU. 1491e0aecdd8STejun Heo */ 149236e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 149336e227d2STejun Heo return 1; 149436e227d2STejun Heo } 149536e227d2STejun Heo 149636e227d2STejun Heo /* try to claim PENDING the normal way */ 1497bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1498bf4ede01STejun Heo return 0; 1499bf4ede01STejun Heo 150024acfb71SThomas Gleixner rcu_read_lock(); 1501bf4ede01STejun Heo /* 1502bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1503bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1504bf4ede01STejun Heo */ 1505d565ed63STejun Heo pool = get_work_pool(work); 1506d565ed63STejun Heo if (!pool) 1507bbb68dfaSTejun Heo goto fail; 1508bf4ede01STejun Heo 1509a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pool->lock); 1510bf4ede01STejun Heo /* 1511112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 1512112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 1513112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 1514112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 1515112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 15160b3dae68SLai Jiangshan * item is currently queued on that pool. 1517bf4ede01STejun Heo */ 1518112202d9STejun Heo pwq = get_work_pwq(work); 1519112202d9STejun Heo if (pwq && pwq->pool == pool) { 1520bf4ede01STejun Heo debug_work_deactivate(work); 15213aa62497SLai Jiangshan 15223aa62497SLai Jiangshan /* 1523018f3a13SLai Jiangshan * A cancelable inactive work item must be in the 1524018f3a13SLai Jiangshan * pwq->inactive_works since a queued barrier can't be 1525018f3a13SLai Jiangshan * canceled (see the comments in insert_wq_barrier()). 1526018f3a13SLai Jiangshan * 1527f97a4a1aSLai Jiangshan * An inactive work item cannot be grabbed directly because 1528d812796eSLai Jiangshan * it might have linked barrier work items which, if left 1529f97a4a1aSLai Jiangshan * on the inactive_works list, will confuse pwq->nr_active 153016062836STejun Heo * management later on and cause stall. Make sure the work 153116062836STejun Heo * item is activated before grabbing. 15323aa62497SLai Jiangshan */ 1533f97a4a1aSLai Jiangshan if (*work_data_bits(work) & WORK_STRUCT_INACTIVE) 1534f97a4a1aSLai Jiangshan pwq_activate_inactive_work(work); 15353aa62497SLai Jiangshan 1536bf4ede01STejun Heo list_del_init(&work->entry); 1537c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, *work_data_bits(work)); 153836e227d2STejun Heo 1539112202d9STejun Heo /* work->data points to pwq iff queued, point to pool */ 15404468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 15414468a00fSLai Jiangshan 1542a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 154324acfb71SThomas Gleixner rcu_read_unlock(); 154436e227d2STejun Heo return 1; 1545bf4ede01STejun Heo } 1546a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 1547bbb68dfaSTejun Heo fail: 154824acfb71SThomas Gleixner rcu_read_unlock(); 1549bbb68dfaSTejun Heo local_irq_restore(*flags); 1550bbb68dfaSTejun Heo if (work_is_canceling(work)) 1551bbb68dfaSTejun Heo return -ENOENT; 1552bbb68dfaSTejun Heo cpu_relax(); 155336e227d2STejun Heo return -EAGAIN; 1554bf4ede01STejun Heo } 1555bf4ede01STejun Heo 1556bf4ede01STejun Heo /** 1557706026c2STejun Heo * insert_work - insert a work into a pool 1558112202d9STejun Heo * @pwq: pwq @work belongs to 15594690c4abSTejun Heo * @work: work to insert 15604690c4abSTejun Heo * @head: insertion point 15614690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 15624690c4abSTejun Heo * 1563112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 1564706026c2STejun Heo * work_struct flags. 15654690c4abSTejun Heo * 15664690c4abSTejun Heo * CONTEXT: 1567a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1568365970a1SDavid Howells */ 1569112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 1570112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 1571b89deed3SOleg Nesterov { 1572fe089f87STejun Heo debug_work_activate(work); 1573e1d8aa9fSFrederic Weisbecker 1574e89a85d6SWalter Wu /* record the work call stack in order to print it in KASAN reports */ 1575f70da745SMarco Elver kasan_record_aux_stack_noalloc(work); 1576e89a85d6SWalter Wu 15774690c4abSTejun Heo /* we own @work, set data and link */ 1578112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 15791a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 15808864b4e5STejun Heo get_pwq(pwq); 1581b89deed3SOleg Nesterov } 1582b89deed3SOleg Nesterov 1583c8efcc25STejun Heo /* 1584c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 15858d03ecfeSTejun Heo * same workqueue. 1586c8efcc25STejun Heo */ 1587c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1588c8efcc25STejun Heo { 1589c8efcc25STejun Heo struct worker *worker; 1590c8efcc25STejun Heo 15918d03ecfeSTejun Heo worker = current_wq_worker(); 1592c8efcc25STejun Heo /* 1593bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If 15948d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 1595c8efcc25STejun Heo */ 1596112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 1597c8efcc25STejun Heo } 1598c8efcc25STejun Heo 1599ef557180SMike Galbraith /* 1600ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 1601ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 1602ef557180SMike Galbraith * avoid perturbing sensitive tasks. 1603ef557180SMike Galbraith */ 1604ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 1605ef557180SMike Galbraith { 1606ef557180SMike Galbraith int new_cpu; 1607ef557180SMike Galbraith 1608f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 1609ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 1610ef557180SMike Galbraith return cpu; 1611a8ec5880SAmmar Faizi } else { 1612a8ec5880SAmmar Faizi pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); 1613f303fccbSTejun Heo } 1614f303fccbSTejun Heo 1615ef557180SMike Galbraith if (cpumask_empty(wq_unbound_cpumask)) 1616ef557180SMike Galbraith return cpu; 1617ef557180SMike Galbraith 1618ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 1619ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 1620ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 1621ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 1622ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 1623ef557180SMike Galbraith return cpu; 1624ef557180SMike Galbraith } 1625ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 1626ef557180SMike Galbraith 1627ef557180SMike Galbraith return new_cpu; 1628ef557180SMike Galbraith } 1629ef557180SMike Galbraith 1630d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 16311da177e4SLinus Torvalds struct work_struct *work) 16321da177e4SLinus Torvalds { 1633112202d9STejun Heo struct pool_workqueue *pwq; 1634fe089f87STejun Heo struct worker_pool *last_pool, *pool; 16358a2e8e5dSTejun Heo unsigned int work_flags; 1636b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 16378930cabaSTejun Heo 16388930cabaSTejun Heo /* 16398930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 16408930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 16418930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 16428930cabaSTejun Heo * happen with IRQ disabled. 16438930cabaSTejun Heo */ 16448e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 16451da177e4SLinus Torvalds 16461e19ffc6STejun Heo 164733e3f0a3SRichard Clark /* 164833e3f0a3SRichard Clark * For a draining wq, only works from the same workqueue are 164933e3f0a3SRichard Clark * allowed. The __WQ_DESTROYING helps to spot the issue that 165033e3f0a3SRichard Clark * queues a new work item to a wq after destroy_workqueue(wq). 165133e3f0a3SRichard Clark */ 165233e3f0a3SRichard Clark if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && 165333e3f0a3SRichard Clark WARN_ON_ONCE(!is_chained_work(wq)))) 1654e41e704bSTejun Heo return; 165524acfb71SThomas Gleixner rcu_read_lock(); 16569e8cd2f5STejun Heo retry: 1657aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */ 1658636b927eSTejun Heo if (req_cpu == WORK_CPU_UNBOUND) { 1659636b927eSTejun Heo if (wq->flags & WQ_UNBOUND) 1660ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 1661636b927eSTejun Heo else 1662aa202f1fSHillf Danton cpu = raw_smp_processor_id(); 1663aa202f1fSHillf Danton } 1664f3421797STejun Heo 1665636b927eSTejun Heo pwq = rcu_dereference(*per_cpu_ptr(wq->cpu_pwq, cpu)); 1666fe089f87STejun Heo pool = pwq->pool; 1667fe089f87STejun Heo 166818aa9effSTejun Heo /* 1669c9178087STejun Heo * If @work was previously on a different pool, it might still be 1670c9178087STejun Heo * running there, in which case the work needs to be queued on that 1671c9178087STejun Heo * pool to guarantee non-reentrancy. 167218aa9effSTejun Heo */ 1673c9e7cf27STejun Heo last_pool = get_work_pool(work); 1674fe089f87STejun Heo if (last_pool && last_pool != pool) { 167518aa9effSTejun Heo struct worker *worker; 167618aa9effSTejun Heo 1677a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&last_pool->lock); 167818aa9effSTejun Heo 1679c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 168018aa9effSTejun Heo 1681112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 1682c9178087STejun Heo pwq = worker->current_pwq; 1683fe089f87STejun Heo pool = pwq->pool; 1684fe089f87STejun Heo WARN_ON_ONCE(pool != last_pool); 16858594fadeSLai Jiangshan } else { 168618aa9effSTejun Heo /* meh... not running there, queue here */ 1687a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&last_pool->lock); 1688fe089f87STejun Heo raw_spin_lock(&pool->lock); 168918aa9effSTejun Heo } 16908930cabaSTejun Heo } else { 1691fe089f87STejun Heo raw_spin_lock(&pool->lock); 16928930cabaSTejun Heo } 1693502ca9d8STejun Heo 16949e8cd2f5STejun Heo /* 1695636b927eSTejun Heo * pwq is determined and locked. For unbound pools, we could have raced 1696636b927eSTejun Heo * with pwq release and it could already be dead. If its refcnt is zero, 1697636b927eSTejun Heo * repeat pwq selection. Note that unbound pwqs never die without 1698636b927eSTejun Heo * another pwq replacing it in cpu_pwq or while work items are executing 1699636b927eSTejun Heo * on it, so the retrying is guaranteed to make forward-progress. 17009e8cd2f5STejun Heo */ 17019e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 17029e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 1703fe089f87STejun Heo raw_spin_unlock(&pool->lock); 17049e8cd2f5STejun Heo cpu_relax(); 17059e8cd2f5STejun Heo goto retry; 17069e8cd2f5STejun Heo } 17079e8cd2f5STejun Heo /* oops */ 17089e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 17099e8cd2f5STejun Heo wq->name, cpu); 17109e8cd2f5STejun Heo } 17119e8cd2f5STejun Heo 1712112202d9STejun Heo /* pwq determined, queue */ 1713112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 1714502ca9d8STejun Heo 171524acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry))) 171624acfb71SThomas Gleixner goto out; 17171e19ffc6STejun Heo 1718112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 1719112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 17201e19ffc6STejun Heo 1721112202d9STejun Heo if (likely(pwq->nr_active < pwq->max_active)) { 1722fe089f87STejun Heo if (list_empty(&pool->worklist)) 1723fe089f87STejun Heo pool->watchdog_ts = jiffies; 1724fe089f87STejun Heo 1725cdadf009STejun Heo trace_workqueue_activate_work(work); 1726112202d9STejun Heo pwq->nr_active++; 1727fe089f87STejun Heo insert_work(pwq, work, &pool->worklist, work_flags); 1728fe089f87STejun Heo 1729fe089f87STejun Heo if (__need_more_worker(pool)) 1730fe089f87STejun Heo wake_up_worker(pool); 17318a2e8e5dSTejun Heo } else { 1732f97a4a1aSLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 1733fe089f87STejun Heo insert_work(pwq, work, &pwq->inactive_works, work_flags); 17348a2e8e5dSTejun Heo } 17351e19ffc6STejun Heo 173624acfb71SThomas Gleixner out: 1737fe089f87STejun Heo raw_spin_unlock(&pool->lock); 173824acfb71SThomas Gleixner rcu_read_unlock(); 17391da177e4SLinus Torvalds } 17401da177e4SLinus Torvalds 17410fcb78c2SRolf Eike Beer /** 1742c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1743c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1744c1a220e7SZhang Rui * @wq: workqueue to use 1745c1a220e7SZhang Rui * @work: work to queue 1746c1a220e7SZhang Rui * 1747c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1748443378f0SPaul E. McKenney * can't go away. Callers that fail to ensure that the specified 1749443378f0SPaul E. McKenney * CPU cannot go away will execute on a randomly chosen CPU. 1750854f5cc5SPaul E. McKenney * But note well that callers specifying a CPU that never has been 1751854f5cc5SPaul E. McKenney * online will get a splat. 1752d185af30SYacine Belkadi * 1753d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 1754c1a220e7SZhang Rui */ 1755d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1756d4283e93STejun Heo struct work_struct *work) 1757c1a220e7SZhang Rui { 1758d4283e93STejun Heo bool ret = false; 17598930cabaSTejun Heo unsigned long flags; 17608930cabaSTejun Heo 17618930cabaSTejun Heo local_irq_save(flags); 1762c1a220e7SZhang Rui 176322df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 17644690c4abSTejun Heo __queue_work(cpu, wq, work); 1765d4283e93STejun Heo ret = true; 1766c1a220e7SZhang Rui } 17678930cabaSTejun Heo 17688930cabaSTejun Heo local_irq_restore(flags); 1769c1a220e7SZhang Rui return ret; 1770c1a220e7SZhang Rui } 1771ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 1772c1a220e7SZhang Rui 17738204e0c1SAlexander Duyck /** 1774fef59c9cSTejun Heo * select_numa_node_cpu - Select a CPU based on NUMA node 17758204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from 17768204e0c1SAlexander Duyck * 17778204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given 17788204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return 17798204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any 17808204e0c1SAlexander Duyck * available CPU if we need to schedule this work. 17818204e0c1SAlexander Duyck */ 1782fef59c9cSTejun Heo static int select_numa_node_cpu(int node) 17838204e0c1SAlexander Duyck { 17848204e0c1SAlexander Duyck int cpu; 17858204e0c1SAlexander Duyck 17868204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */ 17878204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) 17888204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 17898204e0c1SAlexander Duyck 17908204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */ 17918204e0c1SAlexander Duyck cpu = raw_smp_processor_id(); 17928204e0c1SAlexander Duyck if (node == cpu_to_node(cpu)) 17938204e0c1SAlexander Duyck return cpu; 17948204e0c1SAlexander Duyck 17958204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */ 17968204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 17978204e0c1SAlexander Duyck 17988204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */ 17998204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; 18008204e0c1SAlexander Duyck } 18018204e0c1SAlexander Duyck 18028204e0c1SAlexander Duyck /** 18038204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node 18048204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for 18058204e0c1SAlexander Duyck * @wq: workqueue to use 18068204e0c1SAlexander Duyck * @work: work to queue 18078204e0c1SAlexander Duyck * 18088204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic 18098204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given 18108204e0c1SAlexander Duyck * NUMA node. 18118204e0c1SAlexander Duyck * 18128204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto 18138204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is 18148204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior. 18158204e0c1SAlexander Duyck * 18168204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the 18178204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we 18188204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU. 18198204e0c1SAlexander Duyck * 18208204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise. 18218204e0c1SAlexander Duyck */ 18228204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq, 18238204e0c1SAlexander Duyck struct work_struct *work) 18248204e0c1SAlexander Duyck { 18258204e0c1SAlexander Duyck unsigned long flags; 18268204e0c1SAlexander Duyck bool ret = false; 18278204e0c1SAlexander Duyck 18288204e0c1SAlexander Duyck /* 18298204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues. 18308204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given 18318204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node. 18328204e0c1SAlexander Duyck * 18338204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in 18348204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for 18358204e0c1SAlexander Duyck * some round robin type logic. 18368204e0c1SAlexander Duyck */ 18378204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 18388204e0c1SAlexander Duyck 18398204e0c1SAlexander Duyck local_irq_save(flags); 18408204e0c1SAlexander Duyck 18418204e0c1SAlexander Duyck if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 1842fef59c9cSTejun Heo int cpu = select_numa_node_cpu(node); 18438204e0c1SAlexander Duyck 18448204e0c1SAlexander Duyck __queue_work(cpu, wq, work); 18458204e0c1SAlexander Duyck ret = true; 18468204e0c1SAlexander Duyck } 18478204e0c1SAlexander Duyck 18488204e0c1SAlexander Duyck local_irq_restore(flags); 18498204e0c1SAlexander Duyck return ret; 18508204e0c1SAlexander Duyck } 18518204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node); 18528204e0c1SAlexander Duyck 18538c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 18541da177e4SLinus Torvalds { 18558c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 18561da177e4SLinus Torvalds 1857e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 185860c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 18591da177e4SLinus Torvalds } 18601438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 18611da177e4SLinus Torvalds 18627beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 186352bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 18641da177e4SLinus Torvalds { 18657beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 18667beb2edfSTejun Heo struct work_struct *work = &dwork->work; 18671da177e4SLinus Torvalds 1868637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 18694b243563SSami Tolvanen WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 1870fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 1871fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 18727beb2edfSTejun Heo 18738852aac2STejun Heo /* 18748852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 18758852aac2STejun Heo * both optimization and correctness. The earliest @timer can 18768852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 18778852aac2STejun Heo * on that there's no such delay when @delay is 0. 18788852aac2STejun Heo */ 18798852aac2STejun Heo if (!delay) { 18808852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 18818852aac2STejun Heo return; 18828852aac2STejun Heo } 18838852aac2STejun Heo 188460c057bcSLai Jiangshan dwork->wq = wq; 18851265057fSTejun Heo dwork->cpu = cpu; 18867beb2edfSTejun Heo timer->expires = jiffies + delay; 18877beb2edfSTejun Heo 1888041bd12eSTejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 18897beb2edfSTejun Heo add_timer_on(timer, cpu); 1890041bd12eSTejun Heo else 1891041bd12eSTejun Heo add_timer(timer); 18927beb2edfSTejun Heo } 18931da177e4SLinus Torvalds 18940fcb78c2SRolf Eike Beer /** 18950fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 18960fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 18970fcb78c2SRolf Eike Beer * @wq: workqueue to use 1898af9997e4SRandy Dunlap * @dwork: work to queue 18990fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 19000fcb78c2SRolf Eike Beer * 1901d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 1902715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 1903715f1300STejun Heo * execution. 19040fcb78c2SRolf Eike Beer */ 1905d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 190652bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 19077a6bc1cdSVenkatesh Pallipadi { 190852bad64dSDavid Howells struct work_struct *work = &dwork->work; 1909d4283e93STejun Heo bool ret = false; 19108930cabaSTejun Heo unsigned long flags; 19118930cabaSTejun Heo 19128930cabaSTejun Heo /* read the comment in __queue_work() */ 19138930cabaSTejun Heo local_irq_save(flags); 19147a6bc1cdSVenkatesh Pallipadi 191522df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 19167beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 1917d4283e93STejun Heo ret = true; 19187a6bc1cdSVenkatesh Pallipadi } 19198930cabaSTejun Heo 19208930cabaSTejun Heo local_irq_restore(flags); 19217a6bc1cdSVenkatesh Pallipadi return ret; 19227a6bc1cdSVenkatesh Pallipadi } 1923ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 19241da177e4SLinus Torvalds 1925c8e55f36STejun Heo /** 19268376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 19278376fe22STejun Heo * @cpu: CPU number to execute work on 19288376fe22STejun Heo * @wq: workqueue to use 19298376fe22STejun Heo * @dwork: work to queue 19308376fe22STejun Heo * @delay: number of jiffies to wait before queueing 19318376fe22STejun Heo * 19328376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 19338376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 19348376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 19358376fe22STejun Heo * current state. 19368376fe22STejun Heo * 1937d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 19388376fe22STejun Heo * pending and its timer was modified. 19398376fe22STejun Heo * 1940e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 19418376fe22STejun Heo * See try_to_grab_pending() for details. 19428376fe22STejun Heo */ 19438376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 19448376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 19458376fe22STejun Heo { 19468376fe22STejun Heo unsigned long flags; 19478376fe22STejun Heo int ret; 19488376fe22STejun Heo 19498376fe22STejun Heo do { 19508376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 19518376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 19528376fe22STejun Heo 19538376fe22STejun Heo if (likely(ret >= 0)) { 19548376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 19558376fe22STejun Heo local_irq_restore(flags); 19568376fe22STejun Heo } 19578376fe22STejun Heo 19588376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 19598376fe22STejun Heo return ret; 19608376fe22STejun Heo } 19618376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 19628376fe22STejun Heo 196305f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 196405f0fe6bSTejun Heo { 196505f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 196605f0fe6bSTejun Heo 196705f0fe6bSTejun Heo /* read the comment in __queue_work() */ 196805f0fe6bSTejun Heo local_irq_disable(); 196905f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 197005f0fe6bSTejun Heo local_irq_enable(); 197105f0fe6bSTejun Heo } 197205f0fe6bSTejun Heo 197305f0fe6bSTejun Heo /** 197405f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 197505f0fe6bSTejun Heo * @wq: workqueue to use 197605f0fe6bSTejun Heo * @rwork: work to queue 197705f0fe6bSTejun Heo * 197805f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 197905f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 1980bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the 198105f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 198205f0fe6bSTejun Heo */ 198305f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 198405f0fe6bSTejun Heo { 198505f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 198605f0fe6bSTejun Heo 198705f0fe6bSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 198805f0fe6bSTejun Heo rwork->wq = wq; 1989a7e30c0eSUladzislau Rezki call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); 199005f0fe6bSTejun Heo return true; 199105f0fe6bSTejun Heo } 199205f0fe6bSTejun Heo 199305f0fe6bSTejun Heo return false; 199405f0fe6bSTejun Heo } 199505f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 199605f0fe6bSTejun Heo 1997f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 1998c34056a3STejun Heo { 1999c34056a3STejun Heo struct worker *worker; 2000c34056a3STejun Heo 2001f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 2002c8e55f36STejun Heo if (worker) { 2003c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 2004affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 2005da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 2006e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 2007e22bee78STejun Heo worker->flags = WORKER_PREP; 2008c8e55f36STejun Heo } 2009c34056a3STejun Heo return worker; 2010c34056a3STejun Heo } 2011c34056a3STejun Heo 2012c34056a3STejun Heo /** 20134736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 20144736cbf7SLai Jiangshan * @worker: worker to be attached 20154736cbf7SLai Jiangshan * @pool: the target pool 20164736cbf7SLai Jiangshan * 20174736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 20184736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 20194736cbf7SLai Jiangshan * cpu-[un]hotplugs. 20204736cbf7SLai Jiangshan */ 20214736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 20224736cbf7SLai Jiangshan struct worker_pool *pool) 20234736cbf7SLai Jiangshan { 20241258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 20254736cbf7SLai Jiangshan 20264736cbf7SLai Jiangshan /* 20271258fae7STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains 20281258fae7STejun Heo * stable across this function. See the comments above the flag 20291258fae7STejun Heo * definition for details. 20304736cbf7SLai Jiangshan */ 20314736cbf7SLai Jiangshan if (pool->flags & POOL_DISASSOCIATED) 20324736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 20335c25b5ffSPeter Zijlstra else 20345c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu); 20354736cbf7SLai Jiangshan 2036640f17c8SPeter Zijlstra if (worker->rescue_wq) 2037640f17c8SPeter Zijlstra set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask); 2038640f17c8SPeter Zijlstra 20394736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 2040a2d812a2STejun Heo worker->pool = pool; 20414736cbf7SLai Jiangshan 20421258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 20434736cbf7SLai Jiangshan } 20444736cbf7SLai Jiangshan 20454736cbf7SLai Jiangshan /** 204660f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 204760f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 204860f5a4bcSLai Jiangshan * 20494736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 20504736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 20514736cbf7SLai Jiangshan * other reference to the pool. 205260f5a4bcSLai Jiangshan */ 2053a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 205460f5a4bcSLai Jiangshan { 2055a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 205660f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 205760f5a4bcSLai Jiangshan 20581258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 2059a2d812a2STejun Heo 20605c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, -1); 2061da028469SLai Jiangshan list_del(&worker->node); 2062a2d812a2STejun Heo worker->pool = NULL; 2063a2d812a2STejun Heo 2064e02b9312SValentin Schneider if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) 206560f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 20661258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 206760f5a4bcSLai Jiangshan 2068b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 2069b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 2070b62c0751SLai Jiangshan 207160f5a4bcSLai Jiangshan if (detach_completion) 207260f5a4bcSLai Jiangshan complete(detach_completion); 207360f5a4bcSLai Jiangshan } 207460f5a4bcSLai Jiangshan 207560f5a4bcSLai Jiangshan /** 2076c34056a3STejun Heo * create_worker - create a new workqueue worker 207763d95a91STejun Heo * @pool: pool the new worker will belong to 2078c34056a3STejun Heo * 2079051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 2080c34056a3STejun Heo * 2081c34056a3STejun Heo * CONTEXT: 2082c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 2083c34056a3STejun Heo * 2084d185af30SYacine Belkadi * Return: 2085c34056a3STejun Heo * Pointer to the newly created worker. 2086c34056a3STejun Heo */ 2087bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 2088c34056a3STejun Heo { 2089e441b56fSZhen Lei struct worker *worker; 2090e441b56fSZhen Lei int id; 2091e3c916a4STejun Heo char id_buf[16]; 2092c34056a3STejun Heo 20937cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 2094e441b56fSZhen Lei id = ida_alloc(&pool->worker_ida, GFP_KERNEL); 20953f0ea0b8SPetr Mladek if (id < 0) { 20963f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", 20973f0ea0b8SPetr Mladek ERR_PTR(id)); 2098e441b56fSZhen Lei return NULL; 20993f0ea0b8SPetr Mladek } 2100c34056a3STejun Heo 2101f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 21023f0ea0b8SPetr Mladek if (!worker) { 21033f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker\n"); 2104c34056a3STejun Heo goto fail; 21053f0ea0b8SPetr Mladek } 2106c34056a3STejun Heo 2107c34056a3STejun Heo worker->id = id; 2108c34056a3STejun Heo 210929c91e99STejun Heo if (pool->cpu >= 0) 2110e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 2111e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 2112f3421797STejun Heo else 2113e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 2114e3c916a4STejun Heo 2115f3f90ad4STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, pool->node, 2116e3c916a4STejun Heo "kworker/%s", id_buf); 21173f0ea0b8SPetr Mladek if (IS_ERR(worker->task)) { 211860f54038SPetr Mladek if (PTR_ERR(worker->task) == -EINTR) { 211960f54038SPetr Mladek pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", 212060f54038SPetr Mladek id_buf); 212160f54038SPetr Mladek } else { 21223f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to create a worker thread: %pe", 21233f0ea0b8SPetr Mladek worker->task); 212460f54038SPetr Mladek } 2125c34056a3STejun Heo goto fail; 21263f0ea0b8SPetr Mladek } 2127c34056a3STejun Heo 212891151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 212925834c73SPeter Zijlstra kthread_bind_mask(worker->task, pool->attrs->cpumask); 213091151228SOleg Nesterov 2131da028469SLai Jiangshan /* successful, attach the worker to the pool */ 21324736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 2133822d8405STejun Heo 2134051e1850SLai Jiangshan /* start the newly created worker */ 2135a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2136051e1850SLai Jiangshan worker->pool->nr_workers++; 2137051e1850SLai Jiangshan worker_enter_idle(worker); 2138051e1850SLai Jiangshan wake_up_process(worker->task); 2139a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2140051e1850SLai Jiangshan 2141c34056a3STejun Heo return worker; 2142822d8405STejun Heo 2143c34056a3STejun Heo fail: 2144e441b56fSZhen Lei ida_free(&pool->worker_ida, id); 2145c34056a3STejun Heo kfree(worker); 2146c34056a3STejun Heo return NULL; 2147c34056a3STejun Heo } 2148c34056a3STejun Heo 2149793777bcSValentin Schneider static void unbind_worker(struct worker *worker) 2150793777bcSValentin Schneider { 2151793777bcSValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2152793777bcSValentin Schneider 2153793777bcSValentin Schneider kthread_set_per_cpu(worker->task, -1); 2154793777bcSValentin Schneider if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) 2155793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); 2156793777bcSValentin Schneider else 2157793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); 2158793777bcSValentin Schneider } 2159793777bcSValentin Schneider 2160e02b9312SValentin Schneider static void wake_dying_workers(struct list_head *cull_list) 2161e02b9312SValentin Schneider { 2162e02b9312SValentin Schneider struct worker *worker, *tmp; 2163e02b9312SValentin Schneider 2164e02b9312SValentin Schneider list_for_each_entry_safe(worker, tmp, cull_list, entry) { 2165e02b9312SValentin Schneider list_del_init(&worker->entry); 2166e02b9312SValentin Schneider unbind_worker(worker); 2167e02b9312SValentin Schneider /* 2168e02b9312SValentin Schneider * If the worker was somehow already running, then it had to be 2169e02b9312SValentin Schneider * in pool->idle_list when set_worker_dying() happened or we 2170e02b9312SValentin Schneider * wouldn't have gotten here. 2171c34056a3STejun Heo * 2172e02b9312SValentin Schneider * Thus, the worker must either have observed the WORKER_DIE 2173e02b9312SValentin Schneider * flag, or have set its state to TASK_IDLE. Either way, the 2174e02b9312SValentin Schneider * below will be observed by the worker and is safe to do 2175e02b9312SValentin Schneider * outside of pool->lock. 2176e02b9312SValentin Schneider */ 2177e02b9312SValentin Schneider wake_up_process(worker->task); 2178e02b9312SValentin Schneider } 2179e02b9312SValentin Schneider } 2180e02b9312SValentin Schneider 2181e02b9312SValentin Schneider /** 2182e02b9312SValentin Schneider * set_worker_dying - Tag a worker for destruction 2183e02b9312SValentin Schneider * @worker: worker to be destroyed 2184e02b9312SValentin Schneider * @list: transfer worker away from its pool->idle_list and into list 2185e02b9312SValentin Schneider * 2186e02b9312SValentin Schneider * Tag @worker for destruction and adjust @pool stats accordingly. The worker 2187e02b9312SValentin Schneider * should be idle. 2188c8e55f36STejun Heo * 2189c8e55f36STejun Heo * CONTEXT: 2190a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2191c34056a3STejun Heo */ 2192e02b9312SValentin Schneider static void set_worker_dying(struct worker *worker, struct list_head *list) 2193c34056a3STejun Heo { 2194bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2195c34056a3STejun Heo 2196cd549687STejun Heo lockdep_assert_held(&pool->lock); 2197e02b9312SValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2198cd549687STejun Heo 2199c34056a3STejun Heo /* sanity check frenzy */ 22006183c009STejun Heo if (WARN_ON(worker->current_work) || 220173eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 220273eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 22036183c009STejun Heo return; 2204c34056a3STejun Heo 2205bd7bdd43STejun Heo pool->nr_workers--; 2206bd7bdd43STejun Heo pool->nr_idle--; 2207c8e55f36STejun Heo 2208cb444766STejun Heo worker->flags |= WORKER_DIE; 2209e02b9312SValentin Schneider 2210e02b9312SValentin Schneider list_move(&worker->entry, list); 2211e02b9312SValentin Schneider list_move(&worker->node, &pool->dying_workers); 2212c34056a3STejun Heo } 2213c34056a3STejun Heo 22143f959aa3SValentin Schneider /** 22153f959aa3SValentin Schneider * idle_worker_timeout - check if some idle workers can now be deleted. 22163f959aa3SValentin Schneider * @t: The pool's idle_timer that just expired 22173f959aa3SValentin Schneider * 22183f959aa3SValentin Schneider * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in 22193f959aa3SValentin Schneider * worker_leave_idle(), as a worker flicking between idle and active while its 22203f959aa3SValentin Schneider * pool is at the too_many_workers() tipping point would cause too much timer 22213f959aa3SValentin Schneider * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let 22223f959aa3SValentin Schneider * it expire and re-evaluate things from there. 22233f959aa3SValentin Schneider */ 222432a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 2225e22bee78STejun Heo { 222632a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 22273f959aa3SValentin Schneider bool do_cull = false; 22283f959aa3SValentin Schneider 22293f959aa3SValentin Schneider if (work_pending(&pool->idle_cull_work)) 22303f959aa3SValentin Schneider return; 22313f959aa3SValentin Schneider 22323f959aa3SValentin Schneider raw_spin_lock_irq(&pool->lock); 22333f959aa3SValentin Schneider 22343f959aa3SValentin Schneider if (too_many_workers(pool)) { 22353f959aa3SValentin Schneider struct worker *worker; 22363f959aa3SValentin Schneider unsigned long expires; 22373f959aa3SValentin Schneider 22383f959aa3SValentin Schneider /* idle_list is kept in LIFO order, check the last one */ 22393f959aa3SValentin Schneider worker = list_entry(pool->idle_list.prev, struct worker, entry); 22403f959aa3SValentin Schneider expires = worker->last_active + IDLE_WORKER_TIMEOUT; 22413f959aa3SValentin Schneider do_cull = !time_before(jiffies, expires); 22423f959aa3SValentin Schneider 22433f959aa3SValentin Schneider if (!do_cull) 22443f959aa3SValentin Schneider mod_timer(&pool->idle_timer, expires); 22453f959aa3SValentin Schneider } 22463f959aa3SValentin Schneider raw_spin_unlock_irq(&pool->lock); 22473f959aa3SValentin Schneider 22483f959aa3SValentin Schneider if (do_cull) 22493f959aa3SValentin Schneider queue_work(system_unbound_wq, &pool->idle_cull_work); 22503f959aa3SValentin Schneider } 22513f959aa3SValentin Schneider 22523f959aa3SValentin Schneider /** 22533f959aa3SValentin Schneider * idle_cull_fn - cull workers that have been idle for too long. 22543f959aa3SValentin Schneider * @work: the pool's work for handling these idle workers 22553f959aa3SValentin Schneider * 22563f959aa3SValentin Schneider * This goes through a pool's idle workers and gets rid of those that have been 22573f959aa3SValentin Schneider * idle for at least IDLE_WORKER_TIMEOUT seconds. 2258e02b9312SValentin Schneider * 2259e02b9312SValentin Schneider * We don't want to disturb isolated CPUs because of a pcpu kworker being 2260e02b9312SValentin Schneider * culled, so this also resets worker affinity. This requires a sleepable 2261e02b9312SValentin Schneider * context, hence the split between timer callback and work item. 22623f959aa3SValentin Schneider */ 22633f959aa3SValentin Schneider static void idle_cull_fn(struct work_struct *work) 22643f959aa3SValentin Schneider { 22653f959aa3SValentin Schneider struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); 22669680540cSYang Yingliang LIST_HEAD(cull_list); 2267e22bee78STejun Heo 2268e02b9312SValentin Schneider /* 2269e02b9312SValentin Schneider * Grabbing wq_pool_attach_mutex here ensures an already-running worker 2270e02b9312SValentin Schneider * cannot proceed beyong worker_detach_from_pool() in its self-destruct 2271e02b9312SValentin Schneider * path. This is required as a previously-preempted worker could run after 2272e02b9312SValentin Schneider * set_worker_dying() has happened but before wake_dying_workers() did. 2273e02b9312SValentin Schneider */ 2274e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 2275a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2276e22bee78STejun Heo 22773347fc9fSLai Jiangshan while (too_many_workers(pool)) { 2278e22bee78STejun Heo struct worker *worker; 2279e22bee78STejun Heo unsigned long expires; 2280e22bee78STejun Heo 228163d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 2282e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 2283e22bee78STejun Heo 22843347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 228563d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 22863347fc9fSLai Jiangshan break; 2287e22bee78STejun Heo } 22883347fc9fSLai Jiangshan 2289e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 2290e22bee78STejun Heo } 2291e22bee78STejun Heo 2292a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2293e02b9312SValentin Schneider wake_dying_workers(&cull_list); 2294e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 2295e22bee78STejun Heo } 2296e22bee78STejun Heo 2297493a1724STejun Heo static void send_mayday(struct work_struct *work) 2298e22bee78STejun Heo { 2299112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2300112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 2301493a1724STejun Heo 23022e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 2303e22bee78STejun Heo 2304493008a8STejun Heo if (!wq->rescuer) 2305493a1724STejun Heo return; 2306e22bee78STejun Heo 2307e22bee78STejun Heo /* mayday mayday mayday */ 2308493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 230977668c8bSLai Jiangshan /* 231077668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 231177668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 231277668c8bSLai Jiangshan * rescuer is done with it. 231377668c8bSLai Jiangshan */ 231477668c8bSLai Jiangshan get_pwq(pwq); 2315493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2316e22bee78STejun Heo wake_up_process(wq->rescuer->task); 2317725e8ec5STejun Heo pwq->stats[PWQ_STAT_MAYDAY]++; 2318493a1724STejun Heo } 2319e22bee78STejun Heo } 2320e22bee78STejun Heo 232132a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 2322e22bee78STejun Heo { 232332a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 2324e22bee78STejun Heo struct work_struct *work; 2325e22bee78STejun Heo 2326a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2327a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ 2328e22bee78STejun Heo 232963d95a91STejun Heo if (need_to_create_worker(pool)) { 2330e22bee78STejun Heo /* 2331e22bee78STejun Heo * We've been trying to create a new worker but 2332e22bee78STejun Heo * haven't been successful. We might be hitting an 2333e22bee78STejun Heo * allocation deadlock. Send distress signals to 2334e22bee78STejun Heo * rescuers. 2335e22bee78STejun Heo */ 233663d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 2337e22bee78STejun Heo send_mayday(work); 2338e22bee78STejun Heo } 2339e22bee78STejun Heo 2340a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2341a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2342e22bee78STejun Heo 234363d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 2344e22bee78STejun Heo } 2345e22bee78STejun Heo 2346e22bee78STejun Heo /** 2347e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 234863d95a91STejun Heo * @pool: pool to create a new worker for 2349e22bee78STejun Heo * 235063d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 2351e22bee78STejun Heo * have at least one idle worker on return from this function. If 2352e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 235363d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 2354e22bee78STejun Heo * possible allocation deadlock. 2355e22bee78STejun Heo * 2356c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 2357c5aa87bbSTejun Heo * may_start_working() %true. 2358e22bee78STejun Heo * 2359e22bee78STejun Heo * LOCKING: 2360a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2361e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 2362e22bee78STejun Heo * manager. 2363e22bee78STejun Heo */ 236429187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 2365d565ed63STejun Heo __releases(&pool->lock) 2366d565ed63STejun Heo __acquires(&pool->lock) 2367e22bee78STejun Heo { 2368e22bee78STejun Heo restart: 2369a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 23709f9c2364STejun Heo 2371e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 237263d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 2373e22bee78STejun Heo 2374e22bee78STejun Heo while (true) { 2375051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 2376e22bee78STejun Heo break; 2377e22bee78STejun Heo 2378e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 23799f9c2364STejun Heo 238063d95a91STejun Heo if (!need_to_create_worker(pool)) 2381e22bee78STejun Heo break; 2382e22bee78STejun Heo } 2383e22bee78STejun Heo 238463d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 2385a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2386051e1850SLai Jiangshan /* 2387051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 2388051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 2389051e1850SLai Jiangshan * already become busy. 2390051e1850SLai Jiangshan */ 239163d95a91STejun Heo if (need_to_create_worker(pool)) 2392e22bee78STejun Heo goto restart; 2393e22bee78STejun Heo } 2394e22bee78STejun Heo 2395e22bee78STejun Heo /** 2396e22bee78STejun Heo * manage_workers - manage worker pool 2397e22bee78STejun Heo * @worker: self 2398e22bee78STejun Heo * 2399706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 2400e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 2401706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 2402e22bee78STejun Heo * 2403e22bee78STejun Heo * The caller can safely start processing works on false return. On 2404e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 2405e22bee78STejun Heo * and may_start_working() is true. 2406e22bee78STejun Heo * 2407e22bee78STejun Heo * CONTEXT: 2408a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2409e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 2410e22bee78STejun Heo * 2411d185af30SYacine Belkadi * Return: 241229187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 241329187a9eSTejun Heo * start processing works, %true if management function was performed and 241429187a9eSTejun Heo * the conditions that the caller verified before calling the function may 241529187a9eSTejun Heo * no longer be true. 2416e22bee78STejun Heo */ 2417e22bee78STejun Heo static bool manage_workers(struct worker *worker) 2418e22bee78STejun Heo { 241963d95a91STejun Heo struct worker_pool *pool = worker->pool; 2420e22bee78STejun Heo 2421692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 242229187a9eSTejun Heo return false; 2423692b4825STejun Heo 2424692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 24252607d7a6STejun Heo pool->manager = worker; 2426e22bee78STejun Heo 242729187a9eSTejun Heo maybe_create_worker(pool); 2428e22bee78STejun Heo 24292607d7a6STejun Heo pool->manager = NULL; 2430692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 2431d8bb65abSSebastian Andrzej Siewior rcuwait_wake_up(&manager_wait); 243229187a9eSTejun Heo return true; 2433e22bee78STejun Heo } 2434e22bee78STejun Heo 2435a62428c0STejun Heo /** 2436a62428c0STejun Heo * process_one_work - process single work 2437c34056a3STejun Heo * @worker: self 2438a62428c0STejun Heo * @work: work to process 2439a62428c0STejun Heo * 2440a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2441a62428c0STejun Heo * process a single work including synchronization against and 2442a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2443a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2444a62428c0STejun Heo * call this function to process a work. 2445a62428c0STejun Heo * 2446a62428c0STejun Heo * CONTEXT: 2447a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which is released and regrabbed. 2448a62428c0STejun Heo */ 2449c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 2450d565ed63STejun Heo __releases(&pool->lock) 2451d565ed63STejun Heo __acquires(&pool->lock) 24521da177e4SLinus Torvalds { 2453112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2454bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2455c4560c2cSLai Jiangshan unsigned long work_data; 24567e11629dSTejun Heo struct worker *collision; 24574e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 24584e6045f1SJohannes Berg /* 2459a62428c0STejun Heo * It is permissible to free the struct work_struct from 2460a62428c0STejun Heo * inside the function that is called from it, this we need to 2461a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2462a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2463a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 24644e6045f1SJohannes Berg */ 24654d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 24664d82a1deSPeter Zijlstra 24674d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 24684e6045f1SJohannes Berg #endif 2469807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 247085327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 2471ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 247225511a47STejun Heo 24737e11629dSTejun Heo /* 24747e11629dSTejun Heo * A single work shouldn't be executed concurrently by 24757e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 24767e11629dSTejun Heo * already processing the work. If so, defer the work to the 24777e11629dSTejun Heo * currently executing one. 24787e11629dSTejun Heo */ 2479c9e7cf27STejun Heo collision = find_worker_executing_work(pool, work); 24807e11629dSTejun Heo if (unlikely(collision)) { 24817e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 24827e11629dSTejun Heo return; 24837e11629dSTejun Heo } 24841da177e4SLinus Torvalds 24858930cabaSTejun Heo /* claim and dequeue */ 24861da177e4SLinus Torvalds debug_work_deactivate(work); 2487c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 2488c34056a3STejun Heo worker->current_work = work; 2489a2c1c57bSTejun Heo worker->current_func = work->func; 2490112202d9STejun Heo worker->current_pwq = pwq; 2491616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 2492c4560c2cSLai Jiangshan work_data = *work_data_bits(work); 2493d812796eSLai Jiangshan worker->current_color = get_work_color(work_data); 24947a22ad75STejun Heo 24958bf89593STejun Heo /* 24968bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 24978bf89593STejun Heo * overridden through set_worker_desc(). 24988bf89593STejun Heo */ 24998bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 25008bf89593STejun Heo 2501a62428c0STejun Heo list_del_init(&work->entry); 2502a62428c0STejun Heo 2503649027d7STejun Heo /* 2504228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 2505228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 2506228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 2507228f1d00SLai Jiangshan * execution of the pending work items. 2508fb0e7bebSTejun Heo */ 2509616db877STejun Heo if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE)) 2510228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 2511fb0e7bebSTejun Heo 2512974271c4STejun Heo /* 2513a489a03eSLai Jiangshan * Wake up another worker if necessary. The condition is always 2514a489a03eSLai Jiangshan * false for normal per-cpu workers since nr_running would always 2515a489a03eSLai Jiangshan * be >= 1 at this point. This is used to chain execution of the 2516a489a03eSLai Jiangshan * pending work items for WORKER_NOT_RUNNING workers such as the 2517228f1d00SLai Jiangshan * UNBOUND and CPU_INTENSIVE ones. 2518974271c4STejun Heo */ 2519a489a03eSLai Jiangshan if (need_more_worker(pool)) 252063d95a91STejun Heo wake_up_worker(pool); 2521974271c4STejun Heo 25228930cabaSTejun Heo /* 25237c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 2524d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 252523657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 252623657bb1STejun Heo * disabled. 25278930cabaSTejun Heo */ 25287c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 25291da177e4SLinus Torvalds 2530a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2531365970a1SDavid Howells 2532a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 25333295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2534e6f3faa7SPeter Zijlstra /* 2535f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 2536f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 2537e6f3faa7SPeter Zijlstra * 2538e6f3faa7SPeter Zijlstra * However, that would result in: 2539e6f3faa7SPeter Zijlstra * 2540e6f3faa7SPeter Zijlstra * A(W1) 2541e6f3faa7SPeter Zijlstra * WFC(C) 2542e6f3faa7SPeter Zijlstra * A(W1) 2543e6f3faa7SPeter Zijlstra * C(C) 2544e6f3faa7SPeter Zijlstra * 2545e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 2546e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 2547e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 2548f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 2549e6f3faa7SPeter Zijlstra * these locks. 2550e6f3faa7SPeter Zijlstra * 2551e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 2552e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 2553e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 2554e6f3faa7SPeter Zijlstra */ 2555f52be570SPeter Zijlstra lockdep_invariant_state(true); 2556725e8ec5STejun Heo pwq->stats[PWQ_STAT_STARTED]++; 2557e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 2558a2c1c57bSTejun Heo worker->current_func(work); 2559e36c886aSArjan van de Ven /* 2560e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2561e36c886aSArjan van de Ven * point will only record its address. 2562e36c886aSArjan van de Ven */ 25631c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func); 2564725e8ec5STejun Heo pwq->stats[PWQ_STAT_COMPLETED]++; 25653295f0efSIngo Molnar lock_map_release(&lockdep_map); 2566112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 25671da177e4SLinus Torvalds 2568d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2569044c782cSValentin Ilie pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n" 2570d75f773cSSakari Ailus " last function: %ps\n", 2571a2c1c57bSTejun Heo current->comm, preempt_count(), task_pid_nr(current), 2572a2c1c57bSTejun Heo worker->current_func); 2573d5abe669SPeter Zijlstra debug_show_held_locks(current); 2574d5abe669SPeter Zijlstra dump_stack(); 2575d5abe669SPeter Zijlstra } 2576d5abe669SPeter Zijlstra 2577b22ce278STejun Heo /* 2578025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION 2579b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 2580b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 2581b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 2582789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 2583789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 2584b22ce278STejun Heo */ 2585a7e6425eSPaul E. McKenney cond_resched(); 2586b22ce278STejun Heo 2587a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2588a62428c0STejun Heo 2589616db877STejun Heo /* 2590616db877STejun Heo * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked 2591616db877STejun Heo * CPU intensive by wq_worker_tick() if @work hogged CPU longer than 2592616db877STejun Heo * wq_cpu_intensive_thresh_us. Clear it. 2593616db877STejun Heo */ 2594fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2595fb0e7bebSTejun Heo 25961b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 25971b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 25981b69ac6bSJohannes Weiner 2599a62428c0STejun Heo /* we're done with it, release */ 260042f8570fSSasha Levin hash_del(&worker->hentry); 2601c34056a3STejun Heo worker->current_work = NULL; 2602a2c1c57bSTejun Heo worker->current_func = NULL; 2603112202d9STejun Heo worker->current_pwq = NULL; 2604d812796eSLai Jiangshan worker->current_color = INT_MAX; 2605c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, work_data); 26061da177e4SLinus Torvalds } 26071da177e4SLinus Torvalds 2608affee4b2STejun Heo /** 2609affee4b2STejun Heo * process_scheduled_works - process scheduled works 2610affee4b2STejun Heo * @worker: self 2611affee4b2STejun Heo * 2612affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2613affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2614affee4b2STejun Heo * fetches a work from the top and executes it. 2615affee4b2STejun Heo * 2616affee4b2STejun Heo * CONTEXT: 2617a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2618affee4b2STejun Heo * multiple times. 2619affee4b2STejun Heo */ 2620affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 26211da177e4SLinus Torvalds { 2622c0ab017dSTejun Heo struct work_struct *work; 2623c0ab017dSTejun Heo bool first = true; 2624c0ab017dSTejun Heo 2625c0ab017dSTejun Heo while ((work = list_first_entry_or_null(&worker->scheduled, 2626c0ab017dSTejun Heo struct work_struct, entry))) { 2627c0ab017dSTejun Heo if (first) { 2628c0ab017dSTejun Heo worker->pool->watchdog_ts = jiffies; 2629c0ab017dSTejun Heo first = false; 2630c0ab017dSTejun Heo } 2631c34056a3STejun Heo process_one_work(worker, work); 2632a62428c0STejun Heo } 26331da177e4SLinus Torvalds } 26341da177e4SLinus Torvalds 2635197f6accSTejun Heo static void set_pf_worker(bool val) 2636197f6accSTejun Heo { 2637197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 2638197f6accSTejun Heo if (val) 2639197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 2640197f6accSTejun Heo else 2641197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 2642197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 2643197f6accSTejun Heo } 2644197f6accSTejun Heo 26454690c4abSTejun Heo /** 26464690c4abSTejun Heo * worker_thread - the worker thread function 2647c34056a3STejun Heo * @__worker: self 26484690c4abSTejun Heo * 2649c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 2650c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 2651c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 2652c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 2653c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 2654d185af30SYacine Belkadi * 2655d185af30SYacine Belkadi * Return: 0 26564690c4abSTejun Heo */ 2657c34056a3STejun Heo static int worker_thread(void *__worker) 26581da177e4SLinus Torvalds { 2659c34056a3STejun Heo struct worker *worker = __worker; 2660bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 26611da177e4SLinus Torvalds 2662e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2663197f6accSTejun Heo set_pf_worker(true); 2664c8e55f36STejun Heo woke_up: 2665a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2666affee4b2STejun Heo 2667a9ab775bSTejun Heo /* am I supposed to die? */ 2668a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 2669a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2670197f6accSTejun Heo set_pf_worker(false); 267160f5a4bcSLai Jiangshan 267260f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 2673e441b56fSZhen Lei ida_free(&pool->worker_ida, worker->id); 2674a2d812a2STejun Heo worker_detach_from_pool(worker); 2675e02b9312SValentin Schneider WARN_ON_ONCE(!list_empty(&worker->entry)); 267660f5a4bcSLai Jiangshan kfree(worker); 2677c8e55f36STejun Heo return 0; 2678c8e55f36STejun Heo } 2679c8e55f36STejun Heo 2680c8e55f36STejun Heo worker_leave_idle(worker); 2681db7bccf4STejun Heo recheck: 2682e22bee78STejun Heo /* no more worker necessary? */ 268363d95a91STejun Heo if (!need_more_worker(pool)) 2684e22bee78STejun Heo goto sleep; 2685e22bee78STejun Heo 2686e22bee78STejun Heo /* do we need to manage? */ 268763d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2688e22bee78STejun Heo goto recheck; 2689e22bee78STejun Heo 2690c8e55f36STejun Heo /* 2691c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2692c8e55f36STejun Heo * preparing to process a work or actually processing it. 2693c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2694c8e55f36STejun Heo */ 26956183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 2696c8e55f36STejun Heo 2697e22bee78STejun Heo /* 2698a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 2699a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 2700a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 2701a9ab775bSTejun Heo * management if applicable and concurrency management is restored 2702a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 2703e22bee78STejun Heo */ 2704a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 2705e22bee78STejun Heo 2706e22bee78STejun Heo do { 2707affee4b2STejun Heo struct work_struct *work = 2708bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2709affee4b2STejun Heo struct work_struct, entry); 2710affee4b2STejun Heo 2711c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2712affee4b2STejun Heo process_scheduled_works(worker); 271363d95a91STejun Heo } while (keep_working(pool)); 2714affee4b2STejun Heo 2715228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 2716d313dd85STejun Heo sleep: 2717c8e55f36STejun Heo /* 2718d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 2719d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 2720d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 2721d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 2722d565ed63STejun Heo * event. 2723c8e55f36STejun Heo */ 2724c8e55f36STejun Heo worker_enter_idle(worker); 2725c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 2726a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 27271da177e4SLinus Torvalds schedule(); 2728c8e55f36STejun Heo goto woke_up; 27291da177e4SLinus Torvalds } 27301da177e4SLinus Torvalds 2731e22bee78STejun Heo /** 2732e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2733111c225aSTejun Heo * @__rescuer: self 2734e22bee78STejun Heo * 2735e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2736493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 2737e22bee78STejun Heo * 2738706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 2739e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2740e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2741e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2742e22bee78STejun Heo * the problem rescuer solves. 2743e22bee78STejun Heo * 2744706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 2745706026c2STejun Heo * workqueues which have works queued on the pool and let them process 2746e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2747e22bee78STejun Heo * 2748e22bee78STejun Heo * This should happen rarely. 2749d185af30SYacine Belkadi * 2750d185af30SYacine Belkadi * Return: 0 2751e22bee78STejun Heo */ 2752111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 2753e22bee78STejun Heo { 2754111c225aSTejun Heo struct worker *rescuer = __rescuer; 2755111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 2756e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 27574d595b86SLai Jiangshan bool should_stop; 2758e22bee78STejun Heo 2759e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2760111c225aSTejun Heo 2761111c225aSTejun Heo /* 2762111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 2763111c225aSTejun Heo * doesn't participate in concurrency management. 2764111c225aSTejun Heo */ 2765197f6accSTejun Heo set_pf_worker(true); 2766e22bee78STejun Heo repeat: 2767c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 27681da177e4SLinus Torvalds 27694d595b86SLai Jiangshan /* 27704d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 27714d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 27724d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 27734d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 27744d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 27754d595b86SLai Jiangshan * list is always empty on exit. 27764d595b86SLai Jiangshan */ 27774d595b86SLai Jiangshan should_stop = kthread_should_stop(); 27781da177e4SLinus Torvalds 2779493a1724STejun Heo /* see whether any pwq is asking for help */ 2780a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 2781493a1724STejun Heo 2782493a1724STejun Heo while (!list_empty(&wq->maydays)) { 2783493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 2784493a1724STejun Heo struct pool_workqueue, mayday_node); 2785112202d9STejun Heo struct worker_pool *pool = pwq->pool; 2786e22bee78STejun Heo struct work_struct *work, *n; 2787e22bee78STejun Heo 2788e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2789493a1724STejun Heo list_del_init(&pwq->mayday_node); 2790493a1724STejun Heo 2791a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 2792e22bee78STejun Heo 279351697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 279451697d39SLai Jiangshan 2795a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2796e22bee78STejun Heo 2797e22bee78STejun Heo /* 2798e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2799e22bee78STejun Heo * process'em. 2800e22bee78STejun Heo */ 28010479c8c5STejun Heo WARN_ON_ONCE(!list_empty(scheduled)); 280282607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 280382607adcSTejun Heo if (get_work_pwq(work) == pwq) { 2804e22bee78STejun Heo move_linked_works(work, scheduled, &n); 2805725e8ec5STejun Heo pwq->stats[PWQ_STAT_RESCUED]++; 280682607adcSTejun Heo } 280782607adcSTejun Heo } 2808e22bee78STejun Heo 2809008847f6SNeilBrown if (!list_empty(scheduled)) { 2810e22bee78STejun Heo process_scheduled_works(rescuer); 28117576958aSTejun Heo 28127576958aSTejun Heo /* 2813008847f6SNeilBrown * The above execution of rescued work items could 2814008847f6SNeilBrown * have created more to rescue through 2815f97a4a1aSLai Jiangshan * pwq_activate_first_inactive() or chained 2816008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 2817008847f6SNeilBrown * that such back-to-back work items, which may be 2818008847f6SNeilBrown * being used to relieve memory pressure, don't 2819008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 2820008847f6SNeilBrown */ 28214f3f4cf3SLai Jiangshan if (pwq->nr_active && need_to_create_worker(pool)) { 2822a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); 2823e66b39afSTejun Heo /* 2824e66b39afSTejun Heo * Queue iff we aren't racing destruction 2825e66b39afSTejun Heo * and somebody else hasn't queued it already. 2826e66b39afSTejun Heo */ 2827e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) { 2828008847f6SNeilBrown get_pwq(pwq); 2829e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2830e66b39afSTejun Heo } 2831a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2832008847f6SNeilBrown } 2833008847f6SNeilBrown } 2834008847f6SNeilBrown 2835008847f6SNeilBrown /* 283677668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 283713b1d625SLai Jiangshan * go away while we're still attached to it. 283877668c8bSLai Jiangshan */ 283977668c8bSLai Jiangshan put_pwq(pwq); 284077668c8bSLai Jiangshan 284177668c8bSLai Jiangshan /* 2842d8ca83e6SLai Jiangshan * Leave this pool. If need_more_worker() is %true, notify a 28437576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 28447576958aSTejun Heo * and stalling the execution. 28457576958aSTejun Heo */ 2846d8ca83e6SLai Jiangshan if (need_more_worker(pool)) 284763d95a91STejun Heo wake_up_worker(pool); 28487576958aSTejun Heo 2849a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 285013b1d625SLai Jiangshan 2851a2d812a2STejun Heo worker_detach_from_pool(rescuer); 285213b1d625SLai Jiangshan 2853a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 28541da177e4SLinus Torvalds } 28551da177e4SLinus Torvalds 2856a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 2857493a1724STejun Heo 28584d595b86SLai Jiangshan if (should_stop) { 28594d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 2860197f6accSTejun Heo set_pf_worker(false); 28614d595b86SLai Jiangshan return 0; 28624d595b86SLai Jiangshan } 28634d595b86SLai Jiangshan 2864111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 2865111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 2866e22bee78STejun Heo schedule(); 2867e22bee78STejun Heo goto repeat; 28681da177e4SLinus Torvalds } 28691da177e4SLinus Torvalds 2870fca839c0STejun Heo /** 2871fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 2872fca839c0STejun Heo * @target_wq: workqueue being flushed 2873fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 2874fca839c0STejun Heo * 2875fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 2876fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 2877fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 2878fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 2879fca839c0STejun Heo * a deadlock. 2880fca839c0STejun Heo */ 2881fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 2882fca839c0STejun Heo struct work_struct *target_work) 2883fca839c0STejun Heo { 2884fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 2885fca839c0STejun Heo struct worker *worker; 2886fca839c0STejun Heo 2887fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 2888fca839c0STejun Heo return; 2889fca839c0STejun Heo 2890fca839c0STejun Heo worker = current_wq_worker(); 2891fca839c0STejun Heo 2892fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 2893d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", 2894fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 289523d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 289623d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 2897d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", 2898fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 2899fca839c0STejun Heo target_wq->name, target_func); 2900fca839c0STejun Heo } 2901fca839c0STejun Heo 2902fc2e4d70SOleg Nesterov struct wq_barrier { 2903fc2e4d70SOleg Nesterov struct work_struct work; 2904fc2e4d70SOleg Nesterov struct completion done; 29052607d7a6STejun Heo struct task_struct *task; /* purely informational */ 2906fc2e4d70SOleg Nesterov }; 2907fc2e4d70SOleg Nesterov 2908fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2909fc2e4d70SOleg Nesterov { 2910fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2911fc2e4d70SOleg Nesterov complete(&barr->done); 2912fc2e4d70SOleg Nesterov } 2913fc2e4d70SOleg Nesterov 29144690c4abSTejun Heo /** 29154690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 2916112202d9STejun Heo * @pwq: pwq to insert barrier into 29174690c4abSTejun Heo * @barr: wq_barrier to insert 2918affee4b2STejun Heo * @target: target work to attach @barr to 2919affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 29204690c4abSTejun Heo * 2921affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2922affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2923affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2924affee4b2STejun Heo * cpu. 2925affee4b2STejun Heo * 2926affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2927affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2928affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2929affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2930affee4b2STejun Heo * after a work with LINKED flag set. 2931affee4b2STejun Heo * 2932affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2933112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 29344690c4abSTejun Heo * 29354690c4abSTejun Heo * CONTEXT: 2936a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 29374690c4abSTejun Heo */ 2938112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 2939affee4b2STejun Heo struct wq_barrier *barr, 2940affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2941fc2e4d70SOleg Nesterov { 2942d812796eSLai Jiangshan unsigned int work_flags = 0; 2943d812796eSLai Jiangshan unsigned int work_color; 2944affee4b2STejun Heo struct list_head *head; 2945affee4b2STejun Heo 2946dc186ad7SThomas Gleixner /* 2947d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 2948dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2949dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2950dc186ad7SThomas Gleixner * might deadlock. 2951dc186ad7SThomas Gleixner */ 2952ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 295322df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 295452fa5bc5SBoqun Feng 2955fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 2956fd1a5b04SByungchul Park 29572607d7a6STejun Heo barr->task = current; 295883c22520SOleg Nesterov 2959018f3a13SLai Jiangshan /* The barrier work item does not participate in pwq->nr_active. */ 2960018f3a13SLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 2961018f3a13SLai Jiangshan 2962affee4b2STejun Heo /* 2963affee4b2STejun Heo * If @target is currently being executed, schedule the 2964affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2965affee4b2STejun Heo */ 2966d812796eSLai Jiangshan if (worker) { 2967affee4b2STejun Heo head = worker->scheduled.next; 2968d812796eSLai Jiangshan work_color = worker->current_color; 2969d812796eSLai Jiangshan } else { 2970affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2971affee4b2STejun Heo 2972affee4b2STejun Heo head = target->entry.next; 2973affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2974d21cece0SLai Jiangshan work_flags |= *bits & WORK_STRUCT_LINKED; 2975d812796eSLai Jiangshan work_color = get_work_color(*bits); 2976affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2977affee4b2STejun Heo } 2978affee4b2STejun Heo 2979d812796eSLai Jiangshan pwq->nr_in_flight[work_color]++; 2980d812796eSLai Jiangshan work_flags |= work_color_to_flags(work_color); 2981d812796eSLai Jiangshan 2982d21cece0SLai Jiangshan insert_work(pwq, &barr->work, head, work_flags); 2983fc2e4d70SOleg Nesterov } 2984fc2e4d70SOleg Nesterov 298573f53c4aSTejun Heo /** 2986112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 298773f53c4aSTejun Heo * @wq: workqueue being flushed 298873f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 298973f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 299073f53c4aSTejun Heo * 2991112202d9STejun Heo * Prepare pwqs for workqueue flushing. 299273f53c4aSTejun Heo * 2993112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 2994112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 2995112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 2996112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 2997112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 299873f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 299973f53c4aSTejun Heo * 300073f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 300173f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 300273f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 300373f53c4aSTejun Heo * is returned. 300473f53c4aSTejun Heo * 3005112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 300673f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 300773f53c4aSTejun Heo * advanced to @work_color. 300873f53c4aSTejun Heo * 300973f53c4aSTejun Heo * CONTEXT: 30103c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 301173f53c4aSTejun Heo * 3012d185af30SYacine Belkadi * Return: 301373f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 301473f53c4aSTejun Heo * otherwise. 301573f53c4aSTejun Heo */ 3016112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 301773f53c4aSTejun Heo int flush_color, int work_color) 30181da177e4SLinus Torvalds { 301973f53c4aSTejun Heo bool wait = false; 302049e3cf44STejun Heo struct pool_workqueue *pwq; 30211da177e4SLinus Torvalds 302273f53c4aSTejun Heo if (flush_color >= 0) { 30236183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 3024112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 3025dc186ad7SThomas Gleixner } 302614441960SOleg Nesterov 302749e3cf44STejun Heo for_each_pwq(pwq, wq) { 3028112202d9STejun Heo struct worker_pool *pool = pwq->pool; 30291da177e4SLinus Torvalds 3030a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 303173f53c4aSTejun Heo 303273f53c4aSTejun Heo if (flush_color >= 0) { 30336183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 303473f53c4aSTejun Heo 3035112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 3036112202d9STejun Heo pwq->flush_color = flush_color; 3037112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 303873f53c4aSTejun Heo wait = true; 30391da177e4SLinus Torvalds } 304073f53c4aSTejun Heo } 304173f53c4aSTejun Heo 304273f53c4aSTejun Heo if (work_color >= 0) { 30436183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 3044112202d9STejun Heo pwq->work_color = work_color; 304573f53c4aSTejun Heo } 304673f53c4aSTejun Heo 3047a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 30481da177e4SLinus Torvalds } 30491da177e4SLinus Torvalds 3050112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 305173f53c4aSTejun Heo complete(&wq->first_flusher->done); 305273f53c4aSTejun Heo 305373f53c4aSTejun Heo return wait; 305483c22520SOleg Nesterov } 30551da177e4SLinus Torvalds 30560fcb78c2SRolf Eike Beer /** 3057c4f135d6STetsuo Handa * __flush_workqueue - ensure that any scheduled work has run to completion. 30580fcb78c2SRolf Eike Beer * @wq: workqueue to flush 30591da177e4SLinus Torvalds * 3060c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 3061c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 30621da177e4SLinus Torvalds */ 3063c4f135d6STetsuo Handa void __flush_workqueue(struct workqueue_struct *wq) 30641da177e4SLinus Torvalds { 306573f53c4aSTejun Heo struct wq_flusher this_flusher = { 306673f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 306773f53c4aSTejun Heo .flush_color = -1, 3068fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 306973f53c4aSTejun Heo }; 307073f53c4aSTejun Heo int next_color; 3071b1f4ec17SOleg Nesterov 30723347fa09STejun Heo if (WARN_ON(!wq_online)) 30733347fa09STejun Heo return; 30743347fa09STejun Heo 307587915adcSJohannes Berg lock_map_acquire(&wq->lockdep_map); 307687915adcSJohannes Berg lock_map_release(&wq->lockdep_map); 307787915adcSJohannes Berg 30783c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 307973f53c4aSTejun Heo 308073f53c4aSTejun Heo /* 308173f53c4aSTejun Heo * Start-to-wait phase 308273f53c4aSTejun Heo */ 308373f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 308473f53c4aSTejun Heo 308573f53c4aSTejun Heo if (next_color != wq->flush_color) { 308673f53c4aSTejun Heo /* 308773f53c4aSTejun Heo * Color space is not full. The current work_color 308873f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 308973f53c4aSTejun Heo * by one. 309073f53c4aSTejun Heo */ 30916183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 309273f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 309373f53c4aSTejun Heo wq->work_color = next_color; 309473f53c4aSTejun Heo 309573f53c4aSTejun Heo if (!wq->first_flusher) { 309673f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 30976183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 309873f53c4aSTejun Heo 309973f53c4aSTejun Heo wq->first_flusher = &this_flusher; 310073f53c4aSTejun Heo 3101112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 310273f53c4aSTejun Heo wq->work_color)) { 310373f53c4aSTejun Heo /* nothing to flush, done */ 310473f53c4aSTejun Heo wq->flush_color = next_color; 310573f53c4aSTejun Heo wq->first_flusher = NULL; 310673f53c4aSTejun Heo goto out_unlock; 310773f53c4aSTejun Heo } 310873f53c4aSTejun Heo } else { 310973f53c4aSTejun Heo /* wait in queue */ 31106183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 311173f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 3112112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 311373f53c4aSTejun Heo } 311473f53c4aSTejun Heo } else { 311573f53c4aSTejun Heo /* 311673f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 311773f53c4aSTejun Heo * The next flush completion will assign us 311873f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 311973f53c4aSTejun Heo */ 312073f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 312173f53c4aSTejun Heo } 312273f53c4aSTejun Heo 3123fca839c0STejun Heo check_flush_dependency(wq, NULL); 3124fca839c0STejun Heo 31253c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 312673f53c4aSTejun Heo 312773f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 312873f53c4aSTejun Heo 312973f53c4aSTejun Heo /* 313073f53c4aSTejun Heo * Wake-up-and-cascade phase 313173f53c4aSTejun Heo * 313273f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 313373f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 313473f53c4aSTejun Heo */ 313500d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher) 313673f53c4aSTejun Heo return; 313773f53c4aSTejun Heo 31383c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 313973f53c4aSTejun Heo 31404ce48b37STejun Heo /* we might have raced, check again with mutex held */ 31414ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 31424ce48b37STejun Heo goto out_unlock; 31434ce48b37STejun Heo 314400d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL); 314573f53c4aSTejun Heo 31466183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 31476183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 314873f53c4aSTejun Heo 314973f53c4aSTejun Heo while (true) { 315073f53c4aSTejun Heo struct wq_flusher *next, *tmp; 315173f53c4aSTejun Heo 315273f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 315373f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 315473f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 315573f53c4aSTejun Heo break; 315673f53c4aSTejun Heo list_del_init(&next->list); 315773f53c4aSTejun Heo complete(&next->done); 315873f53c4aSTejun Heo } 315973f53c4aSTejun Heo 31606183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 316173f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 316273f53c4aSTejun Heo 316373f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 316473f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 316573f53c4aSTejun Heo 316673f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 316773f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 316873f53c4aSTejun Heo /* 316973f53c4aSTejun Heo * Assign the same color to all overflowed 317073f53c4aSTejun Heo * flushers, advance work_color and append to 317173f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 317273f53c4aSTejun Heo * phase for these overflowed flushers. 317373f53c4aSTejun Heo */ 317473f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 317573f53c4aSTejun Heo tmp->flush_color = wq->work_color; 317673f53c4aSTejun Heo 317773f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 317873f53c4aSTejun Heo 317973f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 318073f53c4aSTejun Heo &wq->flusher_queue); 3181112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 318273f53c4aSTejun Heo } 318373f53c4aSTejun Heo 318473f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 31856183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 318673f53c4aSTejun Heo break; 318773f53c4aSTejun Heo } 318873f53c4aSTejun Heo 318973f53c4aSTejun Heo /* 319073f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 3191112202d9STejun Heo * the new first flusher and arm pwqs. 319273f53c4aSTejun Heo */ 31936183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 31946183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 319573f53c4aSTejun Heo 319673f53c4aSTejun Heo list_del_init(&next->list); 319773f53c4aSTejun Heo wq->first_flusher = next; 319873f53c4aSTejun Heo 3199112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 320073f53c4aSTejun Heo break; 320173f53c4aSTejun Heo 320273f53c4aSTejun Heo /* 320373f53c4aSTejun Heo * Meh... this color is already done, clear first 320473f53c4aSTejun Heo * flusher and repeat cascading. 320573f53c4aSTejun Heo */ 320673f53c4aSTejun Heo wq->first_flusher = NULL; 320773f53c4aSTejun Heo } 320873f53c4aSTejun Heo 320973f53c4aSTejun Heo out_unlock: 32103c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 32111da177e4SLinus Torvalds } 3212c4f135d6STetsuo Handa EXPORT_SYMBOL(__flush_workqueue); 32131da177e4SLinus Torvalds 32149c5a2ba7STejun Heo /** 32159c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 32169c5a2ba7STejun Heo * @wq: workqueue to drain 32179c5a2ba7STejun Heo * 32189c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 32199c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 32209c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 3221b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 32229c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 32239c5a2ba7STejun Heo * takes too long. 32249c5a2ba7STejun Heo */ 32259c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 32269c5a2ba7STejun Heo { 32279c5a2ba7STejun Heo unsigned int flush_cnt = 0; 322849e3cf44STejun Heo struct pool_workqueue *pwq; 32299c5a2ba7STejun Heo 32309c5a2ba7STejun Heo /* 32319c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 32329c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 3233618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 32349c5a2ba7STejun Heo */ 323587fc741eSLai Jiangshan mutex_lock(&wq->mutex); 32369c5a2ba7STejun Heo if (!wq->nr_drainers++) 3237618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 323887fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 32399c5a2ba7STejun Heo reflush: 3240c4f135d6STetsuo Handa __flush_workqueue(wq); 32419c5a2ba7STejun Heo 3242b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 324376af4d93STejun Heo 324449e3cf44STejun Heo for_each_pwq(pwq, wq) { 3245fa2563e4SThomas Tuttle bool drained; 32469c5a2ba7STejun Heo 3247a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 3248f97a4a1aSLai Jiangshan drained = !pwq->nr_active && list_empty(&pwq->inactive_works); 3249a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 3250fa2563e4SThomas Tuttle 3251fa2563e4SThomas Tuttle if (drained) 32529c5a2ba7STejun Heo continue; 32539c5a2ba7STejun Heo 32549c5a2ba7STejun Heo if (++flush_cnt == 10 || 32559c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 3256e9ad2eb3SStephen Zhang pr_warn("workqueue %s: %s() isn't complete after %u tries\n", 3257e9ad2eb3SStephen Zhang wq->name, __func__, flush_cnt); 325876af4d93STejun Heo 3259b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 32609c5a2ba7STejun Heo goto reflush; 32619c5a2ba7STejun Heo } 32629c5a2ba7STejun Heo 32639c5a2ba7STejun Heo if (!--wq->nr_drainers) 3264618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 326587fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 32669c5a2ba7STejun Heo } 32679c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 32689c5a2ba7STejun Heo 3269d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 3270d6e89786SJohannes Berg bool from_cancel) 3271baf59022STejun Heo { 3272baf59022STejun Heo struct worker *worker = NULL; 3273c9e7cf27STejun Heo struct worker_pool *pool; 3274112202d9STejun Heo struct pool_workqueue *pwq; 3275baf59022STejun Heo 3276baf59022STejun Heo might_sleep(); 3277baf59022STejun Heo 327824acfb71SThomas Gleixner rcu_read_lock(); 3279fa1b54e6STejun Heo pool = get_work_pool(work); 3280fa1b54e6STejun Heo if (!pool) { 328124acfb71SThomas Gleixner rcu_read_unlock(); 3282fa1b54e6STejun Heo return false; 3283fa1b54e6STejun Heo } 3284fa1b54e6STejun Heo 3285a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 32860b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 3287112202d9STejun Heo pwq = get_work_pwq(work); 3288112202d9STejun Heo if (pwq) { 3289112202d9STejun Heo if (unlikely(pwq->pool != pool)) 3290baf59022STejun Heo goto already_gone; 3291606a5020STejun Heo } else { 3292c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 3293baf59022STejun Heo if (!worker) 3294baf59022STejun Heo goto already_gone; 3295112202d9STejun Heo pwq = worker->current_pwq; 3296606a5020STejun Heo } 3297baf59022STejun Heo 3298fca839c0STejun Heo check_flush_dependency(pwq->wq, work); 3299fca839c0STejun Heo 3300112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 3301a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3302baf59022STejun Heo 3303e159489bSTejun Heo /* 3304a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 3305a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 3306a1d14934SPeter Zijlstra * 3307a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 3308a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 3309a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 3310a1d14934SPeter Zijlstra * forward progress. 3311e159489bSTejun Heo */ 3312d6e89786SJohannes Berg if (!from_cancel && 3313d6e89786SJohannes Berg (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)) { 3314112202d9STejun Heo lock_map_acquire(&pwq->wq->lockdep_map); 3315112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 3316a1d14934SPeter Zijlstra } 331724acfb71SThomas Gleixner rcu_read_unlock(); 3318baf59022STejun Heo return true; 3319baf59022STejun Heo already_gone: 3320a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 332124acfb71SThomas Gleixner rcu_read_unlock(); 3322baf59022STejun Heo return false; 3323baf59022STejun Heo } 3324baf59022STejun Heo 3325d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 3326d6e89786SJohannes Berg { 3327d6e89786SJohannes Berg struct wq_barrier barr; 3328d6e89786SJohannes Berg 3329d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 3330d6e89786SJohannes Berg return false; 3331d6e89786SJohannes Berg 33324d43d395STetsuo Handa if (WARN_ON(!work->func)) 33334d43d395STetsuo Handa return false; 33344d43d395STetsuo Handa 333587915adcSJohannes Berg lock_map_acquire(&work->lockdep_map); 333687915adcSJohannes Berg lock_map_release(&work->lockdep_map); 333787915adcSJohannes Berg 3338d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 3339d6e89786SJohannes Berg wait_for_completion(&barr.done); 3340d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 3341d6e89786SJohannes Berg return true; 3342d6e89786SJohannes Berg } else { 3343d6e89786SJohannes Berg return false; 3344d6e89786SJohannes Berg } 3345d6e89786SJohannes Berg } 3346d6e89786SJohannes Berg 3347db700897SOleg Nesterov /** 3348401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 3349401a8d04STejun Heo * @work: the work to flush 3350db700897SOleg Nesterov * 3351606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 3352606a5020STejun Heo * on return if it hasn't been requeued since flush started. 3353401a8d04STejun Heo * 3354d185af30SYacine Belkadi * Return: 3355401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3356401a8d04STejun Heo * %false if it was already idle. 3357db700897SOleg Nesterov */ 3358401a8d04STejun Heo bool flush_work(struct work_struct *work) 3359db700897SOleg Nesterov { 3360d6e89786SJohannes Berg return __flush_work(work, false); 3361606a5020STejun Heo } 3362db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 3363db700897SOleg Nesterov 33648603e1b3STejun Heo struct cwt_wait { 3365ac6424b9SIngo Molnar wait_queue_entry_t wait; 33668603e1b3STejun Heo struct work_struct *work; 33678603e1b3STejun Heo }; 33688603e1b3STejun Heo 3369ac6424b9SIngo Molnar static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 33708603e1b3STejun Heo { 33718603e1b3STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 33728603e1b3STejun Heo 33738603e1b3STejun Heo if (cwait->work != key) 33748603e1b3STejun Heo return 0; 33758603e1b3STejun Heo return autoremove_wake_function(wait, mode, sync, key); 33768603e1b3STejun Heo } 33778603e1b3STejun Heo 337836e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 3379401a8d04STejun Heo { 33808603e1b3STejun Heo static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); 3381bbb68dfaSTejun Heo unsigned long flags; 33821f1f642eSOleg Nesterov int ret; 33831f1f642eSOleg Nesterov 33841f1f642eSOleg Nesterov do { 3385bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 3386bbb68dfaSTejun Heo /* 33878603e1b3STejun Heo * If someone else is already canceling, wait for it to 33888603e1b3STejun Heo * finish. flush_work() doesn't work for PREEMPT_NONE 33898603e1b3STejun Heo * because we may get scheduled between @work's completion 33908603e1b3STejun Heo * and the other canceling task resuming and clearing 33918603e1b3STejun Heo * CANCELING - flush_work() will return false immediately 33928603e1b3STejun Heo * as @work is no longer busy, try_to_grab_pending() will 33938603e1b3STejun Heo * return -ENOENT as @work is still being canceled and the 33948603e1b3STejun Heo * other canceling task won't be able to clear CANCELING as 33958603e1b3STejun Heo * we're hogging the CPU. 33968603e1b3STejun Heo * 33978603e1b3STejun Heo * Let's wait for completion using a waitqueue. As this 33988603e1b3STejun Heo * may lead to the thundering herd problem, use a custom 33998603e1b3STejun Heo * wake function which matches @work along with exclusive 34008603e1b3STejun Heo * wait and wakeup. 3401bbb68dfaSTejun Heo */ 34028603e1b3STejun Heo if (unlikely(ret == -ENOENT)) { 34038603e1b3STejun Heo struct cwt_wait cwait; 34048603e1b3STejun Heo 34058603e1b3STejun Heo init_wait(&cwait.wait); 34068603e1b3STejun Heo cwait.wait.func = cwt_wakefn; 34078603e1b3STejun Heo cwait.work = work; 34088603e1b3STejun Heo 34098603e1b3STejun Heo prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, 34108603e1b3STejun Heo TASK_UNINTERRUPTIBLE); 34118603e1b3STejun Heo if (work_is_canceling(work)) 34128603e1b3STejun Heo schedule(); 34138603e1b3STejun Heo finish_wait(&cancel_waitq, &cwait.wait); 34148603e1b3STejun Heo } 34151f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 34161f1f642eSOleg Nesterov 3417bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 3418bbb68dfaSTejun Heo mark_work_canceling(work); 3419bbb68dfaSTejun Heo local_irq_restore(flags); 3420bbb68dfaSTejun Heo 34213347fa09STejun Heo /* 34223347fa09STejun Heo * This allows canceling during early boot. We know that @work 34233347fa09STejun Heo * isn't executing. 34243347fa09STejun Heo */ 34253347fa09STejun Heo if (wq_online) 3426d6e89786SJohannes Berg __flush_work(work, true); 34273347fa09STejun Heo 34287a22ad75STejun Heo clear_work_data(work); 34298603e1b3STejun Heo 34308603e1b3STejun Heo /* 34318603e1b3STejun Heo * Paired with prepare_to_wait() above so that either 34328603e1b3STejun Heo * waitqueue_active() is visible here or !work_is_canceling() is 34338603e1b3STejun Heo * visible there. 34348603e1b3STejun Heo */ 34358603e1b3STejun Heo smp_mb(); 34368603e1b3STejun Heo if (waitqueue_active(&cancel_waitq)) 34378603e1b3STejun Heo __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); 34388603e1b3STejun Heo 34391f1f642eSOleg Nesterov return ret; 34401f1f642eSOleg Nesterov } 34411f1f642eSOleg Nesterov 34426e84d644SOleg Nesterov /** 3443401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 3444401a8d04STejun Heo * @work: the work to cancel 34456e84d644SOleg Nesterov * 3446401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 3447401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 3448401a8d04STejun Heo * another workqueue. On return from this function, @work is 3449401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 34501f1f642eSOleg Nesterov * 3451401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 3452401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 34536e84d644SOleg Nesterov * 3454401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 34556e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 3456401a8d04STejun Heo * 3457d185af30SYacine Belkadi * Return: 3458401a8d04STejun Heo * %true if @work was pending, %false otherwise. 34596e84d644SOleg Nesterov */ 3460401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 34616e84d644SOleg Nesterov { 346236e227d2STejun Heo return __cancel_work_timer(work, false); 3463b89deed3SOleg Nesterov } 346428e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 3465b89deed3SOleg Nesterov 34666e84d644SOleg Nesterov /** 3467401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 3468401a8d04STejun Heo * @dwork: the delayed work to flush 34696e84d644SOleg Nesterov * 3470401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 3471401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 3472401a8d04STejun Heo * considers the last queueing instance of @dwork. 34731f1f642eSOleg Nesterov * 3474d185af30SYacine Belkadi * Return: 3475401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3476401a8d04STejun Heo * %false if it was already idle. 34776e84d644SOleg Nesterov */ 3478401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 3479401a8d04STejun Heo { 34808930cabaSTejun Heo local_irq_disable(); 3481401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 348260c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 34838930cabaSTejun Heo local_irq_enable(); 3484401a8d04STejun Heo return flush_work(&dwork->work); 3485401a8d04STejun Heo } 3486401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 3487401a8d04STejun Heo 348805f0fe6bSTejun Heo /** 348905f0fe6bSTejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 349005f0fe6bSTejun Heo * @rwork: the rcu work to flush 349105f0fe6bSTejun Heo * 349205f0fe6bSTejun Heo * Return: 349305f0fe6bSTejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 349405f0fe6bSTejun Heo * %false if it was already idle. 349505f0fe6bSTejun Heo */ 349605f0fe6bSTejun Heo bool flush_rcu_work(struct rcu_work *rwork) 349705f0fe6bSTejun Heo { 349805f0fe6bSTejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 349905f0fe6bSTejun Heo rcu_barrier(); 350005f0fe6bSTejun Heo flush_work(&rwork->work); 350105f0fe6bSTejun Heo return true; 350205f0fe6bSTejun Heo } else { 350305f0fe6bSTejun Heo return flush_work(&rwork->work); 350405f0fe6bSTejun Heo } 350505f0fe6bSTejun Heo } 350605f0fe6bSTejun Heo EXPORT_SYMBOL(flush_rcu_work); 350705f0fe6bSTejun Heo 3508f72b8792SJens Axboe static bool __cancel_work(struct work_struct *work, bool is_dwork) 3509f72b8792SJens Axboe { 3510f72b8792SJens Axboe unsigned long flags; 3511f72b8792SJens Axboe int ret; 3512f72b8792SJens Axboe 3513f72b8792SJens Axboe do { 3514f72b8792SJens Axboe ret = try_to_grab_pending(work, is_dwork, &flags); 3515f72b8792SJens Axboe } while (unlikely(ret == -EAGAIN)); 3516f72b8792SJens Axboe 3517f72b8792SJens Axboe if (unlikely(ret < 0)) 3518f72b8792SJens Axboe return false; 3519f72b8792SJens Axboe 3520f72b8792SJens Axboe set_work_pool_and_clear_pending(work, get_work_pool_id(work)); 3521f72b8792SJens Axboe local_irq_restore(flags); 3522f72b8792SJens Axboe return ret; 3523f72b8792SJens Axboe } 3524f72b8792SJens Axboe 352573b4b532SAndrey Grodzovsky /* 352673b4b532SAndrey Grodzovsky * See cancel_delayed_work() 352773b4b532SAndrey Grodzovsky */ 352873b4b532SAndrey Grodzovsky bool cancel_work(struct work_struct *work) 352973b4b532SAndrey Grodzovsky { 353073b4b532SAndrey Grodzovsky return __cancel_work(work, false); 353173b4b532SAndrey Grodzovsky } 353273b4b532SAndrey Grodzovsky EXPORT_SYMBOL(cancel_work); 353373b4b532SAndrey Grodzovsky 3534401a8d04STejun Heo /** 353557b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 353657b30ae7STejun Heo * @dwork: delayed_work to cancel 353709383498STejun Heo * 3538d185af30SYacine Belkadi * Kill off a pending delayed_work. 3539d185af30SYacine Belkadi * 3540d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 3541d185af30SYacine Belkadi * pending. 3542d185af30SYacine Belkadi * 3543d185af30SYacine Belkadi * Note: 3544d185af30SYacine Belkadi * The work callback function may still be running on return, unless 3545d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 3546d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 354709383498STejun Heo * 354857b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 354909383498STejun Heo */ 355057b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 355109383498STejun Heo { 3552f72b8792SJens Axboe return __cancel_work(&dwork->work, true); 355309383498STejun Heo } 355457b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 355509383498STejun Heo 355609383498STejun Heo /** 3557401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 3558401a8d04STejun Heo * @dwork: the delayed work cancel 3559401a8d04STejun Heo * 3560401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 3561401a8d04STejun Heo * 3562d185af30SYacine Belkadi * Return: 3563401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 3564401a8d04STejun Heo */ 3565401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 35666e84d644SOleg Nesterov { 356736e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 35686e84d644SOleg Nesterov } 3569f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 35701da177e4SLinus Torvalds 35710fcb78c2SRolf Eike Beer /** 357231ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 3573b6136773SAndrew Morton * @func: the function to call 3574b6136773SAndrew Morton * 357531ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 357631ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 3577b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 357831ddd871STejun Heo * 3579d185af30SYacine Belkadi * Return: 358031ddd871STejun Heo * 0 on success, -errno on failure. 3581b6136773SAndrew Morton */ 358265f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 358315316ba8SChristoph Lameter { 358415316ba8SChristoph Lameter int cpu; 358538f51568SNamhyung Kim struct work_struct __percpu *works; 358615316ba8SChristoph Lameter 3587b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 3588b6136773SAndrew Morton if (!works) 358915316ba8SChristoph Lameter return -ENOMEM; 3590b6136773SAndrew Morton 3591ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 359293981800STejun Heo 359315316ba8SChristoph Lameter for_each_online_cpu(cpu) { 35949bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 35959bfb1839SIngo Molnar 35969bfb1839SIngo Molnar INIT_WORK(work, func); 35978de6d308SOleg Nesterov schedule_work_on(cpu, work); 359815316ba8SChristoph Lameter } 359993981800STejun Heo 360093981800STejun Heo for_each_online_cpu(cpu) 36018616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 360293981800STejun Heo 3603ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 3604b6136773SAndrew Morton free_percpu(works); 360515316ba8SChristoph Lameter return 0; 360615316ba8SChristoph Lameter } 360715316ba8SChristoph Lameter 3608eef6a7d5SAlan Stern /** 36091fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 36101fa44ecaSJames Bottomley * @fn: the function to execute 36111fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 36121fa44ecaSJames Bottomley * be available when the work executes) 36131fa44ecaSJames Bottomley * 36141fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 36151fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 36161fa44ecaSJames Bottomley * 3617d185af30SYacine Belkadi * Return: 0 - function was executed 36181fa44ecaSJames Bottomley * 1 - function was scheduled for execution 36191fa44ecaSJames Bottomley */ 362065f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 36211fa44ecaSJames Bottomley { 36221fa44ecaSJames Bottomley if (!in_interrupt()) { 362365f27f38SDavid Howells fn(&ew->work); 36241fa44ecaSJames Bottomley return 0; 36251fa44ecaSJames Bottomley } 36261fa44ecaSJames Bottomley 362765f27f38SDavid Howells INIT_WORK(&ew->work, fn); 36281fa44ecaSJames Bottomley schedule_work(&ew->work); 36291fa44ecaSJames Bottomley 36301fa44ecaSJames Bottomley return 1; 36311fa44ecaSJames Bottomley } 36321fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 36331fa44ecaSJames Bottomley 36347a4e344cSTejun Heo /** 36357a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 36367a4e344cSTejun Heo * @attrs: workqueue_attrs to free 36377a4e344cSTejun Heo * 36387a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 36397a4e344cSTejun Heo */ 3640513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs) 36417a4e344cSTejun Heo { 36427a4e344cSTejun Heo if (attrs) { 36437a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 36447a4e344cSTejun Heo kfree(attrs); 36457a4e344cSTejun Heo } 36467a4e344cSTejun Heo } 36477a4e344cSTejun Heo 36487a4e344cSTejun Heo /** 36497a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 36507a4e344cSTejun Heo * 36517a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 3652d185af30SYacine Belkadi * return it. 3653d185af30SYacine Belkadi * 3654d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 36557a4e344cSTejun Heo */ 3656513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void) 36577a4e344cSTejun Heo { 36587a4e344cSTejun Heo struct workqueue_attrs *attrs; 36597a4e344cSTejun Heo 3660be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 36617a4e344cSTejun Heo if (!attrs) 36627a4e344cSTejun Heo goto fail; 3663be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) 36647a4e344cSTejun Heo goto fail; 36657a4e344cSTejun Heo 366613e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 366784193c07STejun Heo attrs->affn_scope = WQ_AFFN_DFL; 36687a4e344cSTejun Heo return attrs; 36697a4e344cSTejun Heo fail: 36707a4e344cSTejun Heo free_workqueue_attrs(attrs); 36717a4e344cSTejun Heo return NULL; 36727a4e344cSTejun Heo } 36737a4e344cSTejun Heo 367429c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 367529c91e99STejun Heo const struct workqueue_attrs *from) 367629c91e99STejun Heo { 367729c91e99STejun Heo to->nice = from->nice; 367829c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 367984193c07STejun Heo 36802865a8fbSShaohua Li /* 368184193c07STejun Heo * Unlike hash and equality test, copying shouldn't ignore wq-only 368284193c07STejun Heo * fields as copying is used for both pool and wq attrs. Instead, 368384193c07STejun Heo * get_unbound_pool() explicitly clears the fields. 36842865a8fbSShaohua Li */ 368584193c07STejun Heo to->affn_scope = from->affn_scope; 3686af73f5c9STejun Heo to->ordered = from->ordered; 368729c91e99STejun Heo } 368829c91e99STejun Heo 36895de7a03cSTejun Heo /* 36905de7a03cSTejun Heo * Some attrs fields are workqueue-only. Clear them for worker_pool's. See the 36915de7a03cSTejun Heo * comments in 'struct workqueue_attrs' definition. 36925de7a03cSTejun Heo */ 36935de7a03cSTejun Heo static void wqattrs_clear_for_pool(struct workqueue_attrs *attrs) 36945de7a03cSTejun Heo { 369584193c07STejun Heo attrs->affn_scope = WQ_AFFN_NR_TYPES; 36965de7a03cSTejun Heo attrs->ordered = false; 36975de7a03cSTejun Heo } 36985de7a03cSTejun Heo 369929c91e99STejun Heo /* hash value of the content of @attr */ 370029c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 370129c91e99STejun Heo { 370229c91e99STejun Heo u32 hash = 0; 370329c91e99STejun Heo 370429c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 370513e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 370613e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 370729c91e99STejun Heo return hash; 370829c91e99STejun Heo } 370929c91e99STejun Heo 371029c91e99STejun Heo /* content equality test */ 371129c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 371229c91e99STejun Heo const struct workqueue_attrs *b) 371329c91e99STejun Heo { 371429c91e99STejun Heo if (a->nice != b->nice) 371529c91e99STejun Heo return false; 371629c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 371729c91e99STejun Heo return false; 371829c91e99STejun Heo return true; 371929c91e99STejun Heo } 372029c91e99STejun Heo 37210f36ee24STejun Heo /* Update @attrs with actually available CPUs */ 37220f36ee24STejun Heo static void wqattrs_actualize_cpumask(struct workqueue_attrs *attrs, 37230f36ee24STejun Heo const cpumask_t *unbound_cpumask) 37240f36ee24STejun Heo { 37250f36ee24STejun Heo /* 37260f36ee24STejun Heo * Calculate the effective CPU mask of @attrs given @unbound_cpumask. If 37270f36ee24STejun Heo * @attrs->cpumask doesn't overlap with @unbound_cpumask, we fallback to 37280f36ee24STejun Heo * @unbound_cpumask. 37290f36ee24STejun Heo */ 37300f36ee24STejun Heo cpumask_and(attrs->cpumask, attrs->cpumask, unbound_cpumask); 37310f36ee24STejun Heo if (unlikely(cpumask_empty(attrs->cpumask))) 37320f36ee24STejun Heo cpumask_copy(attrs->cpumask, unbound_cpumask); 37330f36ee24STejun Heo } 37340f36ee24STejun Heo 373584193c07STejun Heo /* find wq_pod_type to use for @attrs */ 373684193c07STejun Heo static const struct wq_pod_type * 373784193c07STejun Heo wqattrs_pod_type(const struct workqueue_attrs *attrs) 373884193c07STejun Heo { 373984193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[attrs->affn_scope]; 374084193c07STejun Heo 374184193c07STejun Heo if (!WARN_ON_ONCE(attrs->affn_scope == WQ_AFFN_NR_TYPES) && 374284193c07STejun Heo likely(pt->nr_pods)) 374384193c07STejun Heo return pt; 374484193c07STejun Heo 374584193c07STejun Heo /* 374684193c07STejun Heo * Before workqueue_init_topology(), only SYSTEM is available which is 374784193c07STejun Heo * initialized in workqueue_init_early(). 374884193c07STejun Heo */ 374984193c07STejun Heo pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 375084193c07STejun Heo BUG_ON(!pt->nr_pods); 375184193c07STejun Heo return pt; 375284193c07STejun Heo } 375384193c07STejun Heo 37547a4e344cSTejun Heo /** 37557a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 37567a4e344cSTejun Heo * @pool: worker_pool to initialize 37577a4e344cSTejun Heo * 3758402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 3759d185af30SYacine Belkadi * 3760d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 376129c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 376229c91e99STejun Heo * on @pool safely to release it. 37637a4e344cSTejun Heo */ 37647a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 37654e1a1f9aSTejun Heo { 3766a9b8a985SSebastian Andrzej Siewior raw_spin_lock_init(&pool->lock); 376729c91e99STejun Heo pool->id = -1; 376829c91e99STejun Heo pool->cpu = -1; 3769f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 37704e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 377182607adcSTejun Heo pool->watchdog_ts = jiffies; 37724e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 37734e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 37744e1a1f9aSTejun Heo hash_init(pool->busy_hash); 37754e1a1f9aSTejun Heo 377632a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 37773f959aa3SValentin Schneider INIT_WORK(&pool->idle_cull_work, idle_cull_fn); 37784e1a1f9aSTejun Heo 377932a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 37804e1a1f9aSTejun Heo 3781da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 3782e02b9312SValentin Schneider INIT_LIST_HEAD(&pool->dying_workers); 37837a4e344cSTejun Heo 37847cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 378529c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 378629c91e99STejun Heo pool->refcnt = 1; 378729c91e99STejun Heo 378829c91e99STejun Heo /* shouldn't fail above this point */ 3789be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs(); 37907a4e344cSTejun Heo if (!pool->attrs) 37917a4e344cSTejun Heo return -ENOMEM; 37925de7a03cSTejun Heo 37935de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 37945de7a03cSTejun Heo 37957a4e344cSTejun Heo return 0; 37964e1a1f9aSTejun Heo } 37974e1a1f9aSTejun Heo 3798669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP 3799669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 3800669de8bdSBart Van Assche { 3801669de8bdSBart Van Assche char *lock_name; 3802669de8bdSBart Van Assche 3803669de8bdSBart Van Assche lockdep_register_key(&wq->key); 3804669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); 3805669de8bdSBart Van Assche if (!lock_name) 3806669de8bdSBart Van Assche lock_name = wq->name; 380769a106c0SQian Cai 380869a106c0SQian Cai wq->lock_name = lock_name; 3809669de8bdSBart Van Assche lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); 3810669de8bdSBart Van Assche } 3811669de8bdSBart Van Assche 3812669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 3813669de8bdSBart Van Assche { 3814669de8bdSBart Van Assche lockdep_unregister_key(&wq->key); 3815669de8bdSBart Van Assche } 3816669de8bdSBart Van Assche 3817669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 3818669de8bdSBart Van Assche { 3819669de8bdSBart Van Assche if (wq->lock_name != wq->name) 3820669de8bdSBart Van Assche kfree(wq->lock_name); 3821669de8bdSBart Van Assche } 3822669de8bdSBart Van Assche #else 3823669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 3824669de8bdSBart Van Assche { 3825669de8bdSBart Van Assche } 3826669de8bdSBart Van Assche 3827669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 3828669de8bdSBart Van Assche { 3829669de8bdSBart Van Assche } 3830669de8bdSBart Van Assche 3831669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 3832669de8bdSBart Van Assche { 3833669de8bdSBart Van Assche } 3834669de8bdSBart Van Assche #endif 3835669de8bdSBart Van Assche 3836e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 3837e2dca7adSTejun Heo { 3838e2dca7adSTejun Heo struct workqueue_struct *wq = 3839e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 3840e2dca7adSTejun Heo 3841669de8bdSBart Van Assche wq_free_lockdep(wq); 3842ee1ceef7STejun Heo free_percpu(wq->cpu_pwq); 3843e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 3844e2dca7adSTejun Heo kfree(wq); 3845e2dca7adSTejun Heo } 3846e2dca7adSTejun Heo 384729c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 384829c91e99STejun Heo { 384929c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 385029c91e99STejun Heo 38517cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 385229c91e99STejun Heo free_workqueue_attrs(pool->attrs); 385329c91e99STejun Heo kfree(pool); 385429c91e99STejun Heo } 385529c91e99STejun Heo 385629c91e99STejun Heo /** 385729c91e99STejun Heo * put_unbound_pool - put a worker_pool 385829c91e99STejun Heo * @pool: worker_pool to put 385929c91e99STejun Heo * 386024acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU 3861c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 3862c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 3863c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 3864a892caccSTejun Heo * 3865a892caccSTejun Heo * Should be called with wq_pool_mutex held. 386629c91e99STejun Heo */ 386729c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 386829c91e99STejun Heo { 386960f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 387029c91e99STejun Heo struct worker *worker; 38719680540cSYang Yingliang LIST_HEAD(cull_list); 3872e02b9312SValentin Schneider 3873a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3874a892caccSTejun Heo 3875a892caccSTejun Heo if (--pool->refcnt) 387629c91e99STejun Heo return; 387729c91e99STejun Heo 387829c91e99STejun Heo /* sanity checks */ 387961d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 3880a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 388129c91e99STejun Heo return; 388229c91e99STejun Heo 388329c91e99STejun Heo /* release id and unhash */ 388429c91e99STejun Heo if (pool->id >= 0) 388529c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 388629c91e99STejun Heo hash_del(&pool->hash_node); 388729c91e99STejun Heo 3888c5aa87bbSTejun Heo /* 3889692b4825STejun Heo * Become the manager and destroy all workers. This prevents 3890692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 3891692b4825STejun Heo * manager and @pool gets freed with the flag set. 38929ab03be4SValentin Schneider * 38939ab03be4SValentin Schneider * Having a concurrent manager is quite unlikely to happen as we can 38949ab03be4SValentin Schneider * only get here with 38959ab03be4SValentin Schneider * pwq->refcnt == pool->refcnt == 0 38969ab03be4SValentin Schneider * which implies no work queued to the pool, which implies no worker can 38979ab03be4SValentin Schneider * become the manager. However a worker could have taken the role of 38989ab03be4SValentin Schneider * manager before the refcnts dropped to 0, since maybe_create_worker() 38999ab03be4SValentin Schneider * drops pool->lock 3900c5aa87bbSTejun Heo */ 39019ab03be4SValentin Schneider while (true) { 39029ab03be4SValentin Schneider rcuwait_wait_event(&manager_wait, 39039ab03be4SValentin Schneider !(pool->flags & POOL_MANAGER_ACTIVE), 3904d8bb65abSSebastian Andrzej Siewior TASK_UNINTERRUPTIBLE); 3905e02b9312SValentin Schneider 3906e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 39079ab03be4SValentin Schneider raw_spin_lock_irq(&pool->lock); 39089ab03be4SValentin Schneider if (!(pool->flags & POOL_MANAGER_ACTIVE)) { 3909692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 39109ab03be4SValentin Schneider break; 39119ab03be4SValentin Schneider } 39129ab03be4SValentin Schneider raw_spin_unlock_irq(&pool->lock); 3913e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 39149ab03be4SValentin Schneider } 3915692b4825STejun Heo 39161037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 3917e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 391829c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 3919a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 392060f5a4bcSLai Jiangshan 3921e02b9312SValentin Schneider wake_dying_workers(&cull_list); 3922e02b9312SValentin Schneider 3923e02b9312SValentin Schneider if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) 392460f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 39251258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 392660f5a4bcSLai Jiangshan 392760f5a4bcSLai Jiangshan if (pool->detach_completion) 392860f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 392960f5a4bcSLai Jiangshan 393029c91e99STejun Heo /* shut down the timers */ 393129c91e99STejun Heo del_timer_sync(&pool->idle_timer); 39323f959aa3SValentin Schneider cancel_work_sync(&pool->idle_cull_work); 393329c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 393429c91e99STejun Heo 393524acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */ 393625b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 393729c91e99STejun Heo } 393829c91e99STejun Heo 393929c91e99STejun Heo /** 394029c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 394129c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 394229c91e99STejun Heo * 394329c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 394429c91e99STejun Heo * reference count and return it. If there already is a matching 394529c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 3946d185af30SYacine Belkadi * create a new one. 3947a892caccSTejun Heo * 3948a892caccSTejun Heo * Should be called with wq_pool_mutex held. 3949d185af30SYacine Belkadi * 3950d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 3951d185af30SYacine Belkadi * On failure, %NULL. 395229c91e99STejun Heo */ 395329c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 395429c91e99STejun Heo { 395584193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_NUMA]; 395629c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 395729c91e99STejun Heo struct worker_pool *pool; 395884193c07STejun Heo int pod, node = NUMA_NO_NODE; 395929c91e99STejun Heo 3960a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 396129c91e99STejun Heo 396229c91e99STejun Heo /* do we already have a matching pool? */ 396329c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 396429c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 396529c91e99STejun Heo pool->refcnt++; 39663fb1823cSLai Jiangshan return pool; 396729c91e99STejun Heo } 396829c91e99STejun Heo } 396929c91e99STejun Heo 397084193c07STejun Heo /* If cpumask is contained inside a NUMA pod, that's our NUMA node */ 397184193c07STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) { 397284193c07STejun Heo if (cpumask_subset(attrs->cpumask, pt->pod_cpus[pod])) { 397384193c07STejun Heo node = pt->pod_node[pod]; 3974e2273584SXunlei Pang break; 3975e2273584SXunlei Pang } 3976e2273584SXunlei Pang } 3977e2273584SXunlei Pang 397829c91e99STejun Heo /* nope, create a new one */ 397984193c07STejun Heo pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, node); 398029c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 398129c91e99STejun Heo goto fail; 398229c91e99STejun Heo 398384193c07STejun Heo pool->node = node; 39845de7a03cSTejun Heo copy_workqueue_attrs(pool->attrs, attrs); 39855de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 39862865a8fbSShaohua Li 398729c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 398829c91e99STejun Heo goto fail; 398929c91e99STejun Heo 399029c91e99STejun Heo /* create and start the initial worker */ 39913347fa09STejun Heo if (wq_online && !create_worker(pool)) 399229c91e99STejun Heo goto fail; 399329c91e99STejun Heo 399429c91e99STejun Heo /* install */ 399529c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 39963fb1823cSLai Jiangshan 399729c91e99STejun Heo return pool; 399829c91e99STejun Heo fail: 399929c91e99STejun Heo if (pool) 400029c91e99STejun Heo put_unbound_pool(pool); 400129c91e99STejun Heo return NULL; 400229c91e99STejun Heo } 400329c91e99STejun Heo 40048864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 40058864b4e5STejun Heo { 40068864b4e5STejun Heo kmem_cache_free(pwq_cache, 40078864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 40088864b4e5STejun Heo } 40098864b4e5STejun Heo 40108864b4e5STejun Heo /* 4011967b494eSTejun Heo * Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero 4012967b494eSTejun Heo * refcnt and needs to be destroyed. 40138864b4e5STejun Heo */ 4014687a9aa5STejun Heo static void pwq_release_workfn(struct kthread_work *work) 40158864b4e5STejun Heo { 40168864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 4017687a9aa5STejun Heo release_work); 40188864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 40198864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 4020b42b0bddSYang Yingliang bool is_last = false; 40218864b4e5STejun Heo 4022b42b0bddSYang Yingliang /* 4023687a9aa5STejun Heo * When @pwq is not linked, it doesn't hold any reference to the 4024b42b0bddSYang Yingliang * @wq, and @wq is invalid to access. 4025b42b0bddSYang Yingliang */ 4026b42b0bddSYang Yingliang if (!list_empty(&pwq->pwqs_node)) { 40273c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 40288864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 4029bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 40303c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 4031b42b0bddSYang Yingliang } 40328864b4e5STejun Heo 4033687a9aa5STejun Heo if (wq->flags & WQ_UNBOUND) { 4034a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 40358864b4e5STejun Heo put_unbound_pool(pool); 4036a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 4037687a9aa5STejun Heo } 4038a892caccSTejun Heo 403925b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 40408864b4e5STejun Heo 40418864b4e5STejun Heo /* 40428864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 4043e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 40448864b4e5STejun Heo */ 4045669de8bdSBart Van Assche if (is_last) { 4046669de8bdSBart Van Assche wq_unregister_lockdep(wq); 404725b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 40486029a918STejun Heo } 4049669de8bdSBart Van Assche } 40508864b4e5STejun Heo 40510fbd95aaSTejun Heo /** 4052699ce097STejun Heo * pwq_adjust_max_active - update a pwq's max_active to the current setting 40530fbd95aaSTejun Heo * @pwq: target pool_workqueue 40540fbd95aaSTejun Heo * 4055699ce097STejun Heo * If @pwq isn't freezing, set @pwq->max_active to the associated 4056f97a4a1aSLai Jiangshan * workqueue's saved_max_active and activate inactive work items 4057699ce097STejun Heo * accordingly. If @pwq is freezing, clear @pwq->max_active to zero. 40580fbd95aaSTejun Heo */ 4059699ce097STejun Heo static void pwq_adjust_max_active(struct pool_workqueue *pwq) 40600fbd95aaSTejun Heo { 4061699ce097STejun Heo struct workqueue_struct *wq = pwq->wq; 4062699ce097STejun Heo bool freezable = wq->flags & WQ_FREEZABLE; 40633347fa09STejun Heo unsigned long flags; 4064699ce097STejun Heo 4065699ce097STejun Heo /* for @wq->saved_max_active */ 4066a357fc03SLai Jiangshan lockdep_assert_held(&wq->mutex); 4067699ce097STejun Heo 4068699ce097STejun Heo /* fast exit for non-freezable wqs */ 4069699ce097STejun Heo if (!freezable && pwq->max_active == wq->saved_max_active) 4070699ce097STejun Heo return; 4071699ce097STejun Heo 40723347fa09STejun Heo /* this function can be called during early boot w/ irq disabled */ 4073a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pwq->pool->lock, flags); 4074699ce097STejun Heo 407574b414eaSLai Jiangshan /* 407674b414eaSLai Jiangshan * During [un]freezing, the caller is responsible for ensuring that 407774b414eaSLai Jiangshan * this function is called at least once after @workqueue_freezing 407874b414eaSLai Jiangshan * is updated and visible. 407974b414eaSLai Jiangshan */ 408074b414eaSLai Jiangshan if (!freezable || !workqueue_freezing) { 408101341fbdSYunfeng Ye bool kick = false; 408201341fbdSYunfeng Ye 4083699ce097STejun Heo pwq->max_active = wq->saved_max_active; 40840fbd95aaSTejun Heo 4085f97a4a1aSLai Jiangshan while (!list_empty(&pwq->inactive_works) && 408601341fbdSYunfeng Ye pwq->nr_active < pwq->max_active) { 4087f97a4a1aSLai Jiangshan pwq_activate_first_inactive(pwq); 408801341fbdSYunfeng Ye kick = true; 408901341fbdSYunfeng Ye } 4090951a078aSLai Jiangshan 4091951a078aSLai Jiangshan /* 4092951a078aSLai Jiangshan * Need to kick a worker after thawed or an unbound wq's 409301341fbdSYunfeng Ye * max_active is bumped. In realtime scenarios, always kicking a 409401341fbdSYunfeng Ye * worker will cause interference on the isolated cpu cores, so 409501341fbdSYunfeng Ye * let's kick iff work items were activated. 4096951a078aSLai Jiangshan */ 409701341fbdSYunfeng Ye if (kick) 4098951a078aSLai Jiangshan wake_up_worker(pwq->pool); 4099699ce097STejun Heo } else { 4100699ce097STejun Heo pwq->max_active = 0; 4101699ce097STejun Heo } 4102699ce097STejun Heo 4103a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 41040fbd95aaSTejun Heo } 41050fbd95aaSTejun Heo 410667dc8325SCai Huoqing /* initialize newly allocated @pwq which is associated with @wq and @pool */ 4107f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 4108f147f29eSTejun Heo struct worker_pool *pool) 4109d2c1d404STejun Heo { 4110d2c1d404STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 4111d2c1d404STejun Heo 4112e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 4113e50aba9aSTejun Heo 4114d2c1d404STejun Heo pwq->pool = pool; 4115d2c1d404STejun Heo pwq->wq = wq; 4116d2c1d404STejun Heo pwq->flush_color = -1; 41178864b4e5STejun Heo pwq->refcnt = 1; 4118f97a4a1aSLai Jiangshan INIT_LIST_HEAD(&pwq->inactive_works); 41191befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 4120d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 4121687a9aa5STejun Heo kthread_init_work(&pwq->release_work, pwq_release_workfn); 4122f147f29eSTejun Heo } 4123d2c1d404STejun Heo 4124f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 41251befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 4126f147f29eSTejun Heo { 4127f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 4128f147f29eSTejun Heo 4129f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 413075ccf595STejun Heo 41311befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 41321befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 41331befcf30STejun Heo return; 41341befcf30STejun Heo 413529b1cb41SLai Jiangshan /* set the matching work_color */ 413675ccf595STejun Heo pwq->work_color = wq->work_color; 4137983ca25eSTejun Heo 4138983ca25eSTejun Heo /* sync max_active to the current setting */ 4139983ca25eSTejun Heo pwq_adjust_max_active(pwq); 4140983ca25eSTejun Heo 4141983ca25eSTejun Heo /* link in @pwq */ 41429e8cd2f5STejun Heo list_add_rcu(&pwq->pwqs_node, &wq->pwqs); 4143df2d5ae4STejun Heo } 41446029a918STejun Heo 4145f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 4146f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 4147f147f29eSTejun Heo const struct workqueue_attrs *attrs) 4148f147f29eSTejun Heo { 4149f147f29eSTejun Heo struct worker_pool *pool; 4150f147f29eSTejun Heo struct pool_workqueue *pwq; 4151f147f29eSTejun Heo 4152f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4153f147f29eSTejun Heo 4154f147f29eSTejun Heo pool = get_unbound_pool(attrs); 4155f147f29eSTejun Heo if (!pool) 4156f147f29eSTejun Heo return NULL; 4157f147f29eSTejun Heo 4158e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 4159f147f29eSTejun Heo if (!pwq) { 4160f147f29eSTejun Heo put_unbound_pool(pool); 4161f147f29eSTejun Heo return NULL; 4162f147f29eSTejun Heo } 4163f147f29eSTejun Heo 4164f147f29eSTejun Heo init_pwq(pwq, wq, pool); 4165f147f29eSTejun Heo return pwq; 4166d2c1d404STejun Heo } 4167d2c1d404STejun Heo 41684c16bd32STejun Heo /** 4169fef59c9cSTejun Heo * wq_calc_pod_cpumask - calculate a wq_attrs' cpumask for a pod 4170042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 417184193c07STejun Heo * @cpu: the target CPU 41724c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 41734c16bd32STejun Heo * @cpumask: outarg, the resulting cpumask 41744c16bd32STejun Heo * 4175fef59c9cSTejun Heo * Calculate the cpumask a workqueue with @attrs should use on @pod. If 4176fef59c9cSTejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during calculation. 4177fef59c9cSTejun Heo * The result is stored in @cpumask. 41784c16bd32STejun Heo * 4179fef59c9cSTejun Heo * If pod affinity is not enabled, @attrs->cpumask is always used. If enabled 4180fef59c9cSTejun Heo * and @pod has online CPUs requested by @attrs, the returned cpumask is the 4181fef59c9cSTejun Heo * intersection of the possible CPUs of @pod and @attrs->cpumask. 41824c16bd32STejun Heo * 4183fef59c9cSTejun Heo * The caller is responsible for ensuring that the cpumask of @pod stays stable. 41844c16bd32STejun Heo */ 418584193c07STejun Heo static void wq_calc_pod_cpumask(const struct workqueue_attrs *attrs, int cpu, 41864c16bd32STejun Heo int cpu_going_down, cpumask_t *cpumask) 41874c16bd32STejun Heo { 418884193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 418984193c07STejun Heo int pod = pt->cpu_pod[cpu]; 41904c16bd32STejun Heo 4191fef59c9cSTejun Heo /* does @pod have any online CPUs @attrs wants? */ 419284193c07STejun Heo cpumask_and(cpumask, pt->pod_cpus[pod], attrs->cpumask); 419384193c07STejun Heo cpumask_and(cpumask, cpumask, cpu_online_mask); 41944c16bd32STejun Heo if (cpu_going_down >= 0) 41954c16bd32STejun Heo cpumask_clear_cpu(cpu_going_down, cpumask); 41964c16bd32STejun Heo 419784193c07STejun Heo if (cpumask_empty(cpumask)) { 419884193c07STejun Heo cpumask_copy(cpumask, attrs->cpumask); 419984193c07STejun Heo return; 420084193c07STejun Heo } 42014c16bd32STejun Heo 4202fef59c9cSTejun Heo /* yeap, return possible CPUs in @pod that @attrs wants */ 420384193c07STejun Heo cpumask_and(cpumask, attrs->cpumask, pt->pod_cpus[pod]); 42041ad0f0a7SMichael Bringmann 4205636b927eSTejun Heo if (cpumask_empty(cpumask)) 42061ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 42071ad0f0a7SMichael Bringmann "possible intersect\n"); 42084c16bd32STejun Heo } 42094c16bd32STejun Heo 4210636b927eSTejun Heo /* install @pwq into @wq's cpu_pwq and return the old pwq */ 4211636b927eSTejun Heo static struct pool_workqueue *install_unbound_pwq(struct workqueue_struct *wq, 4212636b927eSTejun Heo int cpu, struct pool_workqueue *pwq) 42131befcf30STejun Heo { 42141befcf30STejun Heo struct pool_workqueue *old_pwq; 42151befcf30STejun Heo 42165b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 42171befcf30STejun Heo lockdep_assert_held(&wq->mutex); 42181befcf30STejun Heo 42191befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 42201befcf30STejun Heo link_pwq(pwq); 42211befcf30STejun Heo 4222636b927eSTejun Heo old_pwq = rcu_access_pointer(*per_cpu_ptr(wq->cpu_pwq, cpu)); 4223636b927eSTejun Heo rcu_assign_pointer(*per_cpu_ptr(wq->cpu_pwq, cpu), pwq); 42241befcf30STejun Heo return old_pwq; 42251befcf30STejun Heo } 42261befcf30STejun Heo 42272d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 42282d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 42292d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 42302d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 4231042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 42322d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 42332d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 42342d5f0764SLai Jiangshan }; 42352d5f0764SLai Jiangshan 42362d5f0764SLai Jiangshan /* free the resources after success or abort */ 42372d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 42382d5f0764SLai Jiangshan { 42392d5f0764SLai Jiangshan if (ctx) { 4240636b927eSTejun Heo int cpu; 42412d5f0764SLai Jiangshan 4242636b927eSTejun Heo for_each_possible_cpu(cpu) 4243636b927eSTejun Heo put_pwq_unlocked(ctx->pwq_tbl[cpu]); 42442d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 42452d5f0764SLai Jiangshan 42462d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 42472d5f0764SLai Jiangshan 42482d5f0764SLai Jiangshan kfree(ctx); 42492d5f0764SLai Jiangshan } 42502d5f0764SLai Jiangshan } 42512d5f0764SLai Jiangshan 42522d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 42532d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 42542d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 425599c621efSLai Jiangshan const struct workqueue_attrs *attrs, 425699c621efSLai Jiangshan const cpumask_var_t unbound_cpumask) 42572d5f0764SLai Jiangshan { 42582d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 42592d5f0764SLai Jiangshan struct workqueue_attrs *new_attrs, *tmp_attrs; 4260636b927eSTejun Heo int cpu; 42612d5f0764SLai Jiangshan 42622d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 42632d5f0764SLai Jiangshan 426484193c07STejun Heo if (WARN_ON(attrs->affn_scope < 0 || 426584193c07STejun Heo attrs->affn_scope >= WQ_AFFN_NR_TYPES)) 426684193c07STejun Heo return ERR_PTR(-EINVAL); 426784193c07STejun Heo 4268636b927eSTejun Heo ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_cpu_ids), GFP_KERNEL); 42692d5f0764SLai Jiangshan 4270be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs(); 4271be69d00dSThomas Gleixner tmp_attrs = alloc_workqueue_attrs(); 42722d5f0764SLai Jiangshan if (!ctx || !new_attrs || !tmp_attrs) 42732d5f0764SLai Jiangshan goto out_free; 42742d5f0764SLai Jiangshan 4275042f7df1SLai Jiangshan /* 42762d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 42772d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 42782d5f0764SLai Jiangshan * it even if we don't use it immediately. 42792d5f0764SLai Jiangshan */ 42800f36ee24STejun Heo copy_workqueue_attrs(new_attrs, attrs); 42810f36ee24STejun Heo wqattrs_actualize_cpumask(new_attrs, unbound_cpumask); 42822d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 42832d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 42842d5f0764SLai Jiangshan goto out_free; 42852d5f0764SLai Jiangshan 42860f36ee24STejun Heo /* 42870f36ee24STejun Heo * We may create multiple pwqs with differing cpumasks. Make a copy of 42880f36ee24STejun Heo * @new_attrs which will be modified and used to obtain pools. 42890f36ee24STejun Heo */ 42900f36ee24STejun Heo copy_workqueue_attrs(tmp_attrs, new_attrs); 42910f36ee24STejun Heo 4292636b927eSTejun Heo for_each_possible_cpu(cpu) { 4293af73f5c9STejun Heo if (new_attrs->ordered) { 42942d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 4295636b927eSTejun Heo ctx->pwq_tbl[cpu] = ctx->dfl_pwq; 4296636b927eSTejun Heo } else { 429784193c07STejun Heo wq_calc_pod_cpumask(new_attrs, cpu, -1, tmp_attrs->cpumask); 4298636b927eSTejun Heo ctx->pwq_tbl[cpu] = alloc_unbound_pwq(wq, tmp_attrs); 4299636b927eSTejun Heo if (!ctx->pwq_tbl[cpu]) 4300636b927eSTejun Heo goto out_free; 43012d5f0764SLai Jiangshan } 43022d5f0764SLai Jiangshan } 43032d5f0764SLai Jiangshan 4304042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 4305042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 4306042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 43072d5f0764SLai Jiangshan ctx->attrs = new_attrs; 4308042f7df1SLai Jiangshan 43092d5f0764SLai Jiangshan ctx->wq = wq; 43102d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 43112d5f0764SLai Jiangshan return ctx; 43122d5f0764SLai Jiangshan 43132d5f0764SLai Jiangshan out_free: 43142d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 43152d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 43162d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 431784193c07STejun Heo return ERR_PTR(-ENOMEM); 43182d5f0764SLai Jiangshan } 43192d5f0764SLai Jiangshan 43202d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 43212d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 43222d5f0764SLai Jiangshan { 4323636b927eSTejun Heo int cpu; 43242d5f0764SLai Jiangshan 43252d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 43262d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 43272d5f0764SLai Jiangshan 43282d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 43292d5f0764SLai Jiangshan 43302d5f0764SLai Jiangshan /* save the previous pwq and install the new one */ 4331636b927eSTejun Heo for_each_possible_cpu(cpu) 4332636b927eSTejun Heo ctx->pwq_tbl[cpu] = install_unbound_pwq(ctx->wq, cpu, 4333636b927eSTejun Heo ctx->pwq_tbl[cpu]); 43342d5f0764SLai Jiangshan 43352d5f0764SLai Jiangshan /* @dfl_pwq might not have been used, ensure it's linked */ 43362d5f0764SLai Jiangshan link_pwq(ctx->dfl_pwq); 43372d5f0764SLai Jiangshan swap(ctx->wq->dfl_pwq, ctx->dfl_pwq); 43382d5f0764SLai Jiangshan 43392d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 43402d5f0764SLai Jiangshan } 43412d5f0764SLai Jiangshan 4342a0111cf6SLai Jiangshan static void apply_wqattrs_lock(void) 4343a0111cf6SLai Jiangshan { 4344a0111cf6SLai Jiangshan /* CPUs should stay stable across pwq creations and installations */ 4345ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 4346a0111cf6SLai Jiangshan mutex_lock(&wq_pool_mutex); 4347a0111cf6SLai Jiangshan } 4348a0111cf6SLai Jiangshan 4349a0111cf6SLai Jiangshan static void apply_wqattrs_unlock(void) 4350a0111cf6SLai Jiangshan { 4351a0111cf6SLai Jiangshan mutex_unlock(&wq_pool_mutex); 4352ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4353a0111cf6SLai Jiangshan } 4354a0111cf6SLai Jiangshan 4355a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 4356a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 4357a0111cf6SLai Jiangshan { 4358a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 4359a0111cf6SLai Jiangshan 4360a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 4361a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 4362a0111cf6SLai Jiangshan return -EINVAL; 4363a0111cf6SLai Jiangshan 4364a0111cf6SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 43650a94efb5STejun Heo if (!list_empty(&wq->pwqs)) { 43660a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 4367a0111cf6SLai Jiangshan return -EINVAL; 4368a0111cf6SLai Jiangshan 43690a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 43700a94efb5STejun Heo } 43710a94efb5STejun Heo 437299c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); 437384193c07STejun Heo if (IS_ERR(ctx)) 437484193c07STejun Heo return PTR_ERR(ctx); 4375a0111cf6SLai Jiangshan 4376a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 4377a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 4378a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 4379a0111cf6SLai Jiangshan 43806201171eSwanghaibin return 0; 4381a0111cf6SLai Jiangshan } 4382a0111cf6SLai Jiangshan 43839e8cd2f5STejun Heo /** 43849e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 43859e8cd2f5STejun Heo * @wq: the target workqueue 43869e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 43879e8cd2f5STejun Heo * 4388fef59c9cSTejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, this function maps 4389fef59c9cSTejun Heo * a separate pwq to each CPU pod with possibles CPUs in @attrs->cpumask so that 4390fef59c9cSTejun Heo * work items are affine to the pod it was issued on. Older pwqs are released as 4391fef59c9cSTejun Heo * in-flight work items finish. Note that a work item which repeatedly requeues 4392fef59c9cSTejun Heo * itself back-to-back will stay on its current pwq. 43939e8cd2f5STejun Heo * 4394d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 4395d185af30SYacine Belkadi * 4396ffd8bea8SSebastian Andrzej Siewior * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). 4397509b3204SDaniel Jordan * 4398d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 43999e8cd2f5STejun Heo */ 4400513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 44019e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 44029e8cd2f5STejun Heo { 4403a0111cf6SLai Jiangshan int ret; 44049e8cd2f5STejun Heo 4405509b3204SDaniel Jordan lockdep_assert_cpus_held(); 4406509b3204SDaniel Jordan 4407509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex); 4408a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 4409509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex); 44102d5f0764SLai Jiangshan 44112d5f0764SLai Jiangshan return ret; 44129e8cd2f5STejun Heo } 44139e8cd2f5STejun Heo 44144c16bd32STejun Heo /** 4415fef59c9cSTejun Heo * wq_update_pod - update pod affinity of a wq for CPU hot[un]plug 44164c16bd32STejun Heo * @wq: the target workqueue 44174cbfd3deSTejun Heo * @cpu: the CPU to update pool association for 44184cbfd3deSTejun Heo * @hotplug_cpu: the CPU coming up or going down 44194c16bd32STejun Heo * @online: whether @cpu is coming up or going down 44204c16bd32STejun Heo * 44214c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 4422fef59c9cSTejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update pod affinity of 44234c16bd32STejun Heo * @wq accordingly. 44244c16bd32STejun Heo * 44254c16bd32STejun Heo * 4426fef59c9cSTejun Heo * If pod affinity can't be adjusted due to memory allocation failure, it falls 4427fef59c9cSTejun Heo * back to @wq->dfl_pwq which may not be optimal but is always correct. 4428fef59c9cSTejun Heo * 4429fef59c9cSTejun Heo * Note that when the last allowed CPU of a pod goes offline for a workqueue 4430fef59c9cSTejun Heo * with a cpumask spanning multiple pods, the workers which were already 4431fef59c9cSTejun Heo * executing the work items for the workqueue will lose their CPU affinity and 4432fef59c9cSTejun Heo * may execute on any CPU. This is similar to how per-cpu workqueues behave on 4433fef59c9cSTejun Heo * CPU_DOWN. If a workqueue user wants strict affinity, it's the user's 4434fef59c9cSTejun Heo * responsibility to flush the work item from CPU_DOWN_PREPARE. 44354c16bd32STejun Heo */ 4436fef59c9cSTejun Heo static void wq_update_pod(struct workqueue_struct *wq, int cpu, 44374cbfd3deSTejun Heo int hotplug_cpu, bool online) 44384c16bd32STejun Heo { 44394cbfd3deSTejun Heo int off_cpu = online ? -1 : hotplug_cpu; 44404c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 44414c16bd32STejun Heo struct workqueue_attrs *target_attrs; 44424c16bd32STejun Heo cpumask_t *cpumask; 44434c16bd32STejun Heo 44444c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 44454c16bd32STejun Heo 444684193c07STejun Heo if (!(wq->flags & WQ_UNBOUND) || wq->unbound_attrs->ordered) 44474c16bd32STejun Heo return; 44484c16bd32STejun Heo 44494c16bd32STejun Heo /* 44504c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 44514c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 44524c16bd32STejun Heo * CPU hotplug exclusion. 44534c16bd32STejun Heo */ 4454fef59c9cSTejun Heo target_attrs = wq_update_pod_attrs_buf; 44550f36ee24STejun Heo cpumask = wq_update_pod_cpumask_buf; 44564c16bd32STejun Heo 44574c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 44580f36ee24STejun Heo wqattrs_actualize_cpumask(target_attrs, wq_unbound_cpumask); 44594c16bd32STejun Heo 4460636b927eSTejun Heo /* nothing to do if the target cpumask matches the current pwq */ 446184193c07STejun Heo wq_calc_pod_cpumask(target_attrs, cpu, off_cpu, cpumask); 4462636b927eSTejun Heo pwq = rcu_dereference_protected(*per_cpu_ptr(wq->cpu_pwq, cpu), 4463636b927eSTejun Heo lockdep_is_held(&wq_pool_mutex)); 44644c16bd32STejun Heo if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask)) 4465f7142ed4SLai Jiangshan return; 44664c16bd32STejun Heo 44674c16bd32STejun Heo /* create a new pwq */ 44680f36ee24STejun Heo cpumask_copy(target_attrs->cpumask, cpumask); 44694c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 44704c16bd32STejun Heo if (!pwq) { 4471fef59c9cSTejun Heo pr_warn("workqueue: allocation failed while updating CPU pod affinity of \"%s\"\n", 44724c16bd32STejun Heo wq->name); 447377f300b1SDaeseok Youn goto use_dfl_pwq; 44744c16bd32STejun Heo } 44754c16bd32STejun Heo 4476f7142ed4SLai Jiangshan /* Install the new pwq. */ 44774c16bd32STejun Heo mutex_lock(&wq->mutex); 4478636b927eSTejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 44794c16bd32STejun Heo goto out_unlock; 44804c16bd32STejun Heo 44814c16bd32STejun Heo use_dfl_pwq: 4482f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 4483a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq->dfl_pwq->pool->lock); 44844c16bd32STejun Heo get_pwq(wq->dfl_pwq); 4485a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq->dfl_pwq->pool->lock); 4486636b927eSTejun Heo old_pwq = install_unbound_pwq(wq, cpu, wq->dfl_pwq); 44874c16bd32STejun Heo out_unlock: 44884c16bd32STejun Heo mutex_unlock(&wq->mutex); 44894c16bd32STejun Heo put_pwq_unlocked(old_pwq); 44904c16bd32STejun Heo } 44914c16bd32STejun Heo 449230cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 44931da177e4SLinus Torvalds { 449449e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 44958a2b7538STejun Heo int cpu, ret; 4496e1d8aa9fSFrederic Weisbecker 4497687a9aa5STejun Heo wq->cpu_pwq = alloc_percpu(struct pool_workqueue *); 4498ee1ceef7STejun Heo if (!wq->cpu_pwq) 4499687a9aa5STejun Heo goto enomem; 450030cdf249STejun Heo 4501636b927eSTejun Heo if (!(wq->flags & WQ_UNBOUND)) { 450230cdf249STejun Heo for_each_possible_cpu(cpu) { 4503687a9aa5STejun Heo struct pool_workqueue **pwq_p = 4504ee1ceef7STejun Heo per_cpu_ptr(wq->cpu_pwq, cpu); 4505687a9aa5STejun Heo struct worker_pool *pool = 4506687a9aa5STejun Heo &(per_cpu_ptr(cpu_worker_pools, cpu)[highpri]); 450730cdf249STejun Heo 4508687a9aa5STejun Heo *pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, 4509687a9aa5STejun Heo pool->node); 4510687a9aa5STejun Heo if (!*pwq_p) 4511687a9aa5STejun Heo goto enomem; 4512687a9aa5STejun Heo 4513687a9aa5STejun Heo init_pwq(*pwq_p, wq, pool); 4514f147f29eSTejun Heo 4515f147f29eSTejun Heo mutex_lock(&wq->mutex); 4516687a9aa5STejun Heo link_pwq(*pwq_p); 4517f147f29eSTejun Heo mutex_unlock(&wq->mutex); 451830cdf249STejun Heo } 451930cdf249STejun Heo return 0; 4520509b3204SDaniel Jordan } 4521509b3204SDaniel Jordan 4522ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 4523509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) { 45248a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 45258a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 45268a2b7538STejun Heo WARN(!ret && (wq->pwqs.next != &wq->dfl_pwq->pwqs_node || 45278a2b7538STejun Heo wq->pwqs.prev != &wq->dfl_pwq->pwqs_node), 45288a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 45299e8cd2f5STejun Heo } else { 4530509b3204SDaniel Jordan ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 45319e8cd2f5STejun Heo } 4532ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4533509b3204SDaniel Jordan 4534509b3204SDaniel Jordan return ret; 4535687a9aa5STejun Heo 4536687a9aa5STejun Heo enomem: 4537687a9aa5STejun Heo if (wq->cpu_pwq) { 4538687a9aa5STejun Heo for_each_possible_cpu(cpu) 4539687a9aa5STejun Heo kfree(*per_cpu_ptr(wq->cpu_pwq, cpu)); 4540687a9aa5STejun Heo free_percpu(wq->cpu_pwq); 4541687a9aa5STejun Heo wq->cpu_pwq = NULL; 4542687a9aa5STejun Heo } 4543687a9aa5STejun Heo return -ENOMEM; 45440f900049STejun Heo } 45450f900049STejun Heo 4546f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 4547f3421797STejun Heo const char *name) 4548b71ab8c2STejun Heo { 4549636b927eSTejun Heo if (max_active < 1 || max_active > WQ_MAX_ACTIVE) 4550044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 4551636b927eSTejun Heo max_active, name, 1, WQ_MAX_ACTIVE); 4552b71ab8c2STejun Heo 4553636b927eSTejun Heo return clamp_val(max_active, 1, WQ_MAX_ACTIVE); 4554b71ab8c2STejun Heo } 4555b71ab8c2STejun Heo 4556983c7515STejun Heo /* 4557983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 4558983c7515STejun Heo * to guarantee forward progress. 4559983c7515STejun Heo */ 4560983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 4561983c7515STejun Heo { 4562983c7515STejun Heo struct worker *rescuer; 4563b92b36eaSDan Carpenter int ret; 4564983c7515STejun Heo 4565983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 4566983c7515STejun Heo return 0; 4567983c7515STejun Heo 4568983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 45694c0736a7SPetr Mladek if (!rescuer) { 45704c0736a7SPetr Mladek pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", 45714c0736a7SPetr Mladek wq->name); 4572983c7515STejun Heo return -ENOMEM; 45734c0736a7SPetr Mladek } 4574983c7515STejun Heo 4575983c7515STejun Heo rescuer->rescue_wq = wq; 4576983c7515STejun Heo rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", wq->name); 4577f187b697SSean Fu if (IS_ERR(rescuer->task)) { 4578b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task); 45794c0736a7SPetr Mladek pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", 45804c0736a7SPetr Mladek wq->name, ERR_PTR(ret)); 4581983c7515STejun Heo kfree(rescuer); 4582b92b36eaSDan Carpenter return ret; 4583983c7515STejun Heo } 4584983c7515STejun Heo 4585983c7515STejun Heo wq->rescuer = rescuer; 4586983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 4587983c7515STejun Heo wake_up_process(rescuer->task); 4588983c7515STejun Heo 4589983c7515STejun Heo return 0; 4590983c7515STejun Heo } 4591983c7515STejun Heo 4592a2775bbcSMathieu Malaterre __printf(1, 4) 4593669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt, 459497e37d7bSTejun Heo unsigned int flags, 4595669de8bdSBart Van Assche int max_active, ...) 45963af24433SOleg Nesterov { 4597ecf6881fSTejun Heo va_list args; 45983af24433SOleg Nesterov struct workqueue_struct *wq; 459949e3cf44STejun Heo struct pool_workqueue *pwq; 4600b196be89STejun Heo 46015c0338c6STejun Heo /* 4602fef59c9cSTejun Heo * Unbound && max_active == 1 used to imply ordered, which is no longer 4603fef59c9cSTejun Heo * the case on many machines due to per-pod pools. While 46045c0338c6STejun Heo * alloc_ordered_workqueue() is the right way to create an ordered 4605fef59c9cSTejun Heo * workqueue, keep the previous behavior to avoid subtle breakages. 46065c0338c6STejun Heo */ 46075c0338c6STejun Heo if ((flags & WQ_UNBOUND) && max_active == 1) 46085c0338c6STejun Heo flags |= __WQ_ORDERED; 46095c0338c6STejun Heo 4610cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 4611cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 4612cee22a15SViresh Kumar flags |= WQ_UNBOUND; 4613cee22a15SViresh Kumar 4614ecf6881fSTejun Heo /* allocate wq and format name */ 4615636b927eSTejun Heo wq = kzalloc(sizeof(*wq), GFP_KERNEL); 4616b196be89STejun Heo if (!wq) 4617d2c1d404STejun Heo return NULL; 4618b196be89STejun Heo 46196029a918STejun Heo if (flags & WQ_UNBOUND) { 4620be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs(); 46216029a918STejun Heo if (!wq->unbound_attrs) 46226029a918STejun Heo goto err_free_wq; 46236029a918STejun Heo } 46246029a918STejun Heo 4625669de8bdSBart Van Assche va_start(args, max_active); 4626ecf6881fSTejun Heo vsnprintf(wq->name, sizeof(wq->name), fmt, args); 4627b196be89STejun Heo va_end(args); 46283af24433SOleg Nesterov 4629d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 4630b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 46313af24433SOleg Nesterov 4632b196be89STejun Heo /* init wq */ 463397e37d7bSTejun Heo wq->flags = flags; 4634a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 46353c25a55dSLai Jiangshan mutex_init(&wq->mutex); 4636112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 463730cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 463873f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 463973f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 4640493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 46413af24433SOleg Nesterov 4642669de8bdSBart Van Assche wq_init_lockdep(wq); 4643cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 46443af24433SOleg Nesterov 464530cdf249STejun Heo if (alloc_and_link_pwqs(wq) < 0) 464682efcab3SBart Van Assche goto err_unreg_lockdep; 46471537663fSTejun Heo 464840c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 4649d2c1d404STejun Heo goto err_destroy; 4650e22bee78STejun Heo 4651226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 4652226223abSTejun Heo goto err_destroy; 4653226223abSTejun Heo 46546af8bf3dSOleg Nesterov /* 465568e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 465668e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 465768e13a67SLai Jiangshan * list. 46586af8bf3dSOleg Nesterov */ 465968e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4660a0a1a5fdSTejun Heo 4661a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 466249e3cf44STejun Heo for_each_pwq(pwq, wq) 4663699ce097STejun Heo pwq_adjust_max_active(pwq); 4664a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4665a0a1a5fdSTejun Heo 4666e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 4667a0a1a5fdSTejun Heo 466868e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 46693af24433SOleg Nesterov 46703af24433SOleg Nesterov return wq; 4671d2c1d404STejun Heo 467282efcab3SBart Van Assche err_unreg_lockdep: 4673009bb421SBart Van Assche wq_unregister_lockdep(wq); 4674009bb421SBart Van Assche wq_free_lockdep(wq); 467582efcab3SBart Van Assche err_free_wq: 46766029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 46774690c4abSTejun Heo kfree(wq); 4678d2c1d404STejun Heo return NULL; 4679d2c1d404STejun Heo err_destroy: 4680d2c1d404STejun Heo destroy_workqueue(wq); 46814690c4abSTejun Heo return NULL; 46821da177e4SLinus Torvalds } 4683669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue); 46841da177e4SLinus Torvalds 4685c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq) 4686c29eb853STejun Heo { 4687c29eb853STejun Heo int i; 4688c29eb853STejun Heo 4689c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 4690c29eb853STejun Heo if (pwq->nr_in_flight[i]) 4691c29eb853STejun Heo return true; 4692c29eb853STejun Heo 4693c29eb853STejun Heo if ((pwq != pwq->wq->dfl_pwq) && (pwq->refcnt > 1)) 4694c29eb853STejun Heo return true; 4695f97a4a1aSLai Jiangshan if (pwq->nr_active || !list_empty(&pwq->inactive_works)) 4696c29eb853STejun Heo return true; 4697c29eb853STejun Heo 4698c29eb853STejun Heo return false; 4699c29eb853STejun Heo } 4700c29eb853STejun Heo 47013af24433SOleg Nesterov /** 47023af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 47033af24433SOleg Nesterov * @wq: target workqueue 47043af24433SOleg Nesterov * 47053af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 47063af24433SOleg Nesterov */ 47073af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 47083af24433SOleg Nesterov { 470949e3cf44STejun Heo struct pool_workqueue *pwq; 4710636b927eSTejun Heo int cpu; 47113af24433SOleg Nesterov 4712def98c84STejun Heo /* 4713def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't 4714def98c84STejun Heo * lead to sysfs name conflicts. 4715def98c84STejun Heo */ 4716def98c84STejun Heo workqueue_sysfs_unregister(wq); 4717def98c84STejun Heo 471833e3f0a3SRichard Clark /* mark the workqueue destruction is in progress */ 471933e3f0a3SRichard Clark mutex_lock(&wq->mutex); 472033e3f0a3SRichard Clark wq->flags |= __WQ_DESTROYING; 472133e3f0a3SRichard Clark mutex_unlock(&wq->mutex); 472233e3f0a3SRichard Clark 47239c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 47249c5a2ba7STejun Heo drain_workqueue(wq); 4725c8efcc25STejun Heo 4726def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ 4727def98c84STejun Heo if (wq->rescuer) { 4728def98c84STejun Heo struct worker *rescuer = wq->rescuer; 4729def98c84STejun Heo 4730def98c84STejun Heo /* this prevents new queueing */ 4731a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 4732def98c84STejun Heo wq->rescuer = NULL; 4733a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 4734def98c84STejun Heo 4735def98c84STejun Heo /* rescuer will empty maydays list before exiting */ 4736def98c84STejun Heo kthread_stop(rescuer->task); 47378efe1223STejun Heo kfree(rescuer); 4738def98c84STejun Heo } 4739def98c84STejun Heo 4740c29eb853STejun Heo /* 4741c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all 4742c29eb853STejun Heo * in-flight operations which may do put_pwq(). 4743c29eb853STejun Heo */ 4744c29eb853STejun Heo mutex_lock(&wq_pool_mutex); 4745b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 474649e3cf44STejun Heo for_each_pwq(pwq, wq) { 4747a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 4748c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) { 47491d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n", 4750e66b39afSTejun Heo __func__, wq->name); 4751c29eb853STejun Heo show_pwq(pwq); 4752a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 4753b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 4754c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 475555df0933SImran Khan show_one_workqueue(wq); 47566183c009STejun Heo return; 475776af4d93STejun Heo } 4758a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 475976af4d93STejun Heo } 4760b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 47616183c009STejun Heo 4762a0a1a5fdSTejun Heo /* 4763a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 4764a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 4765a0a1a5fdSTejun Heo */ 4766e2dca7adSTejun Heo list_del_rcu(&wq->list); 476768e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 47683af24433SOleg Nesterov 47698864b4e5STejun Heo /* 4770636b927eSTejun Heo * We're the sole accessor of @wq. Directly access cpu_pwq and dfl_pwq 4771636b927eSTejun Heo * to put the base refs. @wq will be auto-destroyed from the last 4772636b927eSTejun Heo * pwq_put. RCU read lock prevents @wq from going away from under us. 47738864b4e5STejun Heo */ 4774636b927eSTejun Heo rcu_read_lock(); 4775636b927eSTejun Heo 4776636b927eSTejun Heo for_each_possible_cpu(cpu) { 4777636b927eSTejun Heo pwq = rcu_access_pointer(*per_cpu_ptr(wq->cpu_pwq, cpu)); 4778636b927eSTejun Heo RCU_INIT_POINTER(*per_cpu_ptr(wq->cpu_pwq, cpu), NULL); 47794c16bd32STejun Heo put_pwq_unlocked(pwq); 47804c16bd32STejun Heo } 47814c16bd32STejun Heo 4782636b927eSTejun Heo put_pwq_unlocked(wq->dfl_pwq); 47834c16bd32STejun Heo wq->dfl_pwq = NULL; 4784636b927eSTejun Heo 4785636b927eSTejun Heo rcu_read_unlock(); 47863af24433SOleg Nesterov } 47873af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 47883af24433SOleg Nesterov 4789dcd989cbSTejun Heo /** 4790dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 4791dcd989cbSTejun Heo * @wq: target workqueue 4792dcd989cbSTejun Heo * @max_active: new max_active value. 4793dcd989cbSTejun Heo * 4794dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 4795dcd989cbSTejun Heo * 4796dcd989cbSTejun Heo * CONTEXT: 4797dcd989cbSTejun Heo * Don't call from IRQ context. 4798dcd989cbSTejun Heo */ 4799dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 4800dcd989cbSTejun Heo { 480149e3cf44STejun Heo struct pool_workqueue *pwq; 4802dcd989cbSTejun Heo 48038719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 48040a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 48058719dceaSTejun Heo return; 48068719dceaSTejun Heo 4807f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 4808dcd989cbSTejun Heo 4809a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4810dcd989cbSTejun Heo 48110a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 4812dcd989cbSTejun Heo wq->saved_max_active = max_active; 4813dcd989cbSTejun Heo 4814699ce097STejun Heo for_each_pwq(pwq, wq) 4815699ce097STejun Heo pwq_adjust_max_active(pwq); 4816dcd989cbSTejun Heo 4817a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4818dcd989cbSTejun Heo } 4819dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 4820dcd989cbSTejun Heo 4821dcd989cbSTejun Heo /** 482227d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 482327d4ee03SLukas Wunner * 482427d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 482527d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 482627d4ee03SLukas Wunner * 482727d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 482827d4ee03SLukas Wunner */ 482927d4ee03SLukas Wunner struct work_struct *current_work(void) 483027d4ee03SLukas Wunner { 483127d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 483227d4ee03SLukas Wunner 483327d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 483427d4ee03SLukas Wunner } 483527d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 483627d4ee03SLukas Wunner 483727d4ee03SLukas Wunner /** 4838e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 4839e6267616STejun Heo * 4840e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 4841e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 4842d185af30SYacine Belkadi * 4843d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 4844e6267616STejun Heo */ 4845e6267616STejun Heo bool current_is_workqueue_rescuer(void) 4846e6267616STejun Heo { 4847e6267616STejun Heo struct worker *worker = current_wq_worker(); 4848e6267616STejun Heo 48496a092dfdSLai Jiangshan return worker && worker->rescue_wq; 4850e6267616STejun Heo } 4851e6267616STejun Heo 4852e6267616STejun Heo /** 4853dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 4854dcd989cbSTejun Heo * @cpu: CPU in question 4855dcd989cbSTejun Heo * @wq: target workqueue 4856dcd989cbSTejun Heo * 4857dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 4858dcd989cbSTejun Heo * no synchronization around this function and the test result is 4859dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4860dcd989cbSTejun Heo * 4861d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 4862636b927eSTejun Heo * 4863636b927eSTejun Heo * With the exception of ordered workqueues, all workqueues have per-cpu 4864636b927eSTejun Heo * pool_workqueues, each with its own congested state. A workqueue being 4865636b927eSTejun Heo * congested on one CPU doesn't mean that the workqueue is contested on any 4866636b927eSTejun Heo * other CPUs. 4867d3251859STejun Heo * 4868d185af30SYacine Belkadi * Return: 4869dcd989cbSTejun Heo * %true if congested, %false otherwise. 4870dcd989cbSTejun Heo */ 4871d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 4872dcd989cbSTejun Heo { 48737fb98ea7STejun Heo struct pool_workqueue *pwq; 487476af4d93STejun Heo bool ret; 487576af4d93STejun Heo 487624acfb71SThomas Gleixner rcu_read_lock(); 487724acfb71SThomas Gleixner preempt_disable(); 48787fb98ea7STejun Heo 4879d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 4880d3251859STejun Heo cpu = smp_processor_id(); 4881d3251859STejun Heo 4882687a9aa5STejun Heo pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 4883f97a4a1aSLai Jiangshan ret = !list_empty(&pwq->inactive_works); 4884636b927eSTejun Heo 488524acfb71SThomas Gleixner preempt_enable(); 488624acfb71SThomas Gleixner rcu_read_unlock(); 488776af4d93STejun Heo 488876af4d93STejun Heo return ret; 4889dcd989cbSTejun Heo } 4890dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 4891dcd989cbSTejun Heo 4892dcd989cbSTejun Heo /** 4893dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 4894dcd989cbSTejun Heo * @work: the work to be tested 4895dcd989cbSTejun Heo * 4896dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 4897dcd989cbSTejun Heo * synchronization around this function and the test result is 4898dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4899dcd989cbSTejun Heo * 4900d185af30SYacine Belkadi * Return: 4901dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 4902dcd989cbSTejun Heo */ 4903dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 4904dcd989cbSTejun Heo { 4905fa1b54e6STejun Heo struct worker_pool *pool; 4906dcd989cbSTejun Heo unsigned long flags; 4907dcd989cbSTejun Heo unsigned int ret = 0; 4908dcd989cbSTejun Heo 4909dcd989cbSTejun Heo if (work_pending(work)) 4910dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 4911038366c5SLai Jiangshan 491224acfb71SThomas Gleixner rcu_read_lock(); 4913fa1b54e6STejun Heo pool = get_work_pool(work); 4914038366c5SLai Jiangshan if (pool) { 4915a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 4916c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 4917dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 4918a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 4919038366c5SLai Jiangshan } 492024acfb71SThomas Gleixner rcu_read_unlock(); 4921dcd989cbSTejun Heo 4922dcd989cbSTejun Heo return ret; 4923dcd989cbSTejun Heo } 4924dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 4925dcd989cbSTejun Heo 49263d1cb205STejun Heo /** 49273d1cb205STejun Heo * set_worker_desc - set description for the current work item 49283d1cb205STejun Heo * @fmt: printf-style format string 49293d1cb205STejun Heo * @...: arguments for the format string 49303d1cb205STejun Heo * 49313d1cb205STejun Heo * This function can be called by a running work function to describe what 49323d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 49333d1cb205STejun Heo * information will be printed out together to help debugging. The 49343d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 49353d1cb205STejun Heo */ 49363d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 49373d1cb205STejun Heo { 49383d1cb205STejun Heo struct worker *worker = current_wq_worker(); 49393d1cb205STejun Heo va_list args; 49403d1cb205STejun Heo 49413d1cb205STejun Heo if (worker) { 49423d1cb205STejun Heo va_start(args, fmt); 49433d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 49443d1cb205STejun Heo va_end(args); 49453d1cb205STejun Heo } 49463d1cb205STejun Heo } 49475c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 49483d1cb205STejun Heo 49493d1cb205STejun Heo /** 49503d1cb205STejun Heo * print_worker_info - print out worker information and description 49513d1cb205STejun Heo * @log_lvl: the log level to use when printing 49523d1cb205STejun Heo * @task: target task 49533d1cb205STejun Heo * 49543d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 49553d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 49563d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 49573d1cb205STejun Heo * 49583d1cb205STejun Heo * This function can be safely called on any task as long as the 49593d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 49603d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 49613d1cb205STejun Heo */ 49623d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 49633d1cb205STejun Heo { 49643d1cb205STejun Heo work_func_t *fn = NULL; 49653d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 49663d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 49673d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 49683d1cb205STejun Heo struct workqueue_struct *wq = NULL; 49693d1cb205STejun Heo struct worker *worker; 49703d1cb205STejun Heo 49713d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 49723d1cb205STejun Heo return; 49733d1cb205STejun Heo 49743d1cb205STejun Heo /* 49753d1cb205STejun Heo * This function is called without any synchronization and @task 49763d1cb205STejun Heo * could be in any state. Be careful with dereferences. 49773d1cb205STejun Heo */ 4978e700591aSPetr Mladek worker = kthread_probe_data(task); 49793d1cb205STejun Heo 49803d1cb205STejun Heo /* 49818bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 49828bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 49833d1cb205STejun Heo */ 4984fe557319SChristoph Hellwig copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); 4985fe557319SChristoph Hellwig copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); 4986fe557319SChristoph Hellwig copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); 4987fe557319SChristoph Hellwig copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); 4988fe557319SChristoph Hellwig copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); 49893d1cb205STejun Heo 49903d1cb205STejun Heo if (fn || name[0] || desc[0]) { 4991d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn); 49928bf89593STejun Heo if (strcmp(name, desc)) 49933d1cb205STejun Heo pr_cont(" (%s)", desc); 49943d1cb205STejun Heo pr_cont("\n"); 49953d1cb205STejun Heo } 49963d1cb205STejun Heo } 49973d1cb205STejun Heo 49983494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 49993494fc30STejun Heo { 50003494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 50013494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 50023494fc30STejun Heo pr_cont(" node=%d", pool->node); 50033494fc30STejun Heo pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice); 50043494fc30STejun Heo } 50053494fc30STejun Heo 5006c76feb0dSPaul E. McKenney struct pr_cont_work_struct { 5007c76feb0dSPaul E. McKenney bool comma; 5008c76feb0dSPaul E. McKenney work_func_t func; 5009c76feb0dSPaul E. McKenney long ctr; 5010c76feb0dSPaul E. McKenney }; 5011c76feb0dSPaul E. McKenney 5012c76feb0dSPaul E. McKenney static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) 5013c76feb0dSPaul E. McKenney { 5014c76feb0dSPaul E. McKenney if (!pcwsp->ctr) 5015c76feb0dSPaul E. McKenney goto out_record; 5016c76feb0dSPaul E. McKenney if (func == pcwsp->func) { 5017c76feb0dSPaul E. McKenney pcwsp->ctr++; 5018c76feb0dSPaul E. McKenney return; 5019c76feb0dSPaul E. McKenney } 5020c76feb0dSPaul E. McKenney if (pcwsp->ctr == 1) 5021c76feb0dSPaul E. McKenney pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); 5022c76feb0dSPaul E. McKenney else 5023c76feb0dSPaul E. McKenney pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); 5024c76feb0dSPaul E. McKenney pcwsp->ctr = 0; 5025c76feb0dSPaul E. McKenney out_record: 5026c76feb0dSPaul E. McKenney if ((long)func == -1L) 5027c76feb0dSPaul E. McKenney return; 5028c76feb0dSPaul E. McKenney pcwsp->comma = comma; 5029c76feb0dSPaul E. McKenney pcwsp->func = func; 5030c76feb0dSPaul E. McKenney pcwsp->ctr = 1; 5031c76feb0dSPaul E. McKenney } 5032c76feb0dSPaul E. McKenney 5033c76feb0dSPaul E. McKenney static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) 50343494fc30STejun Heo { 50353494fc30STejun Heo if (work->func == wq_barrier_func) { 50363494fc30STejun Heo struct wq_barrier *barr; 50373494fc30STejun Heo 50383494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 50393494fc30STejun Heo 5040c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 50413494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 50423494fc30STejun Heo task_pid_nr(barr->task)); 50433494fc30STejun Heo } else { 5044c76feb0dSPaul E. McKenney if (!comma) 5045c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 5046c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, work->func, pcwsp); 50473494fc30STejun Heo } 50483494fc30STejun Heo } 50493494fc30STejun Heo 50503494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 50513494fc30STejun Heo { 5052c76feb0dSPaul E. McKenney struct pr_cont_work_struct pcws = { .ctr = 0, }; 50533494fc30STejun Heo struct worker_pool *pool = pwq->pool; 50543494fc30STejun Heo struct work_struct *work; 50553494fc30STejun Heo struct worker *worker; 50563494fc30STejun Heo bool has_in_flight = false, has_pending = false; 50573494fc30STejun Heo int bkt; 50583494fc30STejun Heo 50593494fc30STejun Heo pr_info(" pwq %d:", pool->id); 50603494fc30STejun Heo pr_cont_pool_info(pool); 50613494fc30STejun Heo 5062e66b39afSTejun Heo pr_cont(" active=%d/%d refcnt=%d%s\n", 5063e66b39afSTejun Heo pwq->nr_active, pwq->max_active, pwq->refcnt, 50643494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 50653494fc30STejun Heo 50663494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 50673494fc30STejun Heo if (worker->current_pwq == pwq) { 50683494fc30STejun Heo has_in_flight = true; 50693494fc30STejun Heo break; 50703494fc30STejun Heo } 50713494fc30STejun Heo } 50723494fc30STejun Heo if (has_in_flight) { 50733494fc30STejun Heo bool comma = false; 50743494fc30STejun Heo 50753494fc30STejun Heo pr_info(" in-flight:"); 50763494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 50773494fc30STejun Heo if (worker->current_pwq != pwq) 50783494fc30STejun Heo continue; 50793494fc30STejun Heo 5080d75f773cSSakari Ailus pr_cont("%s %d%s:%ps", comma ? "," : "", 50813494fc30STejun Heo task_pid_nr(worker->task), 508230ae2fc0STejun Heo worker->rescue_wq ? "(RESCUER)" : "", 50833494fc30STejun Heo worker->current_func); 50843494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 5085c76feb0dSPaul E. McKenney pr_cont_work(false, work, &pcws); 5086c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 50873494fc30STejun Heo comma = true; 50883494fc30STejun Heo } 50893494fc30STejun Heo pr_cont("\n"); 50903494fc30STejun Heo } 50913494fc30STejun Heo 50923494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 50933494fc30STejun Heo if (get_work_pwq(work) == pwq) { 50943494fc30STejun Heo has_pending = true; 50953494fc30STejun Heo break; 50963494fc30STejun Heo } 50973494fc30STejun Heo } 50983494fc30STejun Heo if (has_pending) { 50993494fc30STejun Heo bool comma = false; 51003494fc30STejun Heo 51013494fc30STejun Heo pr_info(" pending:"); 51023494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 51033494fc30STejun Heo if (get_work_pwq(work) != pwq) 51043494fc30STejun Heo continue; 51053494fc30STejun Heo 5106c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 51073494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 51083494fc30STejun Heo } 5109c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 51103494fc30STejun Heo pr_cont("\n"); 51113494fc30STejun Heo } 51123494fc30STejun Heo 5113f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 51143494fc30STejun Heo bool comma = false; 51153494fc30STejun Heo 5116f97a4a1aSLai Jiangshan pr_info(" inactive:"); 5117f97a4a1aSLai Jiangshan list_for_each_entry(work, &pwq->inactive_works, entry) { 5118c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 51193494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 51203494fc30STejun Heo } 5121c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 51223494fc30STejun Heo pr_cont("\n"); 51233494fc30STejun Heo } 51243494fc30STejun Heo } 51253494fc30STejun Heo 51263494fc30STejun Heo /** 512755df0933SImran Khan * show_one_workqueue - dump state of specified workqueue 512855df0933SImran Khan * @wq: workqueue whose state will be printed 51293494fc30STejun Heo */ 513055df0933SImran Khan void show_one_workqueue(struct workqueue_struct *wq) 51313494fc30STejun Heo { 51323494fc30STejun Heo struct pool_workqueue *pwq; 51333494fc30STejun Heo bool idle = true; 513455df0933SImran Khan unsigned long flags; 51353494fc30STejun Heo 51363494fc30STejun Heo for_each_pwq(pwq, wq) { 5137f97a4a1aSLai Jiangshan if (pwq->nr_active || !list_empty(&pwq->inactive_works)) { 51383494fc30STejun Heo idle = false; 51393494fc30STejun Heo break; 51403494fc30STejun Heo } 51413494fc30STejun Heo } 514255df0933SImran Khan if (idle) /* Nothing to print for idle workqueue */ 514355df0933SImran Khan return; 51443494fc30STejun Heo 51453494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 51463494fc30STejun Heo 51473494fc30STejun Heo for_each_pwq(pwq, wq) { 5148a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pwq->pool->lock, flags); 514957116ce1SJohan Hovold if (pwq->nr_active || !list_empty(&pwq->inactive_works)) { 515057116ce1SJohan Hovold /* 515157116ce1SJohan Hovold * Defer printing to avoid deadlocks in console 515257116ce1SJohan Hovold * drivers that queue work while holding locks 515357116ce1SJohan Hovold * also taken in their write paths. 515457116ce1SJohan Hovold */ 515557116ce1SJohan Hovold printk_deferred_enter(); 51563494fc30STejun Heo show_pwq(pwq); 515757116ce1SJohan Hovold printk_deferred_exit(); 515857116ce1SJohan Hovold } 5159a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 516062635ea8SSergey Senozhatsky /* 516162635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 516255df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 516362635ea8SSergey Senozhatsky * hard lockup. 516462635ea8SSergey Senozhatsky */ 516562635ea8SSergey Senozhatsky touch_nmi_watchdog(); 51663494fc30STejun Heo } 516755df0933SImran Khan 51683494fc30STejun Heo } 51693494fc30STejun Heo 517055df0933SImran Khan /** 517155df0933SImran Khan * show_one_worker_pool - dump state of specified worker pool 517255df0933SImran Khan * @pool: worker pool whose state will be printed 517355df0933SImran Khan */ 517455df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool) 517555df0933SImran Khan { 51763494fc30STejun Heo struct worker *worker; 51773494fc30STejun Heo bool first = true; 517855df0933SImran Khan unsigned long flags; 5179335a42ebSPetr Mladek unsigned long hung = 0; 51803494fc30STejun Heo 5181a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 51823494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 51833494fc30STejun Heo goto next_pool; 5184335a42ebSPetr Mladek 5185335a42ebSPetr Mladek /* How long the first pending work is waiting for a worker. */ 5186335a42ebSPetr Mladek if (!list_empty(&pool->worklist)) 5187335a42ebSPetr Mladek hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; 5188335a42ebSPetr Mladek 518957116ce1SJohan Hovold /* 519057116ce1SJohan Hovold * Defer printing to avoid deadlocks in console drivers that 519157116ce1SJohan Hovold * queue work while holding locks also taken in their write 519257116ce1SJohan Hovold * paths. 519357116ce1SJohan Hovold */ 519457116ce1SJohan Hovold printk_deferred_enter(); 51953494fc30STejun Heo pr_info("pool %d:", pool->id); 51963494fc30STejun Heo pr_cont_pool_info(pool); 5197335a42ebSPetr Mladek pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); 51983494fc30STejun Heo if (pool->manager) 51993494fc30STejun Heo pr_cont(" manager: %d", 52003494fc30STejun Heo task_pid_nr(pool->manager->task)); 52013494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 52023494fc30STejun Heo pr_cont(" %s%d", first ? "idle: " : "", 52033494fc30STejun Heo task_pid_nr(worker->task)); 52043494fc30STejun Heo first = false; 52053494fc30STejun Heo } 52063494fc30STejun Heo pr_cont("\n"); 520757116ce1SJohan Hovold printk_deferred_exit(); 52083494fc30STejun Heo next_pool: 5209a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 521062635ea8SSergey Senozhatsky /* 521162635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 521255df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 521362635ea8SSergey Senozhatsky * hard lockup. 521462635ea8SSergey Senozhatsky */ 521562635ea8SSergey Senozhatsky touch_nmi_watchdog(); 521655df0933SImran Khan 52173494fc30STejun Heo } 52183494fc30STejun Heo 521955df0933SImran Khan /** 522055df0933SImran Khan * show_all_workqueues - dump workqueue state 522155df0933SImran Khan * 5222704bc669SJungseung Lee * Called from a sysrq handler and prints out all busy workqueues and pools. 522355df0933SImran Khan */ 522455df0933SImran Khan void show_all_workqueues(void) 522555df0933SImran Khan { 522655df0933SImran Khan struct workqueue_struct *wq; 522755df0933SImran Khan struct worker_pool *pool; 522855df0933SImran Khan int pi; 522955df0933SImran Khan 523055df0933SImran Khan rcu_read_lock(); 523155df0933SImran Khan 523255df0933SImran Khan pr_info("Showing busy workqueues and worker pools:\n"); 523355df0933SImran Khan 523455df0933SImran Khan list_for_each_entry_rcu(wq, &workqueues, list) 523555df0933SImran Khan show_one_workqueue(wq); 523655df0933SImran Khan 523755df0933SImran Khan for_each_pool(pool, pi) 523855df0933SImran Khan show_one_worker_pool(pool); 523955df0933SImran Khan 524024acfb71SThomas Gleixner rcu_read_unlock(); 52413494fc30STejun Heo } 52423494fc30STejun Heo 5243704bc669SJungseung Lee /** 5244704bc669SJungseung Lee * show_freezable_workqueues - dump freezable workqueue state 5245704bc669SJungseung Lee * 5246704bc669SJungseung Lee * Called from try_to_freeze_tasks() and prints out all freezable workqueues 5247704bc669SJungseung Lee * still busy. 5248704bc669SJungseung Lee */ 5249704bc669SJungseung Lee void show_freezable_workqueues(void) 5250704bc669SJungseung Lee { 5251704bc669SJungseung Lee struct workqueue_struct *wq; 5252704bc669SJungseung Lee 5253704bc669SJungseung Lee rcu_read_lock(); 5254704bc669SJungseung Lee 5255704bc669SJungseung Lee pr_info("Showing freezable workqueues that are still busy:\n"); 5256704bc669SJungseung Lee 5257704bc669SJungseung Lee list_for_each_entry_rcu(wq, &workqueues, list) { 5258704bc669SJungseung Lee if (!(wq->flags & WQ_FREEZABLE)) 5259704bc669SJungseung Lee continue; 5260704bc669SJungseung Lee show_one_workqueue(wq); 5261704bc669SJungseung Lee } 5262704bc669SJungseung Lee 5263704bc669SJungseung Lee rcu_read_unlock(); 5264704bc669SJungseung Lee } 5265704bc669SJungseung Lee 52666b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 52676b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 52686b59808bSTejun Heo { 52696b59808bSTejun Heo int off; 52706b59808bSTejun Heo 52716b59808bSTejun Heo /* always show the actual comm */ 52726b59808bSTejun Heo off = strscpy(buf, task->comm, size); 52736b59808bSTejun Heo if (off < 0) 52746b59808bSTejun Heo return; 52756b59808bSTejun Heo 5276197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 52776b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 52786b59808bSTejun Heo 5279197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 5280197f6accSTejun Heo struct worker *worker = kthread_data(task); 5281197f6accSTejun Heo struct worker_pool *pool = worker->pool; 52826b59808bSTejun Heo 52836b59808bSTejun Heo if (pool) { 5284a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 52856b59808bSTejun Heo /* 5286197f6accSTejun Heo * ->desc tracks information (wq name or 5287197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 5288197f6accSTejun Heo * current, prepend '+', otherwise '-'. 52896b59808bSTejun Heo */ 52906b59808bSTejun Heo if (worker->desc[0] != '\0') { 52916b59808bSTejun Heo if (worker->current_work) 52926b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 52936b59808bSTejun Heo worker->desc); 52946b59808bSTejun Heo else 52956b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 52966b59808bSTejun Heo worker->desc); 52976b59808bSTejun Heo } 5298a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 52996b59808bSTejun Heo } 5300197f6accSTejun Heo } 53016b59808bSTejun Heo 53026b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 53036b59808bSTejun Heo } 53046b59808bSTejun Heo 530566448bc2SMathieu Malaterre #ifdef CONFIG_SMP 530666448bc2SMathieu Malaterre 5307db7bccf4STejun Heo /* 5308db7bccf4STejun Heo * CPU hotplug. 5309db7bccf4STejun Heo * 5310e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 5311112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 5312706026c2STejun Heo * pool which make migrating pending and scheduled works very 5313e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 531494cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 5315e22bee78STejun Heo * blocked draining impractical. 5316e22bee78STejun Heo * 531724647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 5318628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 5319628c78e7STejun Heo * cpu comes back online. 5320db7bccf4STejun Heo */ 5321db7bccf4STejun Heo 5322e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 5323db7bccf4STejun Heo { 53244ce62e9eSTejun Heo struct worker_pool *pool; 5325db7bccf4STejun Heo struct worker *worker; 5326db7bccf4STejun Heo 5327f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 53281258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 5329a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 5330e22bee78STejun Heo 5331f2d5a0eeSTejun Heo /* 533292f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 533394cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 533411b45b0bSLai Jiangshan * must be on the cpu. After this, they may become diasporas. 5335b4ac9384SLai Jiangshan * And the preemption disabled section in their sched callbacks 5336b4ac9384SLai Jiangshan * are guaranteed to see WORKER_UNBOUND since the code here 5337b4ac9384SLai Jiangshan * is on the same cpu. 5338f2d5a0eeSTejun Heo */ 5339da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5340403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 5341db7bccf4STejun Heo 534224647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 5343f2d5a0eeSTejun Heo 5344e22bee78STejun Heo /* 5345989442d7SLai Jiangshan * The handling of nr_running in sched callbacks are disabled 5346989442d7SLai Jiangshan * now. Zap nr_running. After this, nr_running stays zero and 5347989442d7SLai Jiangshan * need_more_worker() and keep_working() are always true as 5348989442d7SLai Jiangshan * long as the worklist is not empty. This pool now behaves as 5349989442d7SLai Jiangshan * an unbound (in terms of concurrency management) pool which 5350eb283428SLai Jiangshan * are served by workers tied to the pool. 5351e22bee78STejun Heo */ 5352bc35f7efSLai Jiangshan pool->nr_running = 0; 5353eb283428SLai Jiangshan 5354eb283428SLai Jiangshan /* 5355eb283428SLai Jiangshan * With concurrency management just turned off, a busy 5356eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 5357eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 5358eb283428SLai Jiangshan */ 5359eb283428SLai Jiangshan wake_up_worker(pool); 5360989442d7SLai Jiangshan 5361a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 5362989442d7SLai Jiangshan 5363793777bcSValentin Schneider for_each_pool_worker(worker, pool) 5364793777bcSValentin Schneider unbind_worker(worker); 5365989442d7SLai Jiangshan 5366989442d7SLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 5367eb283428SLai Jiangshan } 5368db7bccf4STejun Heo } 5369db7bccf4STejun Heo 5370bd7c089eSTejun Heo /** 5371bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 5372bd7c089eSTejun Heo * @pool: pool of interest 5373bd7c089eSTejun Heo * 5374a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 5375bd7c089eSTejun Heo */ 5376bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 5377bd7c089eSTejun Heo { 5378a9ab775bSTejun Heo struct worker *worker; 5379bd7c089eSTejun Heo 53801258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 5381bd7c089eSTejun Heo 5382bd7c089eSTejun Heo /* 5383a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 5384a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 5385402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 5386a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 5387a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 5388bd7c089eSTejun Heo */ 5389c63a2e52SValentin Schneider for_each_pool_worker(worker, pool) { 5390c63a2e52SValentin Schneider kthread_set_per_cpu(worker->task, pool->cpu); 5391c63a2e52SValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 5392c63a2e52SValentin Schneider pool->attrs->cpumask) < 0); 5393c63a2e52SValentin Schneider } 5394a9ab775bSTejun Heo 5395a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 5396f7c17d26SWanpeng Li 53973de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 5398a9ab775bSTejun Heo 5399da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 5400a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 5401a9ab775bSTejun Heo 5402a9ab775bSTejun Heo /* 5403a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 5404a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 5405a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 5406a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 5407a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 5408a9ab775bSTejun Heo * concurrency management. Note that when or whether 5409a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 5410a9ab775bSTejun Heo * 5411c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 5412a9ab775bSTejun Heo * tested without holding any lock in 54136d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may 5414a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 5415a9ab775bSTejun Heo * management operations. 5416bd7c089eSTejun Heo */ 5417a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 5418a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 5419a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 5420c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 5421bd7c089eSTejun Heo } 5422a9ab775bSTejun Heo 5423a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 5424bd7c089eSTejun Heo } 5425bd7c089eSTejun Heo 54267dbc725eSTejun Heo /** 54277dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 54287dbc725eSTejun Heo * @pool: unbound pool of interest 54297dbc725eSTejun Heo * @cpu: the CPU which is coming up 54307dbc725eSTejun Heo * 54317dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 54327dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 54337dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 54347dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 54357dbc725eSTejun Heo */ 54367dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 54377dbc725eSTejun Heo { 54387dbc725eSTejun Heo static cpumask_t cpumask; 54397dbc725eSTejun Heo struct worker *worker; 54407dbc725eSTejun Heo 54411258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 54427dbc725eSTejun Heo 54437dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 54447dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 54457dbc725eSTejun Heo return; 54467dbc725eSTejun Heo 54477dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 54487dbc725eSTejun Heo 54497dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 5450da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5451d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 54527dbc725eSTejun Heo } 54537dbc725eSTejun Heo 54547ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 54551da177e4SLinus Torvalds { 54564ce62e9eSTejun Heo struct worker_pool *pool; 54571da177e4SLinus Torvalds 5458f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 54593ce63377STejun Heo if (pool->nr_workers) 54603ce63377STejun Heo continue; 5461051e1850SLai Jiangshan if (!create_worker(pool)) 54627ee681b2SThomas Gleixner return -ENOMEM; 54633af24433SOleg Nesterov } 54647ee681b2SThomas Gleixner return 0; 54657ee681b2SThomas Gleixner } 54661da177e4SLinus Torvalds 54677ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 54687ee681b2SThomas Gleixner { 54697ee681b2SThomas Gleixner struct worker_pool *pool; 54707ee681b2SThomas Gleixner struct workqueue_struct *wq; 54717ee681b2SThomas Gleixner int pi; 54727ee681b2SThomas Gleixner 547368e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 54747dbc725eSTejun Heo 54757dbc725eSTejun Heo for_each_pool(pool, pi) { 54761258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 547794cf58bbSTejun Heo 5478f05b558dSLai Jiangshan if (pool->cpu == cpu) 547994cf58bbSTejun Heo rebind_workers(pool); 5480f05b558dSLai Jiangshan else if (pool->cpu < 0) 54817dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 548294cf58bbSTejun Heo 54831258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 548494cf58bbSTejun Heo } 54857dbc725eSTejun Heo 5486fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 54874cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 548884193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 548984193c07STejun Heo 549084193c07STejun Heo if (attrs) { 549184193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 54924cbfd3deSTejun Heo int tcpu; 54934cbfd3deSTejun Heo 549484193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 5495fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, true); 54964cbfd3deSTejun Heo } 54974cbfd3deSTejun Heo } 54984c16bd32STejun Heo 549968e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 55007ee681b2SThomas Gleixner return 0; 550165758202STejun Heo } 550265758202STejun Heo 55037ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 550465758202STejun Heo { 55054c16bd32STejun Heo struct workqueue_struct *wq; 55068db25e78STejun Heo 55074c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 5508e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 5509e8b3f8dbSLai Jiangshan return -1; 5510e8b3f8dbSLai Jiangshan 5511e8b3f8dbSLai Jiangshan unbind_workers(cpu); 55124c16bd32STejun Heo 5513fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 55144c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 55154cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 551684193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 551784193c07STejun Heo 551884193c07STejun Heo if (attrs) { 551984193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 55204cbfd3deSTejun Heo int tcpu; 55214cbfd3deSTejun Heo 552284193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 5523fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, false); 55244cbfd3deSTejun Heo } 55254cbfd3deSTejun Heo } 55264c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 55274c16bd32STejun Heo 55287ee681b2SThomas Gleixner return 0; 552965758202STejun Heo } 553065758202STejun Heo 55312d3854a3SRusty Russell struct work_for_cpu { 5532ed48ece2STejun Heo struct work_struct work; 55332d3854a3SRusty Russell long (*fn)(void *); 55342d3854a3SRusty Russell void *arg; 55352d3854a3SRusty Russell long ret; 55362d3854a3SRusty Russell }; 55372d3854a3SRusty Russell 5538ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 55392d3854a3SRusty Russell { 5540ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 5541ed48ece2STejun Heo 55422d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 55432d3854a3SRusty Russell } 55442d3854a3SRusty Russell 55452d3854a3SRusty Russell /** 554622aceb31SAnna-Maria Gleixner * work_on_cpu - run a function in thread context on a particular cpu 55472d3854a3SRusty Russell * @cpu: the cpu to run on 55482d3854a3SRusty Russell * @fn: the function to run 55492d3854a3SRusty Russell * @arg: the function arg 55502d3854a3SRusty Russell * 555131ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 55526b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 5553d185af30SYacine Belkadi * 5554d185af30SYacine Belkadi * Return: The value @fn returns. 55552d3854a3SRusty Russell */ 5556d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg) 55572d3854a3SRusty Russell { 5558ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 55592d3854a3SRusty Russell 5560ed48ece2STejun Heo INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn); 5561ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 556212997d1aSBjorn Helgaas flush_work(&wfc.work); 5563440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 55642d3854a3SRusty Russell return wfc.ret; 55652d3854a3SRusty Russell } 55662d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 55670e8d6a93SThomas Gleixner 55680e8d6a93SThomas Gleixner /** 55690e8d6a93SThomas Gleixner * work_on_cpu_safe - run a function in thread context on a particular cpu 55700e8d6a93SThomas Gleixner * @cpu: the cpu to run on 55710e8d6a93SThomas Gleixner * @fn: the function to run 55720e8d6a93SThomas Gleixner * @arg: the function argument 55730e8d6a93SThomas Gleixner * 55740e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 55750e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 55760e8d6a93SThomas Gleixner * 55770e8d6a93SThomas Gleixner * Return: The value @fn returns. 55780e8d6a93SThomas Gleixner */ 55790e8d6a93SThomas Gleixner long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg) 55800e8d6a93SThomas Gleixner { 55810e8d6a93SThomas Gleixner long ret = -ENODEV; 55820e8d6a93SThomas Gleixner 5583ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 55840e8d6a93SThomas Gleixner if (cpu_online(cpu)) 55850e8d6a93SThomas Gleixner ret = work_on_cpu(cpu, fn, arg); 5586ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 55870e8d6a93SThomas Gleixner return ret; 55880e8d6a93SThomas Gleixner } 55890e8d6a93SThomas Gleixner EXPORT_SYMBOL_GPL(work_on_cpu_safe); 55902d3854a3SRusty Russell #endif /* CONFIG_SMP */ 55912d3854a3SRusty Russell 5592a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 5593e7577c50SRusty Russell 5594a0a1a5fdSTejun Heo /** 5595a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 5596a0a1a5fdSTejun Heo * 559758a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 5598f97a4a1aSLai Jiangshan * workqueues will queue new works to their inactive_works list instead of 5599706026c2STejun Heo * pool->worklist. 5600a0a1a5fdSTejun Heo * 5601a0a1a5fdSTejun Heo * CONTEXT: 5602a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5603a0a1a5fdSTejun Heo */ 5604a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 5605a0a1a5fdSTejun Heo { 560624b8a847STejun Heo struct workqueue_struct *wq; 560724b8a847STejun Heo struct pool_workqueue *pwq; 5608a0a1a5fdSTejun Heo 560968e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5610a0a1a5fdSTejun Heo 56116183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 5612a0a1a5fdSTejun Heo workqueue_freezing = true; 5613a0a1a5fdSTejun Heo 561424b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 5615a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5616699ce097STejun Heo for_each_pwq(pwq, wq) 5617699ce097STejun Heo pwq_adjust_max_active(pwq); 5618a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5619a1056305STejun Heo } 56205bcab335STejun Heo 562168e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5622a0a1a5fdSTejun Heo } 5623a0a1a5fdSTejun Heo 5624a0a1a5fdSTejun Heo /** 562558a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 5626a0a1a5fdSTejun Heo * 5627a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 5628a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 5629a0a1a5fdSTejun Heo * 5630a0a1a5fdSTejun Heo * CONTEXT: 563168e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 5632a0a1a5fdSTejun Heo * 5633d185af30SYacine Belkadi * Return: 563458a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 563558a69cb4STejun Heo * is complete. 5636a0a1a5fdSTejun Heo */ 5637a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 5638a0a1a5fdSTejun Heo { 5639a0a1a5fdSTejun Heo bool busy = false; 564024b8a847STejun Heo struct workqueue_struct *wq; 564124b8a847STejun Heo struct pool_workqueue *pwq; 5642a0a1a5fdSTejun Heo 564368e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5644a0a1a5fdSTejun Heo 56456183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 5646a0a1a5fdSTejun Heo 564724b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 564824b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 564924b8a847STejun Heo continue; 5650a0a1a5fdSTejun Heo /* 5651a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 5652a0a1a5fdSTejun Heo * to peek without lock. 5653a0a1a5fdSTejun Heo */ 565424acfb71SThomas Gleixner rcu_read_lock(); 565524b8a847STejun Heo for_each_pwq(pwq, wq) { 56566183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 5657112202d9STejun Heo if (pwq->nr_active) { 5658a0a1a5fdSTejun Heo busy = true; 565924acfb71SThomas Gleixner rcu_read_unlock(); 5660a0a1a5fdSTejun Heo goto out_unlock; 5661a0a1a5fdSTejun Heo } 5662a0a1a5fdSTejun Heo } 566324acfb71SThomas Gleixner rcu_read_unlock(); 5664a0a1a5fdSTejun Heo } 5665a0a1a5fdSTejun Heo out_unlock: 566668e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5667a0a1a5fdSTejun Heo return busy; 5668a0a1a5fdSTejun Heo } 5669a0a1a5fdSTejun Heo 5670a0a1a5fdSTejun Heo /** 5671a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 5672a0a1a5fdSTejun Heo * 5673a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 5674706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 5675a0a1a5fdSTejun Heo * 5676a0a1a5fdSTejun Heo * CONTEXT: 5677a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5678a0a1a5fdSTejun Heo */ 5679a0a1a5fdSTejun Heo void thaw_workqueues(void) 5680a0a1a5fdSTejun Heo { 568124b8a847STejun Heo struct workqueue_struct *wq; 568224b8a847STejun Heo struct pool_workqueue *pwq; 5683a0a1a5fdSTejun Heo 568468e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5685a0a1a5fdSTejun Heo 5686a0a1a5fdSTejun Heo if (!workqueue_freezing) 5687a0a1a5fdSTejun Heo goto out_unlock; 5688a0a1a5fdSTejun Heo 568974b414eaSLai Jiangshan workqueue_freezing = false; 569024b8a847STejun Heo 569124b8a847STejun Heo /* restore max_active and repopulate worklist */ 569224b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 5693a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5694699ce097STejun Heo for_each_pwq(pwq, wq) 5695699ce097STejun Heo pwq_adjust_max_active(pwq); 5696a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 569724b8a847STejun Heo } 569824b8a847STejun Heo 5699a0a1a5fdSTejun Heo out_unlock: 570068e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5701a0a1a5fdSTejun Heo } 5702a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 5703a0a1a5fdSTejun Heo 570499c621efSLai Jiangshan static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) 5705042f7df1SLai Jiangshan { 5706042f7df1SLai Jiangshan LIST_HEAD(ctxs); 5707042f7df1SLai Jiangshan int ret = 0; 5708042f7df1SLai Jiangshan struct workqueue_struct *wq; 5709042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 5710042f7df1SLai Jiangshan 5711042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5712042f7df1SLai Jiangshan 5713042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 5714042f7df1SLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 5715042f7df1SLai Jiangshan continue; 5716042f7df1SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 5717042f7df1SLai Jiangshan if (wq->flags & __WQ_ORDERED) 5718042f7df1SLai Jiangshan continue; 5719042f7df1SLai Jiangshan 572099c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); 572184193c07STejun Heo if (IS_ERR(ctx)) { 572284193c07STejun Heo ret = PTR_ERR(ctx); 5723042f7df1SLai Jiangshan break; 5724042f7df1SLai Jiangshan } 5725042f7df1SLai Jiangshan 5726042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 5727042f7df1SLai Jiangshan } 5728042f7df1SLai Jiangshan 5729042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 5730042f7df1SLai Jiangshan if (!ret) 5731042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 5732042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 5733042f7df1SLai Jiangshan } 5734042f7df1SLai Jiangshan 573599c621efSLai Jiangshan if (!ret) { 573699c621efSLai Jiangshan mutex_lock(&wq_pool_attach_mutex); 573799c621efSLai Jiangshan cpumask_copy(wq_unbound_cpumask, unbound_cpumask); 573899c621efSLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 573999c621efSLai Jiangshan } 5740042f7df1SLai Jiangshan return ret; 5741042f7df1SLai Jiangshan } 5742042f7df1SLai Jiangshan 5743042f7df1SLai Jiangshan /** 5744042f7df1SLai Jiangshan * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 5745042f7df1SLai Jiangshan * @cpumask: the cpumask to set 5746042f7df1SLai Jiangshan * 5747042f7df1SLai Jiangshan * The low-level workqueues cpumask is a global cpumask that limits 5748042f7df1SLai Jiangshan * the affinity of all unbound workqueues. This function check the @cpumask 5749042f7df1SLai Jiangshan * and apply it to all unbound workqueues and updates all pwqs of them. 5750042f7df1SLai Jiangshan * 575167dc8325SCai Huoqing * Return: 0 - Success 5752042f7df1SLai Jiangshan * -EINVAL - Invalid @cpumask 5753042f7df1SLai Jiangshan * -ENOMEM - Failed to allocate memory for attrs or pwqs. 5754042f7df1SLai Jiangshan */ 5755042f7df1SLai Jiangshan int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 5756042f7df1SLai Jiangshan { 5757042f7df1SLai Jiangshan int ret = -EINVAL; 5758042f7df1SLai Jiangshan 5759c98a9805STal Shorer /* 5760c98a9805STal Shorer * Not excluding isolated cpus on purpose. 5761c98a9805STal Shorer * If the user wishes to include them, we allow that. 5762c98a9805STal Shorer */ 5763042f7df1SLai Jiangshan cpumask_and(cpumask, cpumask, cpu_possible_mask); 5764042f7df1SLai Jiangshan if (!cpumask_empty(cpumask)) { 5765a0111cf6SLai Jiangshan apply_wqattrs_lock(); 5766d25302e4SMenglong Dong if (cpumask_equal(cpumask, wq_unbound_cpumask)) { 5767d25302e4SMenglong Dong ret = 0; 5768d25302e4SMenglong Dong goto out_unlock; 5769d25302e4SMenglong Dong } 5770d25302e4SMenglong Dong 577199c621efSLai Jiangshan ret = workqueue_apply_unbound_cpumask(cpumask); 5772042f7df1SLai Jiangshan 5773d25302e4SMenglong Dong out_unlock: 5774a0111cf6SLai Jiangshan apply_wqattrs_unlock(); 5775042f7df1SLai Jiangshan } 5776042f7df1SLai Jiangshan 5777042f7df1SLai Jiangshan return ret; 5778042f7df1SLai Jiangshan } 5779042f7df1SLai Jiangshan 57806ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 57816ba94429SFrederic Weisbecker /* 57826ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 57836ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 57846ba94429SFrederic Weisbecker * following attributes. 57856ba94429SFrederic Weisbecker * 57866ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 57876ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 57886ba94429SFrederic Weisbecker * 57896ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 57906ba94429SFrederic Weisbecker * 57916ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 57926ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 57936ba94429SFrederic Weisbecker */ 57946ba94429SFrederic Weisbecker struct wq_device { 57956ba94429SFrederic Weisbecker struct workqueue_struct *wq; 57966ba94429SFrederic Weisbecker struct device dev; 57976ba94429SFrederic Weisbecker }; 57986ba94429SFrederic Weisbecker 57996ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 58006ba94429SFrederic Weisbecker { 58016ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 58026ba94429SFrederic Weisbecker 58036ba94429SFrederic Weisbecker return wq_dev->wq; 58046ba94429SFrederic Weisbecker } 58056ba94429SFrederic Weisbecker 58066ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 58076ba94429SFrederic Weisbecker char *buf) 58086ba94429SFrederic Weisbecker { 58096ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 58106ba94429SFrederic Weisbecker 58116ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 58126ba94429SFrederic Weisbecker } 58136ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 58146ba94429SFrederic Weisbecker 58156ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 58166ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 58176ba94429SFrederic Weisbecker { 58186ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 58196ba94429SFrederic Weisbecker 58206ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 58216ba94429SFrederic Weisbecker } 58226ba94429SFrederic Weisbecker 58236ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 58246ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 58256ba94429SFrederic Weisbecker size_t count) 58266ba94429SFrederic Weisbecker { 58276ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 58286ba94429SFrederic Weisbecker int val; 58296ba94429SFrederic Weisbecker 58306ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 58316ba94429SFrederic Weisbecker return -EINVAL; 58326ba94429SFrederic Weisbecker 58336ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 58346ba94429SFrederic Weisbecker return count; 58356ba94429SFrederic Weisbecker } 58366ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 58376ba94429SFrederic Weisbecker 58386ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 58396ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 58406ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 58416ba94429SFrederic Weisbecker NULL, 58426ba94429SFrederic Weisbecker }; 58436ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 58446ba94429SFrederic Weisbecker 58456ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 58466ba94429SFrederic Weisbecker char *buf) 58476ba94429SFrederic Weisbecker { 58486ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 58496ba94429SFrederic Weisbecker int written; 58506ba94429SFrederic Weisbecker 58516ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 58526ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 58536ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 58546ba94429SFrederic Weisbecker 58556ba94429SFrederic Weisbecker return written; 58566ba94429SFrederic Weisbecker } 58576ba94429SFrederic Weisbecker 58586ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 58596ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 58606ba94429SFrederic Weisbecker { 58616ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 58626ba94429SFrederic Weisbecker 5863899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5864899a94feSLai Jiangshan 5865be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs(); 58666ba94429SFrederic Weisbecker if (!attrs) 58676ba94429SFrederic Weisbecker return NULL; 58686ba94429SFrederic Weisbecker 58696ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 58706ba94429SFrederic Weisbecker return attrs; 58716ba94429SFrederic Weisbecker } 58726ba94429SFrederic Weisbecker 58736ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 58746ba94429SFrederic Weisbecker const char *buf, size_t count) 58756ba94429SFrederic Weisbecker { 58766ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 58776ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5878d4d3e257SLai Jiangshan int ret = -ENOMEM; 5879d4d3e257SLai Jiangshan 5880d4d3e257SLai Jiangshan apply_wqattrs_lock(); 58816ba94429SFrederic Weisbecker 58826ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 58836ba94429SFrederic Weisbecker if (!attrs) 5884d4d3e257SLai Jiangshan goto out_unlock; 58856ba94429SFrederic Weisbecker 58866ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 58876ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 5888d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 58896ba94429SFrederic Weisbecker else 58906ba94429SFrederic Weisbecker ret = -EINVAL; 58916ba94429SFrederic Weisbecker 5892d4d3e257SLai Jiangshan out_unlock: 5893d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 58946ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 58956ba94429SFrederic Weisbecker return ret ?: count; 58966ba94429SFrederic Weisbecker } 58976ba94429SFrederic Weisbecker 58986ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 58996ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 59006ba94429SFrederic Weisbecker { 59016ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 59026ba94429SFrederic Weisbecker int written; 59036ba94429SFrederic Weisbecker 59046ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 59056ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 59066ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 59076ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 59086ba94429SFrederic Weisbecker return written; 59096ba94429SFrederic Weisbecker } 59106ba94429SFrederic Weisbecker 59116ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 59126ba94429SFrederic Weisbecker struct device_attribute *attr, 59136ba94429SFrederic Weisbecker const char *buf, size_t count) 59146ba94429SFrederic Weisbecker { 59156ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 59166ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5917d4d3e257SLai Jiangshan int ret = -ENOMEM; 5918d4d3e257SLai Jiangshan 5919d4d3e257SLai Jiangshan apply_wqattrs_lock(); 59206ba94429SFrederic Weisbecker 59216ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 59226ba94429SFrederic Weisbecker if (!attrs) 5923d4d3e257SLai Jiangshan goto out_unlock; 59246ba94429SFrederic Weisbecker 59256ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 59266ba94429SFrederic Weisbecker if (!ret) 5927d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 59286ba94429SFrederic Weisbecker 5929d4d3e257SLai Jiangshan out_unlock: 5930d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 59316ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 59326ba94429SFrederic Weisbecker return ret ?: count; 59336ba94429SFrederic Weisbecker } 59346ba94429SFrederic Weisbecker 59356ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 59366ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 59376ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 59386ba94429SFrederic Weisbecker __ATTR_NULL, 59396ba94429SFrederic Weisbecker }; 59406ba94429SFrederic Weisbecker 59416ba94429SFrederic Weisbecker static struct bus_type wq_subsys = { 59426ba94429SFrederic Weisbecker .name = "workqueue", 59436ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 59446ba94429SFrederic Weisbecker }; 59456ba94429SFrederic Weisbecker 5946b05a7928SFrederic Weisbecker static ssize_t wq_unbound_cpumask_show(struct device *dev, 5947b05a7928SFrederic Weisbecker struct device_attribute *attr, char *buf) 5948b05a7928SFrederic Weisbecker { 5949b05a7928SFrederic Weisbecker int written; 5950b05a7928SFrederic Weisbecker 5951042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 5952b05a7928SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 5953b05a7928SFrederic Weisbecker cpumask_pr_args(wq_unbound_cpumask)); 5954042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5955b05a7928SFrederic Weisbecker 5956b05a7928SFrederic Weisbecker return written; 5957b05a7928SFrederic Weisbecker } 5958b05a7928SFrederic Weisbecker 5959042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 5960042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 5961042f7df1SLai Jiangshan { 5962042f7df1SLai Jiangshan cpumask_var_t cpumask; 5963042f7df1SLai Jiangshan int ret; 5964042f7df1SLai Jiangshan 5965042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 5966042f7df1SLai Jiangshan return -ENOMEM; 5967042f7df1SLai Jiangshan 5968042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 5969042f7df1SLai Jiangshan if (!ret) 5970042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 5971042f7df1SLai Jiangshan 5972042f7df1SLai Jiangshan free_cpumask_var(cpumask); 5973042f7df1SLai Jiangshan return ret ? ret : count; 5974042f7df1SLai Jiangshan } 5975042f7df1SLai Jiangshan 5976b05a7928SFrederic Weisbecker static struct device_attribute wq_sysfs_cpumask_attr = 5977042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 5978042f7df1SLai Jiangshan wq_unbound_cpumask_store); 5979b05a7928SFrederic Weisbecker 59806ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 59816ba94429SFrederic Weisbecker { 5982686f6697SGreg Kroah-Hartman struct device *dev_root; 5983b05a7928SFrederic Weisbecker int err; 5984b05a7928SFrederic Weisbecker 5985b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 5986b05a7928SFrederic Weisbecker if (err) 5987b05a7928SFrederic Weisbecker return err; 5988b05a7928SFrederic Weisbecker 5989686f6697SGreg Kroah-Hartman dev_root = bus_get_dev_root(&wq_subsys); 5990686f6697SGreg Kroah-Hartman if (dev_root) { 5991686f6697SGreg Kroah-Hartman err = device_create_file(dev_root, &wq_sysfs_cpumask_attr); 5992686f6697SGreg Kroah-Hartman put_device(dev_root); 5993686f6697SGreg Kroah-Hartman } 5994686f6697SGreg Kroah-Hartman return err; 59956ba94429SFrederic Weisbecker } 59966ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 59976ba94429SFrederic Weisbecker 59986ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 59996ba94429SFrederic Weisbecker { 60006ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 60016ba94429SFrederic Weisbecker 60026ba94429SFrederic Weisbecker kfree(wq_dev); 60036ba94429SFrederic Weisbecker } 60046ba94429SFrederic Weisbecker 60056ba94429SFrederic Weisbecker /** 60066ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 60076ba94429SFrederic Weisbecker * @wq: the workqueue to register 60086ba94429SFrederic Weisbecker * 60096ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 60106ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 60116ba94429SFrederic Weisbecker * which is the preferred method. 60126ba94429SFrederic Weisbecker * 60136ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 60146ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 60156ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 60166ba94429SFrederic Weisbecker * attributes. 60176ba94429SFrederic Weisbecker * 60186ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 60196ba94429SFrederic Weisbecker */ 60206ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 60216ba94429SFrederic Weisbecker { 60226ba94429SFrederic Weisbecker struct wq_device *wq_dev; 60236ba94429SFrederic Weisbecker int ret; 60246ba94429SFrederic Weisbecker 60256ba94429SFrederic Weisbecker /* 6026402dd89dSShailendra Verma * Adjusting max_active or creating new pwqs by applying 60276ba94429SFrederic Weisbecker * attributes breaks ordering guarantee. Disallow exposing ordered 60286ba94429SFrederic Weisbecker * workqueues. 60296ba94429SFrederic Weisbecker */ 60300a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 60316ba94429SFrederic Weisbecker return -EINVAL; 60326ba94429SFrederic Weisbecker 60336ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 60346ba94429SFrederic Weisbecker if (!wq_dev) 60356ba94429SFrederic Weisbecker return -ENOMEM; 60366ba94429SFrederic Weisbecker 60376ba94429SFrederic Weisbecker wq_dev->wq = wq; 60386ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 60396ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 604023217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 60416ba94429SFrederic Weisbecker 60426ba94429SFrederic Weisbecker /* 60436ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 60446ba94429SFrederic Weisbecker * everything is ready. 60456ba94429SFrederic Weisbecker */ 60466ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 60476ba94429SFrederic Weisbecker 60486ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 60496ba94429SFrederic Weisbecker if (ret) { 6050537f4146SArvind Yadav put_device(&wq_dev->dev); 60516ba94429SFrederic Weisbecker wq->wq_dev = NULL; 60526ba94429SFrederic Weisbecker return ret; 60536ba94429SFrederic Weisbecker } 60546ba94429SFrederic Weisbecker 60556ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 60566ba94429SFrederic Weisbecker struct device_attribute *attr; 60576ba94429SFrederic Weisbecker 60586ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 60596ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 60606ba94429SFrederic Weisbecker if (ret) { 60616ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 60626ba94429SFrederic Weisbecker wq->wq_dev = NULL; 60636ba94429SFrederic Weisbecker return ret; 60646ba94429SFrederic Weisbecker } 60656ba94429SFrederic Weisbecker } 60666ba94429SFrederic Weisbecker } 60676ba94429SFrederic Weisbecker 60686ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 60696ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 60706ba94429SFrederic Weisbecker return 0; 60716ba94429SFrederic Weisbecker } 60726ba94429SFrederic Weisbecker 60736ba94429SFrederic Weisbecker /** 60746ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 60756ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 60766ba94429SFrederic Weisbecker * 60776ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 60786ba94429SFrederic Weisbecker */ 60796ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 60806ba94429SFrederic Weisbecker { 60816ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 60826ba94429SFrederic Weisbecker 60836ba94429SFrederic Weisbecker if (!wq->wq_dev) 60846ba94429SFrederic Weisbecker return; 60856ba94429SFrederic Weisbecker 60866ba94429SFrederic Weisbecker wq->wq_dev = NULL; 60876ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 60886ba94429SFrederic Weisbecker } 60896ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 60906ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 60916ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 60926ba94429SFrederic Weisbecker 609382607adcSTejun Heo /* 609482607adcSTejun Heo * Workqueue watchdog. 609582607adcSTejun Heo * 609682607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 609782607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 609882607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 609982607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 610082607adcSTejun Heo * largely opaque. 610182607adcSTejun Heo * 610282607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 610382607adcSTejun Heo * state if some pools failed to make forward progress for a while where 610482607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 610582607adcSTejun Heo * 610682607adcSTejun Heo * This mechanism is controlled through the kernel parameter 610782607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 610882607adcSTejun Heo * corresponding sysfs parameter file. 610982607adcSTejun Heo */ 611082607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 611182607adcSTejun Heo 611282607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 61135cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 611482607adcSTejun Heo 611582607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 611682607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 611782607adcSTejun Heo 6118cd2440d6SPetr Mladek /* 6119cd2440d6SPetr Mladek * Show workers that might prevent the processing of pending work items. 6120cd2440d6SPetr Mladek * The only candidates are CPU-bound workers in the running state. 6121cd2440d6SPetr Mladek * Pending work items should be handled by another idle worker 6122cd2440d6SPetr Mladek * in all other situations. 6123cd2440d6SPetr Mladek */ 6124cd2440d6SPetr Mladek static void show_cpu_pool_hog(struct worker_pool *pool) 6125cd2440d6SPetr Mladek { 6126cd2440d6SPetr Mladek struct worker *worker; 6127cd2440d6SPetr Mladek unsigned long flags; 6128cd2440d6SPetr Mladek int bkt; 6129cd2440d6SPetr Mladek 6130cd2440d6SPetr Mladek raw_spin_lock_irqsave(&pool->lock, flags); 6131cd2440d6SPetr Mladek 6132cd2440d6SPetr Mladek hash_for_each(pool->busy_hash, bkt, worker, hentry) { 6133cd2440d6SPetr Mladek if (task_is_running(worker->task)) { 6134cd2440d6SPetr Mladek /* 6135cd2440d6SPetr Mladek * Defer printing to avoid deadlocks in console 6136cd2440d6SPetr Mladek * drivers that queue work while holding locks 6137cd2440d6SPetr Mladek * also taken in their write paths. 6138cd2440d6SPetr Mladek */ 6139cd2440d6SPetr Mladek printk_deferred_enter(); 6140cd2440d6SPetr Mladek 6141cd2440d6SPetr Mladek pr_info("pool %d:\n", pool->id); 6142cd2440d6SPetr Mladek sched_show_task(worker->task); 6143cd2440d6SPetr Mladek 6144cd2440d6SPetr Mladek printk_deferred_exit(); 6145cd2440d6SPetr Mladek } 6146cd2440d6SPetr Mladek } 6147cd2440d6SPetr Mladek 6148cd2440d6SPetr Mladek raw_spin_unlock_irqrestore(&pool->lock, flags); 6149cd2440d6SPetr Mladek } 6150cd2440d6SPetr Mladek 6151cd2440d6SPetr Mladek static void show_cpu_pools_hogs(void) 6152cd2440d6SPetr Mladek { 6153cd2440d6SPetr Mladek struct worker_pool *pool; 6154cd2440d6SPetr Mladek int pi; 6155cd2440d6SPetr Mladek 6156cd2440d6SPetr Mladek pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); 6157cd2440d6SPetr Mladek 6158cd2440d6SPetr Mladek rcu_read_lock(); 6159cd2440d6SPetr Mladek 6160cd2440d6SPetr Mladek for_each_pool(pool, pi) { 6161cd2440d6SPetr Mladek if (pool->cpu_stall) 6162cd2440d6SPetr Mladek show_cpu_pool_hog(pool); 6163cd2440d6SPetr Mladek 6164cd2440d6SPetr Mladek } 6165cd2440d6SPetr Mladek 6166cd2440d6SPetr Mladek rcu_read_unlock(); 6167cd2440d6SPetr Mladek } 6168cd2440d6SPetr Mladek 616982607adcSTejun Heo static void wq_watchdog_reset_touched(void) 617082607adcSTejun Heo { 617182607adcSTejun Heo int cpu; 617282607adcSTejun Heo 617382607adcSTejun Heo wq_watchdog_touched = jiffies; 617482607adcSTejun Heo for_each_possible_cpu(cpu) 617582607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 617682607adcSTejun Heo } 617782607adcSTejun Heo 61785cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 617982607adcSTejun Heo { 618082607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 618182607adcSTejun Heo bool lockup_detected = false; 6182cd2440d6SPetr Mladek bool cpu_pool_stall = false; 6183940d71c6SSergey Senozhatsky unsigned long now = jiffies; 618482607adcSTejun Heo struct worker_pool *pool; 618582607adcSTejun Heo int pi; 618682607adcSTejun Heo 618782607adcSTejun Heo if (!thresh) 618882607adcSTejun Heo return; 618982607adcSTejun Heo 619082607adcSTejun Heo rcu_read_lock(); 619182607adcSTejun Heo 619282607adcSTejun Heo for_each_pool(pool, pi) { 619382607adcSTejun Heo unsigned long pool_ts, touched, ts; 619482607adcSTejun Heo 6195cd2440d6SPetr Mladek pool->cpu_stall = false; 619682607adcSTejun Heo if (list_empty(&pool->worklist)) 619782607adcSTejun Heo continue; 619882607adcSTejun Heo 6199940d71c6SSergey Senozhatsky /* 6200940d71c6SSergey Senozhatsky * If a virtual machine is stopped by the host it can look to 6201940d71c6SSergey Senozhatsky * the watchdog like a stall. 6202940d71c6SSergey Senozhatsky */ 6203940d71c6SSergey Senozhatsky kvm_check_and_clear_guest_paused(); 6204940d71c6SSergey Senozhatsky 620582607adcSTejun Heo /* get the latest of pool and touched timestamps */ 620689e28ce6SWang Qing if (pool->cpu >= 0) 620789e28ce6SWang Qing touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); 620889e28ce6SWang Qing else 620982607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 621089e28ce6SWang Qing pool_ts = READ_ONCE(pool->watchdog_ts); 621182607adcSTejun Heo 621282607adcSTejun Heo if (time_after(pool_ts, touched)) 621382607adcSTejun Heo ts = pool_ts; 621482607adcSTejun Heo else 621582607adcSTejun Heo ts = touched; 621682607adcSTejun Heo 621782607adcSTejun Heo /* did we stall? */ 6218940d71c6SSergey Senozhatsky if (time_after(now, ts + thresh)) { 621982607adcSTejun Heo lockup_detected = true; 6220cd2440d6SPetr Mladek if (pool->cpu >= 0) { 6221cd2440d6SPetr Mladek pool->cpu_stall = true; 6222cd2440d6SPetr Mladek cpu_pool_stall = true; 6223cd2440d6SPetr Mladek } 622482607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 622582607adcSTejun Heo pr_cont_pool_info(pool); 622682607adcSTejun Heo pr_cont(" stuck for %us!\n", 6227940d71c6SSergey Senozhatsky jiffies_to_msecs(now - pool_ts) / 1000); 622882607adcSTejun Heo } 6229cd2440d6SPetr Mladek 6230cd2440d6SPetr Mladek 623182607adcSTejun Heo } 623282607adcSTejun Heo 623382607adcSTejun Heo rcu_read_unlock(); 623482607adcSTejun Heo 623582607adcSTejun Heo if (lockup_detected) 623655df0933SImran Khan show_all_workqueues(); 623782607adcSTejun Heo 6238cd2440d6SPetr Mladek if (cpu_pool_stall) 6239cd2440d6SPetr Mladek show_cpu_pools_hogs(); 6240cd2440d6SPetr Mladek 624182607adcSTejun Heo wq_watchdog_reset_touched(); 624282607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 624382607adcSTejun Heo } 624482607adcSTejun Heo 6245cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 624682607adcSTejun Heo { 624782607adcSTejun Heo if (cpu >= 0) 624882607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 624989e28ce6SWang Qing 625082607adcSTejun Heo wq_watchdog_touched = jiffies; 625182607adcSTejun Heo } 625282607adcSTejun Heo 625382607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 625482607adcSTejun Heo { 625582607adcSTejun Heo wq_watchdog_thresh = 0; 625682607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 625782607adcSTejun Heo 625882607adcSTejun Heo if (thresh) { 625982607adcSTejun Heo wq_watchdog_thresh = thresh; 626082607adcSTejun Heo wq_watchdog_reset_touched(); 626182607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 626282607adcSTejun Heo } 626382607adcSTejun Heo } 626482607adcSTejun Heo 626582607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 626682607adcSTejun Heo const struct kernel_param *kp) 626782607adcSTejun Heo { 626882607adcSTejun Heo unsigned long thresh; 626982607adcSTejun Heo int ret; 627082607adcSTejun Heo 627182607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 627282607adcSTejun Heo if (ret) 627382607adcSTejun Heo return ret; 627482607adcSTejun Heo 627582607adcSTejun Heo if (system_wq) 627682607adcSTejun Heo wq_watchdog_set_thresh(thresh); 627782607adcSTejun Heo else 627882607adcSTejun Heo wq_watchdog_thresh = thresh; 627982607adcSTejun Heo 628082607adcSTejun Heo return 0; 628182607adcSTejun Heo } 628282607adcSTejun Heo 628382607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 628482607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 628582607adcSTejun Heo .get = param_get_ulong, 628682607adcSTejun Heo }; 628782607adcSTejun Heo 628882607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 628982607adcSTejun Heo 0644); 629082607adcSTejun Heo 629182607adcSTejun Heo static void wq_watchdog_init(void) 629282607adcSTejun Heo { 62935cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 629482607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 629582607adcSTejun Heo } 629682607adcSTejun Heo 629782607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 629882607adcSTejun Heo 629982607adcSTejun Heo static inline void wq_watchdog_init(void) { } 630082607adcSTejun Heo 630182607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 630282607adcSTejun Heo 63033347fa09STejun Heo /** 63043347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 63053347fa09STejun Heo * 63062930155bSTejun Heo * This is the first step of three-staged workqueue subsystem initialization and 63072930155bSTejun Heo * invoked as soon as the bare basics - memory allocation, cpumasks and idr are 63082930155bSTejun Heo * up. It sets up all the data structures and system workqueues and allows early 63092930155bSTejun Heo * boot code to create workqueues and queue/cancel work items. Actual work item 63102930155bSTejun Heo * execution starts only after kthreads can be created and scheduled right 63112930155bSTejun Heo * before early initcalls. 63123347fa09STejun Heo */ 63132333e829SYu Chen void __init workqueue_init_early(void) 63141da177e4SLinus Torvalds { 631584193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 63167a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 63177a4e344cSTejun Heo int i, cpu; 6318c34056a3STejun Heo 631910cdb157SLai Jiangshan BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 6320e904e6c2STejun Heo 6321b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 632204d4e665SFrederic Weisbecker cpumask_copy(wq_unbound_cpumask, housekeeping_cpumask(HK_TYPE_WQ)); 632304d4e665SFrederic Weisbecker cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, housekeeping_cpumask(HK_TYPE_DOMAIN)); 6324b05a7928SFrederic Weisbecker 6325ace3c549Stiozhang if (!cpumask_empty(&wq_cmdline_cpumask)) 6326ace3c549Stiozhang cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, &wq_cmdline_cpumask); 6327ace3c549Stiozhang 6328e904e6c2STejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 6329e904e6c2STejun Heo 63302930155bSTejun Heo wq_update_pod_attrs_buf = alloc_workqueue_attrs(); 63312930155bSTejun Heo BUG_ON(!wq_update_pod_attrs_buf); 63322930155bSTejun Heo 63330f36ee24STejun Heo BUG_ON(!alloc_cpumask_var(&wq_update_pod_cpumask_buf, GFP_KERNEL)); 63340f36ee24STejun Heo 633584193c07STejun Heo /* initialize WQ_AFFN_SYSTEM pods */ 633684193c07STejun Heo pt->pod_cpus = kcalloc(1, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 633784193c07STejun Heo pt->pod_node = kcalloc(1, sizeof(pt->pod_node[0]), GFP_KERNEL); 633884193c07STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 633984193c07STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node || !pt->cpu_pod); 634084193c07STejun Heo 634184193c07STejun Heo BUG_ON(!zalloc_cpumask_var_node(&pt->pod_cpus[0], GFP_KERNEL, NUMA_NO_NODE)); 634284193c07STejun Heo 634384193c07STejun Heo wq_update_pod_attrs_buf = alloc_workqueue_attrs(); 634484193c07STejun Heo BUG_ON(!wq_update_pod_attrs_buf); 634584193c07STejun Heo 634684193c07STejun Heo pt->nr_pods = 1; 634784193c07STejun Heo cpumask_copy(pt->pod_cpus[0], cpu_possible_mask); 634884193c07STejun Heo pt->pod_node[0] = NUMA_NO_NODE; 634984193c07STejun Heo pt->cpu_pod[0] = 0; 635084193c07STejun Heo 6351706026c2STejun Heo /* initialize CPU pools */ 635229c91e99STejun Heo for_each_possible_cpu(cpu) { 63534ce62e9eSTejun Heo struct worker_pool *pool; 63548b03ae3cSTejun Heo 63557a4e344cSTejun Heo i = 0; 6356f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 63577a4e344cSTejun Heo BUG_ON(init_worker_pool(pool)); 6358ec22ca5eSTejun Heo pool->cpu = cpu; 63597a4e344cSTejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 63607a4e344cSTejun Heo pool->attrs->nice = std_nice[i++]; 6361f3f90ad4STejun Heo pool->node = cpu_to_node(cpu); 63627a4e344cSTejun Heo 63639daf9e67STejun Heo /* alloc pool ID */ 636468e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 63659daf9e67STejun Heo BUG_ON(worker_pool_assign_id(pool)); 636668e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 63674ce62e9eSTejun Heo } 63688b03ae3cSTejun Heo } 63698b03ae3cSTejun Heo 63708a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 637129c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 637229c91e99STejun Heo struct workqueue_attrs *attrs; 637329c91e99STejun Heo 6374be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 637529c91e99STejun Heo attrs->nice = std_nice[i]; 637629c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 63778a2b7538STejun Heo 63788a2b7538STejun Heo /* 63798a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 63808a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 63818a2b7538STejun Heo */ 6382be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 63838a2b7538STejun Heo attrs->nice = std_nice[i]; 6384af73f5c9STejun Heo attrs->ordered = true; 63858a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 638629c91e99STejun Heo } 638729c91e99STejun Heo 6388d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 63891aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 6390d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 6391f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 6392636b927eSTejun Heo WQ_MAX_ACTIVE); 639324d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 639424d51addSTejun Heo WQ_FREEZABLE, 0); 63950668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 63960668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 63970668106cSViresh Kumar system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient", 63980668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 63990668106cSViresh Kumar 0); 64001aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 64010668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 64020668106cSViresh Kumar !system_power_efficient_wq || 64030668106cSViresh Kumar !system_freezable_power_efficient_wq); 64043347fa09STejun Heo } 64053347fa09STejun Heo 6406aa6fde93STejun Heo static void __init wq_cpu_intensive_thresh_init(void) 6407aa6fde93STejun Heo { 6408aa6fde93STejun Heo unsigned long thresh; 6409aa6fde93STejun Heo unsigned long bogo; 6410aa6fde93STejun Heo 6411aa6fde93STejun Heo /* if the user set it to a specific value, keep it */ 6412aa6fde93STejun Heo if (wq_cpu_intensive_thresh_us != ULONG_MAX) 6413aa6fde93STejun Heo return; 6414aa6fde93STejun Heo 6415967b494eSTejun Heo pwq_release_worker = kthread_create_worker(0, "pool_workqueue_release"); 6416967b494eSTejun Heo BUG_ON(IS_ERR(pwq_release_worker)); 6417967b494eSTejun Heo 6418aa6fde93STejun Heo /* 6419aa6fde93STejun Heo * The default of 10ms is derived from the fact that most modern (as of 6420aa6fde93STejun Heo * 2023) processors can do a lot in 10ms and that it's just below what 6421aa6fde93STejun Heo * most consider human-perceivable. However, the kernel also runs on a 6422aa6fde93STejun Heo * lot slower CPUs including microcontrollers where the threshold is way 6423aa6fde93STejun Heo * too low. 6424aa6fde93STejun Heo * 6425aa6fde93STejun Heo * Let's scale up the threshold upto 1 second if BogoMips is below 4000. 6426aa6fde93STejun Heo * This is by no means accurate but it doesn't have to be. The mechanism 6427aa6fde93STejun Heo * is still useful even when the threshold is fully scaled up. Also, as 6428aa6fde93STejun Heo * the reports would usually be applicable to everyone, some machines 6429aa6fde93STejun Heo * operating on longer thresholds won't significantly diminish their 6430aa6fde93STejun Heo * usefulness. 6431aa6fde93STejun Heo */ 6432aa6fde93STejun Heo thresh = 10 * USEC_PER_MSEC; 6433aa6fde93STejun Heo 6434aa6fde93STejun Heo /* see init/calibrate.c for lpj -> BogoMIPS calculation */ 6435aa6fde93STejun Heo bogo = max_t(unsigned long, loops_per_jiffy / 500000 * HZ, 1); 6436aa6fde93STejun Heo if (bogo < 4000) 6437aa6fde93STejun Heo thresh = min_t(unsigned long, thresh * 4000 / bogo, USEC_PER_SEC); 6438aa6fde93STejun Heo 6439aa6fde93STejun Heo pr_debug("wq_cpu_intensive_thresh: lpj=%lu BogoMIPS=%lu thresh_us=%lu\n", 6440aa6fde93STejun Heo loops_per_jiffy, bogo, thresh); 6441aa6fde93STejun Heo 6442aa6fde93STejun Heo wq_cpu_intensive_thresh_us = thresh; 6443aa6fde93STejun Heo } 6444aa6fde93STejun Heo 64453347fa09STejun Heo /** 64463347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 64473347fa09STejun Heo * 64482930155bSTejun Heo * This is the second step of three-staged workqueue subsystem initialization 64492930155bSTejun Heo * and invoked as soon as kthreads can be created and scheduled. Workqueues have 64502930155bSTejun Heo * been created and work items queued on them, but there are no kworkers 64512930155bSTejun Heo * executing the work items yet. Populate the worker pools with the initial 64522930155bSTejun Heo * workers and enable future kworker creations. 64533347fa09STejun Heo */ 64542333e829SYu Chen void __init workqueue_init(void) 64553347fa09STejun Heo { 64562186d9f9STejun Heo struct workqueue_struct *wq; 64573347fa09STejun Heo struct worker_pool *pool; 64583347fa09STejun Heo int cpu, bkt; 64593347fa09STejun Heo 6460aa6fde93STejun Heo wq_cpu_intensive_thresh_init(); 6461aa6fde93STejun Heo 64622186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 64632186d9f9STejun Heo 64642930155bSTejun Heo /* 64652930155bSTejun Heo * Per-cpu pools created earlier could be missing node hint. Fix them 64662930155bSTejun Heo * up. Also, create a rescuer for workqueues that requested it. 64672930155bSTejun Heo */ 64682186d9f9STejun Heo for_each_possible_cpu(cpu) { 64692186d9f9STejun Heo for_each_cpu_worker_pool(pool, cpu) { 64702186d9f9STejun Heo pool->node = cpu_to_node(cpu); 64712186d9f9STejun Heo } 64722186d9f9STejun Heo } 64732186d9f9STejun Heo 647440c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 647540c17f75STejun Heo WARN(init_rescuer(wq), 647640c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 647740c17f75STejun Heo wq->name); 647840c17f75STejun Heo } 64792186d9f9STejun Heo 64802186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 64812186d9f9STejun Heo 64823347fa09STejun Heo /* create the initial workers */ 64833347fa09STejun Heo for_each_online_cpu(cpu) { 64843347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 64853347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 64863347fa09STejun Heo BUG_ON(!create_worker(pool)); 64873347fa09STejun Heo } 64883347fa09STejun Heo } 64893347fa09STejun Heo 64903347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 64913347fa09STejun Heo BUG_ON(!create_worker(pool)); 64923347fa09STejun Heo 64933347fa09STejun Heo wq_online = true; 649482607adcSTejun Heo wq_watchdog_init(); 64951da177e4SLinus Torvalds } 6496c4f135d6STetsuo Handa 6497*025e1684STejun Heo /* 6498*025e1684STejun Heo * Initialize @pt by first initializing @pt->cpu_pod[] with pod IDs according to 6499*025e1684STejun Heo * @cpu_shares_pod(). Each subset of CPUs that share a pod is assigned a unique 6500*025e1684STejun Heo * and consecutive pod ID. The rest of @pt is initialized accordingly. 6501*025e1684STejun Heo */ 6502*025e1684STejun Heo static void __init init_pod_type(struct wq_pod_type *pt, 6503*025e1684STejun Heo bool (*cpus_share_pod)(int, int)) 6504*025e1684STejun Heo { 6505*025e1684STejun Heo int cur, pre, cpu, pod; 6506*025e1684STejun Heo 6507*025e1684STejun Heo pt->nr_pods = 0; 6508*025e1684STejun Heo 6509*025e1684STejun Heo /* init @pt->cpu_pod[] according to @cpus_share_pod() */ 6510*025e1684STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 6511*025e1684STejun Heo BUG_ON(!pt->cpu_pod); 6512*025e1684STejun Heo 6513*025e1684STejun Heo for_each_possible_cpu(cur) { 6514*025e1684STejun Heo for_each_possible_cpu(pre) { 6515*025e1684STejun Heo if (pre >= cur) { 6516*025e1684STejun Heo pt->cpu_pod[cur] = pt->nr_pods++; 6517*025e1684STejun Heo break; 6518*025e1684STejun Heo } 6519*025e1684STejun Heo if (cpus_share_pod(cur, pre)) { 6520*025e1684STejun Heo pt->cpu_pod[cur] = pt->cpu_pod[pre]; 6521*025e1684STejun Heo break; 6522*025e1684STejun Heo } 6523*025e1684STejun Heo } 6524*025e1684STejun Heo } 6525*025e1684STejun Heo 6526*025e1684STejun Heo /* init the rest to match @pt->cpu_pod[] */ 6527*025e1684STejun Heo pt->pod_cpus = kcalloc(pt->nr_pods, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 6528*025e1684STejun Heo pt->pod_node = kcalloc(pt->nr_pods, sizeof(pt->pod_node[0]), GFP_KERNEL); 6529*025e1684STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node); 6530*025e1684STejun Heo 6531*025e1684STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) 6532*025e1684STejun Heo BUG_ON(!zalloc_cpumask_var(&pt->pod_cpus[pod], GFP_KERNEL)); 6533*025e1684STejun Heo 6534*025e1684STejun Heo for_each_possible_cpu(cpu) { 6535*025e1684STejun Heo cpumask_set_cpu(cpu, pt->pod_cpus[pt->cpu_pod[cpu]]); 6536*025e1684STejun Heo pt->pod_node[pt->cpu_pod[cpu]] = cpu_to_node(cpu); 6537*025e1684STejun Heo } 6538*025e1684STejun Heo } 6539*025e1684STejun Heo 6540*025e1684STejun Heo static bool __init cpus_share_numa(int cpu0, int cpu1) 6541*025e1684STejun Heo { 6542*025e1684STejun Heo return cpu_to_node(cpu0) == cpu_to_node(cpu1); 6543*025e1684STejun Heo } 6544*025e1684STejun Heo 65452930155bSTejun Heo /** 65462930155bSTejun Heo * workqueue_init_topology - initialize CPU pods for unbound workqueues 65472930155bSTejun Heo * 65482930155bSTejun Heo * This is the third step of there-staged workqueue subsystem initialization and 65492930155bSTejun Heo * invoked after SMP and topology information are fully initialized. It 65502930155bSTejun Heo * initializes the unbound CPU pods accordingly. 65512930155bSTejun Heo */ 65522930155bSTejun Heo void __init workqueue_init_topology(void) 6553a86feae6STejun Heo { 65542930155bSTejun Heo struct workqueue_struct *wq; 6555*025e1684STejun Heo int cpu; 6556a86feae6STejun Heo 6557*025e1684STejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_NUMA], cpus_share_numa); 6558a86feae6STejun Heo 65592930155bSTejun Heo mutex_lock(&wq_pool_mutex); 6560a86feae6STejun Heo 6561a86feae6STejun Heo /* 65622930155bSTejun Heo * Workqueues allocated earlier would have all CPUs sharing the default 65632930155bSTejun Heo * worker pool. Explicitly call wq_update_pod() on all workqueue and CPU 65642930155bSTejun Heo * combinations to apply per-pod sharing. 65652930155bSTejun Heo */ 65662930155bSTejun Heo list_for_each_entry(wq, &workqueues, list) { 65672930155bSTejun Heo for_each_online_cpu(cpu) { 65682930155bSTejun Heo wq_update_pod(wq, cpu, cpu, true); 65692930155bSTejun Heo } 65702930155bSTejun Heo } 65712930155bSTejun Heo 65722930155bSTejun Heo mutex_unlock(&wq_pool_mutex); 6573a86feae6STejun Heo } 6574a86feae6STejun Heo 657520bdedafSTetsuo Handa void __warn_flushing_systemwide_wq(void) 657620bdedafSTetsuo Handa { 657720bdedafSTetsuo Handa pr_warn("WARNING: Flushing system-wide workqueues will be prohibited in near future.\n"); 657820bdedafSTetsuo Handa dump_stack(); 657920bdedafSTetsuo Handa } 6580c4f135d6STetsuo Handa EXPORT_SYMBOL(__warn_flushing_systemwide_wq); 6581ace3c549Stiozhang 6582ace3c549Stiozhang static int __init workqueue_unbound_cpus_setup(char *str) 6583ace3c549Stiozhang { 6584ace3c549Stiozhang if (cpulist_parse(str, &wq_cmdline_cpumask) < 0) { 6585ace3c549Stiozhang cpumask_clear(&wq_cmdline_cpumask); 6586ace3c549Stiozhang pr_warn("workqueue.unbound_cpus: incorrect CPU range, using default\n"); 6587ace3c549Stiozhang } 6588ace3c549Stiozhang 6589ace3c549Stiozhang return 1; 6590ace3c549Stiozhang } 6591ace3c549Stiozhang __setup("workqueue.unbound_cpus=", workqueue_unbound_cpus_setup); 6592