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]; 34163c5484eSTejun Heo static enum wq_affn_scope wq_affn_dfl = WQ_AFFN_DFL; 34263c5484eSTejun Heo 34363c5484eSTejun Heo static const char *wq_affn_names[WQ_AFFN_NR_TYPES] = { 34463c5484eSTejun Heo [WQ_AFFN_CPU] = "cpu", 34563c5484eSTejun Heo [WQ_AFFN_SMT] = "smt", 34663c5484eSTejun Heo [WQ_AFFN_CACHE] = "cache", 34763c5484eSTejun Heo [WQ_AFFN_NUMA] = "numa", 34863c5484eSTejun Heo [WQ_AFFN_SYSTEM] = "system", 34963c5484eSTejun Heo }; 350bce90380STejun Heo 351616db877STejun Heo /* 352616db877STejun Heo * Per-cpu work items which run for longer than the following threshold are 353616db877STejun Heo * automatically considered CPU intensive and excluded from concurrency 354616db877STejun Heo * management to prevent them from noticeably delaying other per-cpu work items. 355aa6fde93STejun Heo * ULONG_MAX indicates that the user hasn't overridden it with a boot parameter. 356aa6fde93STejun Heo * The actual value is initialized in wq_cpu_intensive_thresh_init(). 357616db877STejun Heo */ 358aa6fde93STejun Heo static unsigned long wq_cpu_intensive_thresh_us = ULONG_MAX; 359616db877STejun Heo module_param_named(cpu_intensive_thresh_us, wq_cpu_intensive_thresh_us, ulong, 0644); 360616db877STejun Heo 361cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 362552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 363cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 364cee22a15SViresh Kumar 365863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */ 3663347fa09STejun Heo 367fef59c9cSTejun Heo /* buf for wq_update_unbound_pod_attrs(), protected by CPU hotplug exclusion */ 368fef59c9cSTejun Heo static struct workqueue_attrs *wq_update_pod_attrs_buf; 3690f36ee24STejun Heo static cpumask_var_t wq_update_pod_cpumask_buf; 3704c16bd32STejun Heo 37168e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 3721258fae7STejun Heo static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ 373a9b8a985SSebastian Andrzej Siewior static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 374d8bb65abSSebastian Andrzej Siewior /* wait for manager to go away */ 375d8bb65abSSebastian Andrzej Siewior static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait); 3765bcab335STejun Heo 377e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 37868e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 3797d19c5ceSTejun Heo 38099c621efSLai Jiangshan /* PL&A: allowable cpus for unbound wqs and work items */ 381ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 382ef557180SMike Galbraith 383ace3c549Stiozhang /* for further constrain wq_unbound_cpumask by cmdline parameter*/ 384ace3c549Stiozhang static struct cpumask wq_cmdline_cpumask __initdata; 385ace3c549Stiozhang 386ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 387ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 388b05a7928SFrederic Weisbecker 389f303fccbSTejun Heo /* 390f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 391f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 392f303fccbSTejun Heo * to uncover usages which depend on it. 393f303fccbSTejun Heo */ 394f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 395f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 396f303fccbSTejun Heo #else 397f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 398f303fccbSTejun Heo #endif 399f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 400f303fccbSTejun Heo 4017d19c5ceSTejun Heo /* the per-cpu worker pools */ 40225528213SPeter Zijlstra static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools); 4037d19c5ceSTejun Heo 40468e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 4057d19c5ceSTejun Heo 40668e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 40729c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 40829c91e99STejun Heo 409c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 41029c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 41129c91e99STejun Heo 4128a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 4138a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 4148a2b7538STejun Heo 415967b494eSTejun Heo /* 416967b494eSTejun Heo * I: kthread_worker to release pwq's. pwq release needs to be bounced to a 417967b494eSTejun Heo * process context while holding a pool lock. Bounce to a dedicated kthread 418967b494eSTejun Heo * worker to avoid A-A deadlocks. 419967b494eSTejun Heo */ 420967b494eSTejun Heo static struct kthread_worker *pwq_release_worker; 421967b494eSTejun Heo 422d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly; 423ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 424044c782cSValentin Ilie struct workqueue_struct *system_highpri_wq __read_mostly; 4251aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 426044c782cSValentin Ilie struct workqueue_struct *system_long_wq __read_mostly; 427d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 428044c782cSValentin Ilie struct workqueue_struct *system_unbound_wq __read_mostly; 429f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 430044c782cSValentin Ilie struct workqueue_struct *system_freezable_wq __read_mostly; 43124d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 4320668106cSViresh Kumar struct workqueue_struct *system_power_efficient_wq __read_mostly; 4330668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 4340668106cSViresh Kumar struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly; 4350668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 436d320c038STejun Heo 4377d19c5ceSTejun Heo static int worker_thread(void *__worker); 4386ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 439c29eb853STejun Heo static void show_pwq(struct pool_workqueue *pwq); 44055df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool); 4417d19c5ceSTejun Heo 44297bd2347STejun Heo #define CREATE_TRACE_POINTS 44397bd2347STejun Heo #include <trace/events/workqueue.h> 44497bd2347STejun Heo 44568e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 44624acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 447f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 44824acfb71SThomas Gleixner "RCU or wq_pool_mutex should be held") 4495bcab335STejun Heo 4505b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 45124acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 452f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 453f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 45424acfb71SThomas Gleixner "RCU, wq->mutex or wq_pool_mutex should be held") 4555b95e1afSLai Jiangshan 456f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 457f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 458f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 4597a62c2c8STejun Heo (pool)++) 4604ce62e9eSTejun Heo 46149e3cf44STejun Heo /** 46217116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 46317116969STejun Heo * @pool: iteration cursor 464611c92a0STejun Heo * @pi: integer used for iteration 465fa1b54e6STejun Heo * 46624acfb71SThomas Gleixner * This must be called either with wq_pool_mutex held or RCU read 46768e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 46868e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 469fa1b54e6STejun Heo * 470fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 471fa1b54e6STejun Heo * ignored. 47217116969STejun Heo */ 473611c92a0STejun Heo #define for_each_pool(pool, pi) \ 474611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 47568e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 476fa1b54e6STejun Heo else 47717116969STejun Heo 47817116969STejun Heo /** 479822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 480822d8405STejun Heo * @worker: iteration cursor 481822d8405STejun Heo * @pool: worker_pool to iterate workers of 482822d8405STejun Heo * 4831258fae7STejun Heo * This must be called with wq_pool_attach_mutex. 484822d8405STejun Heo * 485822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 486822d8405STejun Heo * ignored. 487822d8405STejun Heo */ 488da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 489da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 4901258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 491822d8405STejun Heo else 492822d8405STejun Heo 493822d8405STejun Heo /** 49449e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 49549e3cf44STejun Heo * @pwq: iteration cursor 49649e3cf44STejun Heo * @wq: the target workqueue 49776af4d93STejun Heo * 49824acfb71SThomas Gleixner * This must be called either with wq->mutex held or RCU read locked. 499794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 500794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 50176af4d93STejun Heo * 50276af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 50376af4d93STejun Heo * ignored. 50449e3cf44STejun Heo */ 50549e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 50649e9d1a9SSebastian Andrzej Siewior list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ 5075a644662SJoel Fernandes (Google) lockdep_is_held(&(wq->mutex))) 508f3421797STejun Heo 509dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 510dc186ad7SThomas Gleixner 511f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr; 512dc186ad7SThomas Gleixner 51399777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 51499777288SStanislaw Gruszka { 51599777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 51699777288SStanislaw Gruszka } 51799777288SStanislaw Gruszka 518b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 519b9fdac7fSDu, Changbin { 520b9fdac7fSDu, Changbin struct work_struct *work = addr; 521b9fdac7fSDu, Changbin 522b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 523b9fdac7fSDu, Changbin } 524b9fdac7fSDu, Changbin 525dc186ad7SThomas Gleixner /* 526dc186ad7SThomas Gleixner * fixup_init is called when: 527dc186ad7SThomas Gleixner * - an active object is initialized 528dc186ad7SThomas Gleixner */ 52902a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 530dc186ad7SThomas Gleixner { 531dc186ad7SThomas Gleixner struct work_struct *work = addr; 532dc186ad7SThomas Gleixner 533dc186ad7SThomas Gleixner switch (state) { 534dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 535dc186ad7SThomas Gleixner cancel_work_sync(work); 536dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 53702a982a6SDu, Changbin return true; 538dc186ad7SThomas Gleixner default: 53902a982a6SDu, Changbin return false; 540dc186ad7SThomas Gleixner } 541dc186ad7SThomas Gleixner } 542dc186ad7SThomas Gleixner 543dc186ad7SThomas Gleixner /* 544dc186ad7SThomas Gleixner * fixup_free is called when: 545dc186ad7SThomas Gleixner * - an active object is freed 546dc186ad7SThomas Gleixner */ 54702a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 548dc186ad7SThomas Gleixner { 549dc186ad7SThomas Gleixner struct work_struct *work = addr; 550dc186ad7SThomas Gleixner 551dc186ad7SThomas Gleixner switch (state) { 552dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 553dc186ad7SThomas Gleixner cancel_work_sync(work); 554dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 55502a982a6SDu, Changbin return true; 556dc186ad7SThomas Gleixner default: 55702a982a6SDu, Changbin return false; 558dc186ad7SThomas Gleixner } 559dc186ad7SThomas Gleixner } 560dc186ad7SThomas Gleixner 561f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr = { 562dc186ad7SThomas Gleixner .name = "work_struct", 56399777288SStanislaw Gruszka .debug_hint = work_debug_hint, 564b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 565dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 566dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 567dc186ad7SThomas Gleixner }; 568dc186ad7SThomas Gleixner 569dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 570dc186ad7SThomas Gleixner { 571dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 572dc186ad7SThomas Gleixner } 573dc186ad7SThomas Gleixner 574dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 575dc186ad7SThomas Gleixner { 576dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 577dc186ad7SThomas Gleixner } 578dc186ad7SThomas Gleixner 579dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 580dc186ad7SThomas Gleixner { 581dc186ad7SThomas Gleixner if (onstack) 582dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 583dc186ad7SThomas Gleixner else 584dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 585dc186ad7SThomas Gleixner } 586dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 587dc186ad7SThomas Gleixner 588dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 589dc186ad7SThomas Gleixner { 590dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 591dc186ad7SThomas Gleixner } 592dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 593dc186ad7SThomas Gleixner 594ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 595ea2e64f2SThomas Gleixner { 596ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 597ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 598ea2e64f2SThomas Gleixner } 599ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 600ea2e64f2SThomas Gleixner 601dc186ad7SThomas Gleixner #else 602dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 603dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 604dc186ad7SThomas Gleixner #endif 605dc186ad7SThomas Gleixner 6064e8b22bdSLi Bin /** 60767dc8325SCai Huoqing * worker_pool_assign_id - allocate ID and assign it to @pool 6084e8b22bdSLi Bin * @pool: the pool pointer of interest 6094e8b22bdSLi Bin * 6104e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 6114e8b22bdSLi Bin * successfully, -errno on failure. 6124e8b22bdSLi Bin */ 6139daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 6149daf9e67STejun Heo { 6159daf9e67STejun Heo int ret; 6169daf9e67STejun Heo 61768e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6185bcab335STejun Heo 6194e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 6204e8b22bdSLi Bin GFP_KERNEL); 621229641a6STejun Heo if (ret >= 0) { 622e68035fbSTejun Heo pool->id = ret; 623229641a6STejun Heo return 0; 624229641a6STejun Heo } 6259daf9e67STejun Heo return ret; 6269daf9e67STejun Heo } 6279daf9e67STejun Heo 62873f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 62973f53c4aSTejun Heo { 63073f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 63173f53c4aSTejun Heo } 63273f53c4aSTejun Heo 633c4560c2cSLai Jiangshan static int get_work_color(unsigned long work_data) 63473f53c4aSTejun Heo { 635c4560c2cSLai Jiangshan return (work_data >> WORK_STRUCT_COLOR_SHIFT) & 63673f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 63773f53c4aSTejun Heo } 63873f53c4aSTejun Heo 63973f53c4aSTejun Heo static int work_next_color(int color) 64073f53c4aSTejun Heo { 64173f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 642a848e3b6SOleg Nesterov } 643a848e3b6SOleg Nesterov 6444594bf15SDavid Howells /* 645112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 646112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 6477c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 6487a22ad75STejun Heo * 649112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 650112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 651bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 652bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 6537a22ad75STejun Heo * 654112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 6557c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 656112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 6577c3eed5cSTejun Heo * available only while the work item is queued. 658bbb68dfaSTejun Heo * 659bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 660bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 661bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 662bbb68dfaSTejun Heo * try to steal the PENDING bit. 6634594bf15SDavid Howells */ 6647a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 6657a22ad75STejun Heo unsigned long flags) 6667a22ad75STejun Heo { 6676183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 6687a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 6697a22ad75STejun Heo } 6707a22ad75STejun Heo 671112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 6724690c4abSTejun Heo unsigned long extra_flags) 673365970a1SDavid Howells { 674112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 675112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 676365970a1SDavid Howells } 677365970a1SDavid Howells 6784468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 6794468a00fSLai Jiangshan int pool_id) 6804468a00fSLai Jiangshan { 6814468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 6824468a00fSLai Jiangshan WORK_STRUCT_PENDING); 6834468a00fSLai Jiangshan } 6844468a00fSLai Jiangshan 6857c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 6867c3eed5cSTejun Heo int pool_id) 6874d707b9fSOleg Nesterov { 68823657bb1STejun Heo /* 68923657bb1STejun Heo * The following wmb is paired with the implied mb in 69023657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 69123657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 69223657bb1STejun Heo * owner. 69323657bb1STejun Heo */ 69423657bb1STejun Heo smp_wmb(); 6957c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 696346c09f8SRoman Pen /* 697346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 698346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 699346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 7008bdc6201SLiu Song * reordering can lead to a missed execution on attempt to queue 701346c09f8SRoman Pen * the same @work. E.g. consider this case: 702346c09f8SRoman Pen * 703346c09f8SRoman Pen * CPU#0 CPU#1 704346c09f8SRoman Pen * ---------------------------- -------------------------------- 705346c09f8SRoman Pen * 706346c09f8SRoman Pen * 1 STORE event_indicated 707346c09f8SRoman Pen * 2 queue_work_on() { 708346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 709346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 710346c09f8SRoman Pen * 5 set_work_data() # clear bit 711346c09f8SRoman Pen * 6 smp_mb() 712346c09f8SRoman Pen * 7 work->current_func() { 713346c09f8SRoman Pen * 8 LOAD event_indicated 714346c09f8SRoman Pen * } 715346c09f8SRoman Pen * 716346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 717346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 718346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 719346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 720346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 721346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 722346c09f8SRoman Pen * before actual STORE. 723346c09f8SRoman Pen */ 724346c09f8SRoman Pen smp_mb(); 7254d707b9fSOleg Nesterov } 7264d707b9fSOleg Nesterov 7277a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 728365970a1SDavid Howells { 7297c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 7307c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 7317a22ad75STejun Heo } 7327a22ad75STejun Heo 733afa4bb77SLinus Torvalds static inline struct pool_workqueue *work_struct_pwq(unsigned long data) 734afa4bb77SLinus Torvalds { 735afa4bb77SLinus Torvalds return (struct pool_workqueue *)(data & WORK_STRUCT_WQ_DATA_MASK); 736afa4bb77SLinus Torvalds } 737afa4bb77SLinus Torvalds 738112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 7397a22ad75STejun Heo { 740e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 7417a22ad75STejun Heo 742112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 743afa4bb77SLinus Torvalds return work_struct_pwq(data); 744e120153dSTejun Heo else 745e120153dSTejun Heo return NULL; 7467a22ad75STejun Heo } 7477a22ad75STejun Heo 7487c3eed5cSTejun Heo /** 7497c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 7507c3eed5cSTejun Heo * @work: the work item of interest 7517c3eed5cSTejun Heo * 75268e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 75324acfb71SThomas Gleixner * access under RCU read lock. As such, this function should be 75424acfb71SThomas Gleixner * called under wq_pool_mutex or inside of a rcu_read_lock() region. 755fa1b54e6STejun Heo * 756fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 757fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 758fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 759fa1b54e6STejun Heo * returned pool is and stays online. 760d185af30SYacine Belkadi * 761d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 7627c3eed5cSTejun Heo */ 7637c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 7647a22ad75STejun Heo { 765e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 7667c3eed5cSTejun Heo int pool_id; 7677a22ad75STejun Heo 76868e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 769fa1b54e6STejun Heo 770112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 771afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool; 7727a22ad75STejun Heo 7737c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 7747c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 7757a22ad75STejun Heo return NULL; 7767a22ad75STejun Heo 777fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 7787c3eed5cSTejun Heo } 7797c3eed5cSTejun Heo 7807c3eed5cSTejun Heo /** 7817c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 7827c3eed5cSTejun Heo * @work: the work item of interest 7837c3eed5cSTejun Heo * 784d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 7857c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 7867c3eed5cSTejun Heo */ 7877c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 7887c3eed5cSTejun Heo { 78954d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 7907c3eed5cSTejun Heo 791112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 792afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool->id; 79354d5b7d0SLai Jiangshan 79454d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 7957c3eed5cSTejun Heo } 7967c3eed5cSTejun Heo 797bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 798bbb68dfaSTejun Heo { 7997c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 800bbb68dfaSTejun Heo 8017c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 8027c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 803bbb68dfaSTejun Heo } 804bbb68dfaSTejun Heo 805bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 806bbb68dfaSTejun Heo { 807bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 808bbb68dfaSTejun Heo 809112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 810bbb68dfaSTejun Heo } 811bbb68dfaSTejun Heo 812e22bee78STejun Heo /* 8133270476aSTejun Heo * Policy functions. These define the policies on how the global worker 8143270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 815d565ed63STejun Heo * they're being called with pool->lock held. 816e22bee78STejun Heo */ 817e22bee78STejun Heo 818e22bee78STejun Heo /* 819e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 820e22bee78STejun Heo * running workers. 821974271c4STejun Heo * 822974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 823706026c2STejun Heo * function will always return %true for unbound pools as long as the 824974271c4STejun Heo * worklist isn't empty. 825e22bee78STejun Heo */ 82663d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 827e22bee78STejun Heo { 828*0219a352STejun Heo return !list_empty(&pool->worklist) && !pool->nr_running; 829e22bee78STejun Heo } 830e22bee78STejun Heo 831e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 83263d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 833e22bee78STejun Heo { 83463d95a91STejun Heo return pool->nr_idle; 835e22bee78STejun Heo } 836e22bee78STejun Heo 837e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 83863d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 839e22bee78STejun Heo { 840bc35f7efSLai Jiangshan return !list_empty(&pool->worklist) && (pool->nr_running <= 1); 841e22bee78STejun Heo } 842e22bee78STejun Heo 843e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 84463d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 845e22bee78STejun Heo { 84663d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 847e22bee78STejun Heo } 848e22bee78STejun Heo 849e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 85063d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 851e22bee78STejun Heo { 852692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 85363d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 85463d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 855e22bee78STejun Heo 856e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 857e22bee78STejun Heo } 858e22bee78STejun Heo 8594690c4abSTejun Heo /** 860e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 861cb444766STejun Heo * @worker: self 862d302f017STejun Heo * @flags: flags to set 863d302f017STejun Heo * 864228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 865d302f017STejun Heo */ 866228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 867d302f017STejun Heo { 868bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 869e22bee78STejun Heo 870bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 871cb444766STejun Heo 872228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 873e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 874e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 875bc35f7efSLai Jiangshan pool->nr_running--; 876e22bee78STejun Heo } 877e22bee78STejun Heo 878d302f017STejun Heo worker->flags |= flags; 879d302f017STejun Heo } 880d302f017STejun Heo 881d302f017STejun Heo /** 882e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 883cb444766STejun Heo * @worker: self 884d302f017STejun Heo * @flags: flags to clear 885d302f017STejun Heo * 886e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 887d302f017STejun Heo */ 888d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 889d302f017STejun Heo { 89063d95a91STejun Heo struct worker_pool *pool = worker->pool; 891e22bee78STejun Heo unsigned int oflags = worker->flags; 892e22bee78STejun Heo 893bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 894cb444766STejun Heo 895d302f017STejun Heo worker->flags &= ~flags; 896e22bee78STejun Heo 89742c025f3STejun Heo /* 89842c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 89942c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 90042c025f3STejun Heo * of multiple flags, not a single flag. 90142c025f3STejun Heo */ 902e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 903e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 904bc35f7efSLai Jiangshan pool->nr_running++; 905d302f017STejun Heo } 906d302f017STejun Heo 907797e8345STejun Heo /* Return the first idle worker. Called with pool->lock held. */ 908797e8345STejun Heo static struct worker *first_idle_worker(struct worker_pool *pool) 909797e8345STejun Heo { 910797e8345STejun Heo if (unlikely(list_empty(&pool->idle_list))) 911797e8345STejun Heo return NULL; 912797e8345STejun Heo 913797e8345STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 914797e8345STejun Heo } 915797e8345STejun Heo 916797e8345STejun Heo /** 917797e8345STejun Heo * worker_enter_idle - enter idle state 918797e8345STejun Heo * @worker: worker which is entering idle state 919797e8345STejun Heo * 920797e8345STejun Heo * @worker is entering idle state. Update stats and idle timer if 921797e8345STejun Heo * necessary. 922797e8345STejun Heo * 923797e8345STejun Heo * LOCKING: 924797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 925797e8345STejun Heo */ 926797e8345STejun Heo static void worker_enter_idle(struct worker *worker) 927797e8345STejun Heo { 928797e8345STejun Heo struct worker_pool *pool = worker->pool; 929797e8345STejun Heo 930797e8345STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 931797e8345STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 932797e8345STejun Heo (worker->hentry.next || worker->hentry.pprev))) 933797e8345STejun Heo return; 934797e8345STejun Heo 935797e8345STejun Heo /* can't use worker_set_flags(), also called from create_worker() */ 936797e8345STejun Heo worker->flags |= WORKER_IDLE; 937797e8345STejun Heo pool->nr_idle++; 938797e8345STejun Heo worker->last_active = jiffies; 939797e8345STejun Heo 940797e8345STejun Heo /* idle_list is LIFO */ 941797e8345STejun Heo list_add(&worker->entry, &pool->idle_list); 942797e8345STejun Heo 943797e8345STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 944797e8345STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 945797e8345STejun Heo 946797e8345STejun Heo /* Sanity check nr_running. */ 947797e8345STejun Heo WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); 948797e8345STejun Heo } 949797e8345STejun Heo 950797e8345STejun Heo /** 951797e8345STejun Heo * worker_leave_idle - leave idle state 952797e8345STejun Heo * @worker: worker which is leaving idle state 953797e8345STejun Heo * 954797e8345STejun Heo * @worker is leaving idle state. Update stats. 955797e8345STejun Heo * 956797e8345STejun Heo * LOCKING: 957797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 958797e8345STejun Heo */ 959797e8345STejun Heo static void worker_leave_idle(struct worker *worker) 960797e8345STejun Heo { 961797e8345STejun Heo struct worker_pool *pool = worker->pool; 962797e8345STejun Heo 963797e8345STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 964797e8345STejun Heo return; 965797e8345STejun Heo worker_clr_flags(worker, WORKER_IDLE); 966797e8345STejun Heo pool->nr_idle--; 967797e8345STejun Heo list_del_init(&worker->entry); 968797e8345STejun Heo } 969797e8345STejun Heo 970797e8345STejun Heo /** 971797e8345STejun Heo * find_worker_executing_work - find worker which is executing a work 972797e8345STejun Heo * @pool: pool of interest 973797e8345STejun Heo * @work: work to find worker for 974797e8345STejun Heo * 975797e8345STejun Heo * Find a worker which is executing @work on @pool by searching 976797e8345STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 977797e8345STejun Heo * to match, its current execution should match the address of @work and 978797e8345STejun Heo * its work function. This is to avoid unwanted dependency between 979797e8345STejun Heo * unrelated work executions through a work item being recycled while still 980797e8345STejun Heo * being executed. 981797e8345STejun Heo * 982797e8345STejun Heo * This is a bit tricky. A work item may be freed once its execution 983797e8345STejun Heo * starts and nothing prevents the freed area from being recycled for 984797e8345STejun Heo * another work item. If the same work item address ends up being reused 985797e8345STejun Heo * before the original execution finishes, workqueue will identify the 986797e8345STejun Heo * recycled work item as currently executing and make it wait until the 987797e8345STejun Heo * current execution finishes, introducing an unwanted dependency. 988797e8345STejun Heo * 989797e8345STejun Heo * This function checks the work item address and work function to avoid 990797e8345STejun Heo * false positives. Note that this isn't complete as one may construct a 991797e8345STejun Heo * work function which can introduce dependency onto itself through a 992797e8345STejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 993797e8345STejun Heo * foot that badly, there's only so much we can do, and if such deadlock 994797e8345STejun Heo * actually occurs, it should be easy to locate the culprit work function. 995797e8345STejun Heo * 996797e8345STejun Heo * CONTEXT: 997797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 998797e8345STejun Heo * 999797e8345STejun Heo * Return: 1000797e8345STejun Heo * Pointer to worker which is executing @work if found, %NULL 1001797e8345STejun Heo * otherwise. 1002797e8345STejun Heo */ 1003797e8345STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 1004797e8345STejun Heo struct work_struct *work) 1005797e8345STejun Heo { 1006797e8345STejun Heo struct worker *worker; 1007797e8345STejun Heo 1008797e8345STejun Heo hash_for_each_possible(pool->busy_hash, worker, hentry, 1009797e8345STejun Heo (unsigned long)work) 1010797e8345STejun Heo if (worker->current_work == work && 1011797e8345STejun Heo worker->current_func == work->func) 1012797e8345STejun Heo return worker; 1013797e8345STejun Heo 1014797e8345STejun Heo return NULL; 1015797e8345STejun Heo } 1016797e8345STejun Heo 1017797e8345STejun Heo /** 1018797e8345STejun Heo * move_linked_works - move linked works to a list 1019797e8345STejun Heo * @work: start of series of works to be scheduled 1020797e8345STejun Heo * @head: target list to append @work to 1021797e8345STejun Heo * @nextp: out parameter for nested worklist walking 1022797e8345STejun Heo * 1023873eaca6STejun Heo * Schedule linked works starting from @work to @head. Work series to be 1024873eaca6STejun Heo * scheduled starts at @work and includes any consecutive work with 1025873eaca6STejun Heo * WORK_STRUCT_LINKED set in its predecessor. See assign_work() for details on 1026873eaca6STejun Heo * @nextp. 1027797e8345STejun Heo * 1028797e8345STejun Heo * CONTEXT: 1029797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1030797e8345STejun Heo */ 1031797e8345STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1032797e8345STejun Heo struct work_struct **nextp) 1033797e8345STejun Heo { 1034797e8345STejun Heo struct work_struct *n; 1035797e8345STejun Heo 1036797e8345STejun Heo /* 1037797e8345STejun Heo * Linked worklist will always end before the end of the list, 1038797e8345STejun Heo * use NULL for list head. 1039797e8345STejun Heo */ 1040797e8345STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1041797e8345STejun Heo list_move_tail(&work->entry, head); 1042797e8345STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1043797e8345STejun Heo break; 1044797e8345STejun Heo } 1045797e8345STejun Heo 1046797e8345STejun Heo /* 1047797e8345STejun Heo * If we're already inside safe list traversal and have moved 1048797e8345STejun Heo * multiple works to the scheduled queue, the next position 1049797e8345STejun Heo * needs to be updated. 1050797e8345STejun Heo */ 1051797e8345STejun Heo if (nextp) 1052797e8345STejun Heo *nextp = n; 1053797e8345STejun Heo } 1054797e8345STejun Heo 1055797e8345STejun Heo /** 1056873eaca6STejun Heo * assign_work - assign a work item and its linked work items to a worker 1057873eaca6STejun Heo * @work: work to assign 1058873eaca6STejun Heo * @worker: worker to assign to 1059873eaca6STejun Heo * @nextp: out parameter for nested worklist walking 1060873eaca6STejun Heo * 1061873eaca6STejun Heo * Assign @work and its linked work items to @worker. If @work is already being 1062873eaca6STejun Heo * executed by another worker in the same pool, it'll be punted there. 1063873eaca6STejun Heo * 1064873eaca6STejun Heo * If @nextp is not NULL, it's updated to point to the next work of the last 1065873eaca6STejun Heo * scheduled work. This allows assign_work() to be nested inside 1066873eaca6STejun Heo * list_for_each_entry_safe(). 1067873eaca6STejun Heo * 1068873eaca6STejun Heo * Returns %true if @work was successfully assigned to @worker. %false if @work 1069873eaca6STejun Heo * was punted to another worker already executing it. 1070873eaca6STejun Heo */ 1071873eaca6STejun Heo static bool assign_work(struct work_struct *work, struct worker *worker, 1072873eaca6STejun Heo struct work_struct **nextp) 1073873eaca6STejun Heo { 1074873eaca6STejun Heo struct worker_pool *pool = worker->pool; 1075873eaca6STejun Heo struct worker *collision; 1076873eaca6STejun Heo 1077873eaca6STejun Heo lockdep_assert_held(&pool->lock); 1078873eaca6STejun Heo 1079873eaca6STejun Heo /* 1080873eaca6STejun Heo * A single work shouldn't be executed concurrently by multiple workers. 1081873eaca6STejun Heo * __queue_work() ensures that @work doesn't jump to a different pool 1082873eaca6STejun Heo * while still running in the previous pool. Here, we should ensure that 1083873eaca6STejun Heo * @work is not executed concurrently by multiple workers from the same 1084873eaca6STejun Heo * pool. Check whether anyone is already processing the work. If so, 1085873eaca6STejun Heo * defer the work to the currently executing one. 1086873eaca6STejun Heo */ 1087873eaca6STejun Heo collision = find_worker_executing_work(pool, work); 1088873eaca6STejun Heo if (unlikely(collision)) { 1089873eaca6STejun Heo move_linked_works(work, &collision->scheduled, nextp); 1090873eaca6STejun Heo return false; 1091873eaca6STejun Heo } 1092873eaca6STejun Heo 1093873eaca6STejun Heo move_linked_works(work, &worker->scheduled, nextp); 1094873eaca6STejun Heo return true; 1095873eaca6STejun Heo } 1096873eaca6STejun Heo 1097873eaca6STejun Heo /** 1098*0219a352STejun Heo * kick_pool - wake up an idle worker if necessary 1099*0219a352STejun Heo * @pool: pool to kick 1100797e8345STejun Heo * 1101*0219a352STejun Heo * @pool may have pending work items. Wake up worker if necessary. Returns 1102*0219a352STejun Heo * whether a worker was woken up. 1103797e8345STejun Heo */ 1104*0219a352STejun Heo static bool kick_pool(struct worker_pool *pool) 1105797e8345STejun Heo { 1106797e8345STejun Heo struct worker *worker = first_idle_worker(pool); 1107797e8345STejun Heo 1108*0219a352STejun Heo lockdep_assert_held(&pool->lock); 1109*0219a352STejun Heo 1110*0219a352STejun Heo if (!need_more_worker(pool) || !worker) 1111*0219a352STejun Heo return false; 1112*0219a352STejun Heo 1113797e8345STejun Heo wake_up_process(worker->task); 1114*0219a352STejun Heo return true; 1115797e8345STejun Heo } 1116797e8345STejun Heo 111763638450STejun Heo #ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT 111863638450STejun Heo 111963638450STejun Heo /* 112063638450STejun Heo * Concurrency-managed per-cpu work items that hog CPU for longer than 112163638450STejun Heo * wq_cpu_intensive_thresh_us trigger the automatic CPU_INTENSIVE mechanism, 112263638450STejun Heo * which prevents them from stalling other concurrency-managed work items. If a 112363638450STejun Heo * work function keeps triggering this mechanism, it's likely that the work item 112463638450STejun Heo * should be using an unbound workqueue instead. 112563638450STejun Heo * 112663638450STejun Heo * wq_cpu_intensive_report() tracks work functions which trigger such conditions 112763638450STejun Heo * and report them so that they can be examined and converted to use unbound 112863638450STejun Heo * workqueues as appropriate. To avoid flooding the console, each violating work 112963638450STejun Heo * function is tracked and reported with exponential backoff. 113063638450STejun Heo */ 113163638450STejun Heo #define WCI_MAX_ENTS 128 113263638450STejun Heo 113363638450STejun Heo struct wci_ent { 113463638450STejun Heo work_func_t func; 113563638450STejun Heo atomic64_t cnt; 113663638450STejun Heo struct hlist_node hash_node; 113763638450STejun Heo }; 113863638450STejun Heo 113963638450STejun Heo static struct wci_ent wci_ents[WCI_MAX_ENTS]; 114063638450STejun Heo static int wci_nr_ents; 114163638450STejun Heo static DEFINE_RAW_SPINLOCK(wci_lock); 114263638450STejun Heo static DEFINE_HASHTABLE(wci_hash, ilog2(WCI_MAX_ENTS)); 114363638450STejun Heo 114463638450STejun Heo static struct wci_ent *wci_find_ent(work_func_t func) 114563638450STejun Heo { 114663638450STejun Heo struct wci_ent *ent; 114763638450STejun Heo 114863638450STejun Heo hash_for_each_possible_rcu(wci_hash, ent, hash_node, 114963638450STejun Heo (unsigned long)func) { 115063638450STejun Heo if (ent->func == func) 115163638450STejun Heo return ent; 115263638450STejun Heo } 115363638450STejun Heo return NULL; 115463638450STejun Heo } 115563638450STejun Heo 115663638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) 115763638450STejun Heo { 115863638450STejun Heo struct wci_ent *ent; 115963638450STejun Heo 116063638450STejun Heo restart: 116163638450STejun Heo ent = wci_find_ent(func); 116263638450STejun Heo if (ent) { 116363638450STejun Heo u64 cnt; 116463638450STejun Heo 116563638450STejun Heo /* 116663638450STejun Heo * Start reporting from the fourth time and back off 116763638450STejun Heo * exponentially. 116863638450STejun Heo */ 116963638450STejun Heo cnt = atomic64_inc_return_relaxed(&ent->cnt); 117063638450STejun Heo if (cnt >= 4 && is_power_of_2(cnt)) 117163638450STejun Heo printk_deferred(KERN_WARNING "workqueue: %ps hogged CPU for >%luus %llu times, consider switching to WQ_UNBOUND\n", 117263638450STejun Heo ent->func, wq_cpu_intensive_thresh_us, 117363638450STejun Heo atomic64_read(&ent->cnt)); 117463638450STejun Heo return; 117563638450STejun Heo } 117663638450STejun Heo 117763638450STejun Heo /* 117863638450STejun Heo * @func is a new violation. Allocate a new entry for it. If wcn_ents[] 117963638450STejun Heo * is exhausted, something went really wrong and we probably made enough 118063638450STejun Heo * noise already. 118163638450STejun Heo */ 118263638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) 118363638450STejun Heo return; 118463638450STejun Heo 118563638450STejun Heo raw_spin_lock(&wci_lock); 118663638450STejun Heo 118763638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) { 118863638450STejun Heo raw_spin_unlock(&wci_lock); 118963638450STejun Heo return; 119063638450STejun Heo } 119163638450STejun Heo 119263638450STejun Heo if (wci_find_ent(func)) { 119363638450STejun Heo raw_spin_unlock(&wci_lock); 119463638450STejun Heo goto restart; 119563638450STejun Heo } 119663638450STejun Heo 119763638450STejun Heo ent = &wci_ents[wci_nr_ents++]; 119863638450STejun Heo ent->func = func; 119963638450STejun Heo atomic64_set(&ent->cnt, 1); 120063638450STejun Heo hash_add_rcu(wci_hash, &ent->hash_node, (unsigned long)func); 120163638450STejun Heo 120263638450STejun Heo raw_spin_unlock(&wci_lock); 120363638450STejun Heo } 120463638450STejun Heo 120563638450STejun Heo #else /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 120663638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) {} 120763638450STejun Heo #endif /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 120863638450STejun Heo 1209c54d5046STejun Heo /** 12101da177e4SLinus Torvalds * wq_worker_running - a worker is running again 12111da177e4SLinus Torvalds * @task: task waking up 12121da177e4SLinus Torvalds * 12131da177e4SLinus Torvalds * This function is called when a worker returns from schedule() 12141da177e4SLinus Torvalds */ 12151da177e4SLinus Torvalds void wq_worker_running(struct task_struct *task) 12161da177e4SLinus Torvalds { 12171da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 12181da177e4SLinus Torvalds 1219c8f6219bSZqiang if (!READ_ONCE(worker->sleeping)) 12201da177e4SLinus Torvalds return; 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds /* 12231da177e4SLinus Torvalds * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check 12241da177e4SLinus Torvalds * and the nr_running increment below, we may ruin the nr_running reset 12251da177e4SLinus Torvalds * and leave with an unexpected pool->nr_running == 1 on the newly unbound 12261da177e4SLinus Torvalds * pool. Protect against such race. 12271da177e4SLinus Torvalds */ 12281da177e4SLinus Torvalds preempt_disable(); 12291da177e4SLinus Torvalds if (!(worker->flags & WORKER_NOT_RUNNING)) 12301da177e4SLinus Torvalds worker->pool->nr_running++; 12311da177e4SLinus Torvalds preempt_enable(); 1232616db877STejun Heo 1233616db877STejun Heo /* 1234616db877STejun Heo * CPU intensive auto-detection cares about how long a work item hogged 1235616db877STejun Heo * CPU without sleeping. Reset the starting timestamp on wakeup. 1236616db877STejun Heo */ 1237616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 1238616db877STejun Heo 1239c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 0); 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds 12421da177e4SLinus Torvalds /** 12431da177e4SLinus Torvalds * wq_worker_sleeping - a worker is going to sleep 12441da177e4SLinus Torvalds * @task: task going to sleep 12451da177e4SLinus Torvalds * 12461da177e4SLinus Torvalds * This function is called from schedule() when a busy worker is 12471da177e4SLinus Torvalds * going to sleep. 12481da177e4SLinus Torvalds */ 12491da177e4SLinus Torvalds void wq_worker_sleeping(struct task_struct *task) 12501da177e4SLinus Torvalds { 12511da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 12521da177e4SLinus Torvalds struct worker_pool *pool; 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds /* 12551da177e4SLinus Torvalds * Rescuers, which may not have all the fields set up like normal 12561da177e4SLinus Torvalds * workers, also reach here, let's not access anything before 12571da177e4SLinus Torvalds * checking NOT_RUNNING. 12581da177e4SLinus Torvalds */ 12591da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) 12601da177e4SLinus Torvalds return; 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds pool = worker->pool; 12631da177e4SLinus Torvalds 12641da177e4SLinus Torvalds /* Return if preempted before wq_worker_running() was reached */ 1265c8f6219bSZqiang if (READ_ONCE(worker->sleeping)) 12661da177e4SLinus Torvalds return; 12671da177e4SLinus Torvalds 1268c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 1); 12691da177e4SLinus Torvalds raw_spin_lock_irq(&pool->lock); 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds /* 12721da177e4SLinus Torvalds * Recheck in case unbind_workers() preempted us. We don't 12731da177e4SLinus Torvalds * want to decrement nr_running after the worker is unbound 12741da177e4SLinus Torvalds * and nr_running has been reset. 12751da177e4SLinus Torvalds */ 12761da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) { 12771da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 12781da177e4SLinus Torvalds return; 12791da177e4SLinus Torvalds } 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds pool->nr_running--; 1282*0219a352STejun Heo if (kick_pool(pool)) 1283725e8ec5STejun Heo worker->current_pwq->stats[PWQ_STAT_CM_WAKEUP]++; 1284*0219a352STejun Heo 12851da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 12861da177e4SLinus Torvalds } 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds /** 1289616db877STejun Heo * wq_worker_tick - a scheduler tick occurred while a kworker is running 1290616db877STejun Heo * @task: task currently running 1291616db877STejun Heo * 1292616db877STejun Heo * Called from scheduler_tick(). We're in the IRQ context and the current 1293616db877STejun Heo * worker's fields which follow the 'K' locking rule can be accessed safely. 1294616db877STejun Heo */ 1295616db877STejun Heo void wq_worker_tick(struct task_struct *task) 1296616db877STejun Heo { 1297616db877STejun Heo struct worker *worker = kthread_data(task); 1298616db877STejun Heo struct pool_workqueue *pwq = worker->current_pwq; 1299616db877STejun Heo struct worker_pool *pool = worker->pool; 1300616db877STejun Heo 1301616db877STejun Heo if (!pwq) 1302616db877STejun Heo return; 1303616db877STejun Heo 13048a1dd1e5STejun Heo pwq->stats[PWQ_STAT_CPU_TIME] += TICK_USEC; 13058a1dd1e5STejun Heo 130618c8ae81SZqiang if (!wq_cpu_intensive_thresh_us) 130718c8ae81SZqiang return; 130818c8ae81SZqiang 1309616db877STejun Heo /* 1310616db877STejun Heo * If the current worker is concurrency managed and hogged the CPU for 1311616db877STejun Heo * longer than wq_cpu_intensive_thresh_us, it's automatically marked 1312616db877STejun Heo * CPU_INTENSIVE to avoid stalling other concurrency-managed work items. 1313c8f6219bSZqiang * 1314c8f6219bSZqiang * Set @worker->sleeping means that @worker is in the process of 1315c8f6219bSZqiang * switching out voluntarily and won't be contributing to 1316c8f6219bSZqiang * @pool->nr_running until it wakes up. As wq_worker_sleeping() also 1317c8f6219bSZqiang * decrements ->nr_running, setting CPU_INTENSIVE here can lead to 1318c8f6219bSZqiang * double decrements. The task is releasing the CPU anyway. Let's skip. 1319c8f6219bSZqiang * We probably want to make this prettier in the future. 1320616db877STejun Heo */ 1321c8f6219bSZqiang if ((worker->flags & WORKER_NOT_RUNNING) || READ_ONCE(worker->sleeping) || 1322616db877STejun Heo worker->task->se.sum_exec_runtime - worker->current_at < 1323616db877STejun Heo wq_cpu_intensive_thresh_us * NSEC_PER_USEC) 1324616db877STejun Heo return; 1325616db877STejun Heo 1326616db877STejun Heo raw_spin_lock(&pool->lock); 1327616db877STejun Heo 1328616db877STejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE); 132963638450STejun Heo wq_cpu_intensive_report(worker->current_func); 1330616db877STejun Heo pwq->stats[PWQ_STAT_CPU_INTENSIVE]++; 1331616db877STejun Heo 1332*0219a352STejun Heo if (kick_pool(pool)) 1333616db877STejun Heo pwq->stats[PWQ_STAT_CM_WAKEUP]++; 1334616db877STejun Heo 1335616db877STejun Heo raw_spin_unlock(&pool->lock); 1336616db877STejun Heo } 1337616db877STejun Heo 1338616db877STejun Heo /** 13391da177e4SLinus Torvalds * wq_worker_last_func - retrieve worker's last work function 13401da177e4SLinus Torvalds * @task: Task to retrieve last work function of. 13411da177e4SLinus Torvalds * 13421da177e4SLinus Torvalds * Determine the last function a worker executed. This is called from 13431da177e4SLinus Torvalds * the scheduler to get a worker's last known identity. 13441da177e4SLinus Torvalds * 13451da177e4SLinus Torvalds * CONTEXT: 13469b41ea72SAndrew Morton * raw_spin_lock_irq(rq->lock) 13471da177e4SLinus Torvalds * 13481da177e4SLinus Torvalds * This function is called during schedule() when a kworker is going 1349f756d5e2SNathan Lynch * to sleep. It's used by psi to identify aggregation workers during 1350f756d5e2SNathan Lynch * dequeuing, to allow periodic aggregation to shut-off when that 13511da177e4SLinus Torvalds * worker is the last task in the system or cgroup to go to sleep. 13521da177e4SLinus Torvalds * 13531da177e4SLinus Torvalds * As this function doesn't involve any workqueue-related locking, it 13541da177e4SLinus Torvalds * only returns stable values when called from inside the scheduler's 13551da177e4SLinus Torvalds * queuing and dequeuing paths, when @task, which must be a kworker, 13561da177e4SLinus Torvalds * is guaranteed to not be processing any works. 1357365970a1SDavid Howells * 1358365970a1SDavid Howells * Return: 1359365970a1SDavid Howells * The last work function %current executed as a worker, NULL if it 1360365970a1SDavid Howells * hasn't executed any work yet. 1361365970a1SDavid Howells */ 1362365970a1SDavid Howells work_func_t wq_worker_last_func(struct task_struct *task) 1363365970a1SDavid Howells { 1364365970a1SDavid Howells struct worker *worker = kthread_data(task); 1365365970a1SDavid Howells 1366365970a1SDavid Howells return worker->last_func; 1367365970a1SDavid Howells } 1368365970a1SDavid Howells 1369d302f017STejun Heo /** 13708864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 13718864b4e5STejun Heo * @pwq: pool_workqueue to get 13728864b4e5STejun Heo * 13738864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 13748864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 13758864b4e5STejun Heo */ 13768864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 13778864b4e5STejun Heo { 13788864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 13798864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 13808864b4e5STejun Heo pwq->refcnt++; 13818864b4e5STejun Heo } 13828864b4e5STejun Heo 13838864b4e5STejun Heo /** 13848864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 13858864b4e5STejun Heo * @pwq: pool_workqueue to put 13868864b4e5STejun Heo * 13878864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 13888864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 13898864b4e5STejun Heo */ 13908864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 13918864b4e5STejun Heo { 13928864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 13938864b4e5STejun Heo if (likely(--pwq->refcnt)) 13948864b4e5STejun Heo return; 13958864b4e5STejun Heo /* 1396967b494eSTejun Heo * @pwq can't be released under pool->lock, bounce to a dedicated 1397967b494eSTejun Heo * kthread_worker to avoid A-A deadlocks. 13988864b4e5STejun Heo */ 1399687a9aa5STejun Heo kthread_queue_work(pwq_release_worker, &pwq->release_work); 14008864b4e5STejun Heo } 14018864b4e5STejun Heo 1402dce90d47STejun Heo /** 1403dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1404dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1405dce90d47STejun Heo * 1406dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1407dce90d47STejun Heo */ 1408dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1409dce90d47STejun Heo { 1410dce90d47STejun Heo if (pwq) { 1411dce90d47STejun Heo /* 141224acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the 1413dce90d47STejun Heo * following lock operations are safe. 1414dce90d47STejun Heo */ 1415a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 1416dce90d47STejun Heo put_pwq(pwq); 1417a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 1418dce90d47STejun Heo } 1419dce90d47STejun Heo } 1420dce90d47STejun Heo 1421f97a4a1aSLai Jiangshan static void pwq_activate_inactive_work(struct work_struct *work) 1422bf4ede01STejun Heo { 1423112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1424bf4ede01STejun Heo 1425bf4ede01STejun Heo trace_workqueue_activate_work(work); 142682607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 142782607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1428112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 1429f97a4a1aSLai Jiangshan __clear_bit(WORK_STRUCT_INACTIVE_BIT, work_data_bits(work)); 1430112202d9STejun Heo pwq->nr_active++; 1431bf4ede01STejun Heo } 1432bf4ede01STejun Heo 1433f97a4a1aSLai Jiangshan static void pwq_activate_first_inactive(struct pool_workqueue *pwq) 14343aa62497SLai Jiangshan { 1435f97a4a1aSLai Jiangshan struct work_struct *work = list_first_entry(&pwq->inactive_works, 14363aa62497SLai Jiangshan struct work_struct, entry); 14373aa62497SLai Jiangshan 1438f97a4a1aSLai Jiangshan pwq_activate_inactive_work(work); 14393aa62497SLai Jiangshan } 14403aa62497SLai Jiangshan 1441bf4ede01STejun Heo /** 1442112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1443112202d9STejun Heo * @pwq: pwq of interest 1444c4560c2cSLai Jiangshan * @work_data: work_data of work which left the queue 1445bf4ede01STejun Heo * 1446bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1447112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1448bf4ede01STejun Heo * 1449bf4ede01STejun Heo * CONTEXT: 1450a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1451bf4ede01STejun Heo */ 1452c4560c2cSLai Jiangshan static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) 1453bf4ede01STejun Heo { 1454c4560c2cSLai Jiangshan int color = get_work_color(work_data); 1455c4560c2cSLai Jiangshan 1456018f3a13SLai Jiangshan if (!(work_data & WORK_STRUCT_INACTIVE)) { 1457112202d9STejun Heo pwq->nr_active--; 1458f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 1459f97a4a1aSLai Jiangshan /* one down, submit an inactive one */ 1460112202d9STejun Heo if (pwq->nr_active < pwq->max_active) 1461f97a4a1aSLai Jiangshan pwq_activate_first_inactive(pwq); 1462bf4ede01STejun Heo } 1463018f3a13SLai Jiangshan } 1464018f3a13SLai Jiangshan 1465018f3a13SLai Jiangshan pwq->nr_in_flight[color]--; 1466bf4ede01STejun Heo 1467bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 1468112202d9STejun Heo if (likely(pwq->flush_color != color)) 14698864b4e5STejun Heo goto out_put; 1470bf4ede01STejun Heo 1471bf4ede01STejun Heo /* are there still in-flight works? */ 1472112202d9STejun Heo if (pwq->nr_in_flight[color]) 14738864b4e5STejun Heo goto out_put; 1474bf4ede01STejun Heo 1475112202d9STejun Heo /* this pwq is done, clear flush_color */ 1476112202d9STejun Heo pwq->flush_color = -1; 1477bf4ede01STejun Heo 1478bf4ede01STejun Heo /* 1479112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 1480bf4ede01STejun Heo * will handle the rest. 1481bf4ede01STejun Heo */ 1482112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1483112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 14848864b4e5STejun Heo out_put: 14858864b4e5STejun Heo put_pwq(pwq); 1486bf4ede01STejun Heo } 1487bf4ede01STejun Heo 148836e227d2STejun Heo /** 1489bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 149036e227d2STejun Heo * @work: work item to steal 149136e227d2STejun Heo * @is_dwork: @work is a delayed_work 1492bbb68dfaSTejun Heo * @flags: place to store irq state 149336e227d2STejun Heo * 149436e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 1495d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 149636e227d2STejun Heo * 1497d185af30SYacine Belkadi * Return: 14983eb6b31bSMauro Carvalho Chehab * 14993eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 150036e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 150136e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 150236e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1503bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1504bbb68dfaSTejun Heo * for arbitrarily long 15053eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 150636e227d2STejun Heo * 1507d185af30SYacine Belkadi * Note: 1508bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1509e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 1510e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 1511e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 1512bbb68dfaSTejun Heo * 1513bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1514bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1515bbb68dfaSTejun Heo * 1516e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 1517bf4ede01STejun Heo */ 1518bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1519bbb68dfaSTejun Heo unsigned long *flags) 1520bf4ede01STejun Heo { 1521d565ed63STejun Heo struct worker_pool *pool; 1522112202d9STejun Heo struct pool_workqueue *pwq; 1523bf4ede01STejun Heo 1524bbb68dfaSTejun Heo local_irq_save(*flags); 1525bbb68dfaSTejun Heo 152636e227d2STejun Heo /* try to steal the timer if it exists */ 152736e227d2STejun Heo if (is_dwork) { 152836e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 152936e227d2STejun Heo 1530e0aecdd8STejun Heo /* 1531e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 1532e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 1533e0aecdd8STejun Heo * running on the local CPU. 1534e0aecdd8STejun Heo */ 153536e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 153636e227d2STejun Heo return 1; 153736e227d2STejun Heo } 153836e227d2STejun Heo 153936e227d2STejun Heo /* try to claim PENDING the normal way */ 1540bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1541bf4ede01STejun Heo return 0; 1542bf4ede01STejun Heo 154324acfb71SThomas Gleixner rcu_read_lock(); 1544bf4ede01STejun Heo /* 1545bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1546bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1547bf4ede01STejun Heo */ 1548d565ed63STejun Heo pool = get_work_pool(work); 1549d565ed63STejun Heo if (!pool) 1550bbb68dfaSTejun Heo goto fail; 1551bf4ede01STejun Heo 1552a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pool->lock); 1553bf4ede01STejun Heo /* 1554112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 1555112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 1556112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 1557112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 1558112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 15590b3dae68SLai Jiangshan * item is currently queued on that pool. 1560bf4ede01STejun Heo */ 1561112202d9STejun Heo pwq = get_work_pwq(work); 1562112202d9STejun Heo if (pwq && pwq->pool == pool) { 1563bf4ede01STejun Heo debug_work_deactivate(work); 15643aa62497SLai Jiangshan 15653aa62497SLai Jiangshan /* 1566018f3a13SLai Jiangshan * A cancelable inactive work item must be in the 1567018f3a13SLai Jiangshan * pwq->inactive_works since a queued barrier can't be 1568018f3a13SLai Jiangshan * canceled (see the comments in insert_wq_barrier()). 1569018f3a13SLai Jiangshan * 1570f97a4a1aSLai Jiangshan * An inactive work item cannot be grabbed directly because 1571d812796eSLai Jiangshan * it might have linked barrier work items which, if left 1572f97a4a1aSLai Jiangshan * on the inactive_works list, will confuse pwq->nr_active 157316062836STejun Heo * management later on and cause stall. Make sure the work 157416062836STejun Heo * item is activated before grabbing. 15753aa62497SLai Jiangshan */ 1576f97a4a1aSLai Jiangshan if (*work_data_bits(work) & WORK_STRUCT_INACTIVE) 1577f97a4a1aSLai Jiangshan pwq_activate_inactive_work(work); 15783aa62497SLai Jiangshan 1579bf4ede01STejun Heo list_del_init(&work->entry); 1580c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, *work_data_bits(work)); 158136e227d2STejun Heo 1582112202d9STejun Heo /* work->data points to pwq iff queued, point to pool */ 15834468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 15844468a00fSLai Jiangshan 1585a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 158624acfb71SThomas Gleixner rcu_read_unlock(); 158736e227d2STejun Heo return 1; 1588bf4ede01STejun Heo } 1589a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 1590bbb68dfaSTejun Heo fail: 159124acfb71SThomas Gleixner rcu_read_unlock(); 1592bbb68dfaSTejun Heo local_irq_restore(*flags); 1593bbb68dfaSTejun Heo if (work_is_canceling(work)) 1594bbb68dfaSTejun Heo return -ENOENT; 1595bbb68dfaSTejun Heo cpu_relax(); 159636e227d2STejun Heo return -EAGAIN; 1597bf4ede01STejun Heo } 1598bf4ede01STejun Heo 1599bf4ede01STejun Heo /** 1600706026c2STejun Heo * insert_work - insert a work into a pool 1601112202d9STejun Heo * @pwq: pwq @work belongs to 16024690c4abSTejun Heo * @work: work to insert 16034690c4abSTejun Heo * @head: insertion point 16044690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 16054690c4abSTejun Heo * 1606112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 1607706026c2STejun Heo * work_struct flags. 16084690c4abSTejun Heo * 16094690c4abSTejun Heo * CONTEXT: 1610a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1611365970a1SDavid Howells */ 1612112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 1613112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 1614b89deed3SOleg Nesterov { 1615fe089f87STejun Heo debug_work_activate(work); 1616e1d8aa9fSFrederic Weisbecker 1617e89a85d6SWalter Wu /* record the work call stack in order to print it in KASAN reports */ 1618f70da745SMarco Elver kasan_record_aux_stack_noalloc(work); 1619e89a85d6SWalter Wu 16204690c4abSTejun Heo /* we own @work, set data and link */ 1621112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 16221a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 16238864b4e5STejun Heo get_pwq(pwq); 1624b89deed3SOleg Nesterov } 1625b89deed3SOleg Nesterov 1626c8efcc25STejun Heo /* 1627c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 16288d03ecfeSTejun Heo * same workqueue. 1629c8efcc25STejun Heo */ 1630c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1631c8efcc25STejun Heo { 1632c8efcc25STejun Heo struct worker *worker; 1633c8efcc25STejun Heo 16348d03ecfeSTejun Heo worker = current_wq_worker(); 1635c8efcc25STejun Heo /* 1636bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If 16378d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 1638c8efcc25STejun Heo */ 1639112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 1640c8efcc25STejun Heo } 1641c8efcc25STejun Heo 1642ef557180SMike Galbraith /* 1643ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 1644ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 1645ef557180SMike Galbraith * avoid perturbing sensitive tasks. 1646ef557180SMike Galbraith */ 1647ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 1648ef557180SMike Galbraith { 1649ef557180SMike Galbraith int new_cpu; 1650ef557180SMike Galbraith 1651f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 1652ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 1653ef557180SMike Galbraith return cpu; 1654a8ec5880SAmmar Faizi } else { 1655a8ec5880SAmmar Faizi pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); 1656f303fccbSTejun Heo } 1657f303fccbSTejun Heo 1658ef557180SMike Galbraith if (cpumask_empty(wq_unbound_cpumask)) 1659ef557180SMike Galbraith return cpu; 1660ef557180SMike Galbraith 1661ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 1662ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 1663ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 1664ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 1665ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 1666ef557180SMike Galbraith return cpu; 1667ef557180SMike Galbraith } 1668ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 1669ef557180SMike Galbraith 1670ef557180SMike Galbraith return new_cpu; 1671ef557180SMike Galbraith } 1672ef557180SMike Galbraith 1673d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 16741da177e4SLinus Torvalds struct work_struct *work) 16751da177e4SLinus Torvalds { 1676112202d9STejun Heo struct pool_workqueue *pwq; 1677fe089f87STejun Heo struct worker_pool *last_pool, *pool; 16788a2e8e5dSTejun Heo unsigned int work_flags; 1679b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 16808930cabaSTejun Heo 16818930cabaSTejun Heo /* 16828930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 16838930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 16848930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 16858930cabaSTejun Heo * happen with IRQ disabled. 16868930cabaSTejun Heo */ 16878e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 16881da177e4SLinus Torvalds 16891e19ffc6STejun Heo 169033e3f0a3SRichard Clark /* 169133e3f0a3SRichard Clark * For a draining wq, only works from the same workqueue are 169233e3f0a3SRichard Clark * allowed. The __WQ_DESTROYING helps to spot the issue that 169333e3f0a3SRichard Clark * queues a new work item to a wq after destroy_workqueue(wq). 169433e3f0a3SRichard Clark */ 169533e3f0a3SRichard Clark if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && 169633e3f0a3SRichard Clark WARN_ON_ONCE(!is_chained_work(wq)))) 1697e41e704bSTejun Heo return; 169824acfb71SThomas Gleixner rcu_read_lock(); 16999e8cd2f5STejun Heo retry: 1700aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */ 1701636b927eSTejun Heo if (req_cpu == WORK_CPU_UNBOUND) { 1702636b927eSTejun Heo if (wq->flags & WQ_UNBOUND) 1703ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 1704636b927eSTejun Heo else 1705aa202f1fSHillf Danton cpu = raw_smp_processor_id(); 1706aa202f1fSHillf Danton } 1707f3421797STejun Heo 1708636b927eSTejun Heo pwq = rcu_dereference(*per_cpu_ptr(wq->cpu_pwq, cpu)); 1709fe089f87STejun Heo pool = pwq->pool; 1710fe089f87STejun Heo 171118aa9effSTejun Heo /* 1712c9178087STejun Heo * If @work was previously on a different pool, it might still be 1713c9178087STejun Heo * running there, in which case the work needs to be queued on that 1714c9178087STejun Heo * pool to guarantee non-reentrancy. 171518aa9effSTejun Heo */ 1716c9e7cf27STejun Heo last_pool = get_work_pool(work); 1717fe089f87STejun Heo if (last_pool && last_pool != pool) { 171818aa9effSTejun Heo struct worker *worker; 171918aa9effSTejun Heo 1720a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&last_pool->lock); 172118aa9effSTejun Heo 1722c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 172318aa9effSTejun Heo 1724112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 1725c9178087STejun Heo pwq = worker->current_pwq; 1726fe089f87STejun Heo pool = pwq->pool; 1727fe089f87STejun Heo WARN_ON_ONCE(pool != last_pool); 17288594fadeSLai Jiangshan } else { 172918aa9effSTejun Heo /* meh... not running there, queue here */ 1730a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&last_pool->lock); 1731fe089f87STejun Heo raw_spin_lock(&pool->lock); 173218aa9effSTejun Heo } 17338930cabaSTejun Heo } else { 1734fe089f87STejun Heo raw_spin_lock(&pool->lock); 17358930cabaSTejun Heo } 1736502ca9d8STejun Heo 17379e8cd2f5STejun Heo /* 1738636b927eSTejun Heo * pwq is determined and locked. For unbound pools, we could have raced 1739636b927eSTejun Heo * with pwq release and it could already be dead. If its refcnt is zero, 1740636b927eSTejun Heo * repeat pwq selection. Note that unbound pwqs never die without 1741636b927eSTejun Heo * another pwq replacing it in cpu_pwq or while work items are executing 1742636b927eSTejun Heo * on it, so the retrying is guaranteed to make forward-progress. 17439e8cd2f5STejun Heo */ 17449e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 17459e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 1746fe089f87STejun Heo raw_spin_unlock(&pool->lock); 17479e8cd2f5STejun Heo cpu_relax(); 17489e8cd2f5STejun Heo goto retry; 17499e8cd2f5STejun Heo } 17509e8cd2f5STejun Heo /* oops */ 17519e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 17529e8cd2f5STejun Heo wq->name, cpu); 17539e8cd2f5STejun Heo } 17549e8cd2f5STejun Heo 1755112202d9STejun Heo /* pwq determined, queue */ 1756112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 1757502ca9d8STejun Heo 175824acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry))) 175924acfb71SThomas Gleixner goto out; 17601e19ffc6STejun Heo 1761112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 1762112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 17631e19ffc6STejun Heo 1764112202d9STejun Heo if (likely(pwq->nr_active < pwq->max_active)) { 1765fe089f87STejun Heo if (list_empty(&pool->worklist)) 1766fe089f87STejun Heo pool->watchdog_ts = jiffies; 1767fe089f87STejun Heo 1768cdadf009STejun Heo trace_workqueue_activate_work(work); 1769112202d9STejun Heo pwq->nr_active++; 1770fe089f87STejun Heo insert_work(pwq, work, &pool->worklist, work_flags); 1771*0219a352STejun Heo kick_pool(pool); 17728a2e8e5dSTejun Heo } else { 1773f97a4a1aSLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 1774fe089f87STejun Heo insert_work(pwq, work, &pwq->inactive_works, work_flags); 17758a2e8e5dSTejun Heo } 17761e19ffc6STejun Heo 177724acfb71SThomas Gleixner out: 1778fe089f87STejun Heo raw_spin_unlock(&pool->lock); 177924acfb71SThomas Gleixner rcu_read_unlock(); 17801da177e4SLinus Torvalds } 17811da177e4SLinus Torvalds 17820fcb78c2SRolf Eike Beer /** 1783c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1784c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1785c1a220e7SZhang Rui * @wq: workqueue to use 1786c1a220e7SZhang Rui * @work: work to queue 1787c1a220e7SZhang Rui * 1788c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1789443378f0SPaul E. McKenney * can't go away. Callers that fail to ensure that the specified 1790443378f0SPaul E. McKenney * CPU cannot go away will execute on a randomly chosen CPU. 1791854f5cc5SPaul E. McKenney * But note well that callers specifying a CPU that never has been 1792854f5cc5SPaul E. McKenney * online will get a splat. 1793d185af30SYacine Belkadi * 1794d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 1795c1a220e7SZhang Rui */ 1796d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1797d4283e93STejun Heo struct work_struct *work) 1798c1a220e7SZhang Rui { 1799d4283e93STejun Heo bool ret = false; 18008930cabaSTejun Heo unsigned long flags; 18018930cabaSTejun Heo 18028930cabaSTejun Heo local_irq_save(flags); 1803c1a220e7SZhang Rui 180422df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 18054690c4abSTejun Heo __queue_work(cpu, wq, work); 1806d4283e93STejun Heo ret = true; 1807c1a220e7SZhang Rui } 18088930cabaSTejun Heo 18098930cabaSTejun Heo local_irq_restore(flags); 1810c1a220e7SZhang Rui return ret; 1811c1a220e7SZhang Rui } 1812ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 1813c1a220e7SZhang Rui 18148204e0c1SAlexander Duyck /** 1815fef59c9cSTejun Heo * select_numa_node_cpu - Select a CPU based on NUMA node 18168204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from 18178204e0c1SAlexander Duyck * 18188204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given 18198204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return 18208204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any 18218204e0c1SAlexander Duyck * available CPU if we need to schedule this work. 18228204e0c1SAlexander Duyck */ 1823fef59c9cSTejun Heo static int select_numa_node_cpu(int node) 18248204e0c1SAlexander Duyck { 18258204e0c1SAlexander Duyck int cpu; 18268204e0c1SAlexander Duyck 18278204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */ 18288204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) 18298204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 18308204e0c1SAlexander Duyck 18318204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */ 18328204e0c1SAlexander Duyck cpu = raw_smp_processor_id(); 18338204e0c1SAlexander Duyck if (node == cpu_to_node(cpu)) 18348204e0c1SAlexander Duyck return cpu; 18358204e0c1SAlexander Duyck 18368204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */ 18378204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 18388204e0c1SAlexander Duyck 18398204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */ 18408204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; 18418204e0c1SAlexander Duyck } 18428204e0c1SAlexander Duyck 18438204e0c1SAlexander Duyck /** 18448204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node 18458204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for 18468204e0c1SAlexander Duyck * @wq: workqueue to use 18478204e0c1SAlexander Duyck * @work: work to queue 18488204e0c1SAlexander Duyck * 18498204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic 18508204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given 18518204e0c1SAlexander Duyck * NUMA node. 18528204e0c1SAlexander Duyck * 18538204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto 18548204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is 18558204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior. 18568204e0c1SAlexander Duyck * 18578204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the 18588204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we 18598204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU. 18608204e0c1SAlexander Duyck * 18618204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise. 18628204e0c1SAlexander Duyck */ 18638204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq, 18648204e0c1SAlexander Duyck struct work_struct *work) 18658204e0c1SAlexander Duyck { 18668204e0c1SAlexander Duyck unsigned long flags; 18678204e0c1SAlexander Duyck bool ret = false; 18688204e0c1SAlexander Duyck 18698204e0c1SAlexander Duyck /* 18708204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues. 18718204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given 18728204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node. 18738204e0c1SAlexander Duyck * 18748204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in 18758204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for 18768204e0c1SAlexander Duyck * some round robin type logic. 18778204e0c1SAlexander Duyck */ 18788204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 18798204e0c1SAlexander Duyck 18808204e0c1SAlexander Duyck local_irq_save(flags); 18818204e0c1SAlexander Duyck 18828204e0c1SAlexander Duyck if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 1883fef59c9cSTejun Heo int cpu = select_numa_node_cpu(node); 18848204e0c1SAlexander Duyck 18858204e0c1SAlexander Duyck __queue_work(cpu, wq, work); 18868204e0c1SAlexander Duyck ret = true; 18878204e0c1SAlexander Duyck } 18888204e0c1SAlexander Duyck 18898204e0c1SAlexander Duyck local_irq_restore(flags); 18908204e0c1SAlexander Duyck return ret; 18918204e0c1SAlexander Duyck } 18928204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node); 18938204e0c1SAlexander Duyck 18948c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 18951da177e4SLinus Torvalds { 18968c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 18971da177e4SLinus Torvalds 1898e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 189960c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 19001da177e4SLinus Torvalds } 19011438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 19021da177e4SLinus Torvalds 19037beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 190452bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 19051da177e4SLinus Torvalds { 19067beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 19077beb2edfSTejun Heo struct work_struct *work = &dwork->work; 19081da177e4SLinus Torvalds 1909637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 19104b243563SSami Tolvanen WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 1911fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 1912fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 19137beb2edfSTejun Heo 19148852aac2STejun Heo /* 19158852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 19168852aac2STejun Heo * both optimization and correctness. The earliest @timer can 19178852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 19188852aac2STejun Heo * on that there's no such delay when @delay is 0. 19198852aac2STejun Heo */ 19208852aac2STejun Heo if (!delay) { 19218852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 19228852aac2STejun Heo return; 19238852aac2STejun Heo } 19248852aac2STejun Heo 192560c057bcSLai Jiangshan dwork->wq = wq; 19261265057fSTejun Heo dwork->cpu = cpu; 19277beb2edfSTejun Heo timer->expires = jiffies + delay; 19287beb2edfSTejun Heo 1929041bd12eSTejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 19307beb2edfSTejun Heo add_timer_on(timer, cpu); 1931041bd12eSTejun Heo else 1932041bd12eSTejun Heo add_timer(timer); 19337beb2edfSTejun Heo } 19341da177e4SLinus Torvalds 19350fcb78c2SRolf Eike Beer /** 19360fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 19370fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 19380fcb78c2SRolf Eike Beer * @wq: workqueue to use 1939af9997e4SRandy Dunlap * @dwork: work to queue 19400fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 19410fcb78c2SRolf Eike Beer * 1942d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 1943715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 1944715f1300STejun Heo * execution. 19450fcb78c2SRolf Eike Beer */ 1946d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 194752bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 19487a6bc1cdSVenkatesh Pallipadi { 194952bad64dSDavid Howells struct work_struct *work = &dwork->work; 1950d4283e93STejun Heo bool ret = false; 19518930cabaSTejun Heo unsigned long flags; 19528930cabaSTejun Heo 19538930cabaSTejun Heo /* read the comment in __queue_work() */ 19548930cabaSTejun Heo local_irq_save(flags); 19557a6bc1cdSVenkatesh Pallipadi 195622df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 19577beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 1958d4283e93STejun Heo ret = true; 19597a6bc1cdSVenkatesh Pallipadi } 19608930cabaSTejun Heo 19618930cabaSTejun Heo local_irq_restore(flags); 19627a6bc1cdSVenkatesh Pallipadi return ret; 19637a6bc1cdSVenkatesh Pallipadi } 1964ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 19651da177e4SLinus Torvalds 1966c8e55f36STejun Heo /** 19678376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 19688376fe22STejun Heo * @cpu: CPU number to execute work on 19698376fe22STejun Heo * @wq: workqueue to use 19708376fe22STejun Heo * @dwork: work to queue 19718376fe22STejun Heo * @delay: number of jiffies to wait before queueing 19728376fe22STejun Heo * 19738376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 19748376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 19758376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 19768376fe22STejun Heo * current state. 19778376fe22STejun Heo * 1978d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 19798376fe22STejun Heo * pending and its timer was modified. 19808376fe22STejun Heo * 1981e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 19828376fe22STejun Heo * See try_to_grab_pending() for details. 19838376fe22STejun Heo */ 19848376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 19858376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 19868376fe22STejun Heo { 19878376fe22STejun Heo unsigned long flags; 19888376fe22STejun Heo int ret; 19898376fe22STejun Heo 19908376fe22STejun Heo do { 19918376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 19928376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 19938376fe22STejun Heo 19948376fe22STejun Heo if (likely(ret >= 0)) { 19958376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 19968376fe22STejun Heo local_irq_restore(flags); 19978376fe22STejun Heo } 19988376fe22STejun Heo 19998376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 20008376fe22STejun Heo return ret; 20018376fe22STejun Heo } 20028376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 20038376fe22STejun Heo 200405f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 200505f0fe6bSTejun Heo { 200605f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 200705f0fe6bSTejun Heo 200805f0fe6bSTejun Heo /* read the comment in __queue_work() */ 200905f0fe6bSTejun Heo local_irq_disable(); 201005f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 201105f0fe6bSTejun Heo local_irq_enable(); 201205f0fe6bSTejun Heo } 201305f0fe6bSTejun Heo 201405f0fe6bSTejun Heo /** 201505f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 201605f0fe6bSTejun Heo * @wq: workqueue to use 201705f0fe6bSTejun Heo * @rwork: work to queue 201805f0fe6bSTejun Heo * 201905f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 202005f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 2021bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the 202205f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 202305f0fe6bSTejun Heo */ 202405f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 202505f0fe6bSTejun Heo { 202605f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 202705f0fe6bSTejun Heo 202805f0fe6bSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 202905f0fe6bSTejun Heo rwork->wq = wq; 2030a7e30c0eSUladzislau Rezki call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); 203105f0fe6bSTejun Heo return true; 203205f0fe6bSTejun Heo } 203305f0fe6bSTejun Heo 203405f0fe6bSTejun Heo return false; 203505f0fe6bSTejun Heo } 203605f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 203705f0fe6bSTejun Heo 2038f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 2039c34056a3STejun Heo { 2040c34056a3STejun Heo struct worker *worker; 2041c34056a3STejun Heo 2042f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 2043c8e55f36STejun Heo if (worker) { 2044c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 2045affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 2046da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 2047e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 2048e22bee78STejun Heo worker->flags = WORKER_PREP; 2049c8e55f36STejun Heo } 2050c34056a3STejun Heo return worker; 2051c34056a3STejun Heo } 2052c34056a3STejun Heo 2053c34056a3STejun Heo /** 20544736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 20554736cbf7SLai Jiangshan * @worker: worker to be attached 20564736cbf7SLai Jiangshan * @pool: the target pool 20574736cbf7SLai Jiangshan * 20584736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 20594736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 20604736cbf7SLai Jiangshan * cpu-[un]hotplugs. 20614736cbf7SLai Jiangshan */ 20624736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 20634736cbf7SLai Jiangshan struct worker_pool *pool) 20644736cbf7SLai Jiangshan { 20651258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 20664736cbf7SLai Jiangshan 20674736cbf7SLai Jiangshan /* 20681258fae7STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains 20691258fae7STejun Heo * stable across this function. See the comments above the flag 20701258fae7STejun Heo * definition for details. 20714736cbf7SLai Jiangshan */ 20724736cbf7SLai Jiangshan if (pool->flags & POOL_DISASSOCIATED) 20734736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 20745c25b5ffSPeter Zijlstra else 20755c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu); 20764736cbf7SLai Jiangshan 2077640f17c8SPeter Zijlstra if (worker->rescue_wq) 2078640f17c8SPeter Zijlstra set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask); 2079640f17c8SPeter Zijlstra 20804736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 2081a2d812a2STejun Heo worker->pool = pool; 20824736cbf7SLai Jiangshan 20831258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 20844736cbf7SLai Jiangshan } 20854736cbf7SLai Jiangshan 20864736cbf7SLai Jiangshan /** 208760f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 208860f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 208960f5a4bcSLai Jiangshan * 20904736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 20914736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 20924736cbf7SLai Jiangshan * other reference to the pool. 209360f5a4bcSLai Jiangshan */ 2094a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 209560f5a4bcSLai Jiangshan { 2096a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 209760f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 209860f5a4bcSLai Jiangshan 20991258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 2100a2d812a2STejun Heo 21015c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, -1); 2102da028469SLai Jiangshan list_del(&worker->node); 2103a2d812a2STejun Heo worker->pool = NULL; 2104a2d812a2STejun Heo 2105e02b9312SValentin Schneider if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) 210660f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 21071258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 210860f5a4bcSLai Jiangshan 2109b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 2110b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 2111b62c0751SLai Jiangshan 211260f5a4bcSLai Jiangshan if (detach_completion) 211360f5a4bcSLai Jiangshan complete(detach_completion); 211460f5a4bcSLai Jiangshan } 211560f5a4bcSLai Jiangshan 211660f5a4bcSLai Jiangshan /** 2117c34056a3STejun Heo * create_worker - create a new workqueue worker 211863d95a91STejun Heo * @pool: pool the new worker will belong to 2119c34056a3STejun Heo * 2120051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 2121c34056a3STejun Heo * 2122c34056a3STejun Heo * CONTEXT: 2123c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 2124c34056a3STejun Heo * 2125d185af30SYacine Belkadi * Return: 2126c34056a3STejun Heo * Pointer to the newly created worker. 2127c34056a3STejun Heo */ 2128bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 2129c34056a3STejun Heo { 2130e441b56fSZhen Lei struct worker *worker; 2131e441b56fSZhen Lei int id; 2132e3c916a4STejun Heo char id_buf[16]; 2133c34056a3STejun Heo 21347cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 2135e441b56fSZhen Lei id = ida_alloc(&pool->worker_ida, GFP_KERNEL); 21363f0ea0b8SPetr Mladek if (id < 0) { 21373f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", 21383f0ea0b8SPetr Mladek ERR_PTR(id)); 2139e441b56fSZhen Lei return NULL; 21403f0ea0b8SPetr Mladek } 2141c34056a3STejun Heo 2142f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 21433f0ea0b8SPetr Mladek if (!worker) { 21443f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker\n"); 2145c34056a3STejun Heo goto fail; 21463f0ea0b8SPetr Mladek } 2147c34056a3STejun Heo 2148c34056a3STejun Heo worker->id = id; 2149c34056a3STejun Heo 215029c91e99STejun Heo if (pool->cpu >= 0) 2151e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 2152e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 2153f3421797STejun Heo else 2154e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 2155e3c916a4STejun Heo 2156f3f90ad4STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, pool->node, 2157e3c916a4STejun Heo "kworker/%s", id_buf); 21583f0ea0b8SPetr Mladek if (IS_ERR(worker->task)) { 215960f54038SPetr Mladek if (PTR_ERR(worker->task) == -EINTR) { 216060f54038SPetr Mladek pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", 216160f54038SPetr Mladek id_buf); 216260f54038SPetr Mladek } else { 21633f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to create a worker thread: %pe", 21643f0ea0b8SPetr Mladek worker->task); 216560f54038SPetr Mladek } 2166c34056a3STejun Heo goto fail; 21673f0ea0b8SPetr Mladek } 2168c34056a3STejun Heo 216991151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 217025834c73SPeter Zijlstra kthread_bind_mask(worker->task, pool->attrs->cpumask); 217191151228SOleg Nesterov 2172da028469SLai Jiangshan /* successful, attach the worker to the pool */ 21734736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 2174822d8405STejun Heo 2175051e1850SLai Jiangshan /* start the newly created worker */ 2176a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2177*0219a352STejun Heo 2178051e1850SLai Jiangshan worker->pool->nr_workers++; 2179051e1850SLai Jiangshan worker_enter_idle(worker); 2180*0219a352STejun Heo kick_pool(pool); 2181*0219a352STejun Heo 2182*0219a352STejun Heo /* 2183*0219a352STejun Heo * @worker is waiting on a completion in kthread() and will trigger hung 2184*0219a352STejun Heo * check if not woken up soon. As kick_pool() might not have waken it 2185*0219a352STejun Heo * up, wake it up explicitly once more. 2186*0219a352STejun Heo */ 2187051e1850SLai Jiangshan wake_up_process(worker->task); 2188*0219a352STejun Heo 2189a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2190051e1850SLai Jiangshan 2191c34056a3STejun Heo return worker; 2192822d8405STejun Heo 2193c34056a3STejun Heo fail: 2194e441b56fSZhen Lei ida_free(&pool->worker_ida, id); 2195c34056a3STejun Heo kfree(worker); 2196c34056a3STejun Heo return NULL; 2197c34056a3STejun Heo } 2198c34056a3STejun Heo 2199793777bcSValentin Schneider static void unbind_worker(struct worker *worker) 2200793777bcSValentin Schneider { 2201793777bcSValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2202793777bcSValentin Schneider 2203793777bcSValentin Schneider kthread_set_per_cpu(worker->task, -1); 2204793777bcSValentin Schneider if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) 2205793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); 2206793777bcSValentin Schneider else 2207793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); 2208793777bcSValentin Schneider } 2209793777bcSValentin Schneider 2210e02b9312SValentin Schneider static void wake_dying_workers(struct list_head *cull_list) 2211e02b9312SValentin Schneider { 2212e02b9312SValentin Schneider struct worker *worker, *tmp; 2213e02b9312SValentin Schneider 2214e02b9312SValentin Schneider list_for_each_entry_safe(worker, tmp, cull_list, entry) { 2215e02b9312SValentin Schneider list_del_init(&worker->entry); 2216e02b9312SValentin Schneider unbind_worker(worker); 2217e02b9312SValentin Schneider /* 2218e02b9312SValentin Schneider * If the worker was somehow already running, then it had to be 2219e02b9312SValentin Schneider * in pool->idle_list when set_worker_dying() happened or we 2220e02b9312SValentin Schneider * wouldn't have gotten here. 2221c34056a3STejun Heo * 2222e02b9312SValentin Schneider * Thus, the worker must either have observed the WORKER_DIE 2223e02b9312SValentin Schneider * flag, or have set its state to TASK_IDLE. Either way, the 2224e02b9312SValentin Schneider * below will be observed by the worker and is safe to do 2225e02b9312SValentin Schneider * outside of pool->lock. 2226e02b9312SValentin Schneider */ 2227e02b9312SValentin Schneider wake_up_process(worker->task); 2228e02b9312SValentin Schneider } 2229e02b9312SValentin Schneider } 2230e02b9312SValentin Schneider 2231e02b9312SValentin Schneider /** 2232e02b9312SValentin Schneider * set_worker_dying - Tag a worker for destruction 2233e02b9312SValentin Schneider * @worker: worker to be destroyed 2234e02b9312SValentin Schneider * @list: transfer worker away from its pool->idle_list and into list 2235e02b9312SValentin Schneider * 2236e02b9312SValentin Schneider * Tag @worker for destruction and adjust @pool stats accordingly. The worker 2237e02b9312SValentin Schneider * should be idle. 2238c8e55f36STejun Heo * 2239c8e55f36STejun Heo * CONTEXT: 2240a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2241c34056a3STejun Heo */ 2242e02b9312SValentin Schneider static void set_worker_dying(struct worker *worker, struct list_head *list) 2243c34056a3STejun Heo { 2244bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2245c34056a3STejun Heo 2246cd549687STejun Heo lockdep_assert_held(&pool->lock); 2247e02b9312SValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2248cd549687STejun Heo 2249c34056a3STejun Heo /* sanity check frenzy */ 22506183c009STejun Heo if (WARN_ON(worker->current_work) || 225173eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 225273eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 22536183c009STejun Heo return; 2254c34056a3STejun Heo 2255bd7bdd43STejun Heo pool->nr_workers--; 2256bd7bdd43STejun Heo pool->nr_idle--; 2257c8e55f36STejun Heo 2258cb444766STejun Heo worker->flags |= WORKER_DIE; 2259e02b9312SValentin Schneider 2260e02b9312SValentin Schneider list_move(&worker->entry, list); 2261e02b9312SValentin Schneider list_move(&worker->node, &pool->dying_workers); 2262c34056a3STejun Heo } 2263c34056a3STejun Heo 22643f959aa3SValentin Schneider /** 22653f959aa3SValentin Schneider * idle_worker_timeout - check if some idle workers can now be deleted. 22663f959aa3SValentin Schneider * @t: The pool's idle_timer that just expired 22673f959aa3SValentin Schneider * 22683f959aa3SValentin Schneider * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in 22693f959aa3SValentin Schneider * worker_leave_idle(), as a worker flicking between idle and active while its 22703f959aa3SValentin Schneider * pool is at the too_many_workers() tipping point would cause too much timer 22713f959aa3SValentin Schneider * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let 22723f959aa3SValentin Schneider * it expire and re-evaluate things from there. 22733f959aa3SValentin Schneider */ 227432a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 2275e22bee78STejun Heo { 227632a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 22773f959aa3SValentin Schneider bool do_cull = false; 22783f959aa3SValentin Schneider 22793f959aa3SValentin Schneider if (work_pending(&pool->idle_cull_work)) 22803f959aa3SValentin Schneider return; 22813f959aa3SValentin Schneider 22823f959aa3SValentin Schneider raw_spin_lock_irq(&pool->lock); 22833f959aa3SValentin Schneider 22843f959aa3SValentin Schneider if (too_many_workers(pool)) { 22853f959aa3SValentin Schneider struct worker *worker; 22863f959aa3SValentin Schneider unsigned long expires; 22873f959aa3SValentin Schneider 22883f959aa3SValentin Schneider /* idle_list is kept in LIFO order, check the last one */ 22893f959aa3SValentin Schneider worker = list_entry(pool->idle_list.prev, struct worker, entry); 22903f959aa3SValentin Schneider expires = worker->last_active + IDLE_WORKER_TIMEOUT; 22913f959aa3SValentin Schneider do_cull = !time_before(jiffies, expires); 22923f959aa3SValentin Schneider 22933f959aa3SValentin Schneider if (!do_cull) 22943f959aa3SValentin Schneider mod_timer(&pool->idle_timer, expires); 22953f959aa3SValentin Schneider } 22963f959aa3SValentin Schneider raw_spin_unlock_irq(&pool->lock); 22973f959aa3SValentin Schneider 22983f959aa3SValentin Schneider if (do_cull) 22993f959aa3SValentin Schneider queue_work(system_unbound_wq, &pool->idle_cull_work); 23003f959aa3SValentin Schneider } 23013f959aa3SValentin Schneider 23023f959aa3SValentin Schneider /** 23033f959aa3SValentin Schneider * idle_cull_fn - cull workers that have been idle for too long. 23043f959aa3SValentin Schneider * @work: the pool's work for handling these idle workers 23053f959aa3SValentin Schneider * 23063f959aa3SValentin Schneider * This goes through a pool's idle workers and gets rid of those that have been 23073f959aa3SValentin Schneider * idle for at least IDLE_WORKER_TIMEOUT seconds. 2308e02b9312SValentin Schneider * 2309e02b9312SValentin Schneider * We don't want to disturb isolated CPUs because of a pcpu kworker being 2310e02b9312SValentin Schneider * culled, so this also resets worker affinity. This requires a sleepable 2311e02b9312SValentin Schneider * context, hence the split between timer callback and work item. 23123f959aa3SValentin Schneider */ 23133f959aa3SValentin Schneider static void idle_cull_fn(struct work_struct *work) 23143f959aa3SValentin Schneider { 23153f959aa3SValentin Schneider struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); 23169680540cSYang Yingliang LIST_HEAD(cull_list); 2317e22bee78STejun Heo 2318e02b9312SValentin Schneider /* 2319e02b9312SValentin Schneider * Grabbing wq_pool_attach_mutex here ensures an already-running worker 2320e02b9312SValentin Schneider * cannot proceed beyong worker_detach_from_pool() in its self-destruct 2321e02b9312SValentin Schneider * path. This is required as a previously-preempted worker could run after 2322e02b9312SValentin Schneider * set_worker_dying() has happened but before wake_dying_workers() did. 2323e02b9312SValentin Schneider */ 2324e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 2325a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2326e22bee78STejun Heo 23273347fc9fSLai Jiangshan while (too_many_workers(pool)) { 2328e22bee78STejun Heo struct worker *worker; 2329e22bee78STejun Heo unsigned long expires; 2330e22bee78STejun Heo 233163d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 2332e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 2333e22bee78STejun Heo 23343347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 233563d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 23363347fc9fSLai Jiangshan break; 2337e22bee78STejun Heo } 23383347fc9fSLai Jiangshan 2339e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 2340e22bee78STejun Heo } 2341e22bee78STejun Heo 2342a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2343e02b9312SValentin Schneider wake_dying_workers(&cull_list); 2344e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 2345e22bee78STejun Heo } 2346e22bee78STejun Heo 2347493a1724STejun Heo static void send_mayday(struct work_struct *work) 2348e22bee78STejun Heo { 2349112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2350112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 2351493a1724STejun Heo 23522e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 2353e22bee78STejun Heo 2354493008a8STejun Heo if (!wq->rescuer) 2355493a1724STejun Heo return; 2356e22bee78STejun Heo 2357e22bee78STejun Heo /* mayday mayday mayday */ 2358493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 235977668c8bSLai Jiangshan /* 236077668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 236177668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 236277668c8bSLai Jiangshan * rescuer is done with it. 236377668c8bSLai Jiangshan */ 236477668c8bSLai Jiangshan get_pwq(pwq); 2365493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2366e22bee78STejun Heo wake_up_process(wq->rescuer->task); 2367725e8ec5STejun Heo pwq->stats[PWQ_STAT_MAYDAY]++; 2368493a1724STejun Heo } 2369e22bee78STejun Heo } 2370e22bee78STejun Heo 237132a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 2372e22bee78STejun Heo { 237332a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 2374e22bee78STejun Heo struct work_struct *work; 2375e22bee78STejun Heo 2376a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2377a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ 2378e22bee78STejun Heo 237963d95a91STejun Heo if (need_to_create_worker(pool)) { 2380e22bee78STejun Heo /* 2381e22bee78STejun Heo * We've been trying to create a new worker but 2382e22bee78STejun Heo * haven't been successful. We might be hitting an 2383e22bee78STejun Heo * allocation deadlock. Send distress signals to 2384e22bee78STejun Heo * rescuers. 2385e22bee78STejun Heo */ 238663d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 2387e22bee78STejun Heo send_mayday(work); 2388e22bee78STejun Heo } 2389e22bee78STejun Heo 2390a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2391a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2392e22bee78STejun Heo 239363d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 2394e22bee78STejun Heo } 2395e22bee78STejun Heo 2396e22bee78STejun Heo /** 2397e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 239863d95a91STejun Heo * @pool: pool to create a new worker for 2399e22bee78STejun Heo * 240063d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 2401e22bee78STejun Heo * have at least one idle worker on return from this function. If 2402e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 240363d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 2404e22bee78STejun Heo * possible allocation deadlock. 2405e22bee78STejun Heo * 2406c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 2407c5aa87bbSTejun Heo * may_start_working() %true. 2408e22bee78STejun Heo * 2409e22bee78STejun Heo * LOCKING: 2410a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2411e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 2412e22bee78STejun Heo * manager. 2413e22bee78STejun Heo */ 241429187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 2415d565ed63STejun Heo __releases(&pool->lock) 2416d565ed63STejun Heo __acquires(&pool->lock) 2417e22bee78STejun Heo { 2418e22bee78STejun Heo restart: 2419a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 24209f9c2364STejun Heo 2421e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 242263d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 2423e22bee78STejun Heo 2424e22bee78STejun Heo while (true) { 2425051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 2426e22bee78STejun Heo break; 2427e22bee78STejun Heo 2428e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 24299f9c2364STejun Heo 243063d95a91STejun Heo if (!need_to_create_worker(pool)) 2431e22bee78STejun Heo break; 2432e22bee78STejun Heo } 2433e22bee78STejun Heo 243463d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 2435a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2436051e1850SLai Jiangshan /* 2437051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 2438051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 2439051e1850SLai Jiangshan * already become busy. 2440051e1850SLai Jiangshan */ 244163d95a91STejun Heo if (need_to_create_worker(pool)) 2442e22bee78STejun Heo goto restart; 2443e22bee78STejun Heo } 2444e22bee78STejun Heo 2445e22bee78STejun Heo /** 2446e22bee78STejun Heo * manage_workers - manage worker pool 2447e22bee78STejun Heo * @worker: self 2448e22bee78STejun Heo * 2449706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 2450e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 2451706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 2452e22bee78STejun Heo * 2453e22bee78STejun Heo * The caller can safely start processing works on false return. On 2454e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 2455e22bee78STejun Heo * and may_start_working() is true. 2456e22bee78STejun Heo * 2457e22bee78STejun Heo * CONTEXT: 2458a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2459e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 2460e22bee78STejun Heo * 2461d185af30SYacine Belkadi * Return: 246229187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 246329187a9eSTejun Heo * start processing works, %true if management function was performed and 246429187a9eSTejun Heo * the conditions that the caller verified before calling the function may 246529187a9eSTejun Heo * no longer be true. 2466e22bee78STejun Heo */ 2467e22bee78STejun Heo static bool manage_workers(struct worker *worker) 2468e22bee78STejun Heo { 246963d95a91STejun Heo struct worker_pool *pool = worker->pool; 2470e22bee78STejun Heo 2471692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 247229187a9eSTejun Heo return false; 2473692b4825STejun Heo 2474692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 24752607d7a6STejun Heo pool->manager = worker; 2476e22bee78STejun Heo 247729187a9eSTejun Heo maybe_create_worker(pool); 2478e22bee78STejun Heo 24792607d7a6STejun Heo pool->manager = NULL; 2480692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 2481d8bb65abSSebastian Andrzej Siewior rcuwait_wake_up(&manager_wait); 248229187a9eSTejun Heo return true; 2483e22bee78STejun Heo } 2484e22bee78STejun Heo 2485a62428c0STejun Heo /** 2486a62428c0STejun Heo * process_one_work - process single work 2487c34056a3STejun Heo * @worker: self 2488a62428c0STejun Heo * @work: work to process 2489a62428c0STejun Heo * 2490a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2491a62428c0STejun Heo * process a single work including synchronization against and 2492a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2493a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2494a62428c0STejun Heo * call this function to process a work. 2495a62428c0STejun Heo * 2496a62428c0STejun Heo * CONTEXT: 2497a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which is released and regrabbed. 2498a62428c0STejun Heo */ 2499c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 2500d565ed63STejun Heo __releases(&pool->lock) 2501d565ed63STejun Heo __acquires(&pool->lock) 25021da177e4SLinus Torvalds { 2503112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2504bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2505c4560c2cSLai Jiangshan unsigned long work_data; 25064e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 25074e6045f1SJohannes Berg /* 2508a62428c0STejun Heo * It is permissible to free the struct work_struct from 2509a62428c0STejun Heo * inside the function that is called from it, this we need to 2510a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2511a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2512a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 25134e6045f1SJohannes Berg */ 25144d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 25154d82a1deSPeter Zijlstra 25164d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 25174e6045f1SJohannes Berg #endif 2518807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 251985327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 2520ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 252125511a47STejun Heo 25228930cabaSTejun Heo /* claim and dequeue */ 2523dc186ad7SThomas Gleixner debug_work_deactivate(work); 2524c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 2525c34056a3STejun Heo worker->current_work = work; 2526a2c1c57bSTejun Heo worker->current_func = work->func; 2527112202d9STejun Heo worker->current_pwq = pwq; 2528616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 2529c4560c2cSLai Jiangshan work_data = *work_data_bits(work); 2530d812796eSLai Jiangshan worker->current_color = get_work_color(work_data); 25317a22ad75STejun Heo 25328bf89593STejun Heo /* 25338bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 25348bf89593STejun Heo * overridden through set_worker_desc(). 25358bf89593STejun Heo */ 25368bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 25378bf89593STejun Heo 2538a62428c0STejun Heo list_del_init(&work->entry); 2539a62428c0STejun Heo 2540649027d7STejun Heo /* 2541228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 2542228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 2543228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 2544228f1d00SLai Jiangshan * execution of the pending work items. 2545fb0e7bebSTejun Heo */ 2546616db877STejun Heo if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE)) 2547228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 2548fb0e7bebSTejun Heo 2549974271c4STejun Heo /* 2550*0219a352STejun Heo * Kick @pool if necessary. It's always noop for per-cpu worker pools 2551*0219a352STejun Heo * since nr_running would always be >= 1 at this point. This is used to 2552*0219a352STejun Heo * chain execution of the pending work items for WORKER_NOT_RUNNING 2553*0219a352STejun Heo * workers such as the UNBOUND and CPU_INTENSIVE ones. 2554974271c4STejun Heo */ 2555*0219a352STejun Heo kick_pool(pool); 2556974271c4STejun Heo 25578930cabaSTejun Heo /* 25587c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 2559d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 256023657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 256123657bb1STejun Heo * disabled. 25628930cabaSTejun Heo */ 25637c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 25641da177e4SLinus Torvalds 2565a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2566365970a1SDavid Howells 2567a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 25683295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2569e6f3faa7SPeter Zijlstra /* 2570f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 2571f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 2572e6f3faa7SPeter Zijlstra * 2573e6f3faa7SPeter Zijlstra * However, that would result in: 2574e6f3faa7SPeter Zijlstra * 2575e6f3faa7SPeter Zijlstra * A(W1) 2576e6f3faa7SPeter Zijlstra * WFC(C) 2577e6f3faa7SPeter Zijlstra * A(W1) 2578e6f3faa7SPeter Zijlstra * C(C) 2579e6f3faa7SPeter Zijlstra * 2580e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 2581e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 2582e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 2583f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 2584e6f3faa7SPeter Zijlstra * these locks. 2585e6f3faa7SPeter Zijlstra * 2586e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 2587e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 2588e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 2589e6f3faa7SPeter Zijlstra */ 2590f52be570SPeter Zijlstra lockdep_invariant_state(true); 2591725e8ec5STejun Heo pwq->stats[PWQ_STAT_STARTED]++; 2592e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 2593a2c1c57bSTejun Heo worker->current_func(work); 2594e36c886aSArjan van de Ven /* 2595e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2596e36c886aSArjan van de Ven * point will only record its address. 2597e36c886aSArjan van de Ven */ 25981c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func); 2599725e8ec5STejun Heo pwq->stats[PWQ_STAT_COMPLETED]++; 26003295f0efSIngo Molnar lock_map_release(&lockdep_map); 2601112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 26021da177e4SLinus Torvalds 2603d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2604044c782cSValentin Ilie pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n" 2605d75f773cSSakari Ailus " last function: %ps\n", 2606a2c1c57bSTejun Heo current->comm, preempt_count(), task_pid_nr(current), 2607a2c1c57bSTejun Heo worker->current_func); 2608d5abe669SPeter Zijlstra debug_show_held_locks(current); 2609d5abe669SPeter Zijlstra dump_stack(); 2610d5abe669SPeter Zijlstra } 2611d5abe669SPeter Zijlstra 2612b22ce278STejun Heo /* 2613025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION 2614b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 2615b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 2616b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 2617789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 2618789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 2619b22ce278STejun Heo */ 2620a7e6425eSPaul E. McKenney cond_resched(); 2621b22ce278STejun Heo 2622a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2623a62428c0STejun Heo 2624616db877STejun Heo /* 2625616db877STejun Heo * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked 2626616db877STejun Heo * CPU intensive by wq_worker_tick() if @work hogged CPU longer than 2627616db877STejun Heo * wq_cpu_intensive_thresh_us. Clear it. 2628616db877STejun Heo */ 2629fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2630fb0e7bebSTejun Heo 26311b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 26321b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 26331b69ac6bSJohannes Weiner 2634a62428c0STejun Heo /* we're done with it, release */ 263542f8570fSSasha Levin hash_del(&worker->hentry); 2636c34056a3STejun Heo worker->current_work = NULL; 2637a2c1c57bSTejun Heo worker->current_func = NULL; 2638112202d9STejun Heo worker->current_pwq = NULL; 2639d812796eSLai Jiangshan worker->current_color = INT_MAX; 2640c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, work_data); 26411da177e4SLinus Torvalds } 26421da177e4SLinus Torvalds 2643affee4b2STejun Heo /** 2644affee4b2STejun Heo * process_scheduled_works - process scheduled works 2645affee4b2STejun Heo * @worker: self 2646affee4b2STejun Heo * 2647affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2648affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2649affee4b2STejun Heo * fetches a work from the top and executes it. 2650affee4b2STejun Heo * 2651affee4b2STejun Heo * CONTEXT: 2652a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2653affee4b2STejun Heo * multiple times. 2654affee4b2STejun Heo */ 2655affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 26561da177e4SLinus Torvalds { 2657c0ab017dSTejun Heo struct work_struct *work; 2658c0ab017dSTejun Heo bool first = true; 2659c0ab017dSTejun Heo 2660c0ab017dSTejun Heo while ((work = list_first_entry_or_null(&worker->scheduled, 2661c0ab017dSTejun Heo struct work_struct, entry))) { 2662c0ab017dSTejun Heo if (first) { 2663c0ab017dSTejun Heo worker->pool->watchdog_ts = jiffies; 2664c0ab017dSTejun Heo first = false; 2665c0ab017dSTejun Heo } 2666c34056a3STejun Heo process_one_work(worker, work); 2667a62428c0STejun Heo } 26681da177e4SLinus Torvalds } 26691da177e4SLinus Torvalds 2670197f6accSTejun Heo static void set_pf_worker(bool val) 2671197f6accSTejun Heo { 2672197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 2673197f6accSTejun Heo if (val) 2674197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 2675197f6accSTejun Heo else 2676197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 2677197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 2678197f6accSTejun Heo } 2679197f6accSTejun Heo 26804690c4abSTejun Heo /** 26814690c4abSTejun Heo * worker_thread - the worker thread function 2682c34056a3STejun Heo * @__worker: self 26834690c4abSTejun Heo * 2684c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 2685c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 2686c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 2687c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 2688c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 2689d185af30SYacine Belkadi * 2690d185af30SYacine Belkadi * Return: 0 26914690c4abSTejun Heo */ 2692c34056a3STejun Heo static int worker_thread(void *__worker) 26931da177e4SLinus Torvalds { 2694c34056a3STejun Heo struct worker *worker = __worker; 2695bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 26961da177e4SLinus Torvalds 2697e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2698197f6accSTejun Heo set_pf_worker(true); 2699c8e55f36STejun Heo woke_up: 2700a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2701affee4b2STejun Heo 2702a9ab775bSTejun Heo /* am I supposed to die? */ 2703a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 2704a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2705197f6accSTejun Heo set_pf_worker(false); 270660f5a4bcSLai Jiangshan 270760f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 2708e441b56fSZhen Lei ida_free(&pool->worker_ida, worker->id); 2709a2d812a2STejun Heo worker_detach_from_pool(worker); 2710e02b9312SValentin Schneider WARN_ON_ONCE(!list_empty(&worker->entry)); 271160f5a4bcSLai Jiangshan kfree(worker); 2712c8e55f36STejun Heo return 0; 2713c8e55f36STejun Heo } 2714c8e55f36STejun Heo 2715c8e55f36STejun Heo worker_leave_idle(worker); 2716db7bccf4STejun Heo recheck: 2717e22bee78STejun Heo /* no more worker necessary? */ 271863d95a91STejun Heo if (!need_more_worker(pool)) 2719e22bee78STejun Heo goto sleep; 2720e22bee78STejun Heo 2721e22bee78STejun Heo /* do we need to manage? */ 272263d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2723e22bee78STejun Heo goto recheck; 2724e22bee78STejun Heo 2725c8e55f36STejun Heo /* 2726c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2727c8e55f36STejun Heo * preparing to process a work or actually processing it. 2728c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2729c8e55f36STejun Heo */ 27306183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 2731c8e55f36STejun Heo 2732e22bee78STejun Heo /* 2733a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 2734a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 2735a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 2736a9ab775bSTejun Heo * management if applicable and concurrency management is restored 2737a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 2738e22bee78STejun Heo */ 2739a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 2740e22bee78STejun Heo 2741e22bee78STejun Heo do { 2742affee4b2STejun Heo struct work_struct *work = 2743bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2744affee4b2STejun Heo struct work_struct, entry); 2745affee4b2STejun Heo 2746873eaca6STejun Heo if (assign_work(work, worker, NULL)) 2747affee4b2STejun Heo process_scheduled_works(worker); 274863d95a91STejun Heo } while (keep_working(pool)); 2749affee4b2STejun Heo 2750228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 2751d313dd85STejun Heo sleep: 2752c8e55f36STejun Heo /* 2753d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 2754d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 2755d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 2756d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 2757d565ed63STejun Heo * event. 2758c8e55f36STejun Heo */ 2759c8e55f36STejun Heo worker_enter_idle(worker); 2760c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 2761a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 27621da177e4SLinus Torvalds schedule(); 2763c8e55f36STejun Heo goto woke_up; 27641da177e4SLinus Torvalds } 27651da177e4SLinus Torvalds 2766e22bee78STejun Heo /** 2767e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2768111c225aSTejun Heo * @__rescuer: self 2769e22bee78STejun Heo * 2770e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2771493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 2772e22bee78STejun Heo * 2773706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 2774e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2775e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2776e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2777e22bee78STejun Heo * the problem rescuer solves. 2778e22bee78STejun Heo * 2779706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 2780706026c2STejun Heo * workqueues which have works queued on the pool and let them process 2781e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2782e22bee78STejun Heo * 2783e22bee78STejun Heo * This should happen rarely. 2784d185af30SYacine Belkadi * 2785d185af30SYacine Belkadi * Return: 0 2786e22bee78STejun Heo */ 2787111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 2788e22bee78STejun Heo { 2789111c225aSTejun Heo struct worker *rescuer = __rescuer; 2790111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 27914d595b86SLai Jiangshan bool should_stop; 2792e22bee78STejun Heo 2793e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2794111c225aSTejun Heo 2795111c225aSTejun Heo /* 2796111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 2797111c225aSTejun Heo * doesn't participate in concurrency management. 2798111c225aSTejun Heo */ 2799197f6accSTejun Heo set_pf_worker(true); 2800e22bee78STejun Heo repeat: 2801c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 28021da177e4SLinus Torvalds 28034d595b86SLai Jiangshan /* 28044d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 28054d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 28064d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 28074d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 28084d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 28094d595b86SLai Jiangshan * list is always empty on exit. 28104d595b86SLai Jiangshan */ 28114d595b86SLai Jiangshan should_stop = kthread_should_stop(); 28121da177e4SLinus Torvalds 2813493a1724STejun Heo /* see whether any pwq is asking for help */ 2814a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 2815493a1724STejun Heo 2816493a1724STejun Heo while (!list_empty(&wq->maydays)) { 2817493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 2818493a1724STejun Heo struct pool_workqueue, mayday_node); 2819112202d9STejun Heo struct worker_pool *pool = pwq->pool; 2820e22bee78STejun Heo struct work_struct *work, *n; 2821e22bee78STejun Heo 2822e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2823493a1724STejun Heo list_del_init(&pwq->mayday_node); 2824493a1724STejun Heo 2825a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 2826e22bee78STejun Heo 282751697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 282851697d39SLai Jiangshan 2829a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2830e22bee78STejun Heo 2831e22bee78STejun Heo /* 2832e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2833e22bee78STejun Heo * process'em. 2834e22bee78STejun Heo */ 2835873eaca6STejun Heo WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); 283682607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 2837873eaca6STejun Heo if (get_work_pwq(work) == pwq && 2838873eaca6STejun Heo assign_work(work, rescuer, &n)) 2839725e8ec5STejun Heo pwq->stats[PWQ_STAT_RESCUED]++; 284082607adcSTejun Heo } 2841e22bee78STejun Heo 2842873eaca6STejun Heo if (!list_empty(&rescuer->scheduled)) { 2843e22bee78STejun Heo process_scheduled_works(rescuer); 28447576958aSTejun Heo 28457576958aSTejun Heo /* 2846008847f6SNeilBrown * The above execution of rescued work items could 2847008847f6SNeilBrown * have created more to rescue through 2848f97a4a1aSLai Jiangshan * pwq_activate_first_inactive() or chained 2849008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 2850008847f6SNeilBrown * that such back-to-back work items, which may be 2851008847f6SNeilBrown * being used to relieve memory pressure, don't 2852008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 2853008847f6SNeilBrown */ 28544f3f4cf3SLai Jiangshan if (pwq->nr_active && need_to_create_worker(pool)) { 2855a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); 2856e66b39afSTejun Heo /* 2857e66b39afSTejun Heo * Queue iff we aren't racing destruction 2858e66b39afSTejun Heo * and somebody else hasn't queued it already. 2859e66b39afSTejun Heo */ 2860e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) { 2861008847f6SNeilBrown get_pwq(pwq); 2862e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2863e66b39afSTejun Heo } 2864a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2865008847f6SNeilBrown } 2866008847f6SNeilBrown } 2867008847f6SNeilBrown 2868008847f6SNeilBrown /* 286977668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 287013b1d625SLai Jiangshan * go away while we're still attached to it. 287177668c8bSLai Jiangshan */ 287277668c8bSLai Jiangshan put_pwq(pwq); 287377668c8bSLai Jiangshan 287477668c8bSLai Jiangshan /* 2875*0219a352STejun Heo * Leave this pool. Notify regular workers; otherwise, we end up 2876*0219a352STejun Heo * with 0 concurrency and stalling the execution. 28777576958aSTejun Heo */ 2878*0219a352STejun Heo kick_pool(pool); 28797576958aSTejun Heo 2880a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 288113b1d625SLai Jiangshan 2882a2d812a2STejun Heo worker_detach_from_pool(rescuer); 288313b1d625SLai Jiangshan 2884a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 28851da177e4SLinus Torvalds } 28861da177e4SLinus Torvalds 2887a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 2888493a1724STejun Heo 28894d595b86SLai Jiangshan if (should_stop) { 28904d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 2891197f6accSTejun Heo set_pf_worker(false); 28924d595b86SLai Jiangshan return 0; 28934d595b86SLai Jiangshan } 28944d595b86SLai Jiangshan 2895111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 2896111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 2897e22bee78STejun Heo schedule(); 2898e22bee78STejun Heo goto repeat; 28991da177e4SLinus Torvalds } 29001da177e4SLinus Torvalds 2901fca839c0STejun Heo /** 2902fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 2903fca839c0STejun Heo * @target_wq: workqueue being flushed 2904fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 2905fca839c0STejun Heo * 2906fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 2907fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 2908fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 2909fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 2910fca839c0STejun Heo * a deadlock. 2911fca839c0STejun Heo */ 2912fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 2913fca839c0STejun Heo struct work_struct *target_work) 2914fca839c0STejun Heo { 2915fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 2916fca839c0STejun Heo struct worker *worker; 2917fca839c0STejun Heo 2918fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 2919fca839c0STejun Heo return; 2920fca839c0STejun Heo 2921fca839c0STejun Heo worker = current_wq_worker(); 2922fca839c0STejun Heo 2923fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 2924d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", 2925fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 292623d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 292723d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 2928d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", 2929fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 2930fca839c0STejun Heo target_wq->name, target_func); 2931fca839c0STejun Heo } 2932fca839c0STejun Heo 2933fc2e4d70SOleg Nesterov struct wq_barrier { 2934fc2e4d70SOleg Nesterov struct work_struct work; 2935fc2e4d70SOleg Nesterov struct completion done; 29362607d7a6STejun Heo struct task_struct *task; /* purely informational */ 2937fc2e4d70SOleg Nesterov }; 2938fc2e4d70SOleg Nesterov 2939fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2940fc2e4d70SOleg Nesterov { 2941fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2942fc2e4d70SOleg Nesterov complete(&barr->done); 2943fc2e4d70SOleg Nesterov } 2944fc2e4d70SOleg Nesterov 29454690c4abSTejun Heo /** 29464690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 2947112202d9STejun Heo * @pwq: pwq to insert barrier into 29484690c4abSTejun Heo * @barr: wq_barrier to insert 2949affee4b2STejun Heo * @target: target work to attach @barr to 2950affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 29514690c4abSTejun Heo * 2952affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2953affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2954affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2955affee4b2STejun Heo * cpu. 2956affee4b2STejun Heo * 2957affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2958affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2959affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2960affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2961affee4b2STejun Heo * after a work with LINKED flag set. 2962affee4b2STejun Heo * 2963affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2964112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 29654690c4abSTejun Heo * 29664690c4abSTejun Heo * CONTEXT: 2967a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 29684690c4abSTejun Heo */ 2969112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 2970affee4b2STejun Heo struct wq_barrier *barr, 2971affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2972fc2e4d70SOleg Nesterov { 2973d812796eSLai Jiangshan unsigned int work_flags = 0; 2974d812796eSLai Jiangshan unsigned int work_color; 2975affee4b2STejun Heo struct list_head *head; 2976affee4b2STejun Heo 2977dc186ad7SThomas Gleixner /* 2978d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 2979dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2980dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2981dc186ad7SThomas Gleixner * might deadlock. 2982dc186ad7SThomas Gleixner */ 2983ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 298422df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 298552fa5bc5SBoqun Feng 2986fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 2987fd1a5b04SByungchul Park 29882607d7a6STejun Heo barr->task = current; 298983c22520SOleg Nesterov 2990018f3a13SLai Jiangshan /* The barrier work item does not participate in pwq->nr_active. */ 2991018f3a13SLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 2992018f3a13SLai Jiangshan 2993affee4b2STejun Heo /* 2994affee4b2STejun Heo * If @target is currently being executed, schedule the 2995affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2996affee4b2STejun Heo */ 2997d812796eSLai Jiangshan if (worker) { 2998affee4b2STejun Heo head = worker->scheduled.next; 2999d812796eSLai Jiangshan work_color = worker->current_color; 3000d812796eSLai Jiangshan } else { 3001affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 3002affee4b2STejun Heo 3003affee4b2STejun Heo head = target->entry.next; 3004affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 3005d21cece0SLai Jiangshan work_flags |= *bits & WORK_STRUCT_LINKED; 3006d812796eSLai Jiangshan work_color = get_work_color(*bits); 3007affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 3008affee4b2STejun Heo } 3009affee4b2STejun Heo 3010d812796eSLai Jiangshan pwq->nr_in_flight[work_color]++; 3011d812796eSLai Jiangshan work_flags |= work_color_to_flags(work_color); 3012d812796eSLai Jiangshan 3013d21cece0SLai Jiangshan insert_work(pwq, &barr->work, head, work_flags); 3014fc2e4d70SOleg Nesterov } 3015fc2e4d70SOleg Nesterov 301673f53c4aSTejun Heo /** 3017112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 301873f53c4aSTejun Heo * @wq: workqueue being flushed 301973f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 302073f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 302173f53c4aSTejun Heo * 3022112202d9STejun Heo * Prepare pwqs for workqueue flushing. 302373f53c4aSTejun Heo * 3024112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 3025112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 3026112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 3027112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 3028112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 302973f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 303073f53c4aSTejun Heo * 303173f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 303273f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 303373f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 303473f53c4aSTejun Heo * is returned. 303573f53c4aSTejun Heo * 3036112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 303773f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 303873f53c4aSTejun Heo * advanced to @work_color. 303973f53c4aSTejun Heo * 304073f53c4aSTejun Heo * CONTEXT: 30413c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 304273f53c4aSTejun Heo * 3043d185af30SYacine Belkadi * Return: 304473f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 304573f53c4aSTejun Heo * otherwise. 304673f53c4aSTejun Heo */ 3047112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 304873f53c4aSTejun Heo int flush_color, int work_color) 30491da177e4SLinus Torvalds { 305073f53c4aSTejun Heo bool wait = false; 305149e3cf44STejun Heo struct pool_workqueue *pwq; 30521da177e4SLinus Torvalds 305373f53c4aSTejun Heo if (flush_color >= 0) { 30546183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 3055112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 3056dc186ad7SThomas Gleixner } 305714441960SOleg Nesterov 305849e3cf44STejun Heo for_each_pwq(pwq, wq) { 3059112202d9STejun Heo struct worker_pool *pool = pwq->pool; 30601da177e4SLinus Torvalds 3061a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 306273f53c4aSTejun Heo 306373f53c4aSTejun Heo if (flush_color >= 0) { 30646183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 306573f53c4aSTejun Heo 3066112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 3067112202d9STejun Heo pwq->flush_color = flush_color; 3068112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 306973f53c4aSTejun Heo wait = true; 30701da177e4SLinus Torvalds } 307173f53c4aSTejun Heo } 307273f53c4aSTejun Heo 307373f53c4aSTejun Heo if (work_color >= 0) { 30746183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 3075112202d9STejun Heo pwq->work_color = work_color; 307673f53c4aSTejun Heo } 307773f53c4aSTejun Heo 3078a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 30791da177e4SLinus Torvalds } 30801da177e4SLinus Torvalds 3081112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 308273f53c4aSTejun Heo complete(&wq->first_flusher->done); 308373f53c4aSTejun Heo 308473f53c4aSTejun Heo return wait; 308583c22520SOleg Nesterov } 30861da177e4SLinus Torvalds 30870fcb78c2SRolf Eike Beer /** 3088c4f135d6STetsuo Handa * __flush_workqueue - ensure that any scheduled work has run to completion. 30890fcb78c2SRolf Eike Beer * @wq: workqueue to flush 30901da177e4SLinus Torvalds * 3091c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 3092c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 30931da177e4SLinus Torvalds */ 3094c4f135d6STetsuo Handa void __flush_workqueue(struct workqueue_struct *wq) 30951da177e4SLinus Torvalds { 309673f53c4aSTejun Heo struct wq_flusher this_flusher = { 309773f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 309873f53c4aSTejun Heo .flush_color = -1, 3099fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 310073f53c4aSTejun Heo }; 310173f53c4aSTejun Heo int next_color; 3102b1f4ec17SOleg Nesterov 31033347fa09STejun Heo if (WARN_ON(!wq_online)) 31043347fa09STejun Heo return; 31053347fa09STejun Heo 310687915adcSJohannes Berg lock_map_acquire(&wq->lockdep_map); 310787915adcSJohannes Berg lock_map_release(&wq->lockdep_map); 310887915adcSJohannes Berg 31093c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 311073f53c4aSTejun Heo 311173f53c4aSTejun Heo /* 311273f53c4aSTejun Heo * Start-to-wait phase 311373f53c4aSTejun Heo */ 311473f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 311573f53c4aSTejun Heo 311673f53c4aSTejun Heo if (next_color != wq->flush_color) { 311773f53c4aSTejun Heo /* 311873f53c4aSTejun Heo * Color space is not full. The current work_color 311973f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 312073f53c4aSTejun Heo * by one. 312173f53c4aSTejun Heo */ 31226183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 312373f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 312473f53c4aSTejun Heo wq->work_color = next_color; 312573f53c4aSTejun Heo 312673f53c4aSTejun Heo if (!wq->first_flusher) { 312773f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 31286183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 312973f53c4aSTejun Heo 313073f53c4aSTejun Heo wq->first_flusher = &this_flusher; 313173f53c4aSTejun Heo 3132112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 313373f53c4aSTejun Heo wq->work_color)) { 313473f53c4aSTejun Heo /* nothing to flush, done */ 313573f53c4aSTejun Heo wq->flush_color = next_color; 313673f53c4aSTejun Heo wq->first_flusher = NULL; 313773f53c4aSTejun Heo goto out_unlock; 313873f53c4aSTejun Heo } 313973f53c4aSTejun Heo } else { 314073f53c4aSTejun Heo /* wait in queue */ 31416183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 314273f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 3143112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 314473f53c4aSTejun Heo } 314573f53c4aSTejun Heo } else { 314673f53c4aSTejun Heo /* 314773f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 314873f53c4aSTejun Heo * The next flush completion will assign us 314973f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 315073f53c4aSTejun Heo */ 315173f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 315273f53c4aSTejun Heo } 315373f53c4aSTejun Heo 3154fca839c0STejun Heo check_flush_dependency(wq, NULL); 3155fca839c0STejun Heo 31563c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 315773f53c4aSTejun Heo 315873f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 315973f53c4aSTejun Heo 316073f53c4aSTejun Heo /* 316173f53c4aSTejun Heo * Wake-up-and-cascade phase 316273f53c4aSTejun Heo * 316373f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 316473f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 316573f53c4aSTejun Heo */ 316600d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher) 316773f53c4aSTejun Heo return; 316873f53c4aSTejun Heo 31693c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 317073f53c4aSTejun Heo 31714ce48b37STejun Heo /* we might have raced, check again with mutex held */ 31724ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 31734ce48b37STejun Heo goto out_unlock; 31744ce48b37STejun Heo 317500d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL); 317673f53c4aSTejun Heo 31776183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 31786183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 317973f53c4aSTejun Heo 318073f53c4aSTejun Heo while (true) { 318173f53c4aSTejun Heo struct wq_flusher *next, *tmp; 318273f53c4aSTejun Heo 318373f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 318473f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 318573f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 318673f53c4aSTejun Heo break; 318773f53c4aSTejun Heo list_del_init(&next->list); 318873f53c4aSTejun Heo complete(&next->done); 318973f53c4aSTejun Heo } 319073f53c4aSTejun Heo 31916183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 319273f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 319373f53c4aSTejun Heo 319473f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 319573f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 319673f53c4aSTejun Heo 319773f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 319873f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 319973f53c4aSTejun Heo /* 320073f53c4aSTejun Heo * Assign the same color to all overflowed 320173f53c4aSTejun Heo * flushers, advance work_color and append to 320273f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 320373f53c4aSTejun Heo * phase for these overflowed flushers. 320473f53c4aSTejun Heo */ 320573f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 320673f53c4aSTejun Heo tmp->flush_color = wq->work_color; 320773f53c4aSTejun Heo 320873f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 320973f53c4aSTejun Heo 321073f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 321173f53c4aSTejun Heo &wq->flusher_queue); 3212112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 321373f53c4aSTejun Heo } 321473f53c4aSTejun Heo 321573f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 32166183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 321773f53c4aSTejun Heo break; 321873f53c4aSTejun Heo } 321973f53c4aSTejun Heo 322073f53c4aSTejun Heo /* 322173f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 3222112202d9STejun Heo * the new first flusher and arm pwqs. 322373f53c4aSTejun Heo */ 32246183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 32256183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 322673f53c4aSTejun Heo 322773f53c4aSTejun Heo list_del_init(&next->list); 322873f53c4aSTejun Heo wq->first_flusher = next; 322973f53c4aSTejun Heo 3230112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 323173f53c4aSTejun Heo break; 323273f53c4aSTejun Heo 323373f53c4aSTejun Heo /* 323473f53c4aSTejun Heo * Meh... this color is already done, clear first 323573f53c4aSTejun Heo * flusher and repeat cascading. 323673f53c4aSTejun Heo */ 323773f53c4aSTejun Heo wq->first_flusher = NULL; 323873f53c4aSTejun Heo } 323973f53c4aSTejun Heo 324073f53c4aSTejun Heo out_unlock: 32413c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 32421da177e4SLinus Torvalds } 3243c4f135d6STetsuo Handa EXPORT_SYMBOL(__flush_workqueue); 32441da177e4SLinus Torvalds 32459c5a2ba7STejun Heo /** 32469c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 32479c5a2ba7STejun Heo * @wq: workqueue to drain 32489c5a2ba7STejun Heo * 32499c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 32509c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 32519c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 3252b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 32539c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 32549c5a2ba7STejun Heo * takes too long. 32559c5a2ba7STejun Heo */ 32569c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 32579c5a2ba7STejun Heo { 32589c5a2ba7STejun Heo unsigned int flush_cnt = 0; 325949e3cf44STejun Heo struct pool_workqueue *pwq; 32609c5a2ba7STejun Heo 32619c5a2ba7STejun Heo /* 32629c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 32639c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 3264618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 32659c5a2ba7STejun Heo */ 326687fc741eSLai Jiangshan mutex_lock(&wq->mutex); 32679c5a2ba7STejun Heo if (!wq->nr_drainers++) 3268618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 326987fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 32709c5a2ba7STejun Heo reflush: 3271c4f135d6STetsuo Handa __flush_workqueue(wq); 32729c5a2ba7STejun Heo 3273b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 327476af4d93STejun Heo 327549e3cf44STejun Heo for_each_pwq(pwq, wq) { 3276fa2563e4SThomas Tuttle bool drained; 32779c5a2ba7STejun Heo 3278a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 3279f97a4a1aSLai Jiangshan drained = !pwq->nr_active && list_empty(&pwq->inactive_works); 3280a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 3281fa2563e4SThomas Tuttle 3282fa2563e4SThomas Tuttle if (drained) 32839c5a2ba7STejun Heo continue; 32849c5a2ba7STejun Heo 32859c5a2ba7STejun Heo if (++flush_cnt == 10 || 32869c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 3287e9ad2eb3SStephen Zhang pr_warn("workqueue %s: %s() isn't complete after %u tries\n", 3288e9ad2eb3SStephen Zhang wq->name, __func__, flush_cnt); 328976af4d93STejun Heo 3290b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 32919c5a2ba7STejun Heo goto reflush; 32929c5a2ba7STejun Heo } 32939c5a2ba7STejun Heo 32949c5a2ba7STejun Heo if (!--wq->nr_drainers) 3295618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 329687fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 32979c5a2ba7STejun Heo } 32989c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 32999c5a2ba7STejun Heo 3300d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 3301d6e89786SJohannes Berg bool from_cancel) 3302baf59022STejun Heo { 3303baf59022STejun Heo struct worker *worker = NULL; 3304c9e7cf27STejun Heo struct worker_pool *pool; 3305112202d9STejun Heo struct pool_workqueue *pwq; 3306baf59022STejun Heo 3307baf59022STejun Heo might_sleep(); 3308baf59022STejun Heo 330924acfb71SThomas Gleixner rcu_read_lock(); 3310fa1b54e6STejun Heo pool = get_work_pool(work); 3311fa1b54e6STejun Heo if (!pool) { 331224acfb71SThomas Gleixner rcu_read_unlock(); 3313fa1b54e6STejun Heo return false; 3314fa1b54e6STejun Heo } 3315fa1b54e6STejun Heo 3316a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 33170b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 3318112202d9STejun Heo pwq = get_work_pwq(work); 3319112202d9STejun Heo if (pwq) { 3320112202d9STejun Heo if (unlikely(pwq->pool != pool)) 3321baf59022STejun Heo goto already_gone; 3322606a5020STejun Heo } else { 3323c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 3324baf59022STejun Heo if (!worker) 3325baf59022STejun Heo goto already_gone; 3326112202d9STejun Heo pwq = worker->current_pwq; 3327606a5020STejun Heo } 3328baf59022STejun Heo 3329fca839c0STejun Heo check_flush_dependency(pwq->wq, work); 3330fca839c0STejun Heo 3331112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 3332a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3333baf59022STejun Heo 3334e159489bSTejun Heo /* 3335a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 3336a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 3337a1d14934SPeter Zijlstra * 3338a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 3339a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 3340a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 3341a1d14934SPeter Zijlstra * forward progress. 3342e159489bSTejun Heo */ 3343d6e89786SJohannes Berg if (!from_cancel && 3344d6e89786SJohannes Berg (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)) { 3345112202d9STejun Heo lock_map_acquire(&pwq->wq->lockdep_map); 3346112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 3347a1d14934SPeter Zijlstra } 334824acfb71SThomas Gleixner rcu_read_unlock(); 3349baf59022STejun Heo return true; 3350baf59022STejun Heo already_gone: 3351a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 335224acfb71SThomas Gleixner rcu_read_unlock(); 3353baf59022STejun Heo return false; 3354baf59022STejun Heo } 3355baf59022STejun Heo 3356d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 3357d6e89786SJohannes Berg { 3358d6e89786SJohannes Berg struct wq_barrier barr; 3359d6e89786SJohannes Berg 3360d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 3361d6e89786SJohannes Berg return false; 3362d6e89786SJohannes Berg 33634d43d395STetsuo Handa if (WARN_ON(!work->func)) 33644d43d395STetsuo Handa return false; 33654d43d395STetsuo Handa 336687915adcSJohannes Berg lock_map_acquire(&work->lockdep_map); 336787915adcSJohannes Berg lock_map_release(&work->lockdep_map); 336887915adcSJohannes Berg 3369d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 3370d6e89786SJohannes Berg wait_for_completion(&barr.done); 3371d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 3372d6e89786SJohannes Berg return true; 3373d6e89786SJohannes Berg } else { 3374d6e89786SJohannes Berg return false; 3375d6e89786SJohannes Berg } 3376d6e89786SJohannes Berg } 3377d6e89786SJohannes Berg 3378db700897SOleg Nesterov /** 3379401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 3380401a8d04STejun Heo * @work: the work to flush 3381db700897SOleg Nesterov * 3382606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 3383606a5020STejun Heo * on return if it hasn't been requeued since flush started. 3384401a8d04STejun Heo * 3385d185af30SYacine Belkadi * Return: 3386401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3387401a8d04STejun Heo * %false if it was already idle. 3388db700897SOleg Nesterov */ 3389401a8d04STejun Heo bool flush_work(struct work_struct *work) 3390db700897SOleg Nesterov { 3391d6e89786SJohannes Berg return __flush_work(work, false); 3392606a5020STejun Heo } 3393db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 3394db700897SOleg Nesterov 33958603e1b3STejun Heo struct cwt_wait { 3396ac6424b9SIngo Molnar wait_queue_entry_t wait; 33978603e1b3STejun Heo struct work_struct *work; 33988603e1b3STejun Heo }; 33998603e1b3STejun Heo 3400ac6424b9SIngo Molnar static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 34018603e1b3STejun Heo { 34028603e1b3STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 34038603e1b3STejun Heo 34048603e1b3STejun Heo if (cwait->work != key) 34058603e1b3STejun Heo return 0; 34068603e1b3STejun Heo return autoremove_wake_function(wait, mode, sync, key); 34078603e1b3STejun Heo } 34088603e1b3STejun Heo 340936e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 3410401a8d04STejun Heo { 34118603e1b3STejun Heo static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); 3412bbb68dfaSTejun Heo unsigned long flags; 34131f1f642eSOleg Nesterov int ret; 34141f1f642eSOleg Nesterov 34151f1f642eSOleg Nesterov do { 3416bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 3417bbb68dfaSTejun Heo /* 34188603e1b3STejun Heo * If someone else is already canceling, wait for it to 34198603e1b3STejun Heo * finish. flush_work() doesn't work for PREEMPT_NONE 34208603e1b3STejun Heo * because we may get scheduled between @work's completion 34218603e1b3STejun Heo * and the other canceling task resuming and clearing 34228603e1b3STejun Heo * CANCELING - flush_work() will return false immediately 34238603e1b3STejun Heo * as @work is no longer busy, try_to_grab_pending() will 34248603e1b3STejun Heo * return -ENOENT as @work is still being canceled and the 34258603e1b3STejun Heo * other canceling task won't be able to clear CANCELING as 34268603e1b3STejun Heo * we're hogging the CPU. 34278603e1b3STejun Heo * 34288603e1b3STejun Heo * Let's wait for completion using a waitqueue. As this 34298603e1b3STejun Heo * may lead to the thundering herd problem, use a custom 34308603e1b3STejun Heo * wake function which matches @work along with exclusive 34318603e1b3STejun Heo * wait and wakeup. 3432bbb68dfaSTejun Heo */ 34338603e1b3STejun Heo if (unlikely(ret == -ENOENT)) { 34348603e1b3STejun Heo struct cwt_wait cwait; 34358603e1b3STejun Heo 34368603e1b3STejun Heo init_wait(&cwait.wait); 34378603e1b3STejun Heo cwait.wait.func = cwt_wakefn; 34388603e1b3STejun Heo cwait.work = work; 34398603e1b3STejun Heo 34408603e1b3STejun Heo prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, 34418603e1b3STejun Heo TASK_UNINTERRUPTIBLE); 34428603e1b3STejun Heo if (work_is_canceling(work)) 34438603e1b3STejun Heo schedule(); 34448603e1b3STejun Heo finish_wait(&cancel_waitq, &cwait.wait); 34458603e1b3STejun Heo } 34461f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 34471f1f642eSOleg Nesterov 3448bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 3449bbb68dfaSTejun Heo mark_work_canceling(work); 3450bbb68dfaSTejun Heo local_irq_restore(flags); 3451bbb68dfaSTejun Heo 34523347fa09STejun Heo /* 34533347fa09STejun Heo * This allows canceling during early boot. We know that @work 34543347fa09STejun Heo * isn't executing. 34553347fa09STejun Heo */ 34563347fa09STejun Heo if (wq_online) 3457d6e89786SJohannes Berg __flush_work(work, true); 34583347fa09STejun Heo 34597a22ad75STejun Heo clear_work_data(work); 34608603e1b3STejun Heo 34618603e1b3STejun Heo /* 34628603e1b3STejun Heo * Paired with prepare_to_wait() above so that either 34638603e1b3STejun Heo * waitqueue_active() is visible here or !work_is_canceling() is 34648603e1b3STejun Heo * visible there. 34658603e1b3STejun Heo */ 34668603e1b3STejun Heo smp_mb(); 34678603e1b3STejun Heo if (waitqueue_active(&cancel_waitq)) 34688603e1b3STejun Heo __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); 34698603e1b3STejun Heo 34701f1f642eSOleg Nesterov return ret; 34711f1f642eSOleg Nesterov } 34721f1f642eSOleg Nesterov 34736e84d644SOleg Nesterov /** 3474401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 3475401a8d04STejun Heo * @work: the work to cancel 34766e84d644SOleg Nesterov * 3477401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 3478401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 3479401a8d04STejun Heo * another workqueue. On return from this function, @work is 3480401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 34811f1f642eSOleg Nesterov * 3482401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 3483401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 34846e84d644SOleg Nesterov * 3485401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 34866e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 3487401a8d04STejun Heo * 3488d185af30SYacine Belkadi * Return: 3489401a8d04STejun Heo * %true if @work was pending, %false otherwise. 34906e84d644SOleg Nesterov */ 3491401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 34926e84d644SOleg Nesterov { 349336e227d2STejun Heo return __cancel_work_timer(work, false); 3494b89deed3SOleg Nesterov } 349528e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 3496b89deed3SOleg Nesterov 34976e84d644SOleg Nesterov /** 3498401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 3499401a8d04STejun Heo * @dwork: the delayed work to flush 35006e84d644SOleg Nesterov * 3501401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 3502401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 3503401a8d04STejun Heo * considers the last queueing instance of @dwork. 35041f1f642eSOleg Nesterov * 3505d185af30SYacine Belkadi * Return: 3506401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3507401a8d04STejun Heo * %false if it was already idle. 35086e84d644SOleg Nesterov */ 3509401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 3510401a8d04STejun Heo { 35118930cabaSTejun Heo local_irq_disable(); 3512401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 351360c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 35148930cabaSTejun Heo local_irq_enable(); 3515401a8d04STejun Heo return flush_work(&dwork->work); 3516401a8d04STejun Heo } 3517401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 3518401a8d04STejun Heo 351905f0fe6bSTejun Heo /** 352005f0fe6bSTejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 352105f0fe6bSTejun Heo * @rwork: the rcu work to flush 352205f0fe6bSTejun Heo * 352305f0fe6bSTejun Heo * Return: 352405f0fe6bSTejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 352505f0fe6bSTejun Heo * %false if it was already idle. 352605f0fe6bSTejun Heo */ 352705f0fe6bSTejun Heo bool flush_rcu_work(struct rcu_work *rwork) 352805f0fe6bSTejun Heo { 352905f0fe6bSTejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 353005f0fe6bSTejun Heo rcu_barrier(); 353105f0fe6bSTejun Heo flush_work(&rwork->work); 353205f0fe6bSTejun Heo return true; 353305f0fe6bSTejun Heo } else { 353405f0fe6bSTejun Heo return flush_work(&rwork->work); 353505f0fe6bSTejun Heo } 353605f0fe6bSTejun Heo } 353705f0fe6bSTejun Heo EXPORT_SYMBOL(flush_rcu_work); 353805f0fe6bSTejun Heo 3539f72b8792SJens Axboe static bool __cancel_work(struct work_struct *work, bool is_dwork) 3540f72b8792SJens Axboe { 3541f72b8792SJens Axboe unsigned long flags; 3542f72b8792SJens Axboe int ret; 3543f72b8792SJens Axboe 3544f72b8792SJens Axboe do { 3545f72b8792SJens Axboe ret = try_to_grab_pending(work, is_dwork, &flags); 3546f72b8792SJens Axboe } while (unlikely(ret == -EAGAIN)); 3547f72b8792SJens Axboe 3548f72b8792SJens Axboe if (unlikely(ret < 0)) 3549f72b8792SJens Axboe return false; 3550f72b8792SJens Axboe 3551f72b8792SJens Axboe set_work_pool_and_clear_pending(work, get_work_pool_id(work)); 3552f72b8792SJens Axboe local_irq_restore(flags); 3553f72b8792SJens Axboe return ret; 3554f72b8792SJens Axboe } 3555f72b8792SJens Axboe 355673b4b532SAndrey Grodzovsky /* 355773b4b532SAndrey Grodzovsky * See cancel_delayed_work() 355873b4b532SAndrey Grodzovsky */ 355973b4b532SAndrey Grodzovsky bool cancel_work(struct work_struct *work) 356073b4b532SAndrey Grodzovsky { 356173b4b532SAndrey Grodzovsky return __cancel_work(work, false); 356273b4b532SAndrey Grodzovsky } 356373b4b532SAndrey Grodzovsky EXPORT_SYMBOL(cancel_work); 356473b4b532SAndrey Grodzovsky 3565401a8d04STejun Heo /** 356657b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 356757b30ae7STejun Heo * @dwork: delayed_work to cancel 356809383498STejun Heo * 3569d185af30SYacine Belkadi * Kill off a pending delayed_work. 3570d185af30SYacine Belkadi * 3571d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 3572d185af30SYacine Belkadi * pending. 3573d185af30SYacine Belkadi * 3574d185af30SYacine Belkadi * Note: 3575d185af30SYacine Belkadi * The work callback function may still be running on return, unless 3576d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 3577d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 357809383498STejun Heo * 357957b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 358009383498STejun Heo */ 358157b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 358209383498STejun Heo { 3583f72b8792SJens Axboe return __cancel_work(&dwork->work, true); 358409383498STejun Heo } 358557b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 358609383498STejun Heo 358709383498STejun Heo /** 3588401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 3589401a8d04STejun Heo * @dwork: the delayed work cancel 3590401a8d04STejun Heo * 3591401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 3592401a8d04STejun Heo * 3593d185af30SYacine Belkadi * Return: 3594401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 3595401a8d04STejun Heo */ 3596401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 35976e84d644SOleg Nesterov { 359836e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 35996e84d644SOleg Nesterov } 3600f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 36011da177e4SLinus Torvalds 36020fcb78c2SRolf Eike Beer /** 360331ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 3604b6136773SAndrew Morton * @func: the function to call 3605b6136773SAndrew Morton * 360631ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 360731ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 3608b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 360931ddd871STejun Heo * 3610d185af30SYacine Belkadi * Return: 361131ddd871STejun Heo * 0 on success, -errno on failure. 3612b6136773SAndrew Morton */ 361365f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 361415316ba8SChristoph Lameter { 361515316ba8SChristoph Lameter int cpu; 361638f51568SNamhyung Kim struct work_struct __percpu *works; 361715316ba8SChristoph Lameter 3618b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 3619b6136773SAndrew Morton if (!works) 362015316ba8SChristoph Lameter return -ENOMEM; 3621b6136773SAndrew Morton 3622ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 362393981800STejun Heo 362415316ba8SChristoph Lameter for_each_online_cpu(cpu) { 36259bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 36269bfb1839SIngo Molnar 36279bfb1839SIngo Molnar INIT_WORK(work, func); 36288de6d308SOleg Nesterov schedule_work_on(cpu, work); 362915316ba8SChristoph Lameter } 363093981800STejun Heo 363193981800STejun Heo for_each_online_cpu(cpu) 36328616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 363393981800STejun Heo 3634ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 3635b6136773SAndrew Morton free_percpu(works); 363615316ba8SChristoph Lameter return 0; 363715316ba8SChristoph Lameter } 363815316ba8SChristoph Lameter 3639eef6a7d5SAlan Stern /** 36401fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 36411fa44ecaSJames Bottomley * @fn: the function to execute 36421fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 36431fa44ecaSJames Bottomley * be available when the work executes) 36441fa44ecaSJames Bottomley * 36451fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 36461fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 36471fa44ecaSJames Bottomley * 3648d185af30SYacine Belkadi * Return: 0 - function was executed 36491fa44ecaSJames Bottomley * 1 - function was scheduled for execution 36501fa44ecaSJames Bottomley */ 365165f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 36521fa44ecaSJames Bottomley { 36531fa44ecaSJames Bottomley if (!in_interrupt()) { 365465f27f38SDavid Howells fn(&ew->work); 36551fa44ecaSJames Bottomley return 0; 36561fa44ecaSJames Bottomley } 36571fa44ecaSJames Bottomley 365865f27f38SDavid Howells INIT_WORK(&ew->work, fn); 36591fa44ecaSJames Bottomley schedule_work(&ew->work); 36601fa44ecaSJames Bottomley 36611fa44ecaSJames Bottomley return 1; 36621fa44ecaSJames Bottomley } 36631fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 36641fa44ecaSJames Bottomley 36657a4e344cSTejun Heo /** 36667a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 36677a4e344cSTejun Heo * @attrs: workqueue_attrs to free 36687a4e344cSTejun Heo * 36697a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 36707a4e344cSTejun Heo */ 3671513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs) 36727a4e344cSTejun Heo { 36737a4e344cSTejun Heo if (attrs) { 36747a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 36757a4e344cSTejun Heo kfree(attrs); 36767a4e344cSTejun Heo } 36777a4e344cSTejun Heo } 36787a4e344cSTejun Heo 36797a4e344cSTejun Heo /** 36807a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 36817a4e344cSTejun Heo * 36827a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 3683d185af30SYacine Belkadi * return it. 3684d185af30SYacine Belkadi * 3685d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 36867a4e344cSTejun Heo */ 3687513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void) 36887a4e344cSTejun Heo { 36897a4e344cSTejun Heo struct workqueue_attrs *attrs; 36907a4e344cSTejun Heo 3691be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 36927a4e344cSTejun Heo if (!attrs) 36937a4e344cSTejun Heo goto fail; 3694be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) 36957a4e344cSTejun Heo goto fail; 36967a4e344cSTejun Heo 369713e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 369863c5484eSTejun Heo attrs->affn_scope = wq_affn_dfl; 36997a4e344cSTejun Heo return attrs; 37007a4e344cSTejun Heo fail: 37017a4e344cSTejun Heo free_workqueue_attrs(attrs); 37027a4e344cSTejun Heo return NULL; 37037a4e344cSTejun Heo } 37047a4e344cSTejun Heo 370529c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 370629c91e99STejun Heo const struct workqueue_attrs *from) 370729c91e99STejun Heo { 370829c91e99STejun Heo to->nice = from->nice; 370929c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 371084193c07STejun Heo 37112865a8fbSShaohua Li /* 371284193c07STejun Heo * Unlike hash and equality test, copying shouldn't ignore wq-only 371384193c07STejun Heo * fields as copying is used for both pool and wq attrs. Instead, 371484193c07STejun Heo * get_unbound_pool() explicitly clears the fields. 37152865a8fbSShaohua Li */ 371684193c07STejun Heo to->affn_scope = from->affn_scope; 3717af73f5c9STejun Heo to->ordered = from->ordered; 371829c91e99STejun Heo } 371929c91e99STejun Heo 37205de7a03cSTejun Heo /* 37215de7a03cSTejun Heo * Some attrs fields are workqueue-only. Clear them for worker_pool's. See the 37225de7a03cSTejun Heo * comments in 'struct workqueue_attrs' definition. 37235de7a03cSTejun Heo */ 37245de7a03cSTejun Heo static void wqattrs_clear_for_pool(struct workqueue_attrs *attrs) 37255de7a03cSTejun Heo { 372684193c07STejun Heo attrs->affn_scope = WQ_AFFN_NR_TYPES; 37275de7a03cSTejun Heo attrs->ordered = false; 37285de7a03cSTejun Heo } 37295de7a03cSTejun Heo 373029c91e99STejun Heo /* hash value of the content of @attr */ 373129c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 373229c91e99STejun Heo { 373329c91e99STejun Heo u32 hash = 0; 373429c91e99STejun Heo 373529c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 373613e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 373713e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 373829c91e99STejun Heo return hash; 373929c91e99STejun Heo } 374029c91e99STejun Heo 374129c91e99STejun Heo /* content equality test */ 374229c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 374329c91e99STejun Heo const struct workqueue_attrs *b) 374429c91e99STejun Heo { 374529c91e99STejun Heo if (a->nice != b->nice) 374629c91e99STejun Heo return false; 374729c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 374829c91e99STejun Heo return false; 374929c91e99STejun Heo return true; 375029c91e99STejun Heo } 375129c91e99STejun Heo 37520f36ee24STejun Heo /* Update @attrs with actually available CPUs */ 37530f36ee24STejun Heo static void wqattrs_actualize_cpumask(struct workqueue_attrs *attrs, 37540f36ee24STejun Heo const cpumask_t *unbound_cpumask) 37550f36ee24STejun Heo { 37560f36ee24STejun Heo /* 37570f36ee24STejun Heo * Calculate the effective CPU mask of @attrs given @unbound_cpumask. If 37580f36ee24STejun Heo * @attrs->cpumask doesn't overlap with @unbound_cpumask, we fallback to 37590f36ee24STejun Heo * @unbound_cpumask. 37600f36ee24STejun Heo */ 37610f36ee24STejun Heo cpumask_and(attrs->cpumask, attrs->cpumask, unbound_cpumask); 37620f36ee24STejun Heo if (unlikely(cpumask_empty(attrs->cpumask))) 37630f36ee24STejun Heo cpumask_copy(attrs->cpumask, unbound_cpumask); 37640f36ee24STejun Heo } 37650f36ee24STejun Heo 376684193c07STejun Heo /* find wq_pod_type to use for @attrs */ 376784193c07STejun Heo static const struct wq_pod_type * 376884193c07STejun Heo wqattrs_pod_type(const struct workqueue_attrs *attrs) 376984193c07STejun Heo { 377084193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[attrs->affn_scope]; 377184193c07STejun Heo 377284193c07STejun Heo if (!WARN_ON_ONCE(attrs->affn_scope == WQ_AFFN_NR_TYPES) && 377384193c07STejun Heo likely(pt->nr_pods)) 377484193c07STejun Heo return pt; 377584193c07STejun Heo 377684193c07STejun Heo /* 377784193c07STejun Heo * Before workqueue_init_topology(), only SYSTEM is available which is 377884193c07STejun Heo * initialized in workqueue_init_early(). 377984193c07STejun Heo */ 378084193c07STejun Heo pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 378184193c07STejun Heo BUG_ON(!pt->nr_pods); 378284193c07STejun Heo return pt; 378384193c07STejun Heo } 378484193c07STejun Heo 37857a4e344cSTejun Heo /** 37867a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 37877a4e344cSTejun Heo * @pool: worker_pool to initialize 37887a4e344cSTejun Heo * 3789402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 3790d185af30SYacine Belkadi * 3791d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 379229c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 379329c91e99STejun Heo * on @pool safely to release it. 37947a4e344cSTejun Heo */ 37957a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 37964e1a1f9aSTejun Heo { 3797a9b8a985SSebastian Andrzej Siewior raw_spin_lock_init(&pool->lock); 379829c91e99STejun Heo pool->id = -1; 379929c91e99STejun Heo pool->cpu = -1; 3800f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 38014e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 380282607adcSTejun Heo pool->watchdog_ts = jiffies; 38034e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 38044e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 38054e1a1f9aSTejun Heo hash_init(pool->busy_hash); 38064e1a1f9aSTejun Heo 380732a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 38083f959aa3SValentin Schneider INIT_WORK(&pool->idle_cull_work, idle_cull_fn); 38094e1a1f9aSTejun Heo 381032a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 38114e1a1f9aSTejun Heo 3812da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 3813e02b9312SValentin Schneider INIT_LIST_HEAD(&pool->dying_workers); 38147a4e344cSTejun Heo 38157cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 381629c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 381729c91e99STejun Heo pool->refcnt = 1; 381829c91e99STejun Heo 381929c91e99STejun Heo /* shouldn't fail above this point */ 3820be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs(); 38217a4e344cSTejun Heo if (!pool->attrs) 38227a4e344cSTejun Heo return -ENOMEM; 38235de7a03cSTejun Heo 38245de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 38255de7a03cSTejun Heo 38267a4e344cSTejun Heo return 0; 38274e1a1f9aSTejun Heo } 38284e1a1f9aSTejun Heo 3829669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP 3830669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 3831669de8bdSBart Van Assche { 3832669de8bdSBart Van Assche char *lock_name; 3833669de8bdSBart Van Assche 3834669de8bdSBart Van Assche lockdep_register_key(&wq->key); 3835669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); 3836669de8bdSBart Van Assche if (!lock_name) 3837669de8bdSBart Van Assche lock_name = wq->name; 383869a106c0SQian Cai 383969a106c0SQian Cai wq->lock_name = lock_name; 3840669de8bdSBart Van Assche lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); 3841669de8bdSBart Van Assche } 3842669de8bdSBart Van Assche 3843669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 3844669de8bdSBart Van Assche { 3845669de8bdSBart Van Assche lockdep_unregister_key(&wq->key); 3846669de8bdSBart Van Assche } 3847669de8bdSBart Van Assche 3848669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 3849669de8bdSBart Van Assche { 3850669de8bdSBart Van Assche if (wq->lock_name != wq->name) 3851669de8bdSBart Van Assche kfree(wq->lock_name); 3852669de8bdSBart Van Assche } 3853669de8bdSBart Van Assche #else 3854669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 3855669de8bdSBart Van Assche { 3856669de8bdSBart Van Assche } 3857669de8bdSBart Van Assche 3858669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 3859669de8bdSBart Van Assche { 3860669de8bdSBart Van Assche } 3861669de8bdSBart Van Assche 3862669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 3863669de8bdSBart Van Assche { 3864669de8bdSBart Van Assche } 3865669de8bdSBart Van Assche #endif 3866669de8bdSBart Van Assche 3867e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 3868e2dca7adSTejun Heo { 3869e2dca7adSTejun Heo struct workqueue_struct *wq = 3870e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 3871e2dca7adSTejun Heo 3872669de8bdSBart Van Assche wq_free_lockdep(wq); 3873ee1ceef7STejun Heo free_percpu(wq->cpu_pwq); 3874e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 3875e2dca7adSTejun Heo kfree(wq); 3876e2dca7adSTejun Heo } 3877e2dca7adSTejun Heo 387829c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 387929c91e99STejun Heo { 388029c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 388129c91e99STejun Heo 38827cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 388329c91e99STejun Heo free_workqueue_attrs(pool->attrs); 388429c91e99STejun Heo kfree(pool); 388529c91e99STejun Heo } 388629c91e99STejun Heo 388729c91e99STejun Heo /** 388829c91e99STejun Heo * put_unbound_pool - put a worker_pool 388929c91e99STejun Heo * @pool: worker_pool to put 389029c91e99STejun Heo * 389124acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU 3892c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 3893c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 3894c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 3895a892caccSTejun Heo * 3896a892caccSTejun Heo * Should be called with wq_pool_mutex held. 389729c91e99STejun Heo */ 389829c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 389929c91e99STejun Heo { 390060f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 390129c91e99STejun Heo struct worker *worker; 39029680540cSYang Yingliang LIST_HEAD(cull_list); 3903e02b9312SValentin Schneider 3904a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3905a892caccSTejun Heo 3906a892caccSTejun Heo if (--pool->refcnt) 390729c91e99STejun Heo return; 390829c91e99STejun Heo 390929c91e99STejun Heo /* sanity checks */ 391061d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 3911a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 391229c91e99STejun Heo return; 391329c91e99STejun Heo 391429c91e99STejun Heo /* release id and unhash */ 391529c91e99STejun Heo if (pool->id >= 0) 391629c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 391729c91e99STejun Heo hash_del(&pool->hash_node); 391829c91e99STejun Heo 3919c5aa87bbSTejun Heo /* 3920692b4825STejun Heo * Become the manager and destroy all workers. This prevents 3921692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 3922692b4825STejun Heo * manager and @pool gets freed with the flag set. 39239ab03be4SValentin Schneider * 39249ab03be4SValentin Schneider * Having a concurrent manager is quite unlikely to happen as we can 39259ab03be4SValentin Schneider * only get here with 39269ab03be4SValentin Schneider * pwq->refcnt == pool->refcnt == 0 39279ab03be4SValentin Schneider * which implies no work queued to the pool, which implies no worker can 39289ab03be4SValentin Schneider * become the manager. However a worker could have taken the role of 39299ab03be4SValentin Schneider * manager before the refcnts dropped to 0, since maybe_create_worker() 39309ab03be4SValentin Schneider * drops pool->lock 3931c5aa87bbSTejun Heo */ 39329ab03be4SValentin Schneider while (true) { 39339ab03be4SValentin Schneider rcuwait_wait_event(&manager_wait, 39349ab03be4SValentin Schneider !(pool->flags & POOL_MANAGER_ACTIVE), 3935d8bb65abSSebastian Andrzej Siewior TASK_UNINTERRUPTIBLE); 3936e02b9312SValentin Schneider 3937e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 39389ab03be4SValentin Schneider raw_spin_lock_irq(&pool->lock); 39399ab03be4SValentin Schneider if (!(pool->flags & POOL_MANAGER_ACTIVE)) { 3940692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 39419ab03be4SValentin Schneider break; 39429ab03be4SValentin Schneider } 39439ab03be4SValentin Schneider raw_spin_unlock_irq(&pool->lock); 3944e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 39459ab03be4SValentin Schneider } 3946692b4825STejun Heo 39471037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 3948e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 394929c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 3950a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 395160f5a4bcSLai Jiangshan 3952e02b9312SValentin Schneider wake_dying_workers(&cull_list); 3953e02b9312SValentin Schneider 3954e02b9312SValentin Schneider if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) 395560f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 39561258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 395760f5a4bcSLai Jiangshan 395860f5a4bcSLai Jiangshan if (pool->detach_completion) 395960f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 396060f5a4bcSLai Jiangshan 396129c91e99STejun Heo /* shut down the timers */ 396229c91e99STejun Heo del_timer_sync(&pool->idle_timer); 39633f959aa3SValentin Schneider cancel_work_sync(&pool->idle_cull_work); 396429c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 396529c91e99STejun Heo 396624acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */ 396725b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 396829c91e99STejun Heo } 396929c91e99STejun Heo 397029c91e99STejun Heo /** 397129c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 397229c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 397329c91e99STejun Heo * 397429c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 397529c91e99STejun Heo * reference count and return it. If there already is a matching 397629c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 3977d185af30SYacine Belkadi * create a new one. 3978a892caccSTejun Heo * 3979a892caccSTejun Heo * Should be called with wq_pool_mutex held. 3980d185af30SYacine Belkadi * 3981d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 3982d185af30SYacine Belkadi * On failure, %NULL. 398329c91e99STejun Heo */ 398429c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 398529c91e99STejun Heo { 398684193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_NUMA]; 398729c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 398829c91e99STejun Heo struct worker_pool *pool; 398984193c07STejun Heo int pod, node = NUMA_NO_NODE; 399029c91e99STejun Heo 3991a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 399229c91e99STejun Heo 399329c91e99STejun Heo /* do we already have a matching pool? */ 399429c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 399529c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 399629c91e99STejun Heo pool->refcnt++; 39973fb1823cSLai Jiangshan return pool; 399829c91e99STejun Heo } 399929c91e99STejun Heo } 400029c91e99STejun Heo 400184193c07STejun Heo /* If cpumask is contained inside a NUMA pod, that's our NUMA node */ 400284193c07STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) { 400384193c07STejun Heo if (cpumask_subset(attrs->cpumask, pt->pod_cpus[pod])) { 400484193c07STejun Heo node = pt->pod_node[pod]; 4005e2273584SXunlei Pang break; 4006e2273584SXunlei Pang } 4007e2273584SXunlei Pang } 4008e2273584SXunlei Pang 400929c91e99STejun Heo /* nope, create a new one */ 401084193c07STejun Heo pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, node); 401129c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 401229c91e99STejun Heo goto fail; 401329c91e99STejun Heo 401484193c07STejun Heo pool->node = node; 40155de7a03cSTejun Heo copy_workqueue_attrs(pool->attrs, attrs); 40165de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 40172865a8fbSShaohua Li 401829c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 401929c91e99STejun Heo goto fail; 402029c91e99STejun Heo 402129c91e99STejun Heo /* create and start the initial worker */ 40223347fa09STejun Heo if (wq_online && !create_worker(pool)) 402329c91e99STejun Heo goto fail; 402429c91e99STejun Heo 402529c91e99STejun Heo /* install */ 402629c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 40273fb1823cSLai Jiangshan 402829c91e99STejun Heo return pool; 402929c91e99STejun Heo fail: 403029c91e99STejun Heo if (pool) 403129c91e99STejun Heo put_unbound_pool(pool); 403229c91e99STejun Heo return NULL; 403329c91e99STejun Heo } 403429c91e99STejun Heo 40358864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 40368864b4e5STejun Heo { 40378864b4e5STejun Heo kmem_cache_free(pwq_cache, 40388864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 40398864b4e5STejun Heo } 40408864b4e5STejun Heo 40418864b4e5STejun Heo /* 4042967b494eSTejun Heo * Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero 4043967b494eSTejun Heo * refcnt and needs to be destroyed. 40448864b4e5STejun Heo */ 4045687a9aa5STejun Heo static void pwq_release_workfn(struct kthread_work *work) 40468864b4e5STejun Heo { 40478864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 4048687a9aa5STejun Heo release_work); 40498864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 40508864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 4051b42b0bddSYang Yingliang bool is_last = false; 40528864b4e5STejun Heo 4053b42b0bddSYang Yingliang /* 4054687a9aa5STejun Heo * When @pwq is not linked, it doesn't hold any reference to the 4055b42b0bddSYang Yingliang * @wq, and @wq is invalid to access. 4056b42b0bddSYang Yingliang */ 4057b42b0bddSYang Yingliang if (!list_empty(&pwq->pwqs_node)) { 40583c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 40598864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 4060bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 40613c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 4062b42b0bddSYang Yingliang } 40638864b4e5STejun Heo 4064687a9aa5STejun Heo if (wq->flags & WQ_UNBOUND) { 4065a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 40668864b4e5STejun Heo put_unbound_pool(pool); 4067a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 4068687a9aa5STejun Heo } 4069a892caccSTejun Heo 407025b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 40718864b4e5STejun Heo 40728864b4e5STejun Heo /* 40738864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 4074e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 40758864b4e5STejun Heo */ 4076669de8bdSBart Van Assche if (is_last) { 4077669de8bdSBart Van Assche wq_unregister_lockdep(wq); 407825b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 40796029a918STejun Heo } 4080669de8bdSBart Van Assche } 40818864b4e5STejun Heo 40820fbd95aaSTejun Heo /** 4083699ce097STejun Heo * pwq_adjust_max_active - update a pwq's max_active to the current setting 40840fbd95aaSTejun Heo * @pwq: target pool_workqueue 40850fbd95aaSTejun Heo * 4086699ce097STejun Heo * If @pwq isn't freezing, set @pwq->max_active to the associated 4087f97a4a1aSLai Jiangshan * workqueue's saved_max_active and activate inactive work items 4088699ce097STejun Heo * accordingly. If @pwq is freezing, clear @pwq->max_active to zero. 40890fbd95aaSTejun Heo */ 4090699ce097STejun Heo static void pwq_adjust_max_active(struct pool_workqueue *pwq) 40910fbd95aaSTejun Heo { 4092699ce097STejun Heo struct workqueue_struct *wq = pwq->wq; 4093699ce097STejun Heo bool freezable = wq->flags & WQ_FREEZABLE; 40943347fa09STejun Heo unsigned long flags; 4095699ce097STejun Heo 4096699ce097STejun Heo /* for @wq->saved_max_active */ 4097a357fc03SLai Jiangshan lockdep_assert_held(&wq->mutex); 4098699ce097STejun Heo 4099699ce097STejun Heo /* fast exit for non-freezable wqs */ 4100699ce097STejun Heo if (!freezable && pwq->max_active == wq->saved_max_active) 4101699ce097STejun Heo return; 4102699ce097STejun Heo 41033347fa09STejun Heo /* this function can be called during early boot w/ irq disabled */ 4104a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pwq->pool->lock, flags); 4105699ce097STejun Heo 410674b414eaSLai Jiangshan /* 410774b414eaSLai Jiangshan * During [un]freezing, the caller is responsible for ensuring that 410874b414eaSLai Jiangshan * this function is called at least once after @workqueue_freezing 410974b414eaSLai Jiangshan * is updated and visible. 411074b414eaSLai Jiangshan */ 411174b414eaSLai Jiangshan if (!freezable || !workqueue_freezing) { 4112699ce097STejun Heo pwq->max_active = wq->saved_max_active; 41130fbd95aaSTejun Heo 4114f97a4a1aSLai Jiangshan while (!list_empty(&pwq->inactive_works) && 4115*0219a352STejun Heo pwq->nr_active < pwq->max_active) 4116f97a4a1aSLai Jiangshan pwq_activate_first_inactive(pwq); 4117951a078aSLai Jiangshan 4118*0219a352STejun Heo kick_pool(pwq->pool); 4119699ce097STejun Heo } else { 4120699ce097STejun Heo pwq->max_active = 0; 4121699ce097STejun Heo } 4122699ce097STejun Heo 4123a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 41240fbd95aaSTejun Heo } 41250fbd95aaSTejun Heo 412667dc8325SCai Huoqing /* initialize newly allocated @pwq which is associated with @wq and @pool */ 4127f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 4128f147f29eSTejun Heo struct worker_pool *pool) 4129d2c1d404STejun Heo { 4130d2c1d404STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 4131d2c1d404STejun Heo 4132e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 4133e50aba9aSTejun Heo 4134d2c1d404STejun Heo pwq->pool = pool; 4135d2c1d404STejun Heo pwq->wq = wq; 4136d2c1d404STejun Heo pwq->flush_color = -1; 41378864b4e5STejun Heo pwq->refcnt = 1; 4138f97a4a1aSLai Jiangshan INIT_LIST_HEAD(&pwq->inactive_works); 41391befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 4140d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 4141687a9aa5STejun Heo kthread_init_work(&pwq->release_work, pwq_release_workfn); 4142f147f29eSTejun Heo } 4143d2c1d404STejun Heo 4144f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 41451befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 4146f147f29eSTejun Heo { 4147f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 4148f147f29eSTejun Heo 4149f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 415075ccf595STejun Heo 41511befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 41521befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 41531befcf30STejun Heo return; 41541befcf30STejun Heo 415529b1cb41SLai Jiangshan /* set the matching work_color */ 415675ccf595STejun Heo pwq->work_color = wq->work_color; 4157983ca25eSTejun Heo 4158983ca25eSTejun Heo /* sync max_active to the current setting */ 4159983ca25eSTejun Heo pwq_adjust_max_active(pwq); 4160983ca25eSTejun Heo 4161983ca25eSTejun Heo /* link in @pwq */ 41629e8cd2f5STejun Heo list_add_rcu(&pwq->pwqs_node, &wq->pwqs); 4163df2d5ae4STejun Heo } 41646029a918STejun Heo 4165f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 4166f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 4167f147f29eSTejun Heo const struct workqueue_attrs *attrs) 4168f147f29eSTejun Heo { 4169f147f29eSTejun Heo struct worker_pool *pool; 4170f147f29eSTejun Heo struct pool_workqueue *pwq; 4171f147f29eSTejun Heo 4172f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4173f147f29eSTejun Heo 4174f147f29eSTejun Heo pool = get_unbound_pool(attrs); 4175f147f29eSTejun Heo if (!pool) 4176f147f29eSTejun Heo return NULL; 4177f147f29eSTejun Heo 4178e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 4179f147f29eSTejun Heo if (!pwq) { 4180f147f29eSTejun Heo put_unbound_pool(pool); 4181f147f29eSTejun Heo return NULL; 4182f147f29eSTejun Heo } 4183f147f29eSTejun Heo 4184f147f29eSTejun Heo init_pwq(pwq, wq, pool); 4185f147f29eSTejun Heo return pwq; 4186d2c1d404STejun Heo } 4187d2c1d404STejun Heo 41884c16bd32STejun Heo /** 4189fef59c9cSTejun Heo * wq_calc_pod_cpumask - calculate a wq_attrs' cpumask for a pod 4190042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 419184193c07STejun Heo * @cpu: the target CPU 41924c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 41934c16bd32STejun Heo * @cpumask: outarg, the resulting cpumask 41944c16bd32STejun Heo * 4195fef59c9cSTejun Heo * Calculate the cpumask a workqueue with @attrs should use on @pod. If 4196fef59c9cSTejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during calculation. 4197fef59c9cSTejun Heo * The result is stored in @cpumask. 41984c16bd32STejun Heo * 4199fef59c9cSTejun Heo * If pod affinity is not enabled, @attrs->cpumask is always used. If enabled 4200fef59c9cSTejun Heo * and @pod has online CPUs requested by @attrs, the returned cpumask is the 4201fef59c9cSTejun Heo * intersection of the possible CPUs of @pod and @attrs->cpumask. 42024c16bd32STejun Heo * 4203fef59c9cSTejun Heo * The caller is responsible for ensuring that the cpumask of @pod stays stable. 42044c16bd32STejun Heo */ 420584193c07STejun Heo static void wq_calc_pod_cpumask(const struct workqueue_attrs *attrs, int cpu, 42064c16bd32STejun Heo int cpu_going_down, cpumask_t *cpumask) 42074c16bd32STejun Heo { 420884193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 420984193c07STejun Heo int pod = pt->cpu_pod[cpu]; 42104c16bd32STejun Heo 4211fef59c9cSTejun Heo /* does @pod have any online CPUs @attrs wants? */ 421284193c07STejun Heo cpumask_and(cpumask, pt->pod_cpus[pod], attrs->cpumask); 421384193c07STejun Heo cpumask_and(cpumask, cpumask, cpu_online_mask); 42144c16bd32STejun Heo if (cpu_going_down >= 0) 42154c16bd32STejun Heo cpumask_clear_cpu(cpu_going_down, cpumask); 42164c16bd32STejun Heo 421784193c07STejun Heo if (cpumask_empty(cpumask)) { 421884193c07STejun Heo cpumask_copy(cpumask, attrs->cpumask); 421984193c07STejun Heo return; 422084193c07STejun Heo } 42214c16bd32STejun Heo 4222fef59c9cSTejun Heo /* yeap, return possible CPUs in @pod that @attrs wants */ 422384193c07STejun Heo cpumask_and(cpumask, attrs->cpumask, pt->pod_cpus[pod]); 42241ad0f0a7SMichael Bringmann 4225636b927eSTejun Heo if (cpumask_empty(cpumask)) 42261ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 42271ad0f0a7SMichael Bringmann "possible intersect\n"); 42284c16bd32STejun Heo } 42294c16bd32STejun Heo 4230636b927eSTejun Heo /* install @pwq into @wq's cpu_pwq and return the old pwq */ 4231636b927eSTejun Heo static struct pool_workqueue *install_unbound_pwq(struct workqueue_struct *wq, 4232636b927eSTejun Heo int cpu, struct pool_workqueue *pwq) 42331befcf30STejun Heo { 42341befcf30STejun Heo struct pool_workqueue *old_pwq; 42351befcf30STejun Heo 42365b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 42371befcf30STejun Heo lockdep_assert_held(&wq->mutex); 42381befcf30STejun Heo 42391befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 42401befcf30STejun Heo link_pwq(pwq); 42411befcf30STejun Heo 4242636b927eSTejun Heo old_pwq = rcu_access_pointer(*per_cpu_ptr(wq->cpu_pwq, cpu)); 4243636b927eSTejun Heo rcu_assign_pointer(*per_cpu_ptr(wq->cpu_pwq, cpu), pwq); 42441befcf30STejun Heo return old_pwq; 42451befcf30STejun Heo } 42461befcf30STejun Heo 42472d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 42482d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 42492d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 42502d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 4251042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 42522d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 42532d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 42542d5f0764SLai Jiangshan }; 42552d5f0764SLai Jiangshan 42562d5f0764SLai Jiangshan /* free the resources after success or abort */ 42572d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 42582d5f0764SLai Jiangshan { 42592d5f0764SLai Jiangshan if (ctx) { 4260636b927eSTejun Heo int cpu; 42612d5f0764SLai Jiangshan 4262636b927eSTejun Heo for_each_possible_cpu(cpu) 4263636b927eSTejun Heo put_pwq_unlocked(ctx->pwq_tbl[cpu]); 42642d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 42652d5f0764SLai Jiangshan 42662d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 42672d5f0764SLai Jiangshan 42682d5f0764SLai Jiangshan kfree(ctx); 42692d5f0764SLai Jiangshan } 42702d5f0764SLai Jiangshan } 42712d5f0764SLai Jiangshan 42722d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 42732d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 42742d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 427599c621efSLai Jiangshan const struct workqueue_attrs *attrs, 427699c621efSLai Jiangshan const cpumask_var_t unbound_cpumask) 42772d5f0764SLai Jiangshan { 42782d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 42792d5f0764SLai Jiangshan struct workqueue_attrs *new_attrs, *tmp_attrs; 4280636b927eSTejun Heo int cpu; 42812d5f0764SLai Jiangshan 42822d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 42832d5f0764SLai Jiangshan 428484193c07STejun Heo if (WARN_ON(attrs->affn_scope < 0 || 428584193c07STejun Heo attrs->affn_scope >= WQ_AFFN_NR_TYPES)) 428684193c07STejun Heo return ERR_PTR(-EINVAL); 428784193c07STejun Heo 4288636b927eSTejun Heo ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_cpu_ids), GFP_KERNEL); 42892d5f0764SLai Jiangshan 4290be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs(); 4291be69d00dSThomas Gleixner tmp_attrs = alloc_workqueue_attrs(); 42922d5f0764SLai Jiangshan if (!ctx || !new_attrs || !tmp_attrs) 42932d5f0764SLai Jiangshan goto out_free; 42942d5f0764SLai Jiangshan 4295042f7df1SLai Jiangshan /* 42962d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 42972d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 42982d5f0764SLai Jiangshan * it even if we don't use it immediately. 42992d5f0764SLai Jiangshan */ 43000f36ee24STejun Heo copy_workqueue_attrs(new_attrs, attrs); 43010f36ee24STejun Heo wqattrs_actualize_cpumask(new_attrs, unbound_cpumask); 43022d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 43032d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 43042d5f0764SLai Jiangshan goto out_free; 43052d5f0764SLai Jiangshan 43060f36ee24STejun Heo /* 43070f36ee24STejun Heo * We may create multiple pwqs with differing cpumasks. Make a copy of 43080f36ee24STejun Heo * @new_attrs which will be modified and used to obtain pools. 43090f36ee24STejun Heo */ 43100f36ee24STejun Heo copy_workqueue_attrs(tmp_attrs, new_attrs); 43110f36ee24STejun Heo 4312636b927eSTejun Heo for_each_possible_cpu(cpu) { 4313af73f5c9STejun Heo if (new_attrs->ordered) { 43142d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 4315636b927eSTejun Heo ctx->pwq_tbl[cpu] = ctx->dfl_pwq; 4316636b927eSTejun Heo } else { 431784193c07STejun Heo wq_calc_pod_cpumask(new_attrs, cpu, -1, tmp_attrs->cpumask); 4318636b927eSTejun Heo ctx->pwq_tbl[cpu] = alloc_unbound_pwq(wq, tmp_attrs); 4319636b927eSTejun Heo if (!ctx->pwq_tbl[cpu]) 4320636b927eSTejun Heo goto out_free; 43212d5f0764SLai Jiangshan } 43222d5f0764SLai Jiangshan } 43232d5f0764SLai Jiangshan 4324042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 4325042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 4326042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 43272d5f0764SLai Jiangshan ctx->attrs = new_attrs; 4328042f7df1SLai Jiangshan 43292d5f0764SLai Jiangshan ctx->wq = wq; 43302d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 43312d5f0764SLai Jiangshan return ctx; 43322d5f0764SLai Jiangshan 43332d5f0764SLai Jiangshan out_free: 43342d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 43352d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 43362d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 433784193c07STejun Heo return ERR_PTR(-ENOMEM); 43382d5f0764SLai Jiangshan } 43392d5f0764SLai Jiangshan 43402d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 43412d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 43422d5f0764SLai Jiangshan { 4343636b927eSTejun Heo int cpu; 43442d5f0764SLai Jiangshan 43452d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 43462d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 43472d5f0764SLai Jiangshan 43482d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 43492d5f0764SLai Jiangshan 43502d5f0764SLai Jiangshan /* save the previous pwq and install the new one */ 4351636b927eSTejun Heo for_each_possible_cpu(cpu) 4352636b927eSTejun Heo ctx->pwq_tbl[cpu] = install_unbound_pwq(ctx->wq, cpu, 4353636b927eSTejun Heo ctx->pwq_tbl[cpu]); 43542d5f0764SLai Jiangshan 43552d5f0764SLai Jiangshan /* @dfl_pwq might not have been used, ensure it's linked */ 43562d5f0764SLai Jiangshan link_pwq(ctx->dfl_pwq); 43572d5f0764SLai Jiangshan swap(ctx->wq->dfl_pwq, ctx->dfl_pwq); 43582d5f0764SLai Jiangshan 43592d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 43602d5f0764SLai Jiangshan } 43612d5f0764SLai Jiangshan 4362a0111cf6SLai Jiangshan static void apply_wqattrs_lock(void) 4363a0111cf6SLai Jiangshan { 4364a0111cf6SLai Jiangshan /* CPUs should stay stable across pwq creations and installations */ 4365ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 4366a0111cf6SLai Jiangshan mutex_lock(&wq_pool_mutex); 4367a0111cf6SLai Jiangshan } 4368a0111cf6SLai Jiangshan 4369a0111cf6SLai Jiangshan static void apply_wqattrs_unlock(void) 4370a0111cf6SLai Jiangshan { 4371a0111cf6SLai Jiangshan mutex_unlock(&wq_pool_mutex); 4372ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4373a0111cf6SLai Jiangshan } 4374a0111cf6SLai Jiangshan 4375a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 4376a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 4377a0111cf6SLai Jiangshan { 4378a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 4379a0111cf6SLai Jiangshan 4380a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 4381a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 4382a0111cf6SLai Jiangshan return -EINVAL; 4383a0111cf6SLai Jiangshan 4384a0111cf6SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 43850a94efb5STejun Heo if (!list_empty(&wq->pwqs)) { 43860a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 4387a0111cf6SLai Jiangshan return -EINVAL; 4388a0111cf6SLai Jiangshan 43890a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 43900a94efb5STejun Heo } 43910a94efb5STejun Heo 439299c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); 439384193c07STejun Heo if (IS_ERR(ctx)) 439484193c07STejun Heo return PTR_ERR(ctx); 4395a0111cf6SLai Jiangshan 4396a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 4397a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 4398a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 4399a0111cf6SLai Jiangshan 44006201171eSwanghaibin return 0; 4401a0111cf6SLai Jiangshan } 4402a0111cf6SLai Jiangshan 44039e8cd2f5STejun Heo /** 44049e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 44059e8cd2f5STejun Heo * @wq: the target workqueue 44069e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 44079e8cd2f5STejun Heo * 4408fef59c9cSTejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, this function maps 4409fef59c9cSTejun Heo * a separate pwq to each CPU pod with possibles CPUs in @attrs->cpumask so that 4410fef59c9cSTejun Heo * work items are affine to the pod it was issued on. Older pwqs are released as 4411fef59c9cSTejun Heo * in-flight work items finish. Note that a work item which repeatedly requeues 4412fef59c9cSTejun Heo * itself back-to-back will stay on its current pwq. 44139e8cd2f5STejun Heo * 4414d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 4415d185af30SYacine Belkadi * 4416ffd8bea8SSebastian Andrzej Siewior * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). 4417509b3204SDaniel Jordan * 4418d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 44199e8cd2f5STejun Heo */ 4420513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 44219e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 44229e8cd2f5STejun Heo { 4423a0111cf6SLai Jiangshan int ret; 44249e8cd2f5STejun Heo 4425509b3204SDaniel Jordan lockdep_assert_cpus_held(); 4426509b3204SDaniel Jordan 4427509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex); 4428a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 4429509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex); 44302d5f0764SLai Jiangshan 44312d5f0764SLai Jiangshan return ret; 44329e8cd2f5STejun Heo } 44339e8cd2f5STejun Heo 44344c16bd32STejun Heo /** 4435fef59c9cSTejun Heo * wq_update_pod - update pod affinity of a wq for CPU hot[un]plug 44364c16bd32STejun Heo * @wq: the target workqueue 44374cbfd3deSTejun Heo * @cpu: the CPU to update pool association for 44384cbfd3deSTejun Heo * @hotplug_cpu: the CPU coming up or going down 44394c16bd32STejun Heo * @online: whether @cpu is coming up or going down 44404c16bd32STejun Heo * 44414c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 4442fef59c9cSTejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update pod affinity of 44434c16bd32STejun Heo * @wq accordingly. 44444c16bd32STejun Heo * 44454c16bd32STejun Heo * 4446fef59c9cSTejun Heo * If pod affinity can't be adjusted due to memory allocation failure, it falls 4447fef59c9cSTejun Heo * back to @wq->dfl_pwq which may not be optimal but is always correct. 4448fef59c9cSTejun Heo * 4449fef59c9cSTejun Heo * Note that when the last allowed CPU of a pod goes offline for a workqueue 4450fef59c9cSTejun Heo * with a cpumask spanning multiple pods, the workers which were already 4451fef59c9cSTejun Heo * executing the work items for the workqueue will lose their CPU affinity and 4452fef59c9cSTejun Heo * may execute on any CPU. This is similar to how per-cpu workqueues behave on 4453fef59c9cSTejun Heo * CPU_DOWN. If a workqueue user wants strict affinity, it's the user's 4454fef59c9cSTejun Heo * responsibility to flush the work item from CPU_DOWN_PREPARE. 44554c16bd32STejun Heo */ 4456fef59c9cSTejun Heo static void wq_update_pod(struct workqueue_struct *wq, int cpu, 44574cbfd3deSTejun Heo int hotplug_cpu, bool online) 44584c16bd32STejun Heo { 44594cbfd3deSTejun Heo int off_cpu = online ? -1 : hotplug_cpu; 44604c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 44614c16bd32STejun Heo struct workqueue_attrs *target_attrs; 44624c16bd32STejun Heo cpumask_t *cpumask; 44634c16bd32STejun Heo 44644c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 44654c16bd32STejun Heo 446684193c07STejun Heo if (!(wq->flags & WQ_UNBOUND) || wq->unbound_attrs->ordered) 44674c16bd32STejun Heo return; 44684c16bd32STejun Heo 44694c16bd32STejun Heo /* 44704c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 44714c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 44724c16bd32STejun Heo * CPU hotplug exclusion. 44734c16bd32STejun Heo */ 4474fef59c9cSTejun Heo target_attrs = wq_update_pod_attrs_buf; 44750f36ee24STejun Heo cpumask = wq_update_pod_cpumask_buf; 44764c16bd32STejun Heo 44774c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 44780f36ee24STejun Heo wqattrs_actualize_cpumask(target_attrs, wq_unbound_cpumask); 44794c16bd32STejun Heo 4480636b927eSTejun Heo /* nothing to do if the target cpumask matches the current pwq */ 448184193c07STejun Heo wq_calc_pod_cpumask(target_attrs, cpu, off_cpu, cpumask); 4482636b927eSTejun Heo pwq = rcu_dereference_protected(*per_cpu_ptr(wq->cpu_pwq, cpu), 4483636b927eSTejun Heo lockdep_is_held(&wq_pool_mutex)); 44844c16bd32STejun Heo if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask)) 4485f7142ed4SLai Jiangshan return; 44864c16bd32STejun Heo 44874c16bd32STejun Heo /* create a new pwq */ 44880f36ee24STejun Heo cpumask_copy(target_attrs->cpumask, cpumask); 44894c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 44904c16bd32STejun Heo if (!pwq) { 4491fef59c9cSTejun Heo pr_warn("workqueue: allocation failed while updating CPU pod affinity of \"%s\"\n", 44924c16bd32STejun Heo wq->name); 449377f300b1SDaeseok Youn goto use_dfl_pwq; 44944c16bd32STejun Heo } 44954c16bd32STejun Heo 4496f7142ed4SLai Jiangshan /* Install the new pwq. */ 44974c16bd32STejun Heo mutex_lock(&wq->mutex); 4498636b927eSTejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 44994c16bd32STejun Heo goto out_unlock; 45004c16bd32STejun Heo 45014c16bd32STejun Heo use_dfl_pwq: 4502f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 4503a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq->dfl_pwq->pool->lock); 45044c16bd32STejun Heo get_pwq(wq->dfl_pwq); 4505a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq->dfl_pwq->pool->lock); 4506636b927eSTejun Heo old_pwq = install_unbound_pwq(wq, cpu, wq->dfl_pwq); 45074c16bd32STejun Heo out_unlock: 45084c16bd32STejun Heo mutex_unlock(&wq->mutex); 45094c16bd32STejun Heo put_pwq_unlocked(old_pwq); 45104c16bd32STejun Heo } 45114c16bd32STejun Heo 451230cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 45131da177e4SLinus Torvalds { 451449e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 45158a2b7538STejun Heo int cpu, ret; 4516e1d8aa9fSFrederic Weisbecker 4517687a9aa5STejun Heo wq->cpu_pwq = alloc_percpu(struct pool_workqueue *); 4518ee1ceef7STejun Heo if (!wq->cpu_pwq) 4519687a9aa5STejun Heo goto enomem; 452030cdf249STejun Heo 4521636b927eSTejun Heo if (!(wq->flags & WQ_UNBOUND)) { 452230cdf249STejun Heo for_each_possible_cpu(cpu) { 4523687a9aa5STejun Heo struct pool_workqueue **pwq_p = 4524ee1ceef7STejun Heo per_cpu_ptr(wq->cpu_pwq, cpu); 4525687a9aa5STejun Heo struct worker_pool *pool = 4526687a9aa5STejun Heo &(per_cpu_ptr(cpu_worker_pools, cpu)[highpri]); 452730cdf249STejun Heo 4528687a9aa5STejun Heo *pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, 4529687a9aa5STejun Heo pool->node); 4530687a9aa5STejun Heo if (!*pwq_p) 4531687a9aa5STejun Heo goto enomem; 4532687a9aa5STejun Heo 4533687a9aa5STejun Heo init_pwq(*pwq_p, wq, pool); 4534f147f29eSTejun Heo 4535f147f29eSTejun Heo mutex_lock(&wq->mutex); 4536687a9aa5STejun Heo link_pwq(*pwq_p); 4537f147f29eSTejun Heo mutex_unlock(&wq->mutex); 453830cdf249STejun Heo } 453930cdf249STejun Heo return 0; 4540509b3204SDaniel Jordan } 4541509b3204SDaniel Jordan 4542ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 4543509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) { 45448a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 45458a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 45468a2b7538STejun Heo WARN(!ret && (wq->pwqs.next != &wq->dfl_pwq->pwqs_node || 45478a2b7538STejun Heo wq->pwqs.prev != &wq->dfl_pwq->pwqs_node), 45488a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 45499e8cd2f5STejun Heo } else { 4550509b3204SDaniel Jordan ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 45519e8cd2f5STejun Heo } 4552ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4553509b3204SDaniel Jordan 4554509b3204SDaniel Jordan return ret; 4555687a9aa5STejun Heo 4556687a9aa5STejun Heo enomem: 4557687a9aa5STejun Heo if (wq->cpu_pwq) { 4558687a9aa5STejun Heo for_each_possible_cpu(cpu) 4559687a9aa5STejun Heo kfree(*per_cpu_ptr(wq->cpu_pwq, cpu)); 4560687a9aa5STejun Heo free_percpu(wq->cpu_pwq); 4561687a9aa5STejun Heo wq->cpu_pwq = NULL; 4562687a9aa5STejun Heo } 4563687a9aa5STejun Heo return -ENOMEM; 45640f900049STejun Heo } 45650f900049STejun Heo 4566f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 4567f3421797STejun Heo const char *name) 4568b71ab8c2STejun Heo { 4569636b927eSTejun Heo if (max_active < 1 || max_active > WQ_MAX_ACTIVE) 4570044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 4571636b927eSTejun Heo max_active, name, 1, WQ_MAX_ACTIVE); 4572b71ab8c2STejun Heo 4573636b927eSTejun Heo return clamp_val(max_active, 1, WQ_MAX_ACTIVE); 4574b71ab8c2STejun Heo } 4575b71ab8c2STejun Heo 4576983c7515STejun Heo /* 4577983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 4578983c7515STejun Heo * to guarantee forward progress. 4579983c7515STejun Heo */ 4580983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 4581983c7515STejun Heo { 4582983c7515STejun Heo struct worker *rescuer; 4583b92b36eaSDan Carpenter int ret; 4584983c7515STejun Heo 4585983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 4586983c7515STejun Heo return 0; 4587983c7515STejun Heo 4588983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 45894c0736a7SPetr Mladek if (!rescuer) { 45904c0736a7SPetr Mladek pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", 45914c0736a7SPetr Mladek wq->name); 4592983c7515STejun Heo return -ENOMEM; 45934c0736a7SPetr Mladek } 4594983c7515STejun Heo 4595983c7515STejun Heo rescuer->rescue_wq = wq; 4596983c7515STejun Heo rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", wq->name); 4597f187b697SSean Fu if (IS_ERR(rescuer->task)) { 4598b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task); 45994c0736a7SPetr Mladek pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", 46004c0736a7SPetr Mladek wq->name, ERR_PTR(ret)); 4601983c7515STejun Heo kfree(rescuer); 4602b92b36eaSDan Carpenter return ret; 4603983c7515STejun Heo } 4604983c7515STejun Heo 4605983c7515STejun Heo wq->rescuer = rescuer; 4606983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 4607983c7515STejun Heo wake_up_process(rescuer->task); 4608983c7515STejun Heo 4609983c7515STejun Heo return 0; 4610983c7515STejun Heo } 4611983c7515STejun Heo 4612a2775bbcSMathieu Malaterre __printf(1, 4) 4613669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt, 461497e37d7bSTejun Heo unsigned int flags, 4615669de8bdSBart Van Assche int max_active, ...) 46163af24433SOleg Nesterov { 4617ecf6881fSTejun Heo va_list args; 46183af24433SOleg Nesterov struct workqueue_struct *wq; 461949e3cf44STejun Heo struct pool_workqueue *pwq; 4620b196be89STejun Heo 46215c0338c6STejun Heo /* 4622fef59c9cSTejun Heo * Unbound && max_active == 1 used to imply ordered, which is no longer 4623fef59c9cSTejun Heo * the case on many machines due to per-pod pools. While 46245c0338c6STejun Heo * alloc_ordered_workqueue() is the right way to create an ordered 4625fef59c9cSTejun Heo * workqueue, keep the previous behavior to avoid subtle breakages. 46265c0338c6STejun Heo */ 46275c0338c6STejun Heo if ((flags & WQ_UNBOUND) && max_active == 1) 46285c0338c6STejun Heo flags |= __WQ_ORDERED; 46295c0338c6STejun Heo 4630cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 4631cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 4632cee22a15SViresh Kumar flags |= WQ_UNBOUND; 4633cee22a15SViresh Kumar 4634ecf6881fSTejun Heo /* allocate wq and format name */ 4635636b927eSTejun Heo wq = kzalloc(sizeof(*wq), GFP_KERNEL); 4636b196be89STejun Heo if (!wq) 4637d2c1d404STejun Heo return NULL; 4638b196be89STejun Heo 46396029a918STejun Heo if (flags & WQ_UNBOUND) { 4640be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs(); 46416029a918STejun Heo if (!wq->unbound_attrs) 46426029a918STejun Heo goto err_free_wq; 46436029a918STejun Heo } 46446029a918STejun Heo 4645669de8bdSBart Van Assche va_start(args, max_active); 4646ecf6881fSTejun Heo vsnprintf(wq->name, sizeof(wq->name), fmt, args); 4647b196be89STejun Heo va_end(args); 46483af24433SOleg Nesterov 4649d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 4650b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 46513af24433SOleg Nesterov 4652b196be89STejun Heo /* init wq */ 465397e37d7bSTejun Heo wq->flags = flags; 4654a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 46553c25a55dSLai Jiangshan mutex_init(&wq->mutex); 4656112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 465730cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 465873f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 465973f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 4660493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 46613af24433SOleg Nesterov 4662669de8bdSBart Van Assche wq_init_lockdep(wq); 4663cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 46643af24433SOleg Nesterov 466530cdf249STejun Heo if (alloc_and_link_pwqs(wq) < 0) 466682efcab3SBart Van Assche goto err_unreg_lockdep; 46671537663fSTejun Heo 466840c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 4669d2c1d404STejun Heo goto err_destroy; 4670e22bee78STejun Heo 4671226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 4672226223abSTejun Heo goto err_destroy; 4673226223abSTejun Heo 46746af8bf3dSOleg Nesterov /* 467568e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 467668e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 467768e13a67SLai Jiangshan * list. 46786af8bf3dSOleg Nesterov */ 467968e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4680a0a1a5fdSTejun Heo 4681a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 468249e3cf44STejun Heo for_each_pwq(pwq, wq) 4683699ce097STejun Heo pwq_adjust_max_active(pwq); 4684a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4685a0a1a5fdSTejun Heo 4686e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 4687a0a1a5fdSTejun Heo 468868e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 46893af24433SOleg Nesterov 46903af24433SOleg Nesterov return wq; 4691d2c1d404STejun Heo 469282efcab3SBart Van Assche err_unreg_lockdep: 4693009bb421SBart Van Assche wq_unregister_lockdep(wq); 4694009bb421SBart Van Assche wq_free_lockdep(wq); 469582efcab3SBart Van Assche err_free_wq: 46966029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 46974690c4abSTejun Heo kfree(wq); 4698d2c1d404STejun Heo return NULL; 4699d2c1d404STejun Heo err_destroy: 4700d2c1d404STejun Heo destroy_workqueue(wq); 47014690c4abSTejun Heo return NULL; 47021da177e4SLinus Torvalds } 4703669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue); 47041da177e4SLinus Torvalds 4705c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq) 4706c29eb853STejun Heo { 4707c29eb853STejun Heo int i; 4708c29eb853STejun Heo 4709c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 4710c29eb853STejun Heo if (pwq->nr_in_flight[i]) 4711c29eb853STejun Heo return true; 4712c29eb853STejun Heo 4713c29eb853STejun Heo if ((pwq != pwq->wq->dfl_pwq) && (pwq->refcnt > 1)) 4714c29eb853STejun Heo return true; 4715f97a4a1aSLai Jiangshan if (pwq->nr_active || !list_empty(&pwq->inactive_works)) 4716c29eb853STejun Heo return true; 4717c29eb853STejun Heo 4718c29eb853STejun Heo return false; 4719c29eb853STejun Heo } 4720c29eb853STejun Heo 47213af24433SOleg Nesterov /** 47223af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 47233af24433SOleg Nesterov * @wq: target workqueue 47243af24433SOleg Nesterov * 47253af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 47263af24433SOleg Nesterov */ 47273af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 47283af24433SOleg Nesterov { 472949e3cf44STejun Heo struct pool_workqueue *pwq; 4730636b927eSTejun Heo int cpu; 47313af24433SOleg Nesterov 4732def98c84STejun Heo /* 4733def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't 4734def98c84STejun Heo * lead to sysfs name conflicts. 4735def98c84STejun Heo */ 4736def98c84STejun Heo workqueue_sysfs_unregister(wq); 4737def98c84STejun Heo 473833e3f0a3SRichard Clark /* mark the workqueue destruction is in progress */ 473933e3f0a3SRichard Clark mutex_lock(&wq->mutex); 474033e3f0a3SRichard Clark wq->flags |= __WQ_DESTROYING; 474133e3f0a3SRichard Clark mutex_unlock(&wq->mutex); 474233e3f0a3SRichard Clark 47439c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 47449c5a2ba7STejun Heo drain_workqueue(wq); 4745c8efcc25STejun Heo 4746def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ 4747def98c84STejun Heo if (wq->rescuer) { 4748def98c84STejun Heo struct worker *rescuer = wq->rescuer; 4749def98c84STejun Heo 4750def98c84STejun Heo /* this prevents new queueing */ 4751a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 4752def98c84STejun Heo wq->rescuer = NULL; 4753a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 4754def98c84STejun Heo 4755def98c84STejun Heo /* rescuer will empty maydays list before exiting */ 4756def98c84STejun Heo kthread_stop(rescuer->task); 47578efe1223STejun Heo kfree(rescuer); 4758def98c84STejun Heo } 4759def98c84STejun Heo 4760c29eb853STejun Heo /* 4761c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all 4762c29eb853STejun Heo * in-flight operations which may do put_pwq(). 4763c29eb853STejun Heo */ 4764c29eb853STejun Heo mutex_lock(&wq_pool_mutex); 4765b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 476649e3cf44STejun Heo for_each_pwq(pwq, wq) { 4767a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 4768c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) { 47691d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n", 4770e66b39afSTejun Heo __func__, wq->name); 4771c29eb853STejun Heo show_pwq(pwq); 4772a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 4773b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 4774c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 477555df0933SImran Khan show_one_workqueue(wq); 47766183c009STejun Heo return; 477776af4d93STejun Heo } 4778a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 477976af4d93STejun Heo } 4780b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 47816183c009STejun Heo 4782a0a1a5fdSTejun Heo /* 4783a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 4784a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 4785a0a1a5fdSTejun Heo */ 4786e2dca7adSTejun Heo list_del_rcu(&wq->list); 478768e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 47883af24433SOleg Nesterov 47898864b4e5STejun Heo /* 4790636b927eSTejun Heo * We're the sole accessor of @wq. Directly access cpu_pwq and dfl_pwq 4791636b927eSTejun Heo * to put the base refs. @wq will be auto-destroyed from the last 4792636b927eSTejun Heo * pwq_put. RCU read lock prevents @wq from going away from under us. 47938864b4e5STejun Heo */ 4794636b927eSTejun Heo rcu_read_lock(); 4795636b927eSTejun Heo 4796636b927eSTejun Heo for_each_possible_cpu(cpu) { 4797636b927eSTejun Heo pwq = rcu_access_pointer(*per_cpu_ptr(wq->cpu_pwq, cpu)); 4798636b927eSTejun Heo RCU_INIT_POINTER(*per_cpu_ptr(wq->cpu_pwq, cpu), NULL); 47994c16bd32STejun Heo put_pwq_unlocked(pwq); 48004c16bd32STejun Heo } 48014c16bd32STejun Heo 4802636b927eSTejun Heo put_pwq_unlocked(wq->dfl_pwq); 48034c16bd32STejun Heo wq->dfl_pwq = NULL; 4804636b927eSTejun Heo 4805636b927eSTejun Heo rcu_read_unlock(); 48063af24433SOleg Nesterov } 48073af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 48083af24433SOleg Nesterov 4809dcd989cbSTejun Heo /** 4810dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 4811dcd989cbSTejun Heo * @wq: target workqueue 4812dcd989cbSTejun Heo * @max_active: new max_active value. 4813dcd989cbSTejun Heo * 4814dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 4815dcd989cbSTejun Heo * 4816dcd989cbSTejun Heo * CONTEXT: 4817dcd989cbSTejun Heo * Don't call from IRQ context. 4818dcd989cbSTejun Heo */ 4819dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 4820dcd989cbSTejun Heo { 482149e3cf44STejun Heo struct pool_workqueue *pwq; 4822dcd989cbSTejun Heo 48238719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 48240a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 48258719dceaSTejun Heo return; 48268719dceaSTejun Heo 4827f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 4828dcd989cbSTejun Heo 4829a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4830dcd989cbSTejun Heo 48310a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 4832dcd989cbSTejun Heo wq->saved_max_active = max_active; 4833dcd989cbSTejun Heo 4834699ce097STejun Heo for_each_pwq(pwq, wq) 4835699ce097STejun Heo pwq_adjust_max_active(pwq); 4836dcd989cbSTejun Heo 4837a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4838dcd989cbSTejun Heo } 4839dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 4840dcd989cbSTejun Heo 4841dcd989cbSTejun Heo /** 484227d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 484327d4ee03SLukas Wunner * 484427d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 484527d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 484627d4ee03SLukas Wunner * 484727d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 484827d4ee03SLukas Wunner */ 484927d4ee03SLukas Wunner struct work_struct *current_work(void) 485027d4ee03SLukas Wunner { 485127d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 485227d4ee03SLukas Wunner 485327d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 485427d4ee03SLukas Wunner } 485527d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 485627d4ee03SLukas Wunner 485727d4ee03SLukas Wunner /** 4858e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 4859e6267616STejun Heo * 4860e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 4861e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 4862d185af30SYacine Belkadi * 4863d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 4864e6267616STejun Heo */ 4865e6267616STejun Heo bool current_is_workqueue_rescuer(void) 4866e6267616STejun Heo { 4867e6267616STejun Heo struct worker *worker = current_wq_worker(); 4868e6267616STejun Heo 48696a092dfdSLai Jiangshan return worker && worker->rescue_wq; 4870e6267616STejun Heo } 4871e6267616STejun Heo 4872e6267616STejun Heo /** 4873dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 4874dcd989cbSTejun Heo * @cpu: CPU in question 4875dcd989cbSTejun Heo * @wq: target workqueue 4876dcd989cbSTejun Heo * 4877dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 4878dcd989cbSTejun Heo * no synchronization around this function and the test result is 4879dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4880dcd989cbSTejun Heo * 4881d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 4882636b927eSTejun Heo * 4883636b927eSTejun Heo * With the exception of ordered workqueues, all workqueues have per-cpu 4884636b927eSTejun Heo * pool_workqueues, each with its own congested state. A workqueue being 4885636b927eSTejun Heo * congested on one CPU doesn't mean that the workqueue is contested on any 4886636b927eSTejun Heo * other CPUs. 4887d3251859STejun Heo * 4888d185af30SYacine Belkadi * Return: 4889dcd989cbSTejun Heo * %true if congested, %false otherwise. 4890dcd989cbSTejun Heo */ 4891d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 4892dcd989cbSTejun Heo { 48937fb98ea7STejun Heo struct pool_workqueue *pwq; 489476af4d93STejun Heo bool ret; 489576af4d93STejun Heo 489624acfb71SThomas Gleixner rcu_read_lock(); 489724acfb71SThomas Gleixner preempt_disable(); 48987fb98ea7STejun Heo 4899d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 4900d3251859STejun Heo cpu = smp_processor_id(); 4901d3251859STejun Heo 4902687a9aa5STejun Heo pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 4903f97a4a1aSLai Jiangshan ret = !list_empty(&pwq->inactive_works); 4904636b927eSTejun Heo 490524acfb71SThomas Gleixner preempt_enable(); 490624acfb71SThomas Gleixner rcu_read_unlock(); 490776af4d93STejun Heo 490876af4d93STejun Heo return ret; 4909dcd989cbSTejun Heo } 4910dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 4911dcd989cbSTejun Heo 4912dcd989cbSTejun Heo /** 4913dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 4914dcd989cbSTejun Heo * @work: the work to be tested 4915dcd989cbSTejun Heo * 4916dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 4917dcd989cbSTejun Heo * synchronization around this function and the test result is 4918dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4919dcd989cbSTejun Heo * 4920d185af30SYacine Belkadi * Return: 4921dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 4922dcd989cbSTejun Heo */ 4923dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 4924dcd989cbSTejun Heo { 4925fa1b54e6STejun Heo struct worker_pool *pool; 4926dcd989cbSTejun Heo unsigned long flags; 4927dcd989cbSTejun Heo unsigned int ret = 0; 4928dcd989cbSTejun Heo 4929dcd989cbSTejun Heo if (work_pending(work)) 4930dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 4931038366c5SLai Jiangshan 493224acfb71SThomas Gleixner rcu_read_lock(); 4933fa1b54e6STejun Heo pool = get_work_pool(work); 4934038366c5SLai Jiangshan if (pool) { 4935a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 4936c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 4937dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 4938a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 4939038366c5SLai Jiangshan } 494024acfb71SThomas Gleixner rcu_read_unlock(); 4941dcd989cbSTejun Heo 4942dcd989cbSTejun Heo return ret; 4943dcd989cbSTejun Heo } 4944dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 4945dcd989cbSTejun Heo 49463d1cb205STejun Heo /** 49473d1cb205STejun Heo * set_worker_desc - set description for the current work item 49483d1cb205STejun Heo * @fmt: printf-style format string 49493d1cb205STejun Heo * @...: arguments for the format string 49503d1cb205STejun Heo * 49513d1cb205STejun Heo * This function can be called by a running work function to describe what 49523d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 49533d1cb205STejun Heo * information will be printed out together to help debugging. The 49543d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 49553d1cb205STejun Heo */ 49563d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 49573d1cb205STejun Heo { 49583d1cb205STejun Heo struct worker *worker = current_wq_worker(); 49593d1cb205STejun Heo va_list args; 49603d1cb205STejun Heo 49613d1cb205STejun Heo if (worker) { 49623d1cb205STejun Heo va_start(args, fmt); 49633d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 49643d1cb205STejun Heo va_end(args); 49653d1cb205STejun Heo } 49663d1cb205STejun Heo } 49675c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 49683d1cb205STejun Heo 49693d1cb205STejun Heo /** 49703d1cb205STejun Heo * print_worker_info - print out worker information and description 49713d1cb205STejun Heo * @log_lvl: the log level to use when printing 49723d1cb205STejun Heo * @task: target task 49733d1cb205STejun Heo * 49743d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 49753d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 49763d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 49773d1cb205STejun Heo * 49783d1cb205STejun Heo * This function can be safely called on any task as long as the 49793d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 49803d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 49813d1cb205STejun Heo */ 49823d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 49833d1cb205STejun Heo { 49843d1cb205STejun Heo work_func_t *fn = NULL; 49853d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 49863d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 49873d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 49883d1cb205STejun Heo struct workqueue_struct *wq = NULL; 49893d1cb205STejun Heo struct worker *worker; 49903d1cb205STejun Heo 49913d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 49923d1cb205STejun Heo return; 49933d1cb205STejun Heo 49943d1cb205STejun Heo /* 49953d1cb205STejun Heo * This function is called without any synchronization and @task 49963d1cb205STejun Heo * could be in any state. Be careful with dereferences. 49973d1cb205STejun Heo */ 4998e700591aSPetr Mladek worker = kthread_probe_data(task); 49993d1cb205STejun Heo 50003d1cb205STejun Heo /* 50018bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 50028bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 50033d1cb205STejun Heo */ 5004fe557319SChristoph Hellwig copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); 5005fe557319SChristoph Hellwig copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); 5006fe557319SChristoph Hellwig copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); 5007fe557319SChristoph Hellwig copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); 5008fe557319SChristoph Hellwig copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); 50093d1cb205STejun Heo 50103d1cb205STejun Heo if (fn || name[0] || desc[0]) { 5011d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn); 50128bf89593STejun Heo if (strcmp(name, desc)) 50133d1cb205STejun Heo pr_cont(" (%s)", desc); 50143d1cb205STejun Heo pr_cont("\n"); 50153d1cb205STejun Heo } 50163d1cb205STejun Heo } 50173d1cb205STejun Heo 50183494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 50193494fc30STejun Heo { 50203494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 50213494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 50223494fc30STejun Heo pr_cont(" node=%d", pool->node); 50233494fc30STejun Heo pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice); 50243494fc30STejun Heo } 50253494fc30STejun Heo 5026c76feb0dSPaul E. McKenney struct pr_cont_work_struct { 5027c76feb0dSPaul E. McKenney bool comma; 5028c76feb0dSPaul E. McKenney work_func_t func; 5029c76feb0dSPaul E. McKenney long ctr; 5030c76feb0dSPaul E. McKenney }; 5031c76feb0dSPaul E. McKenney 5032c76feb0dSPaul E. McKenney static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) 5033c76feb0dSPaul E. McKenney { 5034c76feb0dSPaul E. McKenney if (!pcwsp->ctr) 5035c76feb0dSPaul E. McKenney goto out_record; 5036c76feb0dSPaul E. McKenney if (func == pcwsp->func) { 5037c76feb0dSPaul E. McKenney pcwsp->ctr++; 5038c76feb0dSPaul E. McKenney return; 5039c76feb0dSPaul E. McKenney } 5040c76feb0dSPaul E. McKenney if (pcwsp->ctr == 1) 5041c76feb0dSPaul E. McKenney pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); 5042c76feb0dSPaul E. McKenney else 5043c76feb0dSPaul E. McKenney pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); 5044c76feb0dSPaul E. McKenney pcwsp->ctr = 0; 5045c76feb0dSPaul E. McKenney out_record: 5046c76feb0dSPaul E. McKenney if ((long)func == -1L) 5047c76feb0dSPaul E. McKenney return; 5048c76feb0dSPaul E. McKenney pcwsp->comma = comma; 5049c76feb0dSPaul E. McKenney pcwsp->func = func; 5050c76feb0dSPaul E. McKenney pcwsp->ctr = 1; 5051c76feb0dSPaul E. McKenney } 5052c76feb0dSPaul E. McKenney 5053c76feb0dSPaul E. McKenney static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) 50543494fc30STejun Heo { 50553494fc30STejun Heo if (work->func == wq_barrier_func) { 50563494fc30STejun Heo struct wq_barrier *barr; 50573494fc30STejun Heo 50583494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 50593494fc30STejun Heo 5060c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 50613494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 50623494fc30STejun Heo task_pid_nr(barr->task)); 50633494fc30STejun Heo } else { 5064c76feb0dSPaul E. McKenney if (!comma) 5065c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 5066c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, work->func, pcwsp); 50673494fc30STejun Heo } 50683494fc30STejun Heo } 50693494fc30STejun Heo 50703494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 50713494fc30STejun Heo { 5072c76feb0dSPaul E. McKenney struct pr_cont_work_struct pcws = { .ctr = 0, }; 50733494fc30STejun Heo struct worker_pool *pool = pwq->pool; 50743494fc30STejun Heo struct work_struct *work; 50753494fc30STejun Heo struct worker *worker; 50763494fc30STejun Heo bool has_in_flight = false, has_pending = false; 50773494fc30STejun Heo int bkt; 50783494fc30STejun Heo 50793494fc30STejun Heo pr_info(" pwq %d:", pool->id); 50803494fc30STejun Heo pr_cont_pool_info(pool); 50813494fc30STejun Heo 5082e66b39afSTejun Heo pr_cont(" active=%d/%d refcnt=%d%s\n", 5083e66b39afSTejun Heo pwq->nr_active, pwq->max_active, pwq->refcnt, 50843494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 50853494fc30STejun Heo 50863494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 50873494fc30STejun Heo if (worker->current_pwq == pwq) { 50883494fc30STejun Heo has_in_flight = true; 50893494fc30STejun Heo break; 50903494fc30STejun Heo } 50913494fc30STejun Heo } 50923494fc30STejun Heo if (has_in_flight) { 50933494fc30STejun Heo bool comma = false; 50943494fc30STejun Heo 50953494fc30STejun Heo pr_info(" in-flight:"); 50963494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 50973494fc30STejun Heo if (worker->current_pwq != pwq) 50983494fc30STejun Heo continue; 50993494fc30STejun Heo 5100d75f773cSSakari Ailus pr_cont("%s %d%s:%ps", comma ? "," : "", 51013494fc30STejun Heo task_pid_nr(worker->task), 510230ae2fc0STejun Heo worker->rescue_wq ? "(RESCUER)" : "", 51033494fc30STejun Heo worker->current_func); 51043494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 5105c76feb0dSPaul E. McKenney pr_cont_work(false, work, &pcws); 5106c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 51073494fc30STejun Heo comma = true; 51083494fc30STejun Heo } 51093494fc30STejun Heo pr_cont("\n"); 51103494fc30STejun Heo } 51113494fc30STejun Heo 51123494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 51133494fc30STejun Heo if (get_work_pwq(work) == pwq) { 51143494fc30STejun Heo has_pending = true; 51153494fc30STejun Heo break; 51163494fc30STejun Heo } 51173494fc30STejun Heo } 51183494fc30STejun Heo if (has_pending) { 51193494fc30STejun Heo bool comma = false; 51203494fc30STejun Heo 51213494fc30STejun Heo pr_info(" pending:"); 51223494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 51233494fc30STejun Heo if (get_work_pwq(work) != pwq) 51243494fc30STejun Heo continue; 51253494fc30STejun Heo 5126c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 51273494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 51283494fc30STejun Heo } 5129c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 51303494fc30STejun Heo pr_cont("\n"); 51313494fc30STejun Heo } 51323494fc30STejun Heo 5133f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 51343494fc30STejun Heo bool comma = false; 51353494fc30STejun Heo 5136f97a4a1aSLai Jiangshan pr_info(" inactive:"); 5137f97a4a1aSLai Jiangshan list_for_each_entry(work, &pwq->inactive_works, entry) { 5138c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 51393494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 51403494fc30STejun Heo } 5141c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 51423494fc30STejun Heo pr_cont("\n"); 51433494fc30STejun Heo } 51443494fc30STejun Heo } 51453494fc30STejun Heo 51463494fc30STejun Heo /** 514755df0933SImran Khan * show_one_workqueue - dump state of specified workqueue 514855df0933SImran Khan * @wq: workqueue whose state will be printed 51493494fc30STejun Heo */ 515055df0933SImran Khan void show_one_workqueue(struct workqueue_struct *wq) 51513494fc30STejun Heo { 51523494fc30STejun Heo struct pool_workqueue *pwq; 51533494fc30STejun Heo bool idle = true; 515455df0933SImran Khan unsigned long flags; 51553494fc30STejun Heo 51563494fc30STejun Heo for_each_pwq(pwq, wq) { 5157f97a4a1aSLai Jiangshan if (pwq->nr_active || !list_empty(&pwq->inactive_works)) { 51583494fc30STejun Heo idle = false; 51593494fc30STejun Heo break; 51603494fc30STejun Heo } 51613494fc30STejun Heo } 516255df0933SImran Khan if (idle) /* Nothing to print for idle workqueue */ 516355df0933SImran Khan return; 51643494fc30STejun Heo 51653494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 51663494fc30STejun Heo 51673494fc30STejun Heo for_each_pwq(pwq, wq) { 5168a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pwq->pool->lock, flags); 516957116ce1SJohan Hovold if (pwq->nr_active || !list_empty(&pwq->inactive_works)) { 517057116ce1SJohan Hovold /* 517157116ce1SJohan Hovold * Defer printing to avoid deadlocks in console 517257116ce1SJohan Hovold * drivers that queue work while holding locks 517357116ce1SJohan Hovold * also taken in their write paths. 517457116ce1SJohan Hovold */ 517557116ce1SJohan Hovold printk_deferred_enter(); 51763494fc30STejun Heo show_pwq(pwq); 517757116ce1SJohan Hovold printk_deferred_exit(); 517857116ce1SJohan Hovold } 5179a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 518062635ea8SSergey Senozhatsky /* 518162635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 518255df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 518362635ea8SSergey Senozhatsky * hard lockup. 518462635ea8SSergey Senozhatsky */ 518562635ea8SSergey Senozhatsky touch_nmi_watchdog(); 51863494fc30STejun Heo } 518755df0933SImran Khan 51883494fc30STejun Heo } 51893494fc30STejun Heo 519055df0933SImran Khan /** 519155df0933SImran Khan * show_one_worker_pool - dump state of specified worker pool 519255df0933SImran Khan * @pool: worker pool whose state will be printed 519355df0933SImran Khan */ 519455df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool) 519555df0933SImran Khan { 51963494fc30STejun Heo struct worker *worker; 51973494fc30STejun Heo bool first = true; 519855df0933SImran Khan unsigned long flags; 5199335a42ebSPetr Mladek unsigned long hung = 0; 52003494fc30STejun Heo 5201a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 52023494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 52033494fc30STejun Heo goto next_pool; 5204335a42ebSPetr Mladek 5205335a42ebSPetr Mladek /* How long the first pending work is waiting for a worker. */ 5206335a42ebSPetr Mladek if (!list_empty(&pool->worklist)) 5207335a42ebSPetr Mladek hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; 5208335a42ebSPetr Mladek 520957116ce1SJohan Hovold /* 521057116ce1SJohan Hovold * Defer printing to avoid deadlocks in console drivers that 521157116ce1SJohan Hovold * queue work while holding locks also taken in their write 521257116ce1SJohan Hovold * paths. 521357116ce1SJohan Hovold */ 521457116ce1SJohan Hovold printk_deferred_enter(); 52153494fc30STejun Heo pr_info("pool %d:", pool->id); 52163494fc30STejun Heo pr_cont_pool_info(pool); 5217335a42ebSPetr Mladek pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); 52183494fc30STejun Heo if (pool->manager) 52193494fc30STejun Heo pr_cont(" manager: %d", 52203494fc30STejun Heo task_pid_nr(pool->manager->task)); 52213494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 52223494fc30STejun Heo pr_cont(" %s%d", first ? "idle: " : "", 52233494fc30STejun Heo task_pid_nr(worker->task)); 52243494fc30STejun Heo first = false; 52253494fc30STejun Heo } 52263494fc30STejun Heo pr_cont("\n"); 522757116ce1SJohan Hovold printk_deferred_exit(); 52283494fc30STejun Heo next_pool: 5229a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 523062635ea8SSergey Senozhatsky /* 523162635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 523255df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 523362635ea8SSergey Senozhatsky * hard lockup. 523462635ea8SSergey Senozhatsky */ 523562635ea8SSergey Senozhatsky touch_nmi_watchdog(); 523655df0933SImran Khan 52373494fc30STejun Heo } 52383494fc30STejun Heo 523955df0933SImran Khan /** 524055df0933SImran Khan * show_all_workqueues - dump workqueue state 524155df0933SImran Khan * 5242704bc669SJungseung Lee * Called from a sysrq handler and prints out all busy workqueues and pools. 524355df0933SImran Khan */ 524455df0933SImran Khan void show_all_workqueues(void) 524555df0933SImran Khan { 524655df0933SImran Khan struct workqueue_struct *wq; 524755df0933SImran Khan struct worker_pool *pool; 524855df0933SImran Khan int pi; 524955df0933SImran Khan 525055df0933SImran Khan rcu_read_lock(); 525155df0933SImran Khan 525255df0933SImran Khan pr_info("Showing busy workqueues and worker pools:\n"); 525355df0933SImran Khan 525455df0933SImran Khan list_for_each_entry_rcu(wq, &workqueues, list) 525555df0933SImran Khan show_one_workqueue(wq); 525655df0933SImran Khan 525755df0933SImran Khan for_each_pool(pool, pi) 525855df0933SImran Khan show_one_worker_pool(pool); 525955df0933SImran Khan 526024acfb71SThomas Gleixner rcu_read_unlock(); 52613494fc30STejun Heo } 52623494fc30STejun Heo 5263704bc669SJungseung Lee /** 5264704bc669SJungseung Lee * show_freezable_workqueues - dump freezable workqueue state 5265704bc669SJungseung Lee * 5266704bc669SJungseung Lee * Called from try_to_freeze_tasks() and prints out all freezable workqueues 5267704bc669SJungseung Lee * still busy. 5268704bc669SJungseung Lee */ 5269704bc669SJungseung Lee void show_freezable_workqueues(void) 5270704bc669SJungseung Lee { 5271704bc669SJungseung Lee struct workqueue_struct *wq; 5272704bc669SJungseung Lee 5273704bc669SJungseung Lee rcu_read_lock(); 5274704bc669SJungseung Lee 5275704bc669SJungseung Lee pr_info("Showing freezable workqueues that are still busy:\n"); 5276704bc669SJungseung Lee 5277704bc669SJungseung Lee list_for_each_entry_rcu(wq, &workqueues, list) { 5278704bc669SJungseung Lee if (!(wq->flags & WQ_FREEZABLE)) 5279704bc669SJungseung Lee continue; 5280704bc669SJungseung Lee show_one_workqueue(wq); 5281704bc669SJungseung Lee } 5282704bc669SJungseung Lee 5283704bc669SJungseung Lee rcu_read_unlock(); 5284704bc669SJungseung Lee } 5285704bc669SJungseung Lee 52866b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 52876b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 52886b59808bSTejun Heo { 52896b59808bSTejun Heo int off; 52906b59808bSTejun Heo 52916b59808bSTejun Heo /* always show the actual comm */ 52926b59808bSTejun Heo off = strscpy(buf, task->comm, size); 52936b59808bSTejun Heo if (off < 0) 52946b59808bSTejun Heo return; 52956b59808bSTejun Heo 5296197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 52976b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 52986b59808bSTejun Heo 5299197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 5300197f6accSTejun Heo struct worker *worker = kthread_data(task); 5301197f6accSTejun Heo struct worker_pool *pool = worker->pool; 53026b59808bSTejun Heo 53036b59808bSTejun Heo if (pool) { 5304a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 53056b59808bSTejun Heo /* 5306197f6accSTejun Heo * ->desc tracks information (wq name or 5307197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 5308197f6accSTejun Heo * current, prepend '+', otherwise '-'. 53096b59808bSTejun Heo */ 53106b59808bSTejun Heo if (worker->desc[0] != '\0') { 53116b59808bSTejun Heo if (worker->current_work) 53126b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 53136b59808bSTejun Heo worker->desc); 53146b59808bSTejun Heo else 53156b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 53166b59808bSTejun Heo worker->desc); 53176b59808bSTejun Heo } 5318a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 53196b59808bSTejun Heo } 5320197f6accSTejun Heo } 53216b59808bSTejun Heo 53226b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 53236b59808bSTejun Heo } 53246b59808bSTejun Heo 532566448bc2SMathieu Malaterre #ifdef CONFIG_SMP 532666448bc2SMathieu Malaterre 5327db7bccf4STejun Heo /* 5328db7bccf4STejun Heo * CPU hotplug. 5329db7bccf4STejun Heo * 5330e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 5331112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 5332706026c2STejun Heo * pool which make migrating pending and scheduled works very 5333e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 533494cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 5335e22bee78STejun Heo * blocked draining impractical. 5336e22bee78STejun Heo * 533724647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 5338628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 5339628c78e7STejun Heo * cpu comes back online. 5340db7bccf4STejun Heo */ 5341db7bccf4STejun Heo 5342e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 5343db7bccf4STejun Heo { 53444ce62e9eSTejun Heo struct worker_pool *pool; 5345db7bccf4STejun Heo struct worker *worker; 5346db7bccf4STejun Heo 5347f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 53481258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 5349a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 5350e22bee78STejun Heo 5351f2d5a0eeSTejun Heo /* 535292f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 535394cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 535411b45b0bSLai Jiangshan * must be on the cpu. After this, they may become diasporas. 5355b4ac9384SLai Jiangshan * And the preemption disabled section in their sched callbacks 5356b4ac9384SLai Jiangshan * are guaranteed to see WORKER_UNBOUND since the code here 5357b4ac9384SLai Jiangshan * is on the same cpu. 5358f2d5a0eeSTejun Heo */ 5359da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5360403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 5361db7bccf4STejun Heo 536224647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 5363f2d5a0eeSTejun Heo 5364e22bee78STejun Heo /* 5365989442d7SLai Jiangshan * The handling of nr_running in sched callbacks are disabled 5366989442d7SLai Jiangshan * now. Zap nr_running. After this, nr_running stays zero and 5367989442d7SLai Jiangshan * need_more_worker() and keep_working() are always true as 5368989442d7SLai Jiangshan * long as the worklist is not empty. This pool now behaves as 5369989442d7SLai Jiangshan * an unbound (in terms of concurrency management) pool which 5370eb283428SLai Jiangshan * are served by workers tied to the pool. 5371e22bee78STejun Heo */ 5372bc35f7efSLai Jiangshan pool->nr_running = 0; 5373eb283428SLai Jiangshan 5374eb283428SLai Jiangshan /* 5375eb283428SLai Jiangshan * With concurrency management just turned off, a busy 5376eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 5377eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 5378eb283428SLai Jiangshan */ 5379*0219a352STejun Heo kick_pool(pool); 5380989442d7SLai Jiangshan 5381a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 5382989442d7SLai Jiangshan 5383793777bcSValentin Schneider for_each_pool_worker(worker, pool) 5384793777bcSValentin Schneider unbind_worker(worker); 5385989442d7SLai Jiangshan 5386989442d7SLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 5387eb283428SLai Jiangshan } 5388db7bccf4STejun Heo } 5389db7bccf4STejun Heo 5390bd7c089eSTejun Heo /** 5391bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 5392bd7c089eSTejun Heo * @pool: pool of interest 5393bd7c089eSTejun Heo * 5394a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 5395bd7c089eSTejun Heo */ 5396bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 5397bd7c089eSTejun Heo { 5398a9ab775bSTejun Heo struct worker *worker; 5399bd7c089eSTejun Heo 54001258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 5401bd7c089eSTejun Heo 5402bd7c089eSTejun Heo /* 5403a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 5404a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 5405402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 5406a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 5407a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 5408bd7c089eSTejun Heo */ 5409c63a2e52SValentin Schneider for_each_pool_worker(worker, pool) { 5410c63a2e52SValentin Schneider kthread_set_per_cpu(worker->task, pool->cpu); 5411c63a2e52SValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 5412c63a2e52SValentin Schneider pool->attrs->cpumask) < 0); 5413c63a2e52SValentin Schneider } 5414a9ab775bSTejun Heo 5415a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 5416f7c17d26SWanpeng Li 54173de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 5418a9ab775bSTejun Heo 5419da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 5420a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 5421a9ab775bSTejun Heo 5422a9ab775bSTejun Heo /* 5423a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 5424a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 5425a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 5426a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 5427a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 5428a9ab775bSTejun Heo * concurrency management. Note that when or whether 5429a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 5430a9ab775bSTejun Heo * 5431c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 5432a9ab775bSTejun Heo * tested without holding any lock in 54336d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may 5434a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 5435a9ab775bSTejun Heo * management operations. 5436bd7c089eSTejun Heo */ 5437a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 5438a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 5439a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 5440c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 5441bd7c089eSTejun Heo } 5442a9ab775bSTejun Heo 5443a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 5444bd7c089eSTejun Heo } 5445bd7c089eSTejun Heo 54467dbc725eSTejun Heo /** 54477dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 54487dbc725eSTejun Heo * @pool: unbound pool of interest 54497dbc725eSTejun Heo * @cpu: the CPU which is coming up 54507dbc725eSTejun Heo * 54517dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 54527dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 54537dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 54547dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 54557dbc725eSTejun Heo */ 54567dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 54577dbc725eSTejun Heo { 54587dbc725eSTejun Heo static cpumask_t cpumask; 54597dbc725eSTejun Heo struct worker *worker; 54607dbc725eSTejun Heo 54611258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 54627dbc725eSTejun Heo 54637dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 54647dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 54657dbc725eSTejun Heo return; 54667dbc725eSTejun Heo 54677dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 54687dbc725eSTejun Heo 54697dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 5470da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5471d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 54727dbc725eSTejun Heo } 54737dbc725eSTejun Heo 54747ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 54751da177e4SLinus Torvalds { 54764ce62e9eSTejun Heo struct worker_pool *pool; 54771da177e4SLinus Torvalds 5478f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 54793ce63377STejun Heo if (pool->nr_workers) 54803ce63377STejun Heo continue; 5481051e1850SLai Jiangshan if (!create_worker(pool)) 54827ee681b2SThomas Gleixner return -ENOMEM; 54833af24433SOleg Nesterov } 54847ee681b2SThomas Gleixner return 0; 54857ee681b2SThomas Gleixner } 54861da177e4SLinus Torvalds 54877ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 54887ee681b2SThomas Gleixner { 54897ee681b2SThomas Gleixner struct worker_pool *pool; 54907ee681b2SThomas Gleixner struct workqueue_struct *wq; 54917ee681b2SThomas Gleixner int pi; 54927ee681b2SThomas Gleixner 549368e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 54947dbc725eSTejun Heo 54957dbc725eSTejun Heo for_each_pool(pool, pi) { 54961258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 549794cf58bbSTejun Heo 5498f05b558dSLai Jiangshan if (pool->cpu == cpu) 549994cf58bbSTejun Heo rebind_workers(pool); 5500f05b558dSLai Jiangshan else if (pool->cpu < 0) 55017dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 550294cf58bbSTejun Heo 55031258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 550494cf58bbSTejun Heo } 55057dbc725eSTejun Heo 5506fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 55074cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 550884193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 550984193c07STejun Heo 551084193c07STejun Heo if (attrs) { 551184193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 55124cbfd3deSTejun Heo int tcpu; 55134cbfd3deSTejun Heo 551484193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 5515fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, true); 55164cbfd3deSTejun Heo } 55174cbfd3deSTejun Heo } 55184c16bd32STejun Heo 551968e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 55207ee681b2SThomas Gleixner return 0; 552165758202STejun Heo } 552265758202STejun Heo 55237ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 552465758202STejun Heo { 55254c16bd32STejun Heo struct workqueue_struct *wq; 55268db25e78STejun Heo 55274c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 5528e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 5529e8b3f8dbSLai Jiangshan return -1; 5530e8b3f8dbSLai Jiangshan 5531e8b3f8dbSLai Jiangshan unbind_workers(cpu); 55324c16bd32STejun Heo 5533fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 55344c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 55354cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 553684193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 553784193c07STejun Heo 553884193c07STejun Heo if (attrs) { 553984193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 55404cbfd3deSTejun Heo int tcpu; 55414cbfd3deSTejun Heo 554284193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 5543fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, false); 55444cbfd3deSTejun Heo } 55454cbfd3deSTejun Heo } 55464c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 55474c16bd32STejun Heo 55487ee681b2SThomas Gleixner return 0; 554965758202STejun Heo } 555065758202STejun Heo 55512d3854a3SRusty Russell struct work_for_cpu { 5552ed48ece2STejun Heo struct work_struct work; 55532d3854a3SRusty Russell long (*fn)(void *); 55542d3854a3SRusty Russell void *arg; 55552d3854a3SRusty Russell long ret; 55562d3854a3SRusty Russell }; 55572d3854a3SRusty Russell 5558ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 55592d3854a3SRusty Russell { 5560ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 5561ed48ece2STejun Heo 55622d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 55632d3854a3SRusty Russell } 55642d3854a3SRusty Russell 55652d3854a3SRusty Russell /** 556622aceb31SAnna-Maria Gleixner * work_on_cpu - run a function in thread context on a particular cpu 55672d3854a3SRusty Russell * @cpu: the cpu to run on 55682d3854a3SRusty Russell * @fn: the function to run 55692d3854a3SRusty Russell * @arg: the function arg 55702d3854a3SRusty Russell * 557131ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 55726b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 5573d185af30SYacine Belkadi * 5574d185af30SYacine Belkadi * Return: The value @fn returns. 55752d3854a3SRusty Russell */ 5576d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg) 55772d3854a3SRusty Russell { 5578ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 55792d3854a3SRusty Russell 5580ed48ece2STejun Heo INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn); 5581ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 558212997d1aSBjorn Helgaas flush_work(&wfc.work); 5583440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 55842d3854a3SRusty Russell return wfc.ret; 55852d3854a3SRusty Russell } 55862d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 55870e8d6a93SThomas Gleixner 55880e8d6a93SThomas Gleixner /** 55890e8d6a93SThomas Gleixner * work_on_cpu_safe - run a function in thread context on a particular cpu 55900e8d6a93SThomas Gleixner * @cpu: the cpu to run on 55910e8d6a93SThomas Gleixner * @fn: the function to run 55920e8d6a93SThomas Gleixner * @arg: the function argument 55930e8d6a93SThomas Gleixner * 55940e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 55950e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 55960e8d6a93SThomas Gleixner * 55970e8d6a93SThomas Gleixner * Return: The value @fn returns. 55980e8d6a93SThomas Gleixner */ 55990e8d6a93SThomas Gleixner long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg) 56000e8d6a93SThomas Gleixner { 56010e8d6a93SThomas Gleixner long ret = -ENODEV; 56020e8d6a93SThomas Gleixner 5603ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 56040e8d6a93SThomas Gleixner if (cpu_online(cpu)) 56050e8d6a93SThomas Gleixner ret = work_on_cpu(cpu, fn, arg); 5606ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 56070e8d6a93SThomas Gleixner return ret; 56080e8d6a93SThomas Gleixner } 56090e8d6a93SThomas Gleixner EXPORT_SYMBOL_GPL(work_on_cpu_safe); 56102d3854a3SRusty Russell #endif /* CONFIG_SMP */ 56112d3854a3SRusty Russell 5612a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 5613e7577c50SRusty Russell 5614a0a1a5fdSTejun Heo /** 5615a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 5616a0a1a5fdSTejun Heo * 561758a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 5618f97a4a1aSLai Jiangshan * workqueues will queue new works to their inactive_works list instead of 5619706026c2STejun Heo * pool->worklist. 5620a0a1a5fdSTejun Heo * 5621a0a1a5fdSTejun Heo * CONTEXT: 5622a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5623a0a1a5fdSTejun Heo */ 5624a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 5625a0a1a5fdSTejun Heo { 562624b8a847STejun Heo struct workqueue_struct *wq; 562724b8a847STejun Heo struct pool_workqueue *pwq; 5628a0a1a5fdSTejun Heo 562968e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5630a0a1a5fdSTejun Heo 56316183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 5632a0a1a5fdSTejun Heo workqueue_freezing = true; 5633a0a1a5fdSTejun Heo 563424b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 5635a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5636699ce097STejun Heo for_each_pwq(pwq, wq) 5637699ce097STejun Heo pwq_adjust_max_active(pwq); 5638a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5639a1056305STejun Heo } 56405bcab335STejun Heo 564168e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5642a0a1a5fdSTejun Heo } 5643a0a1a5fdSTejun Heo 5644a0a1a5fdSTejun Heo /** 564558a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 5646a0a1a5fdSTejun Heo * 5647a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 5648a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 5649a0a1a5fdSTejun Heo * 5650a0a1a5fdSTejun Heo * CONTEXT: 565168e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 5652a0a1a5fdSTejun Heo * 5653d185af30SYacine Belkadi * Return: 565458a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 565558a69cb4STejun Heo * is complete. 5656a0a1a5fdSTejun Heo */ 5657a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 5658a0a1a5fdSTejun Heo { 5659a0a1a5fdSTejun Heo bool busy = false; 566024b8a847STejun Heo struct workqueue_struct *wq; 566124b8a847STejun Heo struct pool_workqueue *pwq; 5662a0a1a5fdSTejun Heo 566368e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5664a0a1a5fdSTejun Heo 56656183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 5666a0a1a5fdSTejun Heo 566724b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 566824b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 566924b8a847STejun Heo continue; 5670a0a1a5fdSTejun Heo /* 5671a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 5672a0a1a5fdSTejun Heo * to peek without lock. 5673a0a1a5fdSTejun Heo */ 567424acfb71SThomas Gleixner rcu_read_lock(); 567524b8a847STejun Heo for_each_pwq(pwq, wq) { 56766183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 5677112202d9STejun Heo if (pwq->nr_active) { 5678a0a1a5fdSTejun Heo busy = true; 567924acfb71SThomas Gleixner rcu_read_unlock(); 5680a0a1a5fdSTejun Heo goto out_unlock; 5681a0a1a5fdSTejun Heo } 5682a0a1a5fdSTejun Heo } 568324acfb71SThomas Gleixner rcu_read_unlock(); 5684a0a1a5fdSTejun Heo } 5685a0a1a5fdSTejun Heo out_unlock: 568668e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5687a0a1a5fdSTejun Heo return busy; 5688a0a1a5fdSTejun Heo } 5689a0a1a5fdSTejun Heo 5690a0a1a5fdSTejun Heo /** 5691a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 5692a0a1a5fdSTejun Heo * 5693a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 5694706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 5695a0a1a5fdSTejun Heo * 5696a0a1a5fdSTejun Heo * CONTEXT: 5697a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5698a0a1a5fdSTejun Heo */ 5699a0a1a5fdSTejun Heo void thaw_workqueues(void) 5700a0a1a5fdSTejun Heo { 570124b8a847STejun Heo struct workqueue_struct *wq; 570224b8a847STejun Heo struct pool_workqueue *pwq; 5703a0a1a5fdSTejun Heo 570468e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5705a0a1a5fdSTejun Heo 5706a0a1a5fdSTejun Heo if (!workqueue_freezing) 5707a0a1a5fdSTejun Heo goto out_unlock; 5708a0a1a5fdSTejun Heo 570974b414eaSLai Jiangshan workqueue_freezing = false; 571024b8a847STejun Heo 571124b8a847STejun Heo /* restore max_active and repopulate worklist */ 571224b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 5713a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5714699ce097STejun Heo for_each_pwq(pwq, wq) 5715699ce097STejun Heo pwq_adjust_max_active(pwq); 5716a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 571724b8a847STejun Heo } 571824b8a847STejun Heo 5719a0a1a5fdSTejun Heo out_unlock: 572068e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5721a0a1a5fdSTejun Heo } 5722a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 5723a0a1a5fdSTejun Heo 572499c621efSLai Jiangshan static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) 5725042f7df1SLai Jiangshan { 5726042f7df1SLai Jiangshan LIST_HEAD(ctxs); 5727042f7df1SLai Jiangshan int ret = 0; 5728042f7df1SLai Jiangshan struct workqueue_struct *wq; 5729042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 5730042f7df1SLai Jiangshan 5731042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5732042f7df1SLai Jiangshan 5733042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 5734042f7df1SLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 5735042f7df1SLai Jiangshan continue; 5736042f7df1SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 5737042f7df1SLai Jiangshan if (wq->flags & __WQ_ORDERED) 5738042f7df1SLai Jiangshan continue; 5739042f7df1SLai Jiangshan 574099c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); 574184193c07STejun Heo if (IS_ERR(ctx)) { 574284193c07STejun Heo ret = PTR_ERR(ctx); 5743042f7df1SLai Jiangshan break; 5744042f7df1SLai Jiangshan } 5745042f7df1SLai Jiangshan 5746042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 5747042f7df1SLai Jiangshan } 5748042f7df1SLai Jiangshan 5749042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 5750042f7df1SLai Jiangshan if (!ret) 5751042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 5752042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 5753042f7df1SLai Jiangshan } 5754042f7df1SLai Jiangshan 575599c621efSLai Jiangshan if (!ret) { 575699c621efSLai Jiangshan mutex_lock(&wq_pool_attach_mutex); 575799c621efSLai Jiangshan cpumask_copy(wq_unbound_cpumask, unbound_cpumask); 575899c621efSLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 575999c621efSLai Jiangshan } 5760042f7df1SLai Jiangshan return ret; 5761042f7df1SLai Jiangshan } 5762042f7df1SLai Jiangshan 5763042f7df1SLai Jiangshan /** 5764042f7df1SLai Jiangshan * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 5765042f7df1SLai Jiangshan * @cpumask: the cpumask to set 5766042f7df1SLai Jiangshan * 5767042f7df1SLai Jiangshan * The low-level workqueues cpumask is a global cpumask that limits 5768042f7df1SLai Jiangshan * the affinity of all unbound workqueues. This function check the @cpumask 5769042f7df1SLai Jiangshan * and apply it to all unbound workqueues and updates all pwqs of them. 5770042f7df1SLai Jiangshan * 577167dc8325SCai Huoqing * Return: 0 - Success 5772042f7df1SLai Jiangshan * -EINVAL - Invalid @cpumask 5773042f7df1SLai Jiangshan * -ENOMEM - Failed to allocate memory for attrs or pwqs. 5774042f7df1SLai Jiangshan */ 5775042f7df1SLai Jiangshan int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 5776042f7df1SLai Jiangshan { 5777042f7df1SLai Jiangshan int ret = -EINVAL; 5778042f7df1SLai Jiangshan 5779c98a9805STal Shorer /* 5780c98a9805STal Shorer * Not excluding isolated cpus on purpose. 5781c98a9805STal Shorer * If the user wishes to include them, we allow that. 5782c98a9805STal Shorer */ 5783042f7df1SLai Jiangshan cpumask_and(cpumask, cpumask, cpu_possible_mask); 5784042f7df1SLai Jiangshan if (!cpumask_empty(cpumask)) { 5785a0111cf6SLai Jiangshan apply_wqattrs_lock(); 5786d25302e4SMenglong Dong if (cpumask_equal(cpumask, wq_unbound_cpumask)) { 5787d25302e4SMenglong Dong ret = 0; 5788d25302e4SMenglong Dong goto out_unlock; 5789d25302e4SMenglong Dong } 5790d25302e4SMenglong Dong 579199c621efSLai Jiangshan ret = workqueue_apply_unbound_cpumask(cpumask); 5792042f7df1SLai Jiangshan 5793d25302e4SMenglong Dong out_unlock: 5794a0111cf6SLai Jiangshan apply_wqattrs_unlock(); 5795042f7df1SLai Jiangshan } 5796042f7df1SLai Jiangshan 5797042f7df1SLai Jiangshan return ret; 5798042f7df1SLai Jiangshan } 5799042f7df1SLai Jiangshan 580063c5484eSTejun Heo static int parse_affn_scope(const char *val) 580163c5484eSTejun Heo { 580263c5484eSTejun Heo int i; 580363c5484eSTejun Heo 580463c5484eSTejun Heo for (i = 0; i < ARRAY_SIZE(wq_affn_names); i++) { 580563c5484eSTejun Heo if (!strncasecmp(val, wq_affn_names[i], strlen(wq_affn_names[i]))) 580663c5484eSTejun Heo return i; 580763c5484eSTejun Heo } 580863c5484eSTejun Heo return -EINVAL; 580963c5484eSTejun Heo } 581063c5484eSTejun Heo 581163c5484eSTejun Heo static int wq_affn_dfl_set(const char *val, const struct kernel_param *kp) 581263c5484eSTejun Heo { 581363c5484eSTejun Heo int affn; 581463c5484eSTejun Heo 581563c5484eSTejun Heo affn = parse_affn_scope(val); 581663c5484eSTejun Heo if (affn < 0) 581763c5484eSTejun Heo return affn; 581863c5484eSTejun Heo 581963c5484eSTejun Heo wq_affn_dfl = affn; 582063c5484eSTejun Heo return 0; 582163c5484eSTejun Heo } 582263c5484eSTejun Heo 582363c5484eSTejun Heo static int wq_affn_dfl_get(char *buffer, const struct kernel_param *kp) 582463c5484eSTejun Heo { 582563c5484eSTejun Heo return scnprintf(buffer, PAGE_SIZE, "%s\n", wq_affn_names[wq_affn_dfl]); 582663c5484eSTejun Heo } 582763c5484eSTejun Heo 582863c5484eSTejun Heo static const struct kernel_param_ops wq_affn_dfl_ops = { 582963c5484eSTejun Heo .set = wq_affn_dfl_set, 583063c5484eSTejun Heo .get = wq_affn_dfl_get, 583163c5484eSTejun Heo }; 583263c5484eSTejun Heo 583363c5484eSTejun Heo module_param_cb(default_affinity_scope, &wq_affn_dfl_ops, NULL, 0644); 583463c5484eSTejun Heo 58356ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 58366ba94429SFrederic Weisbecker /* 58376ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 58386ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 58396ba94429SFrederic Weisbecker * following attributes. 58406ba94429SFrederic Weisbecker * 58416ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 58426ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 58436ba94429SFrederic Weisbecker * 58446ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 58456ba94429SFrederic Weisbecker * 58466ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 58476ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 584863c5484eSTejun Heo * affinity_scope RW str : worker CPU affinity scope (cache, numa, none) 58496ba94429SFrederic Weisbecker */ 58506ba94429SFrederic Weisbecker struct wq_device { 58516ba94429SFrederic Weisbecker struct workqueue_struct *wq; 58526ba94429SFrederic Weisbecker struct device dev; 58536ba94429SFrederic Weisbecker }; 58546ba94429SFrederic Weisbecker 58556ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 58566ba94429SFrederic Weisbecker { 58576ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 58586ba94429SFrederic Weisbecker 58596ba94429SFrederic Weisbecker return wq_dev->wq; 58606ba94429SFrederic Weisbecker } 58616ba94429SFrederic Weisbecker 58626ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 58636ba94429SFrederic Weisbecker char *buf) 58646ba94429SFrederic Weisbecker { 58656ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 58666ba94429SFrederic Weisbecker 58676ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 58686ba94429SFrederic Weisbecker } 58696ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 58706ba94429SFrederic Weisbecker 58716ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 58726ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 58736ba94429SFrederic Weisbecker { 58746ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 58756ba94429SFrederic Weisbecker 58766ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 58776ba94429SFrederic Weisbecker } 58786ba94429SFrederic Weisbecker 58796ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 58806ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 58816ba94429SFrederic Weisbecker size_t count) 58826ba94429SFrederic Weisbecker { 58836ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 58846ba94429SFrederic Weisbecker int val; 58856ba94429SFrederic Weisbecker 58866ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 58876ba94429SFrederic Weisbecker return -EINVAL; 58886ba94429SFrederic Weisbecker 58896ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 58906ba94429SFrederic Weisbecker return count; 58916ba94429SFrederic Weisbecker } 58926ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 58936ba94429SFrederic Weisbecker 58946ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 58956ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 58966ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 58976ba94429SFrederic Weisbecker NULL, 58986ba94429SFrederic Weisbecker }; 58996ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 59006ba94429SFrederic Weisbecker 59016ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 59026ba94429SFrederic Weisbecker char *buf) 59036ba94429SFrederic Weisbecker { 59046ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 59056ba94429SFrederic Weisbecker int written; 59066ba94429SFrederic Weisbecker 59076ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 59086ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 59096ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 59106ba94429SFrederic Weisbecker 59116ba94429SFrederic Weisbecker return written; 59126ba94429SFrederic Weisbecker } 59136ba94429SFrederic Weisbecker 59146ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 59156ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 59166ba94429SFrederic Weisbecker { 59176ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 59186ba94429SFrederic Weisbecker 5919899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5920899a94feSLai Jiangshan 5921be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs(); 59226ba94429SFrederic Weisbecker if (!attrs) 59236ba94429SFrederic Weisbecker return NULL; 59246ba94429SFrederic Weisbecker 59256ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 59266ba94429SFrederic Weisbecker return attrs; 59276ba94429SFrederic Weisbecker } 59286ba94429SFrederic Weisbecker 59296ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 59306ba94429SFrederic Weisbecker const char *buf, size_t count) 59316ba94429SFrederic Weisbecker { 59326ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 59336ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5934d4d3e257SLai Jiangshan int ret = -ENOMEM; 5935d4d3e257SLai Jiangshan 5936d4d3e257SLai Jiangshan apply_wqattrs_lock(); 59376ba94429SFrederic Weisbecker 59386ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 59396ba94429SFrederic Weisbecker if (!attrs) 5940d4d3e257SLai Jiangshan goto out_unlock; 59416ba94429SFrederic Weisbecker 59426ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 59436ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 5944d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 59456ba94429SFrederic Weisbecker else 59466ba94429SFrederic Weisbecker ret = -EINVAL; 59476ba94429SFrederic Weisbecker 5948d4d3e257SLai Jiangshan out_unlock: 5949d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 59506ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 59516ba94429SFrederic Weisbecker return ret ?: count; 59526ba94429SFrederic Weisbecker } 59536ba94429SFrederic Weisbecker 59546ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 59556ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 59566ba94429SFrederic Weisbecker { 59576ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 59586ba94429SFrederic Weisbecker int written; 59596ba94429SFrederic Weisbecker 59606ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 59616ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 59626ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 59636ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 59646ba94429SFrederic Weisbecker return written; 59656ba94429SFrederic Weisbecker } 59666ba94429SFrederic Weisbecker 59676ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 59686ba94429SFrederic Weisbecker struct device_attribute *attr, 59696ba94429SFrederic Weisbecker const char *buf, size_t count) 59706ba94429SFrederic Weisbecker { 59716ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 59726ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5973d4d3e257SLai Jiangshan int ret = -ENOMEM; 5974d4d3e257SLai Jiangshan 5975d4d3e257SLai Jiangshan apply_wqattrs_lock(); 59766ba94429SFrederic Weisbecker 59776ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 59786ba94429SFrederic Weisbecker if (!attrs) 5979d4d3e257SLai Jiangshan goto out_unlock; 59806ba94429SFrederic Weisbecker 59816ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 59826ba94429SFrederic Weisbecker if (!ret) 5983d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 59846ba94429SFrederic Weisbecker 5985d4d3e257SLai Jiangshan out_unlock: 5986d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 59876ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 59886ba94429SFrederic Weisbecker return ret ?: count; 59896ba94429SFrederic Weisbecker } 59906ba94429SFrederic Weisbecker 599163c5484eSTejun Heo static ssize_t wq_affn_scope_show(struct device *dev, 599263c5484eSTejun Heo struct device_attribute *attr, char *buf) 599363c5484eSTejun Heo { 599463c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 599563c5484eSTejun Heo int written; 599663c5484eSTejun Heo 599763c5484eSTejun Heo mutex_lock(&wq->mutex); 599863c5484eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s\n", 599963c5484eSTejun Heo wq_affn_names[wq->unbound_attrs->affn_scope]); 600063c5484eSTejun Heo mutex_unlock(&wq->mutex); 600163c5484eSTejun Heo 600263c5484eSTejun Heo return written; 600363c5484eSTejun Heo } 600463c5484eSTejun Heo 600563c5484eSTejun Heo static ssize_t wq_affn_scope_store(struct device *dev, 600663c5484eSTejun Heo struct device_attribute *attr, 600763c5484eSTejun Heo const char *buf, size_t count) 600863c5484eSTejun Heo { 600963c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 601063c5484eSTejun Heo struct workqueue_attrs *attrs; 601163c5484eSTejun Heo int affn, ret = -ENOMEM; 601263c5484eSTejun Heo 601363c5484eSTejun Heo affn = parse_affn_scope(buf); 601463c5484eSTejun Heo if (affn < 0) 601563c5484eSTejun Heo return affn; 601663c5484eSTejun Heo 601763c5484eSTejun Heo apply_wqattrs_lock(); 601863c5484eSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 601963c5484eSTejun Heo if (attrs) { 602063c5484eSTejun Heo attrs->affn_scope = affn; 602163c5484eSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 602263c5484eSTejun Heo } 602363c5484eSTejun Heo apply_wqattrs_unlock(); 602463c5484eSTejun Heo free_workqueue_attrs(attrs); 602563c5484eSTejun Heo return ret ?: count; 602663c5484eSTejun Heo } 602763c5484eSTejun Heo 60286ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 60296ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 60306ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 603163c5484eSTejun Heo __ATTR(affinity_scope, 0644, wq_affn_scope_show, wq_affn_scope_store), 60326ba94429SFrederic Weisbecker __ATTR_NULL, 60336ba94429SFrederic Weisbecker }; 60346ba94429SFrederic Weisbecker 60356ba94429SFrederic Weisbecker static struct bus_type wq_subsys = { 60366ba94429SFrederic Weisbecker .name = "workqueue", 60376ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 60386ba94429SFrederic Weisbecker }; 60396ba94429SFrederic Weisbecker 6040b05a7928SFrederic Weisbecker static ssize_t wq_unbound_cpumask_show(struct device *dev, 6041b05a7928SFrederic Weisbecker struct device_attribute *attr, char *buf) 6042b05a7928SFrederic Weisbecker { 6043b05a7928SFrederic Weisbecker int written; 6044b05a7928SFrederic Weisbecker 6045042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 6046b05a7928SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 6047b05a7928SFrederic Weisbecker cpumask_pr_args(wq_unbound_cpumask)); 6048042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6049b05a7928SFrederic Weisbecker 6050b05a7928SFrederic Weisbecker return written; 6051b05a7928SFrederic Weisbecker } 6052b05a7928SFrederic Weisbecker 6053042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 6054042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 6055042f7df1SLai Jiangshan { 6056042f7df1SLai Jiangshan cpumask_var_t cpumask; 6057042f7df1SLai Jiangshan int ret; 6058042f7df1SLai Jiangshan 6059042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 6060042f7df1SLai Jiangshan return -ENOMEM; 6061042f7df1SLai Jiangshan 6062042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 6063042f7df1SLai Jiangshan if (!ret) 6064042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 6065042f7df1SLai Jiangshan 6066042f7df1SLai Jiangshan free_cpumask_var(cpumask); 6067042f7df1SLai Jiangshan return ret ? ret : count; 6068042f7df1SLai Jiangshan } 6069042f7df1SLai Jiangshan 6070b05a7928SFrederic Weisbecker static struct device_attribute wq_sysfs_cpumask_attr = 6071042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 6072042f7df1SLai Jiangshan wq_unbound_cpumask_store); 6073b05a7928SFrederic Weisbecker 60746ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 60756ba94429SFrederic Weisbecker { 6076686f6697SGreg Kroah-Hartman struct device *dev_root; 6077b05a7928SFrederic Weisbecker int err; 6078b05a7928SFrederic Weisbecker 6079b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 6080b05a7928SFrederic Weisbecker if (err) 6081b05a7928SFrederic Weisbecker return err; 6082b05a7928SFrederic Weisbecker 6083686f6697SGreg Kroah-Hartman dev_root = bus_get_dev_root(&wq_subsys); 6084686f6697SGreg Kroah-Hartman if (dev_root) { 6085686f6697SGreg Kroah-Hartman err = device_create_file(dev_root, &wq_sysfs_cpumask_attr); 6086686f6697SGreg Kroah-Hartman put_device(dev_root); 6087686f6697SGreg Kroah-Hartman } 6088686f6697SGreg Kroah-Hartman return err; 60896ba94429SFrederic Weisbecker } 60906ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 60916ba94429SFrederic Weisbecker 60926ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 60936ba94429SFrederic Weisbecker { 60946ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 60956ba94429SFrederic Weisbecker 60966ba94429SFrederic Weisbecker kfree(wq_dev); 60976ba94429SFrederic Weisbecker } 60986ba94429SFrederic Weisbecker 60996ba94429SFrederic Weisbecker /** 61006ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 61016ba94429SFrederic Weisbecker * @wq: the workqueue to register 61026ba94429SFrederic Weisbecker * 61036ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 61046ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 61056ba94429SFrederic Weisbecker * which is the preferred method. 61066ba94429SFrederic Weisbecker * 61076ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 61086ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 61096ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 61106ba94429SFrederic Weisbecker * attributes. 61116ba94429SFrederic Weisbecker * 61126ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 61136ba94429SFrederic Weisbecker */ 61146ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 61156ba94429SFrederic Weisbecker { 61166ba94429SFrederic Weisbecker struct wq_device *wq_dev; 61176ba94429SFrederic Weisbecker int ret; 61186ba94429SFrederic Weisbecker 61196ba94429SFrederic Weisbecker /* 6120402dd89dSShailendra Verma * Adjusting max_active or creating new pwqs by applying 61216ba94429SFrederic Weisbecker * attributes breaks ordering guarantee. Disallow exposing ordered 61226ba94429SFrederic Weisbecker * workqueues. 61236ba94429SFrederic Weisbecker */ 61240a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 61256ba94429SFrederic Weisbecker return -EINVAL; 61266ba94429SFrederic Weisbecker 61276ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 61286ba94429SFrederic Weisbecker if (!wq_dev) 61296ba94429SFrederic Weisbecker return -ENOMEM; 61306ba94429SFrederic Weisbecker 61316ba94429SFrederic Weisbecker wq_dev->wq = wq; 61326ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 61336ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 613423217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 61356ba94429SFrederic Weisbecker 61366ba94429SFrederic Weisbecker /* 61376ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 61386ba94429SFrederic Weisbecker * everything is ready. 61396ba94429SFrederic Weisbecker */ 61406ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 61416ba94429SFrederic Weisbecker 61426ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 61436ba94429SFrederic Weisbecker if (ret) { 6144537f4146SArvind Yadav put_device(&wq_dev->dev); 61456ba94429SFrederic Weisbecker wq->wq_dev = NULL; 61466ba94429SFrederic Weisbecker return ret; 61476ba94429SFrederic Weisbecker } 61486ba94429SFrederic Weisbecker 61496ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 61506ba94429SFrederic Weisbecker struct device_attribute *attr; 61516ba94429SFrederic Weisbecker 61526ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 61536ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 61546ba94429SFrederic Weisbecker if (ret) { 61556ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 61566ba94429SFrederic Weisbecker wq->wq_dev = NULL; 61576ba94429SFrederic Weisbecker return ret; 61586ba94429SFrederic Weisbecker } 61596ba94429SFrederic Weisbecker } 61606ba94429SFrederic Weisbecker } 61616ba94429SFrederic Weisbecker 61626ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 61636ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 61646ba94429SFrederic Weisbecker return 0; 61656ba94429SFrederic Weisbecker } 61666ba94429SFrederic Weisbecker 61676ba94429SFrederic Weisbecker /** 61686ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 61696ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 61706ba94429SFrederic Weisbecker * 61716ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 61726ba94429SFrederic Weisbecker */ 61736ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 61746ba94429SFrederic Weisbecker { 61756ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 61766ba94429SFrederic Weisbecker 61776ba94429SFrederic Weisbecker if (!wq->wq_dev) 61786ba94429SFrederic Weisbecker return; 61796ba94429SFrederic Weisbecker 61806ba94429SFrederic Weisbecker wq->wq_dev = NULL; 61816ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 61826ba94429SFrederic Weisbecker } 61836ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 61846ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 61856ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 61866ba94429SFrederic Weisbecker 618782607adcSTejun Heo /* 618882607adcSTejun Heo * Workqueue watchdog. 618982607adcSTejun Heo * 619082607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 619182607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 619282607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 619382607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 619482607adcSTejun Heo * largely opaque. 619582607adcSTejun Heo * 619682607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 619782607adcSTejun Heo * state if some pools failed to make forward progress for a while where 619882607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 619982607adcSTejun Heo * 620082607adcSTejun Heo * This mechanism is controlled through the kernel parameter 620182607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 620282607adcSTejun Heo * corresponding sysfs parameter file. 620382607adcSTejun Heo */ 620482607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 620582607adcSTejun Heo 620682607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 62075cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 620882607adcSTejun Heo 620982607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 621082607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 621182607adcSTejun Heo 6212cd2440d6SPetr Mladek /* 6213cd2440d6SPetr Mladek * Show workers that might prevent the processing of pending work items. 6214cd2440d6SPetr Mladek * The only candidates are CPU-bound workers in the running state. 6215cd2440d6SPetr Mladek * Pending work items should be handled by another idle worker 6216cd2440d6SPetr Mladek * in all other situations. 6217cd2440d6SPetr Mladek */ 6218cd2440d6SPetr Mladek static void show_cpu_pool_hog(struct worker_pool *pool) 6219cd2440d6SPetr Mladek { 6220cd2440d6SPetr Mladek struct worker *worker; 6221cd2440d6SPetr Mladek unsigned long flags; 6222cd2440d6SPetr Mladek int bkt; 6223cd2440d6SPetr Mladek 6224cd2440d6SPetr Mladek raw_spin_lock_irqsave(&pool->lock, flags); 6225cd2440d6SPetr Mladek 6226cd2440d6SPetr Mladek hash_for_each(pool->busy_hash, bkt, worker, hentry) { 6227cd2440d6SPetr Mladek if (task_is_running(worker->task)) { 6228cd2440d6SPetr Mladek /* 6229cd2440d6SPetr Mladek * Defer printing to avoid deadlocks in console 6230cd2440d6SPetr Mladek * drivers that queue work while holding locks 6231cd2440d6SPetr Mladek * also taken in their write paths. 6232cd2440d6SPetr Mladek */ 6233cd2440d6SPetr Mladek printk_deferred_enter(); 6234cd2440d6SPetr Mladek 6235cd2440d6SPetr Mladek pr_info("pool %d:\n", pool->id); 6236cd2440d6SPetr Mladek sched_show_task(worker->task); 6237cd2440d6SPetr Mladek 6238cd2440d6SPetr Mladek printk_deferred_exit(); 6239cd2440d6SPetr Mladek } 6240cd2440d6SPetr Mladek } 6241cd2440d6SPetr Mladek 6242cd2440d6SPetr Mladek raw_spin_unlock_irqrestore(&pool->lock, flags); 6243cd2440d6SPetr Mladek } 6244cd2440d6SPetr Mladek 6245cd2440d6SPetr Mladek static void show_cpu_pools_hogs(void) 6246cd2440d6SPetr Mladek { 6247cd2440d6SPetr Mladek struct worker_pool *pool; 6248cd2440d6SPetr Mladek int pi; 6249cd2440d6SPetr Mladek 6250cd2440d6SPetr Mladek pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); 6251cd2440d6SPetr Mladek 6252cd2440d6SPetr Mladek rcu_read_lock(); 6253cd2440d6SPetr Mladek 6254cd2440d6SPetr Mladek for_each_pool(pool, pi) { 6255cd2440d6SPetr Mladek if (pool->cpu_stall) 6256cd2440d6SPetr Mladek show_cpu_pool_hog(pool); 6257cd2440d6SPetr Mladek 6258cd2440d6SPetr Mladek } 6259cd2440d6SPetr Mladek 6260cd2440d6SPetr Mladek rcu_read_unlock(); 6261cd2440d6SPetr Mladek } 6262cd2440d6SPetr Mladek 626382607adcSTejun Heo static void wq_watchdog_reset_touched(void) 626482607adcSTejun Heo { 626582607adcSTejun Heo int cpu; 626682607adcSTejun Heo 626782607adcSTejun Heo wq_watchdog_touched = jiffies; 626882607adcSTejun Heo for_each_possible_cpu(cpu) 626982607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 627082607adcSTejun Heo } 627182607adcSTejun Heo 62725cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 627382607adcSTejun Heo { 627482607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 627582607adcSTejun Heo bool lockup_detected = false; 6276cd2440d6SPetr Mladek bool cpu_pool_stall = false; 6277940d71c6SSergey Senozhatsky unsigned long now = jiffies; 627882607adcSTejun Heo struct worker_pool *pool; 627982607adcSTejun Heo int pi; 628082607adcSTejun Heo 628182607adcSTejun Heo if (!thresh) 628282607adcSTejun Heo return; 628382607adcSTejun Heo 628482607adcSTejun Heo rcu_read_lock(); 628582607adcSTejun Heo 628682607adcSTejun Heo for_each_pool(pool, pi) { 628782607adcSTejun Heo unsigned long pool_ts, touched, ts; 628882607adcSTejun Heo 6289cd2440d6SPetr Mladek pool->cpu_stall = false; 629082607adcSTejun Heo if (list_empty(&pool->worklist)) 629182607adcSTejun Heo continue; 629282607adcSTejun Heo 6293940d71c6SSergey Senozhatsky /* 6294940d71c6SSergey Senozhatsky * If a virtual machine is stopped by the host it can look to 6295940d71c6SSergey Senozhatsky * the watchdog like a stall. 6296940d71c6SSergey Senozhatsky */ 6297940d71c6SSergey Senozhatsky kvm_check_and_clear_guest_paused(); 6298940d71c6SSergey Senozhatsky 629982607adcSTejun Heo /* get the latest of pool and touched timestamps */ 630089e28ce6SWang Qing if (pool->cpu >= 0) 630189e28ce6SWang Qing touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); 630289e28ce6SWang Qing else 630382607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 630489e28ce6SWang Qing pool_ts = READ_ONCE(pool->watchdog_ts); 630582607adcSTejun Heo 630682607adcSTejun Heo if (time_after(pool_ts, touched)) 630782607adcSTejun Heo ts = pool_ts; 630882607adcSTejun Heo else 630982607adcSTejun Heo ts = touched; 631082607adcSTejun Heo 631182607adcSTejun Heo /* did we stall? */ 6312940d71c6SSergey Senozhatsky if (time_after(now, ts + thresh)) { 631382607adcSTejun Heo lockup_detected = true; 6314cd2440d6SPetr Mladek if (pool->cpu >= 0) { 6315cd2440d6SPetr Mladek pool->cpu_stall = true; 6316cd2440d6SPetr Mladek cpu_pool_stall = true; 6317cd2440d6SPetr Mladek } 631882607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 631982607adcSTejun Heo pr_cont_pool_info(pool); 632082607adcSTejun Heo pr_cont(" stuck for %us!\n", 6321940d71c6SSergey Senozhatsky jiffies_to_msecs(now - pool_ts) / 1000); 632282607adcSTejun Heo } 6323cd2440d6SPetr Mladek 6324cd2440d6SPetr Mladek 632582607adcSTejun Heo } 632682607adcSTejun Heo 632782607adcSTejun Heo rcu_read_unlock(); 632882607adcSTejun Heo 632982607adcSTejun Heo if (lockup_detected) 633055df0933SImran Khan show_all_workqueues(); 633182607adcSTejun Heo 6332cd2440d6SPetr Mladek if (cpu_pool_stall) 6333cd2440d6SPetr Mladek show_cpu_pools_hogs(); 6334cd2440d6SPetr Mladek 633582607adcSTejun Heo wq_watchdog_reset_touched(); 633682607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 633782607adcSTejun Heo } 633882607adcSTejun Heo 6339cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 634082607adcSTejun Heo { 634182607adcSTejun Heo if (cpu >= 0) 634282607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 634389e28ce6SWang Qing 634482607adcSTejun Heo wq_watchdog_touched = jiffies; 634582607adcSTejun Heo } 634682607adcSTejun Heo 634782607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 634882607adcSTejun Heo { 634982607adcSTejun Heo wq_watchdog_thresh = 0; 635082607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 635182607adcSTejun Heo 635282607adcSTejun Heo if (thresh) { 635382607adcSTejun Heo wq_watchdog_thresh = thresh; 635482607adcSTejun Heo wq_watchdog_reset_touched(); 635582607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 635682607adcSTejun Heo } 635782607adcSTejun Heo } 635882607adcSTejun Heo 635982607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 636082607adcSTejun Heo const struct kernel_param *kp) 636182607adcSTejun Heo { 636282607adcSTejun Heo unsigned long thresh; 636382607adcSTejun Heo int ret; 636482607adcSTejun Heo 636582607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 636682607adcSTejun Heo if (ret) 636782607adcSTejun Heo return ret; 636882607adcSTejun Heo 636982607adcSTejun Heo if (system_wq) 637082607adcSTejun Heo wq_watchdog_set_thresh(thresh); 637182607adcSTejun Heo else 637282607adcSTejun Heo wq_watchdog_thresh = thresh; 637382607adcSTejun Heo 637482607adcSTejun Heo return 0; 637582607adcSTejun Heo } 637682607adcSTejun Heo 637782607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 637882607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 637982607adcSTejun Heo .get = param_get_ulong, 638082607adcSTejun Heo }; 638182607adcSTejun Heo 638282607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 638382607adcSTejun Heo 0644); 638482607adcSTejun Heo 638582607adcSTejun Heo static void wq_watchdog_init(void) 638682607adcSTejun Heo { 63875cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 638882607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 638982607adcSTejun Heo } 639082607adcSTejun Heo 639182607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 639282607adcSTejun Heo 639382607adcSTejun Heo static inline void wq_watchdog_init(void) { } 639482607adcSTejun Heo 639582607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 639682607adcSTejun Heo 63973347fa09STejun Heo /** 63983347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 63993347fa09STejun Heo * 64002930155bSTejun Heo * This is the first step of three-staged workqueue subsystem initialization and 64012930155bSTejun Heo * invoked as soon as the bare basics - memory allocation, cpumasks and idr are 64022930155bSTejun Heo * up. It sets up all the data structures and system workqueues and allows early 64032930155bSTejun Heo * boot code to create workqueues and queue/cancel work items. Actual work item 64042930155bSTejun Heo * execution starts only after kthreads can be created and scheduled right 64052930155bSTejun Heo * before early initcalls. 64063347fa09STejun Heo */ 64072333e829SYu Chen void __init workqueue_init_early(void) 64081da177e4SLinus Torvalds { 640984193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 64107a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 64117a4e344cSTejun Heo int i, cpu; 6412c34056a3STejun Heo 641310cdb157SLai Jiangshan BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 6414e904e6c2STejun Heo 6415b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 641604d4e665SFrederic Weisbecker cpumask_copy(wq_unbound_cpumask, housekeeping_cpumask(HK_TYPE_WQ)); 641704d4e665SFrederic Weisbecker cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, housekeeping_cpumask(HK_TYPE_DOMAIN)); 6418b05a7928SFrederic Weisbecker 6419ace3c549Stiozhang if (!cpumask_empty(&wq_cmdline_cpumask)) 6420ace3c549Stiozhang cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, &wq_cmdline_cpumask); 6421ace3c549Stiozhang 6422e904e6c2STejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 6423e904e6c2STejun Heo 64242930155bSTejun Heo wq_update_pod_attrs_buf = alloc_workqueue_attrs(); 64252930155bSTejun Heo BUG_ON(!wq_update_pod_attrs_buf); 64262930155bSTejun Heo 64270f36ee24STejun Heo BUG_ON(!alloc_cpumask_var(&wq_update_pod_cpumask_buf, GFP_KERNEL)); 64280f36ee24STejun Heo 642984193c07STejun Heo /* initialize WQ_AFFN_SYSTEM pods */ 643084193c07STejun Heo pt->pod_cpus = kcalloc(1, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 643184193c07STejun Heo pt->pod_node = kcalloc(1, sizeof(pt->pod_node[0]), GFP_KERNEL); 643284193c07STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 643384193c07STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node || !pt->cpu_pod); 643484193c07STejun Heo 643584193c07STejun Heo BUG_ON(!zalloc_cpumask_var_node(&pt->pod_cpus[0], GFP_KERNEL, NUMA_NO_NODE)); 643684193c07STejun Heo 643784193c07STejun Heo wq_update_pod_attrs_buf = alloc_workqueue_attrs(); 643884193c07STejun Heo BUG_ON(!wq_update_pod_attrs_buf); 643984193c07STejun Heo 644084193c07STejun Heo pt->nr_pods = 1; 644184193c07STejun Heo cpumask_copy(pt->pod_cpus[0], cpu_possible_mask); 644284193c07STejun Heo pt->pod_node[0] = NUMA_NO_NODE; 644384193c07STejun Heo pt->cpu_pod[0] = 0; 644484193c07STejun Heo 6445706026c2STejun Heo /* initialize CPU pools */ 644629c91e99STejun Heo for_each_possible_cpu(cpu) { 64474ce62e9eSTejun Heo struct worker_pool *pool; 64488b03ae3cSTejun Heo 64497a4e344cSTejun Heo i = 0; 6450f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 64517a4e344cSTejun Heo BUG_ON(init_worker_pool(pool)); 6452ec22ca5eSTejun Heo pool->cpu = cpu; 64537a4e344cSTejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 64547a4e344cSTejun Heo pool->attrs->nice = std_nice[i++]; 6455f3f90ad4STejun Heo pool->node = cpu_to_node(cpu); 64567a4e344cSTejun Heo 64579daf9e67STejun Heo /* alloc pool ID */ 645868e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 64599daf9e67STejun Heo BUG_ON(worker_pool_assign_id(pool)); 646068e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 64614ce62e9eSTejun Heo } 64628b03ae3cSTejun Heo } 64638b03ae3cSTejun Heo 64648a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 646529c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 646629c91e99STejun Heo struct workqueue_attrs *attrs; 646729c91e99STejun Heo 6468be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 646929c91e99STejun Heo attrs->nice = std_nice[i]; 647029c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 64718a2b7538STejun Heo 64728a2b7538STejun Heo /* 64738a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 64748a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 64758a2b7538STejun Heo */ 6476be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 64778a2b7538STejun Heo attrs->nice = std_nice[i]; 6478af73f5c9STejun Heo attrs->ordered = true; 64798a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 648029c91e99STejun Heo } 648129c91e99STejun Heo 6482d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 64831aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 6484d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 6485f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 6486636b927eSTejun Heo WQ_MAX_ACTIVE); 648724d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 648824d51addSTejun Heo WQ_FREEZABLE, 0); 64890668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 64900668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 64910668106cSViresh Kumar system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient", 64920668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 64930668106cSViresh Kumar 0); 64941aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 64950668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 64960668106cSViresh Kumar !system_power_efficient_wq || 64970668106cSViresh Kumar !system_freezable_power_efficient_wq); 64983347fa09STejun Heo } 64993347fa09STejun Heo 6500aa6fde93STejun Heo static void __init wq_cpu_intensive_thresh_init(void) 6501aa6fde93STejun Heo { 6502aa6fde93STejun Heo unsigned long thresh; 6503aa6fde93STejun Heo unsigned long bogo; 6504aa6fde93STejun Heo 6505aa6fde93STejun Heo /* if the user set it to a specific value, keep it */ 6506aa6fde93STejun Heo if (wq_cpu_intensive_thresh_us != ULONG_MAX) 6507aa6fde93STejun Heo return; 6508aa6fde93STejun Heo 6509967b494eSTejun Heo pwq_release_worker = kthread_create_worker(0, "pool_workqueue_release"); 6510967b494eSTejun Heo BUG_ON(IS_ERR(pwq_release_worker)); 6511967b494eSTejun Heo 6512aa6fde93STejun Heo /* 6513aa6fde93STejun Heo * The default of 10ms is derived from the fact that most modern (as of 6514aa6fde93STejun Heo * 2023) processors can do a lot in 10ms and that it's just below what 6515aa6fde93STejun Heo * most consider human-perceivable. However, the kernel also runs on a 6516aa6fde93STejun Heo * lot slower CPUs including microcontrollers where the threshold is way 6517aa6fde93STejun Heo * too low. 6518aa6fde93STejun Heo * 6519aa6fde93STejun Heo * Let's scale up the threshold upto 1 second if BogoMips is below 4000. 6520aa6fde93STejun Heo * This is by no means accurate but it doesn't have to be. The mechanism 6521aa6fde93STejun Heo * is still useful even when the threshold is fully scaled up. Also, as 6522aa6fde93STejun Heo * the reports would usually be applicable to everyone, some machines 6523aa6fde93STejun Heo * operating on longer thresholds won't significantly diminish their 6524aa6fde93STejun Heo * usefulness. 6525aa6fde93STejun Heo */ 6526aa6fde93STejun Heo thresh = 10 * USEC_PER_MSEC; 6527aa6fde93STejun Heo 6528aa6fde93STejun Heo /* see init/calibrate.c for lpj -> BogoMIPS calculation */ 6529aa6fde93STejun Heo bogo = max_t(unsigned long, loops_per_jiffy / 500000 * HZ, 1); 6530aa6fde93STejun Heo if (bogo < 4000) 6531aa6fde93STejun Heo thresh = min_t(unsigned long, thresh * 4000 / bogo, USEC_PER_SEC); 6532aa6fde93STejun Heo 6533aa6fde93STejun Heo pr_debug("wq_cpu_intensive_thresh: lpj=%lu BogoMIPS=%lu thresh_us=%lu\n", 6534aa6fde93STejun Heo loops_per_jiffy, bogo, thresh); 6535aa6fde93STejun Heo 6536aa6fde93STejun Heo wq_cpu_intensive_thresh_us = thresh; 6537aa6fde93STejun Heo } 6538aa6fde93STejun Heo 65393347fa09STejun Heo /** 65403347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 65413347fa09STejun Heo * 65422930155bSTejun Heo * This is the second step of three-staged workqueue subsystem initialization 65432930155bSTejun Heo * and invoked as soon as kthreads can be created and scheduled. Workqueues have 65442930155bSTejun Heo * been created and work items queued on them, but there are no kworkers 65452930155bSTejun Heo * executing the work items yet. Populate the worker pools with the initial 65462930155bSTejun Heo * workers and enable future kworker creations. 65473347fa09STejun Heo */ 65482333e829SYu Chen void __init workqueue_init(void) 65493347fa09STejun Heo { 65502186d9f9STejun Heo struct workqueue_struct *wq; 65513347fa09STejun Heo struct worker_pool *pool; 65523347fa09STejun Heo int cpu, bkt; 65533347fa09STejun Heo 6554aa6fde93STejun Heo wq_cpu_intensive_thresh_init(); 6555aa6fde93STejun Heo 65562186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 65572186d9f9STejun Heo 65582930155bSTejun Heo /* 65592930155bSTejun Heo * Per-cpu pools created earlier could be missing node hint. Fix them 65602930155bSTejun Heo * up. Also, create a rescuer for workqueues that requested it. 65612930155bSTejun Heo */ 65622186d9f9STejun Heo for_each_possible_cpu(cpu) { 65632186d9f9STejun Heo for_each_cpu_worker_pool(pool, cpu) { 65642186d9f9STejun Heo pool->node = cpu_to_node(cpu); 65652186d9f9STejun Heo } 65662186d9f9STejun Heo } 65672186d9f9STejun Heo 656840c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 656940c17f75STejun Heo WARN(init_rescuer(wq), 657040c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 657140c17f75STejun Heo wq->name); 657240c17f75STejun Heo } 65732186d9f9STejun Heo 65742186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 65752186d9f9STejun Heo 65763347fa09STejun Heo /* create the initial workers */ 65773347fa09STejun Heo for_each_online_cpu(cpu) { 65783347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 65793347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 65803347fa09STejun Heo BUG_ON(!create_worker(pool)); 65813347fa09STejun Heo } 65823347fa09STejun Heo } 65833347fa09STejun Heo 65843347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 65853347fa09STejun Heo BUG_ON(!create_worker(pool)); 65863347fa09STejun Heo 65873347fa09STejun Heo wq_online = true; 658882607adcSTejun Heo wq_watchdog_init(); 65891da177e4SLinus Torvalds } 6590c4f135d6STetsuo Handa 6591025e1684STejun Heo /* 6592025e1684STejun Heo * Initialize @pt by first initializing @pt->cpu_pod[] with pod IDs according to 6593025e1684STejun Heo * @cpu_shares_pod(). Each subset of CPUs that share a pod is assigned a unique 6594025e1684STejun Heo * and consecutive pod ID. The rest of @pt is initialized accordingly. 6595025e1684STejun Heo */ 6596025e1684STejun Heo static void __init init_pod_type(struct wq_pod_type *pt, 6597025e1684STejun Heo bool (*cpus_share_pod)(int, int)) 6598025e1684STejun Heo { 6599025e1684STejun Heo int cur, pre, cpu, pod; 6600025e1684STejun Heo 6601025e1684STejun Heo pt->nr_pods = 0; 6602025e1684STejun Heo 6603025e1684STejun Heo /* init @pt->cpu_pod[] according to @cpus_share_pod() */ 6604025e1684STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 6605025e1684STejun Heo BUG_ON(!pt->cpu_pod); 6606025e1684STejun Heo 6607025e1684STejun Heo for_each_possible_cpu(cur) { 6608025e1684STejun Heo for_each_possible_cpu(pre) { 6609025e1684STejun Heo if (pre >= cur) { 6610025e1684STejun Heo pt->cpu_pod[cur] = pt->nr_pods++; 6611025e1684STejun Heo break; 6612025e1684STejun Heo } 6613025e1684STejun Heo if (cpus_share_pod(cur, pre)) { 6614025e1684STejun Heo pt->cpu_pod[cur] = pt->cpu_pod[pre]; 6615025e1684STejun Heo break; 6616025e1684STejun Heo } 6617025e1684STejun Heo } 6618025e1684STejun Heo } 6619025e1684STejun Heo 6620025e1684STejun Heo /* init the rest to match @pt->cpu_pod[] */ 6621025e1684STejun Heo pt->pod_cpus = kcalloc(pt->nr_pods, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 6622025e1684STejun Heo pt->pod_node = kcalloc(pt->nr_pods, sizeof(pt->pod_node[0]), GFP_KERNEL); 6623025e1684STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node); 6624025e1684STejun Heo 6625025e1684STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) 6626025e1684STejun Heo BUG_ON(!zalloc_cpumask_var(&pt->pod_cpus[pod], GFP_KERNEL)); 6627025e1684STejun Heo 6628025e1684STejun Heo for_each_possible_cpu(cpu) { 6629025e1684STejun Heo cpumask_set_cpu(cpu, pt->pod_cpus[pt->cpu_pod[cpu]]); 6630025e1684STejun Heo pt->pod_node[pt->cpu_pod[cpu]] = cpu_to_node(cpu); 6631025e1684STejun Heo } 6632025e1684STejun Heo } 6633025e1684STejun Heo 663463c5484eSTejun Heo static bool __init cpus_dont_share(int cpu0, int cpu1) 663563c5484eSTejun Heo { 663663c5484eSTejun Heo return false; 663763c5484eSTejun Heo } 663863c5484eSTejun Heo 663963c5484eSTejun Heo static bool __init cpus_share_smt(int cpu0, int cpu1) 664063c5484eSTejun Heo { 664163c5484eSTejun Heo #ifdef CONFIG_SCHED_SMT 664263c5484eSTejun Heo return cpumask_test_cpu(cpu0, cpu_smt_mask(cpu1)); 664363c5484eSTejun Heo #else 664463c5484eSTejun Heo return false; 664563c5484eSTejun Heo #endif 664663c5484eSTejun Heo } 664763c5484eSTejun Heo 6648025e1684STejun Heo static bool __init cpus_share_numa(int cpu0, int cpu1) 6649025e1684STejun Heo { 6650025e1684STejun Heo return cpu_to_node(cpu0) == cpu_to_node(cpu1); 6651025e1684STejun Heo } 6652025e1684STejun Heo 66532930155bSTejun Heo /** 66542930155bSTejun Heo * workqueue_init_topology - initialize CPU pods for unbound workqueues 66552930155bSTejun Heo * 66562930155bSTejun Heo * This is the third step of there-staged workqueue subsystem initialization and 66572930155bSTejun Heo * invoked after SMP and topology information are fully initialized. It 66582930155bSTejun Heo * initializes the unbound CPU pods accordingly. 66592930155bSTejun Heo */ 66602930155bSTejun Heo void __init workqueue_init_topology(void) 6661a86feae6STejun Heo { 66622930155bSTejun Heo struct workqueue_struct *wq; 6663025e1684STejun Heo int cpu; 6664a86feae6STejun Heo 666563c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CPU], cpus_dont_share); 666663c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_SMT], cpus_share_smt); 666763c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CACHE], cpus_share_cache); 6668025e1684STejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_NUMA], cpus_share_numa); 6669a86feae6STejun Heo 66702930155bSTejun Heo mutex_lock(&wq_pool_mutex); 6671a86feae6STejun Heo 6672a86feae6STejun Heo /* 66732930155bSTejun Heo * Workqueues allocated earlier would have all CPUs sharing the default 66742930155bSTejun Heo * worker pool. Explicitly call wq_update_pod() on all workqueue and CPU 66752930155bSTejun Heo * combinations to apply per-pod sharing. 66762930155bSTejun Heo */ 66772930155bSTejun Heo list_for_each_entry(wq, &workqueues, list) { 66782930155bSTejun Heo for_each_online_cpu(cpu) { 66792930155bSTejun Heo wq_update_pod(wq, cpu, cpu, true); 66802930155bSTejun Heo } 66812930155bSTejun Heo } 66822930155bSTejun Heo 66832930155bSTejun Heo mutex_unlock(&wq_pool_mutex); 6684a86feae6STejun Heo } 6685a86feae6STejun Heo 668620bdedafSTetsuo Handa void __warn_flushing_systemwide_wq(void) 668720bdedafSTetsuo Handa { 668820bdedafSTetsuo Handa pr_warn("WARNING: Flushing system-wide workqueues will be prohibited in near future.\n"); 668920bdedafSTetsuo Handa dump_stack(); 669020bdedafSTetsuo Handa } 6691c4f135d6STetsuo Handa EXPORT_SYMBOL(__warn_flushing_systemwide_wq); 6692ace3c549Stiozhang 6693ace3c549Stiozhang static int __init workqueue_unbound_cpus_setup(char *str) 6694ace3c549Stiozhang { 6695ace3c549Stiozhang if (cpulist_parse(str, &wq_cmdline_cpumask) < 0) { 6696ace3c549Stiozhang cpumask_clear(&wq_cmdline_cpumask); 6697ace3c549Stiozhang pr_warn("workqueue.unbound_cpus: incorrect CPU range, using default\n"); 6698ace3c549Stiozhang } 6699ace3c549Stiozhang 6700ace3c549Stiozhang return 1; 6701ace3c549Stiozhang } 6702ace3c549Stiozhang __setup("workqueue.unbound_cpus=", workqueue_unbound_cpus_setup); 6703