11da177e4SLinus Torvalds /* 2c54fce6eSTejun Heo * kernel/workqueue.c - generic async execution with shared worker pool 31da177e4SLinus Torvalds * 4c54fce6eSTejun Heo * Copyright (C) 2002 Ingo Molnar 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Derived from the taskqueue/keventd code by: 71da177e4SLinus Torvalds * David Woodhouse <[email protected]> 8e1f8e874SFrancois Cami * Andrew Morton 91da177e4SLinus Torvalds * Kai Petzke <[email protected]> 101da177e4SLinus Torvalds * Theodore Ts'o <[email protected]> 1189ada679SChristoph Lameter * 12cde53535SChristoph Lameter * Made to use alloc_percpu by Christoph Lameter. 13c54fce6eSTejun Heo * 14c54fce6eSTejun Heo * Copyright (C) 2010 SUSE Linux Products GmbH 15c54fce6eSTejun Heo * Copyright (C) 2010 Tejun Heo <[email protected]> 16c54fce6eSTejun Heo * 17c54fce6eSTejun Heo * This is the generic async execution mechanism. Work items as are 18c54fce6eSTejun Heo * executed in process context. The worker pool is shared and 19b11895c4SLibin * automatically managed. There are two worker pools for each CPU (one for 20b11895c4SLibin * normal work items and the other for high priority ones) and some extra 21b11895c4SLibin * pools for workqueues which are not bound to any specific CPU - the 22b11895c4SLibin * number of these backing pools is dynamic. 23c54fce6eSTejun Heo * 249a261491SBenjamin Peterson * Please read Documentation/core-api/workqueue.rst for details. 251da177e4SLinus Torvalds */ 261da177e4SLinus Torvalds 279984de1aSPaul Gortmaker #include <linux/export.h> 281da177e4SLinus Torvalds #include <linux/kernel.h> 291da177e4SLinus Torvalds #include <linux/sched.h> 301da177e4SLinus Torvalds #include <linux/init.h> 311da177e4SLinus Torvalds #include <linux/signal.h> 321da177e4SLinus Torvalds #include <linux/completion.h> 331da177e4SLinus Torvalds #include <linux/workqueue.h> 341da177e4SLinus Torvalds #include <linux/slab.h> 351da177e4SLinus Torvalds #include <linux/cpu.h> 361da177e4SLinus Torvalds #include <linux/notifier.h> 371da177e4SLinus Torvalds #include <linux/kthread.h> 381fa44ecaSJames Bottomley #include <linux/hardirq.h> 3946934023SChristoph Lameter #include <linux/mempolicy.h> 40341a5958SRafael J. Wysocki #include <linux/freezer.h> 41d5abe669SPeter Zijlstra #include <linux/debug_locks.h> 424e6045f1SJohannes Berg #include <linux/lockdep.h> 43c34056a3STejun Heo #include <linux/idr.h> 4429c91e99STejun Heo #include <linux/jhash.h> 4542f8570fSSasha Levin #include <linux/hashtable.h> 4676af4d93STejun Heo #include <linux/rculist.h> 47bce90380STejun Heo #include <linux/nodemask.h> 484c16bd32STejun Heo #include <linux/moduleparam.h> 493d1cb205STejun Heo #include <linux/uaccess.h> 50c98a9805STal Shorer #include <linux/sched/isolation.h> 5162635ea8SSergey Senozhatsky #include <linux/nmi.h> 52e22bee78STejun Heo 53ea138446STejun Heo #include "workqueue_internal.h" 541da177e4SLinus Torvalds 55c8e55f36STejun Heo enum { 56bc2ae0f5STejun Heo /* 5724647570STejun Heo * worker_pool flags 58bc2ae0f5STejun Heo * 5924647570STejun Heo * A bound pool is either associated or disassociated with its CPU. 60bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 61bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 62bc2ae0f5STejun Heo * is in effect. 63bc2ae0f5STejun Heo * 64bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 65bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 6624647570STejun Heo * be executing on any CPU. The pool behaves as an unbound one. 67bc2ae0f5STejun Heo * 68bc3a1afcSTejun Heo * Note that DISASSOCIATED should be flipped only while holding 691258fae7STejun Heo * wq_pool_attach_mutex to avoid changing binding state while 704736cbf7SLai Jiangshan * worker_attach_to_pool() is in progress. 71bc2ae0f5STejun Heo */ 72692b4825STejun Heo POOL_MANAGER_ACTIVE = 1 << 0, /* being managed */ 7324647570STejun Heo POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ 74db7bccf4STejun Heo 75c8e55f36STejun Heo /* worker flags */ 76c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 77c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 78e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 79fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 80f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 81a9ab775bSTejun Heo WORKER_REBOUND = 1 << 8, /* worker was rebound */ 82e22bee78STejun Heo 83a9ab775bSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE | 84a9ab775bSTejun Heo WORKER_UNBOUND | WORKER_REBOUND, 85db7bccf4STejun Heo 86e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 874ce62e9eSTejun Heo 8829c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ 89c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 90db7bccf4STejun Heo 91e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 92e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 93e22bee78STejun Heo 943233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 953233cdbdSTejun Heo /* call for help after 10ms 963233cdbdSTejun Heo (min two ticks) */ 97e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 98e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds /* 101e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 1028698a745SDongsheng Yang * all cpus. Give MIN_NICE. 103e22bee78STejun Heo */ 1048698a745SDongsheng Yang RESCUER_NICE_LEVEL = MIN_NICE, 1058698a745SDongsheng Yang HIGHPRI_NICE_LEVEL = MIN_NICE, 106ecf6881fSTejun Heo 107ecf6881fSTejun Heo WQ_NAME_LEN = 24, 108c8e55f36STejun Heo }; 109c8e55f36STejun Heo 1101da177e4SLinus Torvalds /* 1114690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1124690c4abSTejun Heo * 113e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 114e41e704bSTejun Heo * everyone else. 1154690c4abSTejun Heo * 116e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 117e22bee78STejun Heo * only be modified and accessed from the local cpu. 118e22bee78STejun Heo * 119d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1204690c4abSTejun Heo * 121d565ed63STejun Heo * X: During normal operation, modification requires pool->lock and should 122d565ed63STejun Heo * be done only from local cpu. Either disabling preemption on local 123d565ed63STejun Heo * cpu or grabbing pool->lock is enough for read access. If 124d565ed63STejun Heo * POOL_DISASSOCIATED is set, it's identical to L. 125e22bee78STejun Heo * 1261258fae7STejun Heo * A: wq_pool_attach_mutex protected. 127822d8405STejun Heo * 12868e13a67SLai Jiangshan * PL: wq_pool_mutex protected. 12976af4d93STejun Heo * 13068e13a67SLai Jiangshan * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads. 1315bcab335STejun Heo * 1325b95e1afSLai Jiangshan * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads. 1335b95e1afSLai Jiangshan * 1345b95e1afSLai Jiangshan * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or 1355b95e1afSLai Jiangshan * sched-RCU for reads. 1365b95e1afSLai Jiangshan * 1373c25a55dSLai Jiangshan * WQ: wq->mutex protected. 1383c25a55dSLai Jiangshan * 139b5927605SLai Jiangshan * WR: wq->mutex protected for writes. Sched-RCU protected for reads. 1402e109a28STejun Heo * 1412e109a28STejun Heo * MD: wq_mayday_lock protected. 1424690c4abSTejun Heo */ 1434690c4abSTejun Heo 1442eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 145c34056a3STejun Heo 146bd7bdd43STejun Heo struct worker_pool { 147d565ed63STejun Heo spinlock_t lock; /* the pool lock */ 148d84ff051STejun Heo int cpu; /* I: the associated cpu */ 149f3f90ad4STejun Heo int node; /* I: the associated node ID */ 1509daf9e67STejun Heo int id; /* I: pool ID */ 15111ebea50STejun Heo unsigned int flags; /* X: flags */ 152bd7bdd43STejun Heo 15382607adcSTejun Heo unsigned long watchdog_ts; /* L: watchdog timestamp */ 15482607adcSTejun Heo 155bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 156ea1abd61SLai Jiangshan 1575826cc8fSLai Jiangshan int nr_workers; /* L: total number of workers */ 1585826cc8fSLai Jiangshan int nr_idle; /* L: currently idle workers */ 159bd7bdd43STejun Heo 160bd7bdd43STejun Heo struct list_head idle_list; /* X: list of idle workers */ 161bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 162bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 163bd7bdd43STejun Heo 164c5aa87bbSTejun Heo /* a workers is either on busy_hash or idle_list, or the manager */ 165c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 166c9e7cf27STejun Heo /* L: hash of busy workers */ 167c9e7cf27STejun Heo 1682607d7a6STejun Heo struct worker *manager; /* L: purely informational */ 16992f9c5c4SLai Jiangshan struct list_head workers; /* A: attached workers */ 17060f5a4bcSLai Jiangshan struct completion *detach_completion; /* all workers detached */ 171e19e397aSTejun Heo 1727cda9aaeSLai Jiangshan struct ida worker_ida; /* worker IDs for task name */ 173e19e397aSTejun Heo 1747a4e344cSTejun Heo struct workqueue_attrs *attrs; /* I: worker attributes */ 17568e13a67SLai Jiangshan struct hlist_node hash_node; /* PL: unbound_pool_hash node */ 17668e13a67SLai Jiangshan int refcnt; /* PL: refcnt for unbound pools */ 1777a4e344cSTejun Heo 178e19e397aSTejun Heo /* 179e19e397aSTejun Heo * The current concurrency level. As it's likely to be accessed 180e19e397aSTejun Heo * from other CPUs during try_to_wake_up(), put it in a separate 181e19e397aSTejun Heo * cacheline. 182e19e397aSTejun Heo */ 183e19e397aSTejun Heo atomic_t nr_running ____cacheline_aligned_in_smp; 18429c91e99STejun Heo 18529c91e99STejun Heo /* 18629c91e99STejun Heo * Destruction of pool is sched-RCU protected to allow dereferences 18729c91e99STejun Heo * from get_work_pool(). 18829c91e99STejun Heo */ 18929c91e99STejun Heo struct rcu_head rcu; 1908b03ae3cSTejun Heo } ____cacheline_aligned_in_smp; 1918b03ae3cSTejun Heo 1928b03ae3cSTejun Heo /* 193112202d9STejun Heo * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS 194112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits 195112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the 196112202d9STejun Heo * number of flag bits. 1971da177e4SLinus Torvalds */ 198112202d9STejun Heo struct pool_workqueue { 199bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 2004690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 20173f53c4aSTejun Heo int work_color; /* L: current color */ 20273f53c4aSTejun Heo int flush_color; /* L: flushing color */ 2038864b4e5STejun Heo int refcnt; /* L: reference count */ 20473f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 20573f53c4aSTejun Heo /* L: nr of in_flight works */ 2061e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 207a0a1a5fdSTejun Heo int max_active; /* L: max active works */ 2081e19ffc6STejun Heo struct list_head delayed_works; /* L: delayed works */ 2093c25a55dSLai Jiangshan struct list_head pwqs_node; /* WR: node on wq->pwqs */ 2102e109a28STejun Heo struct list_head mayday_node; /* MD: node on wq->maydays */ 2118864b4e5STejun Heo 2128864b4e5STejun Heo /* 2138864b4e5STejun Heo * Release of unbound pwq is punted to system_wq. See put_pwq() 2148864b4e5STejun Heo * and pwq_unbound_release_workfn() for details. pool_workqueue 2158864b4e5STejun Heo * itself is also sched-RCU protected so that the first pwq can be 216b09f4fd3SLai Jiangshan * determined without grabbing wq->mutex. 2178864b4e5STejun Heo */ 2188864b4e5STejun Heo struct work_struct unbound_release_work; 2198864b4e5STejun Heo struct rcu_head rcu; 220e904e6c2STejun Heo } __aligned(1 << WORK_STRUCT_FLAG_BITS); 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds /* 22373f53c4aSTejun Heo * Structure used to wait for workqueue flush. 22473f53c4aSTejun Heo */ 22573f53c4aSTejun Heo struct wq_flusher { 2263c25a55dSLai Jiangshan struct list_head list; /* WQ: list of flushers */ 2273c25a55dSLai Jiangshan int flush_color; /* WQ: flush color waiting for */ 22873f53c4aSTejun Heo struct completion done; /* flush completion */ 22973f53c4aSTejun Heo }; 2301da177e4SLinus Torvalds 231226223abSTejun Heo struct wq_device; 232226223abSTejun Heo 23373f53c4aSTejun Heo /* 234c5aa87bbSTejun Heo * The externally visible workqueue. It relays the issued work items to 235c5aa87bbSTejun Heo * the appropriate worker_pool through its pool_workqueues. 2361da177e4SLinus Torvalds */ 2371da177e4SLinus Torvalds struct workqueue_struct { 2383c25a55dSLai Jiangshan struct list_head pwqs; /* WR: all pwqs of this wq */ 239e2dca7adSTejun Heo struct list_head list; /* PR: list of all workqueues */ 24073f53c4aSTejun Heo 2413c25a55dSLai Jiangshan struct mutex mutex; /* protects this wq */ 2423c25a55dSLai Jiangshan int work_color; /* WQ: current work color */ 2433c25a55dSLai Jiangshan int flush_color; /* WQ: current flush color */ 244112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 2453c25a55dSLai Jiangshan struct wq_flusher *first_flusher; /* WQ: first flusher */ 2463c25a55dSLai Jiangshan struct list_head flusher_queue; /* WQ: flush waiters */ 2473c25a55dSLai Jiangshan struct list_head flusher_overflow; /* WQ: flush overflow list */ 24873f53c4aSTejun Heo 2492e109a28STejun Heo struct list_head maydays; /* MD: pwqs requesting rescue */ 250e22bee78STejun Heo struct worker *rescuer; /* I: rescue worker */ 251e22bee78STejun Heo 25287fc741eSLai Jiangshan int nr_drainers; /* WQ: drain in progress */ 253a357fc03SLai Jiangshan int saved_max_active; /* WQ: saved pwq max_active */ 254226223abSTejun Heo 2555b95e1afSLai Jiangshan struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */ 2565b95e1afSLai Jiangshan struct pool_workqueue *dfl_pwq; /* PW: only for unbound wqs */ 2576029a918STejun Heo 258226223abSTejun Heo #ifdef CONFIG_SYSFS 259226223abSTejun Heo struct wq_device *wq_dev; /* I: for sysfs interface */ 260226223abSTejun Heo #endif 2614e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 2624e6045f1SJohannes Berg struct lockdep_map lockdep_map; 2634e6045f1SJohannes Berg #endif 264ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */ 2652728fd2fSTejun Heo 266e2dca7adSTejun Heo /* 267e2dca7adSTejun Heo * Destruction of workqueue_struct is sched-RCU protected to allow 268e2dca7adSTejun Heo * walking the workqueues list without grabbing wq_pool_mutex. 269e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq. 270e2dca7adSTejun Heo */ 271e2dca7adSTejun Heo struct rcu_head rcu; 272e2dca7adSTejun Heo 2732728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */ 2742728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ 2752728fd2fSTejun Heo struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */ 2765b95e1afSLai Jiangshan struct pool_workqueue __rcu *numa_pwq_tbl[]; /* PWR: unbound pwqs indexed by node */ 2771da177e4SLinus Torvalds }; 2781da177e4SLinus Torvalds 279e904e6c2STejun Heo static struct kmem_cache *pwq_cache; 280e904e6c2STejun Heo 281bce90380STejun Heo static cpumask_var_t *wq_numa_possible_cpumask; 282bce90380STejun Heo /* possible CPUs of each node */ 283bce90380STejun Heo 284d55262c4STejun Heo static bool wq_disable_numa; 285d55262c4STejun Heo module_param_named(disable_numa, wq_disable_numa, bool, 0444); 286d55262c4STejun Heo 287cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 288552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 289cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 290cee22a15SViresh Kumar 291863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */ 2923347fa09STejun Heo 293bce90380STejun Heo static bool wq_numa_enabled; /* unbound NUMA affinity enabled */ 294bce90380STejun Heo 2954c16bd32STejun Heo /* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */ 2964c16bd32STejun Heo static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf; 2974c16bd32STejun Heo 29868e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 2991258fae7STejun Heo static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ 3002e109a28STejun Heo static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 301692b4825STejun Heo static DECLARE_WAIT_QUEUE_HEAD(wq_manager_wait); /* wait for manager to go away */ 3025bcab335STejun Heo 303e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 30468e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 3057d19c5ceSTejun Heo 306ef557180SMike Galbraith /* PL: allowable cpus for unbound wqs and work items */ 307ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 308ef557180SMike Galbraith 309ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 310ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 311b05a7928SFrederic Weisbecker 312f303fccbSTejun Heo /* 313f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 314f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 315f303fccbSTejun Heo * to uncover usages which depend on it. 316f303fccbSTejun Heo */ 317f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 318f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 319f303fccbSTejun Heo #else 320f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 321f303fccbSTejun Heo #endif 322f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 323f303fccbSTejun Heo 3247d19c5ceSTejun Heo /* the per-cpu worker pools */ 32525528213SPeter Zijlstra static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools); 3267d19c5ceSTejun Heo 32768e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 3287d19c5ceSTejun Heo 32968e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 33029c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 33129c91e99STejun Heo 332c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 33329c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 33429c91e99STejun Heo 3358a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 3368a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 3378a2b7538STejun Heo 338d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly; 339ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 340044c782cSValentin Ilie struct workqueue_struct *system_highpri_wq __read_mostly; 3411aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 342044c782cSValentin Ilie struct workqueue_struct *system_long_wq __read_mostly; 343d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 344044c782cSValentin Ilie struct workqueue_struct *system_unbound_wq __read_mostly; 345f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 346044c782cSValentin Ilie struct workqueue_struct *system_freezable_wq __read_mostly; 34724d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 3480668106cSViresh Kumar struct workqueue_struct *system_power_efficient_wq __read_mostly; 3490668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 3500668106cSViresh Kumar struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly; 3510668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 352d320c038STejun Heo 3537d19c5ceSTejun Heo static int worker_thread(void *__worker); 3546ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 3557d19c5ceSTejun Heo 35697bd2347STejun Heo #define CREATE_TRACE_POINTS 35797bd2347STejun Heo #include <trace/events/workqueue.h> 35897bd2347STejun Heo 35968e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 360f78f5b90SPaul E. McKenney RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \ 361f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 36268e13a67SLai Jiangshan "sched RCU or wq_pool_mutex should be held") 3635bcab335STejun Heo 364b09f4fd3SLai Jiangshan #define assert_rcu_or_wq_mutex(wq) \ 365f78f5b90SPaul E. McKenney RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \ 366f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex), \ 367b09f4fd3SLai Jiangshan "sched RCU or wq->mutex should be held") 36876af4d93STejun Heo 3695b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 370f78f5b90SPaul E. McKenney RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \ 371f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 372f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 3735b95e1afSLai Jiangshan "sched RCU, wq->mutex or wq_pool_mutex should be held") 3745b95e1afSLai Jiangshan 375f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 376f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 377f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 3787a62c2c8STejun Heo (pool)++) 3794ce62e9eSTejun Heo 38049e3cf44STejun Heo /** 38117116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 38217116969STejun Heo * @pool: iteration cursor 383611c92a0STejun Heo * @pi: integer used for iteration 384fa1b54e6STejun Heo * 38568e13a67SLai Jiangshan * This must be called either with wq_pool_mutex held or sched RCU read 38668e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 38768e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 388fa1b54e6STejun Heo * 389fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 390fa1b54e6STejun Heo * ignored. 39117116969STejun Heo */ 392611c92a0STejun Heo #define for_each_pool(pool, pi) \ 393611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 39468e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 395fa1b54e6STejun Heo else 39617116969STejun Heo 39717116969STejun Heo /** 398822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 399822d8405STejun Heo * @worker: iteration cursor 400822d8405STejun Heo * @pool: worker_pool to iterate workers of 401822d8405STejun Heo * 4021258fae7STejun Heo * This must be called with wq_pool_attach_mutex. 403822d8405STejun Heo * 404822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 405822d8405STejun Heo * ignored. 406822d8405STejun Heo */ 407da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 408da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 4091258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 410822d8405STejun Heo else 411822d8405STejun Heo 412822d8405STejun Heo /** 41349e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 41449e3cf44STejun Heo * @pwq: iteration cursor 41549e3cf44STejun Heo * @wq: the target workqueue 41676af4d93STejun Heo * 417b09f4fd3SLai Jiangshan * This must be called either with wq->mutex held or sched RCU read locked. 418794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 419794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 42076af4d93STejun Heo * 42176af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 42276af4d93STejun Heo * ignored. 42349e3cf44STejun Heo */ 42449e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 42576af4d93STejun Heo list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \ 426b09f4fd3SLai Jiangshan if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \ 42776af4d93STejun Heo else 428f3421797STejun Heo 429dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 430dc186ad7SThomas Gleixner 431dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr; 432dc186ad7SThomas Gleixner 43399777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 43499777288SStanislaw Gruszka { 43599777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 43699777288SStanislaw Gruszka } 43799777288SStanislaw Gruszka 438b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 439b9fdac7fSDu, Changbin { 440b9fdac7fSDu, Changbin struct work_struct *work = addr; 441b9fdac7fSDu, Changbin 442b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 443b9fdac7fSDu, Changbin } 444b9fdac7fSDu, Changbin 445dc186ad7SThomas Gleixner /* 446dc186ad7SThomas Gleixner * fixup_init is called when: 447dc186ad7SThomas Gleixner * - an active object is initialized 448dc186ad7SThomas Gleixner */ 44902a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 450dc186ad7SThomas Gleixner { 451dc186ad7SThomas Gleixner struct work_struct *work = addr; 452dc186ad7SThomas Gleixner 453dc186ad7SThomas Gleixner switch (state) { 454dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 455dc186ad7SThomas Gleixner cancel_work_sync(work); 456dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 45702a982a6SDu, Changbin return true; 458dc186ad7SThomas Gleixner default: 45902a982a6SDu, Changbin return false; 460dc186ad7SThomas Gleixner } 461dc186ad7SThomas Gleixner } 462dc186ad7SThomas Gleixner 463dc186ad7SThomas Gleixner /* 464dc186ad7SThomas Gleixner * fixup_free is called when: 465dc186ad7SThomas Gleixner * - an active object is freed 466dc186ad7SThomas Gleixner */ 46702a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 468dc186ad7SThomas Gleixner { 469dc186ad7SThomas Gleixner struct work_struct *work = addr; 470dc186ad7SThomas Gleixner 471dc186ad7SThomas Gleixner switch (state) { 472dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 473dc186ad7SThomas Gleixner cancel_work_sync(work); 474dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 47502a982a6SDu, Changbin return true; 476dc186ad7SThomas Gleixner default: 47702a982a6SDu, Changbin return false; 478dc186ad7SThomas Gleixner } 479dc186ad7SThomas Gleixner } 480dc186ad7SThomas Gleixner 481dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = { 482dc186ad7SThomas Gleixner .name = "work_struct", 48399777288SStanislaw Gruszka .debug_hint = work_debug_hint, 484b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 485dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 486dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 487dc186ad7SThomas Gleixner }; 488dc186ad7SThomas Gleixner 489dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 490dc186ad7SThomas Gleixner { 491dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 492dc186ad7SThomas Gleixner } 493dc186ad7SThomas Gleixner 494dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 495dc186ad7SThomas Gleixner { 496dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 497dc186ad7SThomas Gleixner } 498dc186ad7SThomas Gleixner 499dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 500dc186ad7SThomas Gleixner { 501dc186ad7SThomas Gleixner if (onstack) 502dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 503dc186ad7SThomas Gleixner else 504dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 505dc186ad7SThomas Gleixner } 506dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 507dc186ad7SThomas Gleixner 508dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 509dc186ad7SThomas Gleixner { 510dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 511dc186ad7SThomas Gleixner } 512dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 513dc186ad7SThomas Gleixner 514ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 515ea2e64f2SThomas Gleixner { 516ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 517ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 518ea2e64f2SThomas Gleixner } 519ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 520ea2e64f2SThomas Gleixner 521dc186ad7SThomas Gleixner #else 522dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 523dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 524dc186ad7SThomas Gleixner #endif 525dc186ad7SThomas Gleixner 5264e8b22bdSLi Bin /** 5274e8b22bdSLi Bin * worker_pool_assign_id - allocate ID and assing it to @pool 5284e8b22bdSLi Bin * @pool: the pool pointer of interest 5294e8b22bdSLi Bin * 5304e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 5314e8b22bdSLi Bin * successfully, -errno on failure. 5324e8b22bdSLi Bin */ 5339daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 5349daf9e67STejun Heo { 5359daf9e67STejun Heo int ret; 5369daf9e67STejun Heo 53768e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5385bcab335STejun Heo 5394e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 5404e8b22bdSLi Bin GFP_KERNEL); 541229641a6STejun Heo if (ret >= 0) { 542e68035fbSTejun Heo pool->id = ret; 543229641a6STejun Heo return 0; 544229641a6STejun Heo } 5459daf9e67STejun Heo return ret; 5469daf9e67STejun Heo } 5479daf9e67STejun Heo 54876af4d93STejun Heo /** 549df2d5ae4STejun Heo * unbound_pwq_by_node - return the unbound pool_workqueue for the given node 550df2d5ae4STejun Heo * @wq: the target workqueue 551df2d5ae4STejun Heo * @node: the node ID 552df2d5ae4STejun Heo * 5535b95e1afSLai Jiangshan * This must be called with any of wq_pool_mutex, wq->mutex or sched RCU 5545b95e1afSLai Jiangshan * read locked. 555df2d5ae4STejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 556df2d5ae4STejun Heo * responsible for guaranteeing that the pwq stays online. 557d185af30SYacine Belkadi * 558d185af30SYacine Belkadi * Return: The unbound pool_workqueue for @node. 559df2d5ae4STejun Heo */ 560df2d5ae4STejun Heo static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq, 561df2d5ae4STejun Heo int node) 562df2d5ae4STejun Heo { 5635b95e1afSLai Jiangshan assert_rcu_or_wq_mutex_or_pool_mutex(wq); 564d6e022f1STejun Heo 565d6e022f1STejun Heo /* 566d6e022f1STejun Heo * XXX: @node can be NUMA_NO_NODE if CPU goes offline while a 567d6e022f1STejun Heo * delayed item is pending. The plan is to keep CPU -> NODE 568d6e022f1STejun Heo * mapping valid and stable across CPU on/offlines. Once that 569d6e022f1STejun Heo * happens, this workaround can be removed. 570d6e022f1STejun Heo */ 571d6e022f1STejun Heo if (unlikely(node == NUMA_NO_NODE)) 572d6e022f1STejun Heo return wq->dfl_pwq; 573d6e022f1STejun Heo 574df2d5ae4STejun Heo return rcu_dereference_raw(wq->numa_pwq_tbl[node]); 575df2d5ae4STejun Heo } 576df2d5ae4STejun Heo 57773f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 57873f53c4aSTejun Heo { 57973f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 58073f53c4aSTejun Heo } 58173f53c4aSTejun Heo 58273f53c4aSTejun Heo static int get_work_color(struct work_struct *work) 58373f53c4aSTejun Heo { 58473f53c4aSTejun Heo return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) & 58573f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 58673f53c4aSTejun Heo } 58773f53c4aSTejun Heo 58873f53c4aSTejun Heo static int work_next_color(int color) 58973f53c4aSTejun Heo { 59073f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 591a848e3b6SOleg Nesterov } 592a848e3b6SOleg Nesterov 5934594bf15SDavid Howells /* 594112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 595112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 5967c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 5977a22ad75STejun Heo * 598112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 599112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 600bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 601bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 6027a22ad75STejun Heo * 603112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 6047c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 605112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 6067c3eed5cSTejun Heo * available only while the work item is queued. 607bbb68dfaSTejun Heo * 608bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 609bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 610bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 611bbb68dfaSTejun Heo * try to steal the PENDING bit. 6124594bf15SDavid Howells */ 6137a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 6147a22ad75STejun Heo unsigned long flags) 6157a22ad75STejun Heo { 6166183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 6177a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 6187a22ad75STejun Heo } 6197a22ad75STejun Heo 620112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 6214690c4abSTejun Heo unsigned long extra_flags) 622365970a1SDavid Howells { 623112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 624112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 625365970a1SDavid Howells } 626365970a1SDavid Howells 6274468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 6284468a00fSLai Jiangshan int pool_id) 6294468a00fSLai Jiangshan { 6304468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 6314468a00fSLai Jiangshan WORK_STRUCT_PENDING); 6324468a00fSLai Jiangshan } 6334468a00fSLai Jiangshan 6347c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 6357c3eed5cSTejun Heo int pool_id) 6364d707b9fSOleg Nesterov { 63723657bb1STejun Heo /* 63823657bb1STejun Heo * The following wmb is paired with the implied mb in 63923657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 64023657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 64123657bb1STejun Heo * owner. 64223657bb1STejun Heo */ 64323657bb1STejun Heo smp_wmb(); 6447c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 645346c09f8SRoman Pen /* 646346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 647346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 648346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 649346c09f8SRoman Pen * reordering can lead to a missed execution on attempt to qeueue 650346c09f8SRoman Pen * the same @work. E.g. consider this case: 651346c09f8SRoman Pen * 652346c09f8SRoman Pen * CPU#0 CPU#1 653346c09f8SRoman Pen * ---------------------------- -------------------------------- 654346c09f8SRoman Pen * 655346c09f8SRoman Pen * 1 STORE event_indicated 656346c09f8SRoman Pen * 2 queue_work_on() { 657346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 658346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 659346c09f8SRoman Pen * 5 set_work_data() # clear bit 660346c09f8SRoman Pen * 6 smp_mb() 661346c09f8SRoman Pen * 7 work->current_func() { 662346c09f8SRoman Pen * 8 LOAD event_indicated 663346c09f8SRoman Pen * } 664346c09f8SRoman Pen * 665346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 666346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 667346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 668346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 669346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 670346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 671346c09f8SRoman Pen * before actual STORE. 672346c09f8SRoman Pen */ 673346c09f8SRoman Pen smp_mb(); 6744d707b9fSOleg Nesterov } 6754d707b9fSOleg Nesterov 6767a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 677365970a1SDavid Howells { 6787c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 6797c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 6807a22ad75STejun Heo } 6817a22ad75STejun Heo 682112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 6837a22ad75STejun Heo { 684e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 6857a22ad75STejun Heo 686112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 687e120153dSTejun Heo return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); 688e120153dSTejun Heo else 689e120153dSTejun Heo return NULL; 6907a22ad75STejun Heo } 6917a22ad75STejun Heo 6927c3eed5cSTejun Heo /** 6937c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 6947c3eed5cSTejun Heo * @work: the work item of interest 6957c3eed5cSTejun Heo * 69668e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 69768e13a67SLai Jiangshan * access under sched-RCU read lock. As such, this function should be 69868e13a67SLai Jiangshan * called under wq_pool_mutex or with preemption disabled. 699fa1b54e6STejun Heo * 700fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 701fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 702fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 703fa1b54e6STejun Heo * returned pool is and stays online. 704d185af30SYacine Belkadi * 705d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 7067c3eed5cSTejun Heo */ 7077c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 7087a22ad75STejun Heo { 709e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 7107c3eed5cSTejun Heo int pool_id; 7117a22ad75STejun Heo 71268e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 713fa1b54e6STejun Heo 714112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 715112202d9STejun Heo return ((struct pool_workqueue *) 7167c3eed5cSTejun Heo (data & WORK_STRUCT_WQ_DATA_MASK))->pool; 7177a22ad75STejun Heo 7187c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 7197c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 7207a22ad75STejun Heo return NULL; 7217a22ad75STejun Heo 722fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 7237c3eed5cSTejun Heo } 7247c3eed5cSTejun Heo 7257c3eed5cSTejun Heo /** 7267c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 7277c3eed5cSTejun Heo * @work: the work item of interest 7287c3eed5cSTejun Heo * 729d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 7307c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 7317c3eed5cSTejun Heo */ 7327c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 7337c3eed5cSTejun Heo { 73454d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 7357c3eed5cSTejun Heo 736112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 737112202d9STejun Heo return ((struct pool_workqueue *) 73854d5b7d0SLai Jiangshan (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id; 73954d5b7d0SLai Jiangshan 74054d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 7417c3eed5cSTejun Heo } 7427c3eed5cSTejun Heo 743bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 744bbb68dfaSTejun Heo { 7457c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 746bbb68dfaSTejun Heo 7477c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 7487c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 749bbb68dfaSTejun Heo } 750bbb68dfaSTejun Heo 751bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 752bbb68dfaSTejun Heo { 753bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 754bbb68dfaSTejun Heo 755112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 756bbb68dfaSTejun Heo } 757bbb68dfaSTejun Heo 758e22bee78STejun Heo /* 7593270476aSTejun Heo * Policy functions. These define the policies on how the global worker 7603270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 761d565ed63STejun Heo * they're being called with pool->lock held. 762e22bee78STejun Heo */ 763e22bee78STejun Heo 76463d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool) 765649027d7STejun Heo { 766e19e397aSTejun Heo return !atomic_read(&pool->nr_running); 767649027d7STejun Heo } 768649027d7STejun Heo 769e22bee78STejun Heo /* 770e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 771e22bee78STejun Heo * running workers. 772974271c4STejun Heo * 773974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 774706026c2STejun Heo * function will always return %true for unbound pools as long as the 775974271c4STejun Heo * worklist isn't empty. 776e22bee78STejun Heo */ 77763d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 778e22bee78STejun Heo { 77963d95a91STejun Heo return !list_empty(&pool->worklist) && __need_more_worker(pool); 780e22bee78STejun Heo } 781e22bee78STejun Heo 782e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 78363d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 784e22bee78STejun Heo { 78563d95a91STejun Heo return pool->nr_idle; 786e22bee78STejun Heo } 787e22bee78STejun Heo 788e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 78963d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 790e22bee78STejun Heo { 791e19e397aSTejun Heo return !list_empty(&pool->worklist) && 792e19e397aSTejun Heo atomic_read(&pool->nr_running) <= 1; 793e22bee78STejun Heo } 794e22bee78STejun Heo 795e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 79663d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 797e22bee78STejun Heo { 79863d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 799e22bee78STejun Heo } 800e22bee78STejun Heo 801e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 80263d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 803e22bee78STejun Heo { 804692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 80563d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 80663d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 807e22bee78STejun Heo 808e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 809e22bee78STejun Heo } 810e22bee78STejun Heo 811e22bee78STejun Heo /* 812e22bee78STejun Heo * Wake up functions. 813e22bee78STejun Heo */ 814e22bee78STejun Heo 8151037de36SLai Jiangshan /* Return the first idle worker. Safe with preemption disabled */ 8161037de36SLai Jiangshan static struct worker *first_idle_worker(struct worker_pool *pool) 8177e11629dSTejun Heo { 81863d95a91STejun Heo if (unlikely(list_empty(&pool->idle_list))) 8197e11629dSTejun Heo return NULL; 8207e11629dSTejun Heo 82163d95a91STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 8227e11629dSTejun Heo } 8237e11629dSTejun Heo 8247e11629dSTejun Heo /** 8257e11629dSTejun Heo * wake_up_worker - wake up an idle worker 82663d95a91STejun Heo * @pool: worker pool to wake worker from 8277e11629dSTejun Heo * 82863d95a91STejun Heo * Wake up the first idle worker of @pool. 8297e11629dSTejun Heo * 8307e11629dSTejun Heo * CONTEXT: 831d565ed63STejun Heo * spin_lock_irq(pool->lock). 8327e11629dSTejun Heo */ 83363d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool) 8347e11629dSTejun Heo { 8351037de36SLai Jiangshan struct worker *worker = first_idle_worker(pool); 8367e11629dSTejun Heo 8377e11629dSTejun Heo if (likely(worker)) 8387e11629dSTejun Heo wake_up_process(worker->task); 8397e11629dSTejun Heo } 8407e11629dSTejun Heo 8414690c4abSTejun Heo /** 842e22bee78STejun Heo * wq_worker_waking_up - a worker is waking up 843e22bee78STejun Heo * @task: task waking up 844e22bee78STejun Heo * @cpu: CPU @task is waking up to 845e22bee78STejun Heo * 846e22bee78STejun Heo * This function is called during try_to_wake_up() when a worker is 847e22bee78STejun Heo * being awoken. 848e22bee78STejun Heo * 849e22bee78STejun Heo * CONTEXT: 850e22bee78STejun Heo * spin_lock_irq(rq->lock) 851e22bee78STejun Heo */ 852d84ff051STejun Heo void wq_worker_waking_up(struct task_struct *task, int cpu) 853e22bee78STejun Heo { 854e22bee78STejun Heo struct worker *worker = kthread_data(task); 855e22bee78STejun Heo 85636576000SJoonsoo Kim if (!(worker->flags & WORKER_NOT_RUNNING)) { 857ec22ca5eSTejun Heo WARN_ON_ONCE(worker->pool->cpu != cpu); 858e19e397aSTejun Heo atomic_inc(&worker->pool->nr_running); 859e22bee78STejun Heo } 86036576000SJoonsoo Kim } 861e22bee78STejun Heo 862e22bee78STejun Heo /** 863e22bee78STejun Heo * wq_worker_sleeping - a worker is going to sleep 864e22bee78STejun Heo * @task: task going to sleep 865e22bee78STejun Heo * 866e22bee78STejun Heo * This function is called during schedule() when a busy worker is 867e22bee78STejun Heo * going to sleep. Worker on the same cpu can be woken up by 868e22bee78STejun Heo * returning pointer to its task. 869e22bee78STejun Heo * 870e22bee78STejun Heo * CONTEXT: 871e22bee78STejun Heo * spin_lock_irq(rq->lock) 872e22bee78STejun Heo * 873d185af30SYacine Belkadi * Return: 874e22bee78STejun Heo * Worker task on @cpu to wake up, %NULL if none. 875e22bee78STejun Heo */ 8769b7f6597SAlexander Gordeev struct task_struct *wq_worker_sleeping(struct task_struct *task) 877e22bee78STejun Heo { 878e22bee78STejun Heo struct worker *worker = kthread_data(task), *to_wakeup = NULL; 879111c225aSTejun Heo struct worker_pool *pool; 880e22bee78STejun Heo 881111c225aSTejun Heo /* 882111c225aSTejun Heo * Rescuers, which may not have all the fields set up like normal 883111c225aSTejun Heo * workers, also reach here, let's not access anything before 884111c225aSTejun Heo * checking NOT_RUNNING. 885111c225aSTejun Heo */ 8862d64672eSSteven Rostedt if (worker->flags & WORKER_NOT_RUNNING) 887e22bee78STejun Heo return NULL; 888e22bee78STejun Heo 889111c225aSTejun Heo pool = worker->pool; 890111c225aSTejun Heo 891e22bee78STejun Heo /* this can only happen on the local cpu */ 8929b7f6597SAlexander Gordeev if (WARN_ON_ONCE(pool->cpu != raw_smp_processor_id())) 8936183c009STejun Heo return NULL; 894e22bee78STejun Heo 895e22bee78STejun Heo /* 896e22bee78STejun Heo * The counterpart of the following dec_and_test, implied mb, 897e22bee78STejun Heo * worklist not empty test sequence is in insert_work(). 898e22bee78STejun Heo * Please read comment there. 899e22bee78STejun Heo * 900628c78e7STejun Heo * NOT_RUNNING is clear. This means that we're bound to and 901628c78e7STejun Heo * running on the local cpu w/ rq lock held and preemption 902628c78e7STejun Heo * disabled, which in turn means that none else could be 903d565ed63STejun Heo * manipulating idle_list, so dereferencing idle_list without pool 904628c78e7STejun Heo * lock is safe. 905e22bee78STejun Heo */ 906e19e397aSTejun Heo if (atomic_dec_and_test(&pool->nr_running) && 907e19e397aSTejun Heo !list_empty(&pool->worklist)) 9081037de36SLai Jiangshan to_wakeup = first_idle_worker(pool); 909e22bee78STejun Heo return to_wakeup ? to_wakeup->task : NULL; 910e22bee78STejun Heo } 911e22bee78STejun Heo 912e22bee78STejun Heo /** 913*1b69ac6bSJohannes Weiner * wq_worker_last_func - retrieve worker's last work function 914*1b69ac6bSJohannes Weiner * 915*1b69ac6bSJohannes Weiner * Determine the last function a worker executed. This is called from 916*1b69ac6bSJohannes Weiner * the scheduler to get a worker's last known identity. 917*1b69ac6bSJohannes Weiner * 918*1b69ac6bSJohannes Weiner * CONTEXT: 919*1b69ac6bSJohannes Weiner * spin_lock_irq(rq->lock) 920*1b69ac6bSJohannes Weiner * 921*1b69ac6bSJohannes Weiner * Return: 922*1b69ac6bSJohannes Weiner * The last work function %current executed as a worker, NULL if it 923*1b69ac6bSJohannes Weiner * hasn't executed any work yet. 924*1b69ac6bSJohannes Weiner */ 925*1b69ac6bSJohannes Weiner work_func_t wq_worker_last_func(struct task_struct *task) 926*1b69ac6bSJohannes Weiner { 927*1b69ac6bSJohannes Weiner struct worker *worker = kthread_data(task); 928*1b69ac6bSJohannes Weiner 929*1b69ac6bSJohannes Weiner return worker->last_func; 930*1b69ac6bSJohannes Weiner } 931*1b69ac6bSJohannes Weiner 932*1b69ac6bSJohannes Weiner /** 933e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 934cb444766STejun Heo * @worker: self 935d302f017STejun Heo * @flags: flags to set 936d302f017STejun Heo * 937228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 938d302f017STejun Heo * 939cb444766STejun Heo * CONTEXT: 940d565ed63STejun Heo * spin_lock_irq(pool->lock) 941d302f017STejun Heo */ 942228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 943d302f017STejun Heo { 944bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 945e22bee78STejun Heo 946cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 947cb444766STejun Heo 948228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 949e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 950e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 951e19e397aSTejun Heo atomic_dec(&pool->nr_running); 952e22bee78STejun Heo } 953e22bee78STejun Heo 954d302f017STejun Heo worker->flags |= flags; 955d302f017STejun Heo } 956d302f017STejun Heo 957d302f017STejun Heo /** 958e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 959cb444766STejun Heo * @worker: self 960d302f017STejun Heo * @flags: flags to clear 961d302f017STejun Heo * 962e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 963d302f017STejun Heo * 964cb444766STejun Heo * CONTEXT: 965d565ed63STejun Heo * spin_lock_irq(pool->lock) 966d302f017STejun Heo */ 967d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 968d302f017STejun Heo { 96963d95a91STejun Heo struct worker_pool *pool = worker->pool; 970e22bee78STejun Heo unsigned int oflags = worker->flags; 971e22bee78STejun Heo 972cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 973cb444766STejun Heo 974d302f017STejun Heo worker->flags &= ~flags; 975e22bee78STejun Heo 97642c025f3STejun Heo /* 97742c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 97842c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 97942c025f3STejun Heo * of multiple flags, not a single flag. 98042c025f3STejun Heo */ 981e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 982e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 983e19e397aSTejun Heo atomic_inc(&pool->nr_running); 984d302f017STejun Heo } 985d302f017STejun Heo 986d302f017STejun Heo /** 9878cca0eeaSTejun Heo * find_worker_executing_work - find worker which is executing a work 988c9e7cf27STejun Heo * @pool: pool of interest 9898cca0eeaSTejun Heo * @work: work to find worker for 9908cca0eeaSTejun Heo * 991c9e7cf27STejun Heo * Find a worker which is executing @work on @pool by searching 992c9e7cf27STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 993a2c1c57bSTejun Heo * to match, its current execution should match the address of @work and 994a2c1c57bSTejun Heo * its work function. This is to avoid unwanted dependency between 995a2c1c57bSTejun Heo * unrelated work executions through a work item being recycled while still 996a2c1c57bSTejun Heo * being executed. 997a2c1c57bSTejun Heo * 998a2c1c57bSTejun Heo * This is a bit tricky. A work item may be freed once its execution 999a2c1c57bSTejun Heo * starts and nothing prevents the freed area from being recycled for 1000a2c1c57bSTejun Heo * another work item. If the same work item address ends up being reused 1001a2c1c57bSTejun Heo * before the original execution finishes, workqueue will identify the 1002a2c1c57bSTejun Heo * recycled work item as currently executing and make it wait until the 1003a2c1c57bSTejun Heo * current execution finishes, introducing an unwanted dependency. 1004a2c1c57bSTejun Heo * 1005c5aa87bbSTejun Heo * This function checks the work item address and work function to avoid 1006c5aa87bbSTejun Heo * false positives. Note that this isn't complete as one may construct a 1007c5aa87bbSTejun Heo * work function which can introduce dependency onto itself through a 1008c5aa87bbSTejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 1009c5aa87bbSTejun Heo * foot that badly, there's only so much we can do, and if such deadlock 1010c5aa87bbSTejun Heo * actually occurs, it should be easy to locate the culprit work function. 10118cca0eeaSTejun Heo * 10128cca0eeaSTejun Heo * CONTEXT: 1013d565ed63STejun Heo * spin_lock_irq(pool->lock). 10148cca0eeaSTejun Heo * 1015d185af30SYacine Belkadi * Return: 1016d185af30SYacine Belkadi * Pointer to worker which is executing @work if found, %NULL 10178cca0eeaSTejun Heo * otherwise. 10188cca0eeaSTejun Heo */ 1019c9e7cf27STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 10208cca0eeaSTejun Heo struct work_struct *work) 10218cca0eeaSTejun Heo { 102242f8570fSSasha Levin struct worker *worker; 102342f8570fSSasha Levin 1024b67bfe0dSSasha Levin hash_for_each_possible(pool->busy_hash, worker, hentry, 1025a2c1c57bSTejun Heo (unsigned long)work) 1026a2c1c57bSTejun Heo if (worker->current_work == work && 1027a2c1c57bSTejun Heo worker->current_func == work->func) 102842f8570fSSasha Levin return worker; 102942f8570fSSasha Levin 103042f8570fSSasha Levin return NULL; 10318cca0eeaSTejun Heo } 10328cca0eeaSTejun Heo 10338cca0eeaSTejun Heo /** 1034bf4ede01STejun Heo * move_linked_works - move linked works to a list 1035bf4ede01STejun Heo * @work: start of series of works to be scheduled 1036bf4ede01STejun Heo * @head: target list to append @work to 1037402dd89dSShailendra Verma * @nextp: out parameter for nested worklist walking 1038bf4ede01STejun Heo * 1039bf4ede01STejun Heo * Schedule linked works starting from @work to @head. Work series to 1040bf4ede01STejun Heo * be scheduled starts at @work and includes any consecutive work with 1041bf4ede01STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 1042bf4ede01STejun Heo * 1043bf4ede01STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 1044bf4ede01STejun Heo * the last scheduled work. This allows move_linked_works() to be 1045bf4ede01STejun Heo * nested inside outer list_for_each_entry_safe(). 1046bf4ede01STejun Heo * 1047bf4ede01STejun Heo * CONTEXT: 1048d565ed63STejun Heo * spin_lock_irq(pool->lock). 1049bf4ede01STejun Heo */ 1050bf4ede01STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1051bf4ede01STejun Heo struct work_struct **nextp) 1052bf4ede01STejun Heo { 1053bf4ede01STejun Heo struct work_struct *n; 1054bf4ede01STejun Heo 1055bf4ede01STejun Heo /* 1056bf4ede01STejun Heo * Linked worklist will always end before the end of the list, 1057bf4ede01STejun Heo * use NULL for list head. 1058bf4ede01STejun Heo */ 1059bf4ede01STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1060bf4ede01STejun Heo list_move_tail(&work->entry, head); 1061bf4ede01STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1062bf4ede01STejun Heo break; 1063bf4ede01STejun Heo } 1064bf4ede01STejun Heo 1065bf4ede01STejun Heo /* 1066bf4ede01STejun Heo * If we're already inside safe list traversal and have moved 1067bf4ede01STejun Heo * multiple works to the scheduled queue, the next position 1068bf4ede01STejun Heo * needs to be updated. 1069bf4ede01STejun Heo */ 1070bf4ede01STejun Heo if (nextp) 1071bf4ede01STejun Heo *nextp = n; 1072bf4ede01STejun Heo } 1073bf4ede01STejun Heo 10748864b4e5STejun Heo /** 10758864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 10768864b4e5STejun Heo * @pwq: pool_workqueue to get 10778864b4e5STejun Heo * 10788864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 10798864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 10808864b4e5STejun Heo */ 10818864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 10828864b4e5STejun Heo { 10838864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 10848864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 10858864b4e5STejun Heo pwq->refcnt++; 10868864b4e5STejun Heo } 10878864b4e5STejun Heo 10888864b4e5STejun Heo /** 10898864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 10908864b4e5STejun Heo * @pwq: pool_workqueue to put 10918864b4e5STejun Heo * 10928864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 10938864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 10948864b4e5STejun Heo */ 10958864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 10968864b4e5STejun Heo { 10978864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 10988864b4e5STejun Heo if (likely(--pwq->refcnt)) 10998864b4e5STejun Heo return; 11008864b4e5STejun Heo if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND))) 11018864b4e5STejun Heo return; 11028864b4e5STejun Heo /* 11038864b4e5STejun Heo * @pwq can't be released under pool->lock, bounce to 11048864b4e5STejun Heo * pwq_unbound_release_workfn(). This never recurses on the same 11058864b4e5STejun Heo * pool->lock as this path is taken only for unbound workqueues and 11068864b4e5STejun Heo * the release work item is scheduled on a per-cpu workqueue. To 11078864b4e5STejun Heo * avoid lockdep warning, unbound pool->locks are given lockdep 11088864b4e5STejun Heo * subclass of 1 in get_unbound_pool(). 11098864b4e5STejun Heo */ 11108864b4e5STejun Heo schedule_work(&pwq->unbound_release_work); 11118864b4e5STejun Heo } 11128864b4e5STejun Heo 1113dce90d47STejun Heo /** 1114dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1115dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1116dce90d47STejun Heo * 1117dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1118dce90d47STejun Heo */ 1119dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1120dce90d47STejun Heo { 1121dce90d47STejun Heo if (pwq) { 1122dce90d47STejun Heo /* 1123dce90d47STejun Heo * As both pwqs and pools are sched-RCU protected, the 1124dce90d47STejun Heo * following lock operations are safe. 1125dce90d47STejun Heo */ 1126dce90d47STejun Heo spin_lock_irq(&pwq->pool->lock); 1127dce90d47STejun Heo put_pwq(pwq); 1128dce90d47STejun Heo spin_unlock_irq(&pwq->pool->lock); 1129dce90d47STejun Heo } 1130dce90d47STejun Heo } 1131dce90d47STejun Heo 1132112202d9STejun Heo static void pwq_activate_delayed_work(struct work_struct *work) 1133bf4ede01STejun Heo { 1134112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1135bf4ede01STejun Heo 1136bf4ede01STejun Heo trace_workqueue_activate_work(work); 113782607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 113882607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1139112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 1140bf4ede01STejun Heo __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); 1141112202d9STejun Heo pwq->nr_active++; 1142bf4ede01STejun Heo } 1143bf4ede01STejun Heo 1144112202d9STejun Heo static void pwq_activate_first_delayed(struct pool_workqueue *pwq) 11453aa62497SLai Jiangshan { 1146112202d9STejun Heo struct work_struct *work = list_first_entry(&pwq->delayed_works, 11473aa62497SLai Jiangshan struct work_struct, entry); 11483aa62497SLai Jiangshan 1149112202d9STejun Heo pwq_activate_delayed_work(work); 11503aa62497SLai Jiangshan } 11513aa62497SLai Jiangshan 1152bf4ede01STejun Heo /** 1153112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1154112202d9STejun Heo * @pwq: pwq of interest 1155bf4ede01STejun Heo * @color: color of work which left the queue 1156bf4ede01STejun Heo * 1157bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1158112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1159bf4ede01STejun Heo * 1160bf4ede01STejun Heo * CONTEXT: 1161d565ed63STejun Heo * spin_lock_irq(pool->lock). 1162bf4ede01STejun Heo */ 1163112202d9STejun Heo static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color) 1164bf4ede01STejun Heo { 11658864b4e5STejun Heo /* uncolored work items don't participate in flushing or nr_active */ 1166bf4ede01STejun Heo if (color == WORK_NO_COLOR) 11678864b4e5STejun Heo goto out_put; 1168bf4ede01STejun Heo 1169112202d9STejun Heo pwq->nr_in_flight[color]--; 1170bf4ede01STejun Heo 1171112202d9STejun Heo pwq->nr_active--; 1172112202d9STejun Heo if (!list_empty(&pwq->delayed_works)) { 1173bf4ede01STejun Heo /* one down, submit a delayed one */ 1174112202d9STejun Heo if (pwq->nr_active < pwq->max_active) 1175112202d9STejun Heo pwq_activate_first_delayed(pwq); 1176bf4ede01STejun Heo } 1177bf4ede01STejun Heo 1178bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 1179112202d9STejun Heo if (likely(pwq->flush_color != color)) 11808864b4e5STejun Heo goto out_put; 1181bf4ede01STejun Heo 1182bf4ede01STejun Heo /* are there still in-flight works? */ 1183112202d9STejun Heo if (pwq->nr_in_flight[color]) 11848864b4e5STejun Heo goto out_put; 1185bf4ede01STejun Heo 1186112202d9STejun Heo /* this pwq is done, clear flush_color */ 1187112202d9STejun Heo pwq->flush_color = -1; 1188bf4ede01STejun Heo 1189bf4ede01STejun Heo /* 1190112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 1191bf4ede01STejun Heo * will handle the rest. 1192bf4ede01STejun Heo */ 1193112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1194112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 11958864b4e5STejun Heo out_put: 11968864b4e5STejun Heo put_pwq(pwq); 1197bf4ede01STejun Heo } 1198bf4ede01STejun Heo 119936e227d2STejun Heo /** 1200bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 120136e227d2STejun Heo * @work: work item to steal 120236e227d2STejun Heo * @is_dwork: @work is a delayed_work 1203bbb68dfaSTejun Heo * @flags: place to store irq state 120436e227d2STejun Heo * 120536e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 1206d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 120736e227d2STejun Heo * 1208d185af30SYacine Belkadi * Return: 120936e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 121036e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 121136e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1212bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1213bbb68dfaSTejun Heo * for arbitrarily long 121436e227d2STejun Heo * 1215d185af30SYacine Belkadi * Note: 1216bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1217e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 1218e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 1219e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 1220bbb68dfaSTejun Heo * 1221bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1222bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1223bbb68dfaSTejun Heo * 1224e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 1225bf4ede01STejun Heo */ 1226bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1227bbb68dfaSTejun Heo unsigned long *flags) 1228bf4ede01STejun Heo { 1229d565ed63STejun Heo struct worker_pool *pool; 1230112202d9STejun Heo struct pool_workqueue *pwq; 1231bf4ede01STejun Heo 1232bbb68dfaSTejun Heo local_irq_save(*flags); 1233bbb68dfaSTejun Heo 123436e227d2STejun Heo /* try to steal the timer if it exists */ 123536e227d2STejun Heo if (is_dwork) { 123636e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 123736e227d2STejun Heo 1238e0aecdd8STejun Heo /* 1239e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 1240e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 1241e0aecdd8STejun Heo * running on the local CPU. 1242e0aecdd8STejun Heo */ 124336e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 124436e227d2STejun Heo return 1; 124536e227d2STejun Heo } 124636e227d2STejun Heo 124736e227d2STejun Heo /* try to claim PENDING the normal way */ 1248bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1249bf4ede01STejun Heo return 0; 1250bf4ede01STejun Heo 1251bf4ede01STejun Heo /* 1252bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1253bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1254bf4ede01STejun Heo */ 1255d565ed63STejun Heo pool = get_work_pool(work); 1256d565ed63STejun Heo if (!pool) 1257bbb68dfaSTejun Heo goto fail; 1258bf4ede01STejun Heo 1259d565ed63STejun Heo spin_lock(&pool->lock); 1260bf4ede01STejun Heo /* 1261112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 1262112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 1263112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 1264112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 1265112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 12660b3dae68SLai Jiangshan * item is currently queued on that pool. 1267bf4ede01STejun Heo */ 1268112202d9STejun Heo pwq = get_work_pwq(work); 1269112202d9STejun Heo if (pwq && pwq->pool == pool) { 1270bf4ede01STejun Heo debug_work_deactivate(work); 12713aa62497SLai Jiangshan 12723aa62497SLai Jiangshan /* 127316062836STejun Heo * A delayed work item cannot be grabbed directly because 127416062836STejun Heo * it might have linked NO_COLOR work items which, if left 1275112202d9STejun Heo * on the delayed_list, will confuse pwq->nr_active 127616062836STejun Heo * management later on and cause stall. Make sure the work 127716062836STejun Heo * item is activated before grabbing. 12783aa62497SLai Jiangshan */ 12793aa62497SLai Jiangshan if (*work_data_bits(work) & WORK_STRUCT_DELAYED) 1280112202d9STejun Heo pwq_activate_delayed_work(work); 12813aa62497SLai Jiangshan 1282bf4ede01STejun Heo list_del_init(&work->entry); 12839c34a704SLai Jiangshan pwq_dec_nr_in_flight(pwq, get_work_color(work)); 128436e227d2STejun Heo 1285112202d9STejun Heo /* work->data points to pwq iff queued, point to pool */ 12864468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 12874468a00fSLai Jiangshan 1288d565ed63STejun Heo spin_unlock(&pool->lock); 128936e227d2STejun Heo return 1; 1290bf4ede01STejun Heo } 1291d565ed63STejun Heo spin_unlock(&pool->lock); 1292bbb68dfaSTejun Heo fail: 1293bbb68dfaSTejun Heo local_irq_restore(*flags); 1294bbb68dfaSTejun Heo if (work_is_canceling(work)) 1295bbb68dfaSTejun Heo return -ENOENT; 1296bbb68dfaSTejun Heo cpu_relax(); 129736e227d2STejun Heo return -EAGAIN; 1298bf4ede01STejun Heo } 1299bf4ede01STejun Heo 1300bf4ede01STejun Heo /** 1301706026c2STejun Heo * insert_work - insert a work into a pool 1302112202d9STejun Heo * @pwq: pwq @work belongs to 13034690c4abSTejun Heo * @work: work to insert 13044690c4abSTejun Heo * @head: insertion point 13054690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 13064690c4abSTejun Heo * 1307112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 1308706026c2STejun Heo * work_struct flags. 13094690c4abSTejun Heo * 13104690c4abSTejun Heo * CONTEXT: 1311d565ed63STejun Heo * spin_lock_irq(pool->lock). 1312365970a1SDavid Howells */ 1313112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 1314112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 1315b89deed3SOleg Nesterov { 1316112202d9STejun Heo struct worker_pool *pool = pwq->pool; 1317e1d8aa9fSFrederic Weisbecker 13184690c4abSTejun Heo /* we own @work, set data and link */ 1319112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 13201a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 13218864b4e5STejun Heo get_pwq(pwq); 1322e22bee78STejun Heo 1323e22bee78STejun Heo /* 1324c5aa87bbSTejun Heo * Ensure either wq_worker_sleeping() sees the above 1325c5aa87bbSTejun Heo * list_add_tail() or we see zero nr_running to avoid workers lying 1326c5aa87bbSTejun Heo * around lazily while there are works to be processed. 1327e22bee78STejun Heo */ 1328e22bee78STejun Heo smp_mb(); 1329e22bee78STejun Heo 133063d95a91STejun Heo if (__need_more_worker(pool)) 133163d95a91STejun Heo wake_up_worker(pool); 1332b89deed3SOleg Nesterov } 1333b89deed3SOleg Nesterov 1334c8efcc25STejun Heo /* 1335c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 13368d03ecfeSTejun Heo * same workqueue. 1337c8efcc25STejun Heo */ 1338c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1339c8efcc25STejun Heo { 1340c8efcc25STejun Heo struct worker *worker; 1341c8efcc25STejun Heo 13428d03ecfeSTejun Heo worker = current_wq_worker(); 1343c8efcc25STejun Heo /* 13448d03ecfeSTejun Heo * Return %true iff I'm a worker execuing a work item on @wq. If 13458d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 1346c8efcc25STejun Heo */ 1347112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 1348c8efcc25STejun Heo } 1349c8efcc25STejun Heo 1350ef557180SMike Galbraith /* 1351ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 1352ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 1353ef557180SMike Galbraith * avoid perturbing sensitive tasks. 1354ef557180SMike Galbraith */ 1355ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 1356ef557180SMike Galbraith { 1357f303fccbSTejun Heo static bool printed_dbg_warning; 1358ef557180SMike Galbraith int new_cpu; 1359ef557180SMike Galbraith 1360f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 1361ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 1362ef557180SMike Galbraith return cpu; 1363f303fccbSTejun Heo } else if (!printed_dbg_warning) { 1364f303fccbSTejun Heo pr_warn("workqueue: round-robin CPU selection forced, expect performance impact\n"); 1365f303fccbSTejun Heo printed_dbg_warning = true; 1366f303fccbSTejun Heo } 1367f303fccbSTejun Heo 1368ef557180SMike Galbraith if (cpumask_empty(wq_unbound_cpumask)) 1369ef557180SMike Galbraith return cpu; 1370ef557180SMike Galbraith 1371ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 1372ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 1373ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 1374ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 1375ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 1376ef557180SMike Galbraith return cpu; 1377ef557180SMike Galbraith } 1378ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 1379ef557180SMike Galbraith 1380ef557180SMike Galbraith return new_cpu; 1381ef557180SMike Galbraith } 1382ef557180SMike Galbraith 1383d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 13841da177e4SLinus Torvalds struct work_struct *work) 13851da177e4SLinus Torvalds { 1386112202d9STejun Heo struct pool_workqueue *pwq; 1387c9178087STejun Heo struct worker_pool *last_pool; 13881e19ffc6STejun Heo struct list_head *worklist; 13898a2e8e5dSTejun Heo unsigned int work_flags; 1390b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 13918930cabaSTejun Heo 13928930cabaSTejun Heo /* 13938930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 13948930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 13958930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 13968930cabaSTejun Heo * happen with IRQ disabled. 13978930cabaSTejun Heo */ 13988e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 13991da177e4SLinus Torvalds 1400dc186ad7SThomas Gleixner debug_work_activate(work); 14011e19ffc6STejun Heo 14029ef28a73SLi Bin /* if draining, only works from the same workqueue are allowed */ 1403618b01ebSTejun Heo if (unlikely(wq->flags & __WQ_DRAINING) && 1404c8efcc25STejun Heo WARN_ON_ONCE(!is_chained_work(wq))) 1405e41e704bSTejun Heo return; 14069e8cd2f5STejun Heo retry: 1407df2d5ae4STejun Heo if (req_cpu == WORK_CPU_UNBOUND) 1408ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 1409df2d5ae4STejun Heo 1410df2d5ae4STejun Heo /* pwq which will be used unless @work is executing elsewhere */ 1411df2d5ae4STejun Heo if (!(wq->flags & WQ_UNBOUND)) 1412c9178087STejun Heo pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 1413df2d5ae4STejun Heo else 1414df2d5ae4STejun Heo pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu)); 1415f3421797STejun Heo 141618aa9effSTejun Heo /* 1417c9178087STejun Heo * If @work was previously on a different pool, it might still be 1418c9178087STejun Heo * running there, in which case the work needs to be queued on that 1419c9178087STejun Heo * pool to guarantee non-reentrancy. 142018aa9effSTejun Heo */ 1421c9e7cf27STejun Heo last_pool = get_work_pool(work); 1422112202d9STejun Heo if (last_pool && last_pool != pwq->pool) { 142318aa9effSTejun Heo struct worker *worker; 142418aa9effSTejun Heo 1425d565ed63STejun Heo spin_lock(&last_pool->lock); 142618aa9effSTejun Heo 1427c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 142818aa9effSTejun Heo 1429112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 1430c9178087STejun Heo pwq = worker->current_pwq; 14318594fadeSLai Jiangshan } else { 143218aa9effSTejun Heo /* meh... not running there, queue here */ 1433d565ed63STejun Heo spin_unlock(&last_pool->lock); 1434112202d9STejun Heo spin_lock(&pwq->pool->lock); 143518aa9effSTejun Heo } 14368930cabaSTejun Heo } else { 1437112202d9STejun Heo spin_lock(&pwq->pool->lock); 14388930cabaSTejun Heo } 1439502ca9d8STejun Heo 14409e8cd2f5STejun Heo /* 14419e8cd2f5STejun Heo * pwq is determined and locked. For unbound pools, we could have 14429e8cd2f5STejun Heo * raced with pwq release and it could already be dead. If its 14439e8cd2f5STejun Heo * refcnt is zero, repeat pwq selection. Note that pwqs never die 1444df2d5ae4STejun Heo * without another pwq replacing it in the numa_pwq_tbl or while 1445df2d5ae4STejun Heo * work items are executing on it, so the retrying is guaranteed to 14469e8cd2f5STejun Heo * make forward-progress. 14479e8cd2f5STejun Heo */ 14489e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 14499e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 14509e8cd2f5STejun Heo spin_unlock(&pwq->pool->lock); 14519e8cd2f5STejun Heo cpu_relax(); 14529e8cd2f5STejun Heo goto retry; 14539e8cd2f5STejun Heo } 14549e8cd2f5STejun Heo /* oops */ 14559e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 14569e8cd2f5STejun Heo wq->name, cpu); 14579e8cd2f5STejun Heo } 14589e8cd2f5STejun Heo 1459112202d9STejun Heo /* pwq determined, queue */ 1460112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 1461502ca9d8STejun Heo 1462f5b2552bSDan Carpenter if (WARN_ON(!list_empty(&work->entry))) { 1463112202d9STejun Heo spin_unlock(&pwq->pool->lock); 1464f5b2552bSDan Carpenter return; 1465f5b2552bSDan Carpenter } 14661e19ffc6STejun Heo 1467112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 1468112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 14691e19ffc6STejun Heo 1470112202d9STejun Heo if (likely(pwq->nr_active < pwq->max_active)) { 1471cdadf009STejun Heo trace_workqueue_activate_work(work); 1472112202d9STejun Heo pwq->nr_active++; 1473112202d9STejun Heo worklist = &pwq->pool->worklist; 147482607adcSTejun Heo if (list_empty(worklist)) 147582607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 14768a2e8e5dSTejun Heo } else { 14778a2e8e5dSTejun Heo work_flags |= WORK_STRUCT_DELAYED; 1478112202d9STejun Heo worklist = &pwq->delayed_works; 14798a2e8e5dSTejun Heo } 14801e19ffc6STejun Heo 1481112202d9STejun Heo insert_work(pwq, work, worklist, work_flags); 14821e19ffc6STejun Heo 1483112202d9STejun Heo spin_unlock(&pwq->pool->lock); 14841da177e4SLinus Torvalds } 14851da177e4SLinus Torvalds 14860fcb78c2SRolf Eike Beer /** 1487c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1488c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1489c1a220e7SZhang Rui * @wq: workqueue to use 1490c1a220e7SZhang Rui * @work: work to queue 1491c1a220e7SZhang Rui * 1492c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1493c1a220e7SZhang Rui * can't go away. 1494d185af30SYacine Belkadi * 1495d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 1496c1a220e7SZhang Rui */ 1497d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1498d4283e93STejun Heo struct work_struct *work) 1499c1a220e7SZhang Rui { 1500d4283e93STejun Heo bool ret = false; 15018930cabaSTejun Heo unsigned long flags; 15028930cabaSTejun Heo 15038930cabaSTejun Heo local_irq_save(flags); 1504c1a220e7SZhang Rui 150522df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 15064690c4abSTejun Heo __queue_work(cpu, wq, work); 1507d4283e93STejun Heo ret = true; 1508c1a220e7SZhang Rui } 15098930cabaSTejun Heo 15108930cabaSTejun Heo local_irq_restore(flags); 1511c1a220e7SZhang Rui return ret; 1512c1a220e7SZhang Rui } 1513ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 1514c1a220e7SZhang Rui 15158c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 15161da177e4SLinus Torvalds { 15178c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 15181da177e4SLinus Torvalds 1519e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 152060c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 15211da177e4SLinus Torvalds } 15221438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 15231da177e4SLinus Torvalds 15247beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 152552bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 15261da177e4SLinus Torvalds { 15277beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 15287beb2edfSTejun Heo struct work_struct *work = &dwork->work; 15291da177e4SLinus Torvalds 1530637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 1531841b86f3SKees Cook WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 1532fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 1533fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 15347beb2edfSTejun Heo 15358852aac2STejun Heo /* 15368852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 15378852aac2STejun Heo * both optimization and correctness. The earliest @timer can 15388852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 15398852aac2STejun Heo * on that there's no such delay when @delay is 0. 15408852aac2STejun Heo */ 15418852aac2STejun Heo if (!delay) { 15428852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 15438852aac2STejun Heo return; 15448852aac2STejun Heo } 15458852aac2STejun Heo 154660c057bcSLai Jiangshan dwork->wq = wq; 15471265057fSTejun Heo dwork->cpu = cpu; 15487beb2edfSTejun Heo timer->expires = jiffies + delay; 15497beb2edfSTejun Heo 1550041bd12eSTejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 15517beb2edfSTejun Heo add_timer_on(timer, cpu); 1552041bd12eSTejun Heo else 1553041bd12eSTejun Heo add_timer(timer); 15547beb2edfSTejun Heo } 15551da177e4SLinus Torvalds 15560fcb78c2SRolf Eike Beer /** 15570fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 15580fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 15590fcb78c2SRolf Eike Beer * @wq: workqueue to use 1560af9997e4SRandy Dunlap * @dwork: work to queue 15610fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 15620fcb78c2SRolf Eike Beer * 1563d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 1564715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 1565715f1300STejun Heo * execution. 15660fcb78c2SRolf Eike Beer */ 1567d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 156852bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 15697a6bc1cdSVenkatesh Pallipadi { 157052bad64dSDavid Howells struct work_struct *work = &dwork->work; 1571d4283e93STejun Heo bool ret = false; 15728930cabaSTejun Heo unsigned long flags; 15738930cabaSTejun Heo 15748930cabaSTejun Heo /* read the comment in __queue_work() */ 15758930cabaSTejun Heo local_irq_save(flags); 15767a6bc1cdSVenkatesh Pallipadi 157722df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 15787beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 1579d4283e93STejun Heo ret = true; 15807a6bc1cdSVenkatesh Pallipadi } 15818930cabaSTejun Heo 15828930cabaSTejun Heo local_irq_restore(flags); 15837a6bc1cdSVenkatesh Pallipadi return ret; 15847a6bc1cdSVenkatesh Pallipadi } 1585ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 15861da177e4SLinus Torvalds 1587c8e55f36STejun Heo /** 15888376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 15898376fe22STejun Heo * @cpu: CPU number to execute work on 15908376fe22STejun Heo * @wq: workqueue to use 15918376fe22STejun Heo * @dwork: work to queue 15928376fe22STejun Heo * @delay: number of jiffies to wait before queueing 15938376fe22STejun Heo * 15948376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 15958376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 15968376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 15978376fe22STejun Heo * current state. 15988376fe22STejun Heo * 1599d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 16008376fe22STejun Heo * pending and its timer was modified. 16018376fe22STejun Heo * 1602e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 16038376fe22STejun Heo * See try_to_grab_pending() for details. 16048376fe22STejun Heo */ 16058376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 16068376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 16078376fe22STejun Heo { 16088376fe22STejun Heo unsigned long flags; 16098376fe22STejun Heo int ret; 16108376fe22STejun Heo 16118376fe22STejun Heo do { 16128376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 16138376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 16148376fe22STejun Heo 16158376fe22STejun Heo if (likely(ret >= 0)) { 16168376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 16178376fe22STejun Heo local_irq_restore(flags); 16188376fe22STejun Heo } 16198376fe22STejun Heo 16208376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 16218376fe22STejun Heo return ret; 16228376fe22STejun Heo } 16238376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 16248376fe22STejun Heo 162505f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 162605f0fe6bSTejun Heo { 162705f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 162805f0fe6bSTejun Heo 162905f0fe6bSTejun Heo /* read the comment in __queue_work() */ 163005f0fe6bSTejun Heo local_irq_disable(); 163105f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 163205f0fe6bSTejun Heo local_irq_enable(); 163305f0fe6bSTejun Heo } 163405f0fe6bSTejun Heo 163505f0fe6bSTejun Heo /** 163605f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 163705f0fe6bSTejun Heo * @wq: workqueue to use 163805f0fe6bSTejun Heo * @rwork: work to queue 163905f0fe6bSTejun Heo * 164005f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 164105f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 164205f0fe6bSTejun Heo * While @rwork is guarnateed to be executed after a %false return, the 164305f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 164405f0fe6bSTejun Heo */ 164505f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 164605f0fe6bSTejun Heo { 164705f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 164805f0fe6bSTejun Heo 164905f0fe6bSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 165005f0fe6bSTejun Heo rwork->wq = wq; 165105f0fe6bSTejun Heo call_rcu(&rwork->rcu, rcu_work_rcufn); 165205f0fe6bSTejun Heo return true; 165305f0fe6bSTejun Heo } 165405f0fe6bSTejun Heo 165505f0fe6bSTejun Heo return false; 165605f0fe6bSTejun Heo } 165705f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 165805f0fe6bSTejun Heo 16598376fe22STejun Heo /** 1660c8e55f36STejun Heo * worker_enter_idle - enter idle state 1661c8e55f36STejun Heo * @worker: worker which is entering idle state 1662c8e55f36STejun Heo * 1663c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 1664c8e55f36STejun Heo * necessary. 1665c8e55f36STejun Heo * 1666c8e55f36STejun Heo * LOCKING: 1667d565ed63STejun Heo * spin_lock_irq(pool->lock). 1668c8e55f36STejun Heo */ 1669c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 16701da177e4SLinus Torvalds { 1671bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1672c8e55f36STejun Heo 16736183c009STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 16746183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 16756183c009STejun Heo (worker->hentry.next || worker->hentry.pprev))) 16766183c009STejun Heo return; 1677c8e55f36STejun Heo 1678051e1850SLai Jiangshan /* can't use worker_set_flags(), also called from create_worker() */ 1679cb444766STejun Heo worker->flags |= WORKER_IDLE; 1680bd7bdd43STejun Heo pool->nr_idle++; 1681e22bee78STejun Heo worker->last_active = jiffies; 1682c8e55f36STejun Heo 1683c8e55f36STejun Heo /* idle_list is LIFO */ 1684bd7bdd43STejun Heo list_add(&worker->entry, &pool->idle_list); 1685db7bccf4STejun Heo 168663d95a91STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1687628c78e7STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1688cb444766STejun Heo 1689544ecf31STejun Heo /* 1690e8b3f8dbSLai Jiangshan * Sanity check nr_running. Because unbind_workers() releases 1691d565ed63STejun Heo * pool->lock between setting %WORKER_UNBOUND and zapping 1692628c78e7STejun Heo * nr_running, the warning may trigger spuriously. Check iff 1693628c78e7STejun Heo * unbind is not in progress. 1694544ecf31STejun Heo */ 169524647570STejun Heo WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 1696bd7bdd43STejun Heo pool->nr_workers == pool->nr_idle && 1697e19e397aSTejun Heo atomic_read(&pool->nr_running)); 1698c8e55f36STejun Heo } 1699c8e55f36STejun Heo 1700c8e55f36STejun Heo /** 1701c8e55f36STejun Heo * worker_leave_idle - leave idle state 1702c8e55f36STejun Heo * @worker: worker which is leaving idle state 1703c8e55f36STejun Heo * 1704c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 1705c8e55f36STejun Heo * 1706c8e55f36STejun Heo * LOCKING: 1707d565ed63STejun Heo * spin_lock_irq(pool->lock). 1708c8e55f36STejun Heo */ 1709c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 1710c8e55f36STejun Heo { 1711bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1712c8e55f36STejun Heo 17136183c009STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 17146183c009STejun Heo return; 1715d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1716bd7bdd43STejun Heo pool->nr_idle--; 1717c8e55f36STejun Heo list_del_init(&worker->entry); 1718c8e55f36STejun Heo } 1719c8e55f36STejun Heo 1720f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 1721c34056a3STejun Heo { 1722c34056a3STejun Heo struct worker *worker; 1723c34056a3STejun Heo 1724f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 1725c8e55f36STejun Heo if (worker) { 1726c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 1727affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 1728da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 1729e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 1730e22bee78STejun Heo worker->flags = WORKER_PREP; 1731c8e55f36STejun Heo } 1732c34056a3STejun Heo return worker; 1733c34056a3STejun Heo } 1734c34056a3STejun Heo 1735c34056a3STejun Heo /** 17364736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 17374736cbf7SLai Jiangshan * @worker: worker to be attached 17384736cbf7SLai Jiangshan * @pool: the target pool 17394736cbf7SLai Jiangshan * 17404736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 17414736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 17424736cbf7SLai Jiangshan * cpu-[un]hotplugs. 17434736cbf7SLai Jiangshan */ 17444736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 17454736cbf7SLai Jiangshan struct worker_pool *pool) 17464736cbf7SLai Jiangshan { 17471258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 17484736cbf7SLai Jiangshan 17494736cbf7SLai Jiangshan /* 17504736cbf7SLai Jiangshan * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any 17514736cbf7SLai Jiangshan * online CPUs. It'll be re-applied when any of the CPUs come up. 17524736cbf7SLai Jiangshan */ 17534736cbf7SLai Jiangshan set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask); 17544736cbf7SLai Jiangshan 17554736cbf7SLai Jiangshan /* 17561258fae7STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains 17571258fae7STejun Heo * stable across this function. See the comments above the flag 17581258fae7STejun Heo * definition for details. 17594736cbf7SLai Jiangshan */ 17604736cbf7SLai Jiangshan if (pool->flags & POOL_DISASSOCIATED) 17614736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 17624736cbf7SLai Jiangshan 17634736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 1764a2d812a2STejun Heo worker->pool = pool; 17654736cbf7SLai Jiangshan 17661258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 17674736cbf7SLai Jiangshan } 17684736cbf7SLai Jiangshan 17694736cbf7SLai Jiangshan /** 177060f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 177160f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 177260f5a4bcSLai Jiangshan * 17734736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 17744736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 17754736cbf7SLai Jiangshan * other reference to the pool. 177660f5a4bcSLai Jiangshan */ 1777a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 177860f5a4bcSLai Jiangshan { 1779a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 178060f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 178160f5a4bcSLai Jiangshan 17821258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 1783a2d812a2STejun Heo 1784da028469SLai Jiangshan list_del(&worker->node); 1785a2d812a2STejun Heo worker->pool = NULL; 1786a2d812a2STejun Heo 1787da028469SLai Jiangshan if (list_empty(&pool->workers)) 178860f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 17891258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 179060f5a4bcSLai Jiangshan 1791b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 1792b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 1793b62c0751SLai Jiangshan 179460f5a4bcSLai Jiangshan if (detach_completion) 179560f5a4bcSLai Jiangshan complete(detach_completion); 179660f5a4bcSLai Jiangshan } 179760f5a4bcSLai Jiangshan 179860f5a4bcSLai Jiangshan /** 1799c34056a3STejun Heo * create_worker - create a new workqueue worker 180063d95a91STejun Heo * @pool: pool the new worker will belong to 1801c34056a3STejun Heo * 1802051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 1803c34056a3STejun Heo * 1804c34056a3STejun Heo * CONTEXT: 1805c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 1806c34056a3STejun Heo * 1807d185af30SYacine Belkadi * Return: 1808c34056a3STejun Heo * Pointer to the newly created worker. 1809c34056a3STejun Heo */ 1810bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 1811c34056a3STejun Heo { 1812c34056a3STejun Heo struct worker *worker = NULL; 1813f3421797STejun Heo int id = -1; 1814e3c916a4STejun Heo char id_buf[16]; 1815c34056a3STejun Heo 18167cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 18177cda9aaeSLai Jiangshan id = ida_simple_get(&pool->worker_ida, 0, 0, GFP_KERNEL); 1818822d8405STejun Heo if (id < 0) 1819c34056a3STejun Heo goto fail; 1820c34056a3STejun Heo 1821f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 1822c34056a3STejun Heo if (!worker) 1823c34056a3STejun Heo goto fail; 1824c34056a3STejun Heo 1825c34056a3STejun Heo worker->id = id; 1826c34056a3STejun Heo 182729c91e99STejun Heo if (pool->cpu >= 0) 1828e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 1829e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 1830f3421797STejun Heo else 1831e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 1832e3c916a4STejun Heo 1833f3f90ad4STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, pool->node, 1834e3c916a4STejun Heo "kworker/%s", id_buf); 1835c34056a3STejun Heo if (IS_ERR(worker->task)) 1836c34056a3STejun Heo goto fail; 1837c34056a3STejun Heo 183891151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 183925834c73SPeter Zijlstra kthread_bind_mask(worker->task, pool->attrs->cpumask); 184091151228SOleg Nesterov 1841da028469SLai Jiangshan /* successful, attach the worker to the pool */ 18424736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 1843822d8405STejun Heo 1844051e1850SLai Jiangshan /* start the newly created worker */ 1845051e1850SLai Jiangshan spin_lock_irq(&pool->lock); 1846051e1850SLai Jiangshan worker->pool->nr_workers++; 1847051e1850SLai Jiangshan worker_enter_idle(worker); 1848051e1850SLai Jiangshan wake_up_process(worker->task); 1849051e1850SLai Jiangshan spin_unlock_irq(&pool->lock); 1850051e1850SLai Jiangshan 1851c34056a3STejun Heo return worker; 1852822d8405STejun Heo 1853c34056a3STejun Heo fail: 18549625ab17SLai Jiangshan if (id >= 0) 18557cda9aaeSLai Jiangshan ida_simple_remove(&pool->worker_ida, id); 1856c34056a3STejun Heo kfree(worker); 1857c34056a3STejun Heo return NULL; 1858c34056a3STejun Heo } 1859c34056a3STejun Heo 1860c34056a3STejun Heo /** 1861c34056a3STejun Heo * destroy_worker - destroy a workqueue worker 1862c34056a3STejun Heo * @worker: worker to be destroyed 1863c34056a3STejun Heo * 186473eb7fe7SLai Jiangshan * Destroy @worker and adjust @pool stats accordingly. The worker should 186573eb7fe7SLai Jiangshan * be idle. 1866c8e55f36STejun Heo * 1867c8e55f36STejun Heo * CONTEXT: 186860f5a4bcSLai Jiangshan * spin_lock_irq(pool->lock). 1869c34056a3STejun Heo */ 1870c34056a3STejun Heo static void destroy_worker(struct worker *worker) 1871c34056a3STejun Heo { 1872bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1873c34056a3STejun Heo 1874cd549687STejun Heo lockdep_assert_held(&pool->lock); 1875cd549687STejun Heo 1876c34056a3STejun Heo /* sanity check frenzy */ 18776183c009STejun Heo if (WARN_ON(worker->current_work) || 187873eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 187973eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 18806183c009STejun Heo return; 1881c34056a3STejun Heo 1882bd7bdd43STejun Heo pool->nr_workers--; 1883bd7bdd43STejun Heo pool->nr_idle--; 1884c8e55f36STejun Heo 1885c8e55f36STejun Heo list_del_init(&worker->entry); 1886cb444766STejun Heo worker->flags |= WORKER_DIE; 188760f5a4bcSLai Jiangshan wake_up_process(worker->task); 1888c34056a3STejun Heo } 1889c34056a3STejun Heo 189032a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 1891e22bee78STejun Heo { 189232a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 1893e22bee78STejun Heo 1894d565ed63STejun Heo spin_lock_irq(&pool->lock); 1895e22bee78STejun Heo 18963347fc9fSLai Jiangshan while (too_many_workers(pool)) { 1897e22bee78STejun Heo struct worker *worker; 1898e22bee78STejun Heo unsigned long expires; 1899e22bee78STejun Heo 1900e22bee78STejun Heo /* idle_list is kept in LIFO order, check the last one */ 190163d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1902e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1903e22bee78STejun Heo 19043347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 190563d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 19063347fc9fSLai Jiangshan break; 1907e22bee78STejun Heo } 19083347fc9fSLai Jiangshan 19093347fc9fSLai Jiangshan destroy_worker(worker); 1910e22bee78STejun Heo } 1911e22bee78STejun Heo 1912d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1913e22bee78STejun Heo } 1914e22bee78STejun Heo 1915493a1724STejun Heo static void send_mayday(struct work_struct *work) 1916e22bee78STejun Heo { 1917112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1918112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 1919493a1724STejun Heo 19202e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 1921e22bee78STejun Heo 1922493008a8STejun Heo if (!wq->rescuer) 1923493a1724STejun Heo return; 1924e22bee78STejun Heo 1925e22bee78STejun Heo /* mayday mayday mayday */ 1926493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 192777668c8bSLai Jiangshan /* 192877668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 192977668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 193077668c8bSLai Jiangshan * rescuer is done with it. 193177668c8bSLai Jiangshan */ 193277668c8bSLai Jiangshan get_pwq(pwq); 1933493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 1934e22bee78STejun Heo wake_up_process(wq->rescuer->task); 1935493a1724STejun Heo } 1936e22bee78STejun Heo } 1937e22bee78STejun Heo 193832a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 1939e22bee78STejun Heo { 194032a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 1941e22bee78STejun Heo struct work_struct *work; 1942e22bee78STejun Heo 1943b2d82909STejun Heo spin_lock_irq(&pool->lock); 1944b2d82909STejun Heo spin_lock(&wq_mayday_lock); /* for wq->maydays */ 1945e22bee78STejun Heo 194663d95a91STejun Heo if (need_to_create_worker(pool)) { 1947e22bee78STejun Heo /* 1948e22bee78STejun Heo * We've been trying to create a new worker but 1949e22bee78STejun Heo * haven't been successful. We might be hitting an 1950e22bee78STejun Heo * allocation deadlock. Send distress signals to 1951e22bee78STejun Heo * rescuers. 1952e22bee78STejun Heo */ 195363d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 1954e22bee78STejun Heo send_mayday(work); 1955e22bee78STejun Heo } 1956e22bee78STejun Heo 1957b2d82909STejun Heo spin_unlock(&wq_mayday_lock); 1958b2d82909STejun Heo spin_unlock_irq(&pool->lock); 1959e22bee78STejun Heo 196063d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 1961e22bee78STejun Heo } 1962e22bee78STejun Heo 1963e22bee78STejun Heo /** 1964e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 196563d95a91STejun Heo * @pool: pool to create a new worker for 1966e22bee78STejun Heo * 196763d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 1968e22bee78STejun Heo * have at least one idle worker on return from this function. If 1969e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 197063d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 1971e22bee78STejun Heo * possible allocation deadlock. 1972e22bee78STejun Heo * 1973c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 1974c5aa87bbSTejun Heo * may_start_working() %true. 1975e22bee78STejun Heo * 1976e22bee78STejun Heo * LOCKING: 1977d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1978e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 1979e22bee78STejun Heo * manager. 1980e22bee78STejun Heo */ 198129187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 1982d565ed63STejun Heo __releases(&pool->lock) 1983d565ed63STejun Heo __acquires(&pool->lock) 1984e22bee78STejun Heo { 1985e22bee78STejun Heo restart: 1986d565ed63STejun Heo spin_unlock_irq(&pool->lock); 19879f9c2364STejun Heo 1988e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 198963d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 1990e22bee78STejun Heo 1991e22bee78STejun Heo while (true) { 1992051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 1993e22bee78STejun Heo break; 1994e22bee78STejun Heo 1995e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 19969f9c2364STejun Heo 199763d95a91STejun Heo if (!need_to_create_worker(pool)) 1998e22bee78STejun Heo break; 1999e22bee78STejun Heo } 2000e22bee78STejun Heo 200163d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 2002d565ed63STejun Heo spin_lock_irq(&pool->lock); 2003051e1850SLai Jiangshan /* 2004051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 2005051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 2006051e1850SLai Jiangshan * already become busy. 2007051e1850SLai Jiangshan */ 200863d95a91STejun Heo if (need_to_create_worker(pool)) 2009e22bee78STejun Heo goto restart; 2010e22bee78STejun Heo } 2011e22bee78STejun Heo 2012e22bee78STejun Heo /** 2013e22bee78STejun Heo * manage_workers - manage worker pool 2014e22bee78STejun Heo * @worker: self 2015e22bee78STejun Heo * 2016706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 2017e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 2018706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 2019e22bee78STejun Heo * 2020e22bee78STejun Heo * The caller can safely start processing works on false return. On 2021e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 2022e22bee78STejun Heo * and may_start_working() is true. 2023e22bee78STejun Heo * 2024e22bee78STejun Heo * CONTEXT: 2025d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 2026e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 2027e22bee78STejun Heo * 2028d185af30SYacine Belkadi * Return: 202929187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 203029187a9eSTejun Heo * start processing works, %true if management function was performed and 203129187a9eSTejun Heo * the conditions that the caller verified before calling the function may 203229187a9eSTejun Heo * no longer be true. 2033e22bee78STejun Heo */ 2034e22bee78STejun Heo static bool manage_workers(struct worker *worker) 2035e22bee78STejun Heo { 203663d95a91STejun Heo struct worker_pool *pool = worker->pool; 2037e22bee78STejun Heo 2038692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 203929187a9eSTejun Heo return false; 2040692b4825STejun Heo 2041692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 20422607d7a6STejun Heo pool->manager = worker; 2043e22bee78STejun Heo 204429187a9eSTejun Heo maybe_create_worker(pool); 2045e22bee78STejun Heo 20462607d7a6STejun Heo pool->manager = NULL; 2047692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 2048692b4825STejun Heo wake_up(&wq_manager_wait); 204929187a9eSTejun Heo return true; 2050e22bee78STejun Heo } 2051e22bee78STejun Heo 2052a62428c0STejun Heo /** 2053a62428c0STejun Heo * process_one_work - process single work 2054c34056a3STejun Heo * @worker: self 2055a62428c0STejun Heo * @work: work to process 2056a62428c0STejun Heo * 2057a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2058a62428c0STejun Heo * process a single work including synchronization against and 2059a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2060a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2061a62428c0STejun Heo * call this function to process a work. 2062a62428c0STejun Heo * 2063a62428c0STejun Heo * CONTEXT: 2064d565ed63STejun Heo * spin_lock_irq(pool->lock) which is released and regrabbed. 2065a62428c0STejun Heo */ 2066c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 2067d565ed63STejun Heo __releases(&pool->lock) 2068d565ed63STejun Heo __acquires(&pool->lock) 20691da177e4SLinus Torvalds { 2070112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2071bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2072112202d9STejun Heo bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE; 207373f53c4aSTejun Heo int work_color; 20747e11629dSTejun Heo struct worker *collision; 20754e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 20764e6045f1SJohannes Berg /* 2077a62428c0STejun Heo * It is permissible to free the struct work_struct from 2078a62428c0STejun Heo * inside the function that is called from it, this we need to 2079a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2080a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2081a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 20824e6045f1SJohannes Berg */ 20834d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 20844d82a1deSPeter Zijlstra 20854d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 20864e6045f1SJohannes Berg #endif 2087807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 208885327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 2089ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 209025511a47STejun Heo 20917e11629dSTejun Heo /* 20927e11629dSTejun Heo * A single work shouldn't be executed concurrently by 20937e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 20947e11629dSTejun Heo * already processing the work. If so, defer the work to the 20957e11629dSTejun Heo * currently executing one. 20967e11629dSTejun Heo */ 2097c9e7cf27STejun Heo collision = find_worker_executing_work(pool, work); 20987e11629dSTejun Heo if (unlikely(collision)) { 20997e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 21007e11629dSTejun Heo return; 21017e11629dSTejun Heo } 21021da177e4SLinus Torvalds 21038930cabaSTejun Heo /* claim and dequeue */ 21041da177e4SLinus Torvalds debug_work_deactivate(work); 2105c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 2106c34056a3STejun Heo worker->current_work = work; 2107a2c1c57bSTejun Heo worker->current_func = work->func; 2108112202d9STejun Heo worker->current_pwq = pwq; 210973f53c4aSTejun Heo work_color = get_work_color(work); 21107a22ad75STejun Heo 21118bf89593STejun Heo /* 21128bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 21138bf89593STejun Heo * overridden through set_worker_desc(). 21148bf89593STejun Heo */ 21158bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 21168bf89593STejun Heo 2117a62428c0STejun Heo list_del_init(&work->entry); 2118a62428c0STejun Heo 2119649027d7STejun Heo /* 2120228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 2121228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 2122228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 2123228f1d00SLai Jiangshan * execution of the pending work items. 2124fb0e7bebSTejun Heo */ 2125fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2126228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 2127fb0e7bebSTejun Heo 2128974271c4STejun Heo /* 2129a489a03eSLai Jiangshan * Wake up another worker if necessary. The condition is always 2130a489a03eSLai Jiangshan * false for normal per-cpu workers since nr_running would always 2131a489a03eSLai Jiangshan * be >= 1 at this point. This is used to chain execution of the 2132a489a03eSLai Jiangshan * pending work items for WORKER_NOT_RUNNING workers such as the 2133228f1d00SLai Jiangshan * UNBOUND and CPU_INTENSIVE ones. 2134974271c4STejun Heo */ 2135a489a03eSLai Jiangshan if (need_more_worker(pool)) 213663d95a91STejun Heo wake_up_worker(pool); 2137974271c4STejun Heo 21388930cabaSTejun Heo /* 21397c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 2140d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 214123657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 214223657bb1STejun Heo * disabled. 21438930cabaSTejun Heo */ 21447c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 21451da177e4SLinus Torvalds 2146d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2147365970a1SDavid Howells 2148a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 21493295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2150e6f3faa7SPeter Zijlstra /* 2151f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 2152f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 2153e6f3faa7SPeter Zijlstra * 2154e6f3faa7SPeter Zijlstra * However, that would result in: 2155e6f3faa7SPeter Zijlstra * 2156e6f3faa7SPeter Zijlstra * A(W1) 2157e6f3faa7SPeter Zijlstra * WFC(C) 2158e6f3faa7SPeter Zijlstra * A(W1) 2159e6f3faa7SPeter Zijlstra * C(C) 2160e6f3faa7SPeter Zijlstra * 2161e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 2162e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 2163e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 2164f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 2165e6f3faa7SPeter Zijlstra * these locks. 2166e6f3faa7SPeter Zijlstra * 2167e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 2168e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 2169e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 2170e6f3faa7SPeter Zijlstra */ 2171f52be570SPeter Zijlstra lockdep_invariant_state(true); 2172e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 2173a2c1c57bSTejun Heo worker->current_func(work); 2174e36c886aSArjan van de Ven /* 2175e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2176e36c886aSArjan van de Ven * point will only record its address. 2177e36c886aSArjan van de Ven */ 2178e36c886aSArjan van de Ven trace_workqueue_execute_end(work); 21793295f0efSIngo Molnar lock_map_release(&lockdep_map); 2180112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 21811da177e4SLinus Torvalds 2182d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2183044c782cSValentin Ilie pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n" 2184044c782cSValentin Ilie " last function: %pf\n", 2185a2c1c57bSTejun Heo current->comm, preempt_count(), task_pid_nr(current), 2186a2c1c57bSTejun Heo worker->current_func); 2187d5abe669SPeter Zijlstra debug_show_held_locks(current); 2188d5abe669SPeter Zijlstra dump_stack(); 2189d5abe669SPeter Zijlstra } 2190d5abe669SPeter Zijlstra 2191b22ce278STejun Heo /* 2192b22ce278STejun Heo * The following prevents a kworker from hogging CPU on !PREEMPT 2193b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 2194b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 2195b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 2196789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 2197789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 2198b22ce278STejun Heo */ 2199a7e6425eSPaul E. McKenney cond_resched(); 2200b22ce278STejun Heo 2201d565ed63STejun Heo spin_lock_irq(&pool->lock); 2202a62428c0STejun Heo 2203fb0e7bebSTejun Heo /* clear cpu intensive status */ 2204fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2205fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2206fb0e7bebSTejun Heo 2207*1b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 2208*1b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 2209*1b69ac6bSJohannes Weiner 2210a62428c0STejun Heo /* we're done with it, release */ 221142f8570fSSasha Levin hash_del(&worker->hentry); 2212c34056a3STejun Heo worker->current_work = NULL; 2213a2c1c57bSTejun Heo worker->current_func = NULL; 2214112202d9STejun Heo worker->current_pwq = NULL; 2215112202d9STejun Heo pwq_dec_nr_in_flight(pwq, work_color); 22161da177e4SLinus Torvalds } 22171da177e4SLinus Torvalds 2218affee4b2STejun Heo /** 2219affee4b2STejun Heo * process_scheduled_works - process scheduled works 2220affee4b2STejun Heo * @worker: self 2221affee4b2STejun Heo * 2222affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2223affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2224affee4b2STejun Heo * fetches a work from the top and executes it. 2225affee4b2STejun Heo * 2226affee4b2STejun Heo * CONTEXT: 2227d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 2228affee4b2STejun Heo * multiple times. 2229affee4b2STejun Heo */ 2230affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 22311da177e4SLinus Torvalds { 2232affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2233affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2234a62428c0STejun Heo struct work_struct, entry); 2235c34056a3STejun Heo process_one_work(worker, work); 2236a62428c0STejun Heo } 22371da177e4SLinus Torvalds } 22381da177e4SLinus Torvalds 2239197f6accSTejun Heo static void set_pf_worker(bool val) 2240197f6accSTejun Heo { 2241197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 2242197f6accSTejun Heo if (val) 2243197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 2244197f6accSTejun Heo else 2245197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 2246197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 2247197f6accSTejun Heo } 2248197f6accSTejun Heo 22494690c4abSTejun Heo /** 22504690c4abSTejun Heo * worker_thread - the worker thread function 2251c34056a3STejun Heo * @__worker: self 22524690c4abSTejun Heo * 2253c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 2254c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 2255c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 2256c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 2257c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 2258d185af30SYacine Belkadi * 2259d185af30SYacine Belkadi * Return: 0 22604690c4abSTejun Heo */ 2261c34056a3STejun Heo static int worker_thread(void *__worker) 22621da177e4SLinus Torvalds { 2263c34056a3STejun Heo struct worker *worker = __worker; 2264bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 22651da177e4SLinus Torvalds 2266e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2267197f6accSTejun Heo set_pf_worker(true); 2268c8e55f36STejun Heo woke_up: 2269d565ed63STejun Heo spin_lock_irq(&pool->lock); 2270affee4b2STejun Heo 2271a9ab775bSTejun Heo /* am I supposed to die? */ 2272a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 2273d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2274a9ab775bSTejun Heo WARN_ON_ONCE(!list_empty(&worker->entry)); 2275197f6accSTejun Heo set_pf_worker(false); 227660f5a4bcSLai Jiangshan 227760f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 22787cda9aaeSLai Jiangshan ida_simple_remove(&pool->worker_ida, worker->id); 2279a2d812a2STejun Heo worker_detach_from_pool(worker); 228060f5a4bcSLai Jiangshan kfree(worker); 2281c8e55f36STejun Heo return 0; 2282c8e55f36STejun Heo } 2283c8e55f36STejun Heo 2284c8e55f36STejun Heo worker_leave_idle(worker); 2285db7bccf4STejun Heo recheck: 2286e22bee78STejun Heo /* no more worker necessary? */ 228763d95a91STejun Heo if (!need_more_worker(pool)) 2288e22bee78STejun Heo goto sleep; 2289e22bee78STejun Heo 2290e22bee78STejun Heo /* do we need to manage? */ 229163d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2292e22bee78STejun Heo goto recheck; 2293e22bee78STejun Heo 2294c8e55f36STejun Heo /* 2295c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2296c8e55f36STejun Heo * preparing to process a work or actually processing it. 2297c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2298c8e55f36STejun Heo */ 22996183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 2300c8e55f36STejun Heo 2301e22bee78STejun Heo /* 2302a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 2303a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 2304a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 2305a9ab775bSTejun Heo * management if applicable and concurrency management is restored 2306a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 2307e22bee78STejun Heo */ 2308a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 2309e22bee78STejun Heo 2310e22bee78STejun Heo do { 2311affee4b2STejun Heo struct work_struct *work = 2312bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2313affee4b2STejun Heo struct work_struct, entry); 2314affee4b2STejun Heo 231582607adcSTejun Heo pool->watchdog_ts = jiffies; 231682607adcSTejun Heo 2317c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2318affee4b2STejun Heo /* optimization path, not strictly necessary */ 2319affee4b2STejun Heo process_one_work(worker, work); 2320affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2321affee4b2STejun Heo process_scheduled_works(worker); 2322affee4b2STejun Heo } else { 2323c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2324affee4b2STejun Heo process_scheduled_works(worker); 2325affee4b2STejun Heo } 232663d95a91STejun Heo } while (keep_working(pool)); 2327affee4b2STejun Heo 2328228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 2329d313dd85STejun Heo sleep: 2330c8e55f36STejun Heo /* 2331d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 2332d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 2333d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 2334d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 2335d565ed63STejun Heo * event. 2336c8e55f36STejun Heo */ 2337c8e55f36STejun Heo worker_enter_idle(worker); 2338c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 2339d565ed63STejun Heo spin_unlock_irq(&pool->lock); 23401da177e4SLinus Torvalds schedule(); 2341c8e55f36STejun Heo goto woke_up; 23421da177e4SLinus Torvalds } 23431da177e4SLinus Torvalds 2344e22bee78STejun Heo /** 2345e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2346111c225aSTejun Heo * @__rescuer: self 2347e22bee78STejun Heo * 2348e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2349493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 2350e22bee78STejun Heo * 2351706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 2352e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2353e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2354e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2355e22bee78STejun Heo * the problem rescuer solves. 2356e22bee78STejun Heo * 2357706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 2358706026c2STejun Heo * workqueues which have works queued on the pool and let them process 2359e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2360e22bee78STejun Heo * 2361e22bee78STejun Heo * This should happen rarely. 2362d185af30SYacine Belkadi * 2363d185af30SYacine Belkadi * Return: 0 2364e22bee78STejun Heo */ 2365111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 2366e22bee78STejun Heo { 2367111c225aSTejun Heo struct worker *rescuer = __rescuer; 2368111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 2369e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 23704d595b86SLai Jiangshan bool should_stop; 2371e22bee78STejun Heo 2372e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2373111c225aSTejun Heo 2374111c225aSTejun Heo /* 2375111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 2376111c225aSTejun Heo * doesn't participate in concurrency management. 2377111c225aSTejun Heo */ 2378197f6accSTejun Heo set_pf_worker(true); 2379e22bee78STejun Heo repeat: 2380c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 23811da177e4SLinus Torvalds 23824d595b86SLai Jiangshan /* 23834d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 23844d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 23854d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 23864d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 23874d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 23884d595b86SLai Jiangshan * list is always empty on exit. 23894d595b86SLai Jiangshan */ 23904d595b86SLai Jiangshan should_stop = kthread_should_stop(); 23911da177e4SLinus Torvalds 2392493a1724STejun Heo /* see whether any pwq is asking for help */ 23932e109a28STejun Heo spin_lock_irq(&wq_mayday_lock); 2394493a1724STejun Heo 2395493a1724STejun Heo while (!list_empty(&wq->maydays)) { 2396493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 2397493a1724STejun Heo struct pool_workqueue, mayday_node); 2398112202d9STejun Heo struct worker_pool *pool = pwq->pool; 2399e22bee78STejun Heo struct work_struct *work, *n; 240082607adcSTejun Heo bool first = true; 2401e22bee78STejun Heo 2402e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2403493a1724STejun Heo list_del_init(&pwq->mayday_node); 2404493a1724STejun Heo 24052e109a28STejun Heo spin_unlock_irq(&wq_mayday_lock); 2406e22bee78STejun Heo 240751697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 240851697d39SLai Jiangshan 240951697d39SLai Jiangshan spin_lock_irq(&pool->lock); 2410e22bee78STejun Heo 2411e22bee78STejun Heo /* 2412e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2413e22bee78STejun Heo * process'em. 2414e22bee78STejun Heo */ 24150479c8c5STejun Heo WARN_ON_ONCE(!list_empty(scheduled)); 241682607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 241782607adcSTejun Heo if (get_work_pwq(work) == pwq) { 241882607adcSTejun Heo if (first) 241982607adcSTejun Heo pool->watchdog_ts = jiffies; 2420e22bee78STejun Heo move_linked_works(work, scheduled, &n); 242182607adcSTejun Heo } 242282607adcSTejun Heo first = false; 242382607adcSTejun Heo } 2424e22bee78STejun Heo 2425008847f6SNeilBrown if (!list_empty(scheduled)) { 2426e22bee78STejun Heo process_scheduled_works(rescuer); 24277576958aSTejun Heo 24287576958aSTejun Heo /* 2429008847f6SNeilBrown * The above execution of rescued work items could 2430008847f6SNeilBrown * have created more to rescue through 2431008847f6SNeilBrown * pwq_activate_first_delayed() or chained 2432008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 2433008847f6SNeilBrown * that such back-to-back work items, which may be 2434008847f6SNeilBrown * being used to relieve memory pressure, don't 2435008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 2436008847f6SNeilBrown */ 2437008847f6SNeilBrown if (need_to_create_worker(pool)) { 2438008847f6SNeilBrown spin_lock(&wq_mayday_lock); 2439008847f6SNeilBrown get_pwq(pwq); 2440008847f6SNeilBrown list_move_tail(&pwq->mayday_node, &wq->maydays); 2441008847f6SNeilBrown spin_unlock(&wq_mayday_lock); 2442008847f6SNeilBrown } 2443008847f6SNeilBrown } 2444008847f6SNeilBrown 2445008847f6SNeilBrown /* 244677668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 244713b1d625SLai Jiangshan * go away while we're still attached to it. 244877668c8bSLai Jiangshan */ 244977668c8bSLai Jiangshan put_pwq(pwq); 245077668c8bSLai Jiangshan 245177668c8bSLai Jiangshan /* 2452d8ca83e6SLai Jiangshan * Leave this pool. If need_more_worker() is %true, notify a 24537576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 24547576958aSTejun Heo * and stalling the execution. 24557576958aSTejun Heo */ 2456d8ca83e6SLai Jiangshan if (need_more_worker(pool)) 245763d95a91STejun Heo wake_up_worker(pool); 24587576958aSTejun Heo 245913b1d625SLai Jiangshan spin_unlock_irq(&pool->lock); 246013b1d625SLai Jiangshan 2461a2d812a2STejun Heo worker_detach_from_pool(rescuer); 246213b1d625SLai Jiangshan 246313b1d625SLai Jiangshan spin_lock_irq(&wq_mayday_lock); 24641da177e4SLinus Torvalds } 24651da177e4SLinus Torvalds 24662e109a28STejun Heo spin_unlock_irq(&wq_mayday_lock); 2467493a1724STejun Heo 24684d595b86SLai Jiangshan if (should_stop) { 24694d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 2470197f6accSTejun Heo set_pf_worker(false); 24714d595b86SLai Jiangshan return 0; 24724d595b86SLai Jiangshan } 24734d595b86SLai Jiangshan 2474111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 2475111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 2476e22bee78STejun Heo schedule(); 2477e22bee78STejun Heo goto repeat; 24781da177e4SLinus Torvalds } 24791da177e4SLinus Torvalds 2480fca839c0STejun Heo /** 2481fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 2482fca839c0STejun Heo * @target_wq: workqueue being flushed 2483fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 2484fca839c0STejun Heo * 2485fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 2486fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 2487fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 2488fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 2489fca839c0STejun Heo * a deadlock. 2490fca839c0STejun Heo */ 2491fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 2492fca839c0STejun Heo struct work_struct *target_work) 2493fca839c0STejun Heo { 2494fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 2495fca839c0STejun Heo struct worker *worker; 2496fca839c0STejun Heo 2497fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 2498fca839c0STejun Heo return; 2499fca839c0STejun Heo 2500fca839c0STejun Heo worker = current_wq_worker(); 2501fca839c0STejun Heo 2502fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 2503fca839c0STejun Heo "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%pf", 2504fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 250523d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 250623d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 2507fca839c0STejun Heo "workqueue: WQ_MEM_RECLAIM %s:%pf is flushing !WQ_MEM_RECLAIM %s:%pf", 2508fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 2509fca839c0STejun Heo target_wq->name, target_func); 2510fca839c0STejun Heo } 2511fca839c0STejun Heo 2512fc2e4d70SOleg Nesterov struct wq_barrier { 2513fc2e4d70SOleg Nesterov struct work_struct work; 2514fc2e4d70SOleg Nesterov struct completion done; 25152607d7a6STejun Heo struct task_struct *task; /* purely informational */ 2516fc2e4d70SOleg Nesterov }; 2517fc2e4d70SOleg Nesterov 2518fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2519fc2e4d70SOleg Nesterov { 2520fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2521fc2e4d70SOleg Nesterov complete(&barr->done); 2522fc2e4d70SOleg Nesterov } 2523fc2e4d70SOleg Nesterov 25244690c4abSTejun Heo /** 25254690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 2526112202d9STejun Heo * @pwq: pwq to insert barrier into 25274690c4abSTejun Heo * @barr: wq_barrier to insert 2528affee4b2STejun Heo * @target: target work to attach @barr to 2529affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 25304690c4abSTejun Heo * 2531affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2532affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2533affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2534affee4b2STejun Heo * cpu. 2535affee4b2STejun Heo * 2536affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2537affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2538affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2539affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2540affee4b2STejun Heo * after a work with LINKED flag set. 2541affee4b2STejun Heo * 2542affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2543112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 25444690c4abSTejun Heo * 25454690c4abSTejun Heo * CONTEXT: 2546d565ed63STejun Heo * spin_lock_irq(pool->lock). 25474690c4abSTejun Heo */ 2548112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 2549affee4b2STejun Heo struct wq_barrier *barr, 2550affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2551fc2e4d70SOleg Nesterov { 2552affee4b2STejun Heo struct list_head *head; 2553affee4b2STejun Heo unsigned int linked = 0; 2554affee4b2STejun Heo 2555dc186ad7SThomas Gleixner /* 2556d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 2557dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2558dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2559dc186ad7SThomas Gleixner * might deadlock. 2560dc186ad7SThomas Gleixner */ 2561ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 256222df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 256352fa5bc5SBoqun Feng 2564fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 2565fd1a5b04SByungchul Park 25662607d7a6STejun Heo barr->task = current; 256783c22520SOleg Nesterov 2568affee4b2STejun Heo /* 2569affee4b2STejun Heo * If @target is currently being executed, schedule the 2570affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2571affee4b2STejun Heo */ 2572affee4b2STejun Heo if (worker) 2573affee4b2STejun Heo head = worker->scheduled.next; 2574affee4b2STejun Heo else { 2575affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2576affee4b2STejun Heo 2577affee4b2STejun Heo head = target->entry.next; 2578affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2579affee4b2STejun Heo linked = *bits & WORK_STRUCT_LINKED; 2580affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2581affee4b2STejun Heo } 2582affee4b2STejun Heo 2583dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2584112202d9STejun Heo insert_work(pwq, &barr->work, head, 2585affee4b2STejun Heo work_color_to_flags(WORK_NO_COLOR) | linked); 2586fc2e4d70SOleg Nesterov } 2587fc2e4d70SOleg Nesterov 258873f53c4aSTejun Heo /** 2589112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 259073f53c4aSTejun Heo * @wq: workqueue being flushed 259173f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 259273f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 259373f53c4aSTejun Heo * 2594112202d9STejun Heo * Prepare pwqs for workqueue flushing. 259573f53c4aSTejun Heo * 2596112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 2597112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 2598112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 2599112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 2600112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 260173f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 260273f53c4aSTejun Heo * 260373f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 260473f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 260573f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 260673f53c4aSTejun Heo * is returned. 260773f53c4aSTejun Heo * 2608112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 260973f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 261073f53c4aSTejun Heo * advanced to @work_color. 261173f53c4aSTejun Heo * 261273f53c4aSTejun Heo * CONTEXT: 26133c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 261473f53c4aSTejun Heo * 2615d185af30SYacine Belkadi * Return: 261673f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 261773f53c4aSTejun Heo * otherwise. 261873f53c4aSTejun Heo */ 2619112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 262073f53c4aSTejun Heo int flush_color, int work_color) 26211da177e4SLinus Torvalds { 262273f53c4aSTejun Heo bool wait = false; 262349e3cf44STejun Heo struct pool_workqueue *pwq; 26241da177e4SLinus Torvalds 262573f53c4aSTejun Heo if (flush_color >= 0) { 26266183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 2627112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 2628dc186ad7SThomas Gleixner } 262914441960SOleg Nesterov 263049e3cf44STejun Heo for_each_pwq(pwq, wq) { 2631112202d9STejun Heo struct worker_pool *pool = pwq->pool; 26321da177e4SLinus Torvalds 2633b09f4fd3SLai Jiangshan spin_lock_irq(&pool->lock); 263473f53c4aSTejun Heo 263573f53c4aSTejun Heo if (flush_color >= 0) { 26366183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 263773f53c4aSTejun Heo 2638112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 2639112202d9STejun Heo pwq->flush_color = flush_color; 2640112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 264173f53c4aSTejun Heo wait = true; 26421da177e4SLinus Torvalds } 264373f53c4aSTejun Heo } 264473f53c4aSTejun Heo 264573f53c4aSTejun Heo if (work_color >= 0) { 26466183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 2647112202d9STejun Heo pwq->work_color = work_color; 264873f53c4aSTejun Heo } 264973f53c4aSTejun Heo 2650b09f4fd3SLai Jiangshan spin_unlock_irq(&pool->lock); 26511da177e4SLinus Torvalds } 26521da177e4SLinus Torvalds 2653112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 265473f53c4aSTejun Heo complete(&wq->first_flusher->done); 265573f53c4aSTejun Heo 265673f53c4aSTejun Heo return wait; 265783c22520SOleg Nesterov } 26581da177e4SLinus Torvalds 26590fcb78c2SRolf Eike Beer /** 26601da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 26610fcb78c2SRolf Eike Beer * @wq: workqueue to flush 26621da177e4SLinus Torvalds * 2663c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 2664c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 26651da177e4SLinus Torvalds */ 26667ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 26671da177e4SLinus Torvalds { 266873f53c4aSTejun Heo struct wq_flusher this_flusher = { 266973f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 267073f53c4aSTejun Heo .flush_color = -1, 2671fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 267273f53c4aSTejun Heo }; 267373f53c4aSTejun Heo int next_color; 2674b1f4ec17SOleg Nesterov 26753347fa09STejun Heo if (WARN_ON(!wq_online)) 26763347fa09STejun Heo return; 26773347fa09STejun Heo 267887915adcSJohannes Berg lock_map_acquire(&wq->lockdep_map); 267987915adcSJohannes Berg lock_map_release(&wq->lockdep_map); 268087915adcSJohannes Berg 26813c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 268273f53c4aSTejun Heo 268373f53c4aSTejun Heo /* 268473f53c4aSTejun Heo * Start-to-wait phase 268573f53c4aSTejun Heo */ 268673f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 268773f53c4aSTejun Heo 268873f53c4aSTejun Heo if (next_color != wq->flush_color) { 268973f53c4aSTejun Heo /* 269073f53c4aSTejun Heo * Color space is not full. The current work_color 269173f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 269273f53c4aSTejun Heo * by one. 269373f53c4aSTejun Heo */ 26946183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 269573f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 269673f53c4aSTejun Heo wq->work_color = next_color; 269773f53c4aSTejun Heo 269873f53c4aSTejun Heo if (!wq->first_flusher) { 269973f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 27006183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 270173f53c4aSTejun Heo 270273f53c4aSTejun Heo wq->first_flusher = &this_flusher; 270373f53c4aSTejun Heo 2704112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 270573f53c4aSTejun Heo wq->work_color)) { 270673f53c4aSTejun Heo /* nothing to flush, done */ 270773f53c4aSTejun Heo wq->flush_color = next_color; 270873f53c4aSTejun Heo wq->first_flusher = NULL; 270973f53c4aSTejun Heo goto out_unlock; 271073f53c4aSTejun Heo } 271173f53c4aSTejun Heo } else { 271273f53c4aSTejun Heo /* wait in queue */ 27136183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 271473f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 2715112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 271673f53c4aSTejun Heo } 271773f53c4aSTejun Heo } else { 271873f53c4aSTejun Heo /* 271973f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 272073f53c4aSTejun Heo * The next flush completion will assign us 272173f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 272273f53c4aSTejun Heo */ 272373f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 272473f53c4aSTejun Heo } 272573f53c4aSTejun Heo 2726fca839c0STejun Heo check_flush_dependency(wq, NULL); 2727fca839c0STejun Heo 27283c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 272973f53c4aSTejun Heo 273073f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 273173f53c4aSTejun Heo 273273f53c4aSTejun Heo /* 273373f53c4aSTejun Heo * Wake-up-and-cascade phase 273473f53c4aSTejun Heo * 273573f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 273673f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 273773f53c4aSTejun Heo */ 273873f53c4aSTejun Heo if (wq->first_flusher != &this_flusher) 273973f53c4aSTejun Heo return; 274073f53c4aSTejun Heo 27413c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 274273f53c4aSTejun Heo 27434ce48b37STejun Heo /* we might have raced, check again with mutex held */ 27444ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 27454ce48b37STejun Heo goto out_unlock; 27464ce48b37STejun Heo 274773f53c4aSTejun Heo wq->first_flusher = NULL; 274873f53c4aSTejun Heo 27496183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 27506183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 275173f53c4aSTejun Heo 275273f53c4aSTejun Heo while (true) { 275373f53c4aSTejun Heo struct wq_flusher *next, *tmp; 275473f53c4aSTejun Heo 275573f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 275673f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 275773f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 275873f53c4aSTejun Heo break; 275973f53c4aSTejun Heo list_del_init(&next->list); 276073f53c4aSTejun Heo complete(&next->done); 276173f53c4aSTejun Heo } 276273f53c4aSTejun Heo 27636183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 276473f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 276573f53c4aSTejun Heo 276673f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 276773f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 276873f53c4aSTejun Heo 276973f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 277073f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 277173f53c4aSTejun Heo /* 277273f53c4aSTejun Heo * Assign the same color to all overflowed 277373f53c4aSTejun Heo * flushers, advance work_color and append to 277473f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 277573f53c4aSTejun Heo * phase for these overflowed flushers. 277673f53c4aSTejun Heo */ 277773f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 277873f53c4aSTejun Heo tmp->flush_color = wq->work_color; 277973f53c4aSTejun Heo 278073f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 278173f53c4aSTejun Heo 278273f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 278373f53c4aSTejun Heo &wq->flusher_queue); 2784112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 278573f53c4aSTejun Heo } 278673f53c4aSTejun Heo 278773f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 27886183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 278973f53c4aSTejun Heo break; 279073f53c4aSTejun Heo } 279173f53c4aSTejun Heo 279273f53c4aSTejun Heo /* 279373f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 2794112202d9STejun Heo * the new first flusher and arm pwqs. 279573f53c4aSTejun Heo */ 27966183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 27976183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 279873f53c4aSTejun Heo 279973f53c4aSTejun Heo list_del_init(&next->list); 280073f53c4aSTejun Heo wq->first_flusher = next; 280173f53c4aSTejun Heo 2802112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 280373f53c4aSTejun Heo break; 280473f53c4aSTejun Heo 280573f53c4aSTejun Heo /* 280673f53c4aSTejun Heo * Meh... this color is already done, clear first 280773f53c4aSTejun Heo * flusher and repeat cascading. 280873f53c4aSTejun Heo */ 280973f53c4aSTejun Heo wq->first_flusher = NULL; 281073f53c4aSTejun Heo } 281173f53c4aSTejun Heo 281273f53c4aSTejun Heo out_unlock: 28133c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 28141da177e4SLinus Torvalds } 28151dadafa8STim Gardner EXPORT_SYMBOL(flush_workqueue); 28161da177e4SLinus Torvalds 28179c5a2ba7STejun Heo /** 28189c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 28199c5a2ba7STejun Heo * @wq: workqueue to drain 28209c5a2ba7STejun Heo * 28219c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 28229c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 28239c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 2824b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 28259c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 28269c5a2ba7STejun Heo * takes too long. 28279c5a2ba7STejun Heo */ 28289c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 28299c5a2ba7STejun Heo { 28309c5a2ba7STejun Heo unsigned int flush_cnt = 0; 283149e3cf44STejun Heo struct pool_workqueue *pwq; 28329c5a2ba7STejun Heo 28339c5a2ba7STejun Heo /* 28349c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 28359c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 2836618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 28379c5a2ba7STejun Heo */ 283887fc741eSLai Jiangshan mutex_lock(&wq->mutex); 28399c5a2ba7STejun Heo if (!wq->nr_drainers++) 2840618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 284187fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 28429c5a2ba7STejun Heo reflush: 28439c5a2ba7STejun Heo flush_workqueue(wq); 28449c5a2ba7STejun Heo 2845b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 284676af4d93STejun Heo 284749e3cf44STejun Heo for_each_pwq(pwq, wq) { 2848fa2563e4SThomas Tuttle bool drained; 28499c5a2ba7STejun Heo 2850b09f4fd3SLai Jiangshan spin_lock_irq(&pwq->pool->lock); 2851112202d9STejun Heo drained = !pwq->nr_active && list_empty(&pwq->delayed_works); 2852b09f4fd3SLai Jiangshan spin_unlock_irq(&pwq->pool->lock); 2853fa2563e4SThomas Tuttle 2854fa2563e4SThomas Tuttle if (drained) 28559c5a2ba7STejun Heo continue; 28569c5a2ba7STejun Heo 28579c5a2ba7STejun Heo if (++flush_cnt == 10 || 28589c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 2859c5aa87bbSTejun Heo pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n", 28609c5a2ba7STejun Heo wq->name, flush_cnt); 286176af4d93STejun Heo 2862b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 28639c5a2ba7STejun Heo goto reflush; 28649c5a2ba7STejun Heo } 28659c5a2ba7STejun Heo 28669c5a2ba7STejun Heo if (!--wq->nr_drainers) 2867618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 286887fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 28699c5a2ba7STejun Heo } 28709c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 28719c5a2ba7STejun Heo 2872d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 2873d6e89786SJohannes Berg bool from_cancel) 2874baf59022STejun Heo { 2875baf59022STejun Heo struct worker *worker = NULL; 2876c9e7cf27STejun Heo struct worker_pool *pool; 2877112202d9STejun Heo struct pool_workqueue *pwq; 2878baf59022STejun Heo 2879baf59022STejun Heo might_sleep(); 2880baf59022STejun Heo 2881fa1b54e6STejun Heo local_irq_disable(); 2882fa1b54e6STejun Heo pool = get_work_pool(work); 2883fa1b54e6STejun Heo if (!pool) { 2884fa1b54e6STejun Heo local_irq_enable(); 2885fa1b54e6STejun Heo return false; 2886fa1b54e6STejun Heo } 2887fa1b54e6STejun Heo 2888fa1b54e6STejun Heo spin_lock(&pool->lock); 28890b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 2890112202d9STejun Heo pwq = get_work_pwq(work); 2891112202d9STejun Heo if (pwq) { 2892112202d9STejun Heo if (unlikely(pwq->pool != pool)) 2893baf59022STejun Heo goto already_gone; 2894606a5020STejun Heo } else { 2895c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 2896baf59022STejun Heo if (!worker) 2897baf59022STejun Heo goto already_gone; 2898112202d9STejun Heo pwq = worker->current_pwq; 2899606a5020STejun Heo } 2900baf59022STejun Heo 2901fca839c0STejun Heo check_flush_dependency(pwq->wq, work); 2902fca839c0STejun Heo 2903112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 2904d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2905baf59022STejun Heo 2906e159489bSTejun Heo /* 2907a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 2908a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 2909a1d14934SPeter Zijlstra * 2910a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 2911a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 2912a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 2913a1d14934SPeter Zijlstra * forward progress. 2914e159489bSTejun Heo */ 2915d6e89786SJohannes Berg if (!from_cancel && 2916d6e89786SJohannes Berg (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)) { 2917112202d9STejun Heo lock_map_acquire(&pwq->wq->lockdep_map); 2918112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 2919a1d14934SPeter Zijlstra } 2920e159489bSTejun Heo 2921baf59022STejun Heo return true; 2922baf59022STejun Heo already_gone: 2923d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2924baf59022STejun Heo return false; 2925baf59022STejun Heo } 2926baf59022STejun Heo 2927d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 2928d6e89786SJohannes Berg { 2929d6e89786SJohannes Berg struct wq_barrier barr; 2930d6e89786SJohannes Berg 2931d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 2932d6e89786SJohannes Berg return false; 2933d6e89786SJohannes Berg 293487915adcSJohannes Berg if (!from_cancel) { 293587915adcSJohannes Berg lock_map_acquire(&work->lockdep_map); 293687915adcSJohannes Berg lock_map_release(&work->lockdep_map); 293787915adcSJohannes Berg } 293887915adcSJohannes Berg 2939d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 2940d6e89786SJohannes Berg wait_for_completion(&barr.done); 2941d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 2942d6e89786SJohannes Berg return true; 2943d6e89786SJohannes Berg } else { 2944d6e89786SJohannes Berg return false; 2945d6e89786SJohannes Berg } 2946d6e89786SJohannes Berg } 2947d6e89786SJohannes Berg 2948db700897SOleg Nesterov /** 2949401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 2950401a8d04STejun Heo * @work: the work to flush 2951db700897SOleg Nesterov * 2952606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 2953606a5020STejun Heo * on return if it hasn't been requeued since flush started. 2954401a8d04STejun Heo * 2955d185af30SYacine Belkadi * Return: 2956401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2957401a8d04STejun Heo * %false if it was already idle. 2958db700897SOleg Nesterov */ 2959401a8d04STejun Heo bool flush_work(struct work_struct *work) 2960db700897SOleg Nesterov { 2961d6e89786SJohannes Berg return __flush_work(work, false); 2962606a5020STejun Heo } 2963db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 2964db700897SOleg Nesterov 29658603e1b3STejun Heo struct cwt_wait { 2966ac6424b9SIngo Molnar wait_queue_entry_t wait; 29678603e1b3STejun Heo struct work_struct *work; 29688603e1b3STejun Heo }; 29698603e1b3STejun Heo 2970ac6424b9SIngo Molnar static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 29718603e1b3STejun Heo { 29728603e1b3STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 29738603e1b3STejun Heo 29748603e1b3STejun Heo if (cwait->work != key) 29758603e1b3STejun Heo return 0; 29768603e1b3STejun Heo return autoremove_wake_function(wait, mode, sync, key); 29778603e1b3STejun Heo } 29788603e1b3STejun Heo 297936e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 2980401a8d04STejun Heo { 29818603e1b3STejun Heo static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); 2982bbb68dfaSTejun Heo unsigned long flags; 29831f1f642eSOleg Nesterov int ret; 29841f1f642eSOleg Nesterov 29851f1f642eSOleg Nesterov do { 2986bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 2987bbb68dfaSTejun Heo /* 29888603e1b3STejun Heo * If someone else is already canceling, wait for it to 29898603e1b3STejun Heo * finish. flush_work() doesn't work for PREEMPT_NONE 29908603e1b3STejun Heo * because we may get scheduled between @work's completion 29918603e1b3STejun Heo * and the other canceling task resuming and clearing 29928603e1b3STejun Heo * CANCELING - flush_work() will return false immediately 29938603e1b3STejun Heo * as @work is no longer busy, try_to_grab_pending() will 29948603e1b3STejun Heo * return -ENOENT as @work is still being canceled and the 29958603e1b3STejun Heo * other canceling task won't be able to clear CANCELING as 29968603e1b3STejun Heo * we're hogging the CPU. 29978603e1b3STejun Heo * 29988603e1b3STejun Heo * Let's wait for completion using a waitqueue. As this 29998603e1b3STejun Heo * may lead to the thundering herd problem, use a custom 30008603e1b3STejun Heo * wake function which matches @work along with exclusive 30018603e1b3STejun Heo * wait and wakeup. 3002bbb68dfaSTejun Heo */ 30038603e1b3STejun Heo if (unlikely(ret == -ENOENT)) { 30048603e1b3STejun Heo struct cwt_wait cwait; 30058603e1b3STejun Heo 30068603e1b3STejun Heo init_wait(&cwait.wait); 30078603e1b3STejun Heo cwait.wait.func = cwt_wakefn; 30088603e1b3STejun Heo cwait.work = work; 30098603e1b3STejun Heo 30108603e1b3STejun Heo prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, 30118603e1b3STejun Heo TASK_UNINTERRUPTIBLE); 30128603e1b3STejun Heo if (work_is_canceling(work)) 30138603e1b3STejun Heo schedule(); 30148603e1b3STejun Heo finish_wait(&cancel_waitq, &cwait.wait); 30158603e1b3STejun Heo } 30161f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 30171f1f642eSOleg Nesterov 3018bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 3019bbb68dfaSTejun Heo mark_work_canceling(work); 3020bbb68dfaSTejun Heo local_irq_restore(flags); 3021bbb68dfaSTejun Heo 30223347fa09STejun Heo /* 30233347fa09STejun Heo * This allows canceling during early boot. We know that @work 30243347fa09STejun Heo * isn't executing. 30253347fa09STejun Heo */ 30263347fa09STejun Heo if (wq_online) 3027d6e89786SJohannes Berg __flush_work(work, true); 30283347fa09STejun Heo 30297a22ad75STejun Heo clear_work_data(work); 30308603e1b3STejun Heo 30318603e1b3STejun Heo /* 30328603e1b3STejun Heo * Paired with prepare_to_wait() above so that either 30338603e1b3STejun Heo * waitqueue_active() is visible here or !work_is_canceling() is 30348603e1b3STejun Heo * visible there. 30358603e1b3STejun Heo */ 30368603e1b3STejun Heo smp_mb(); 30378603e1b3STejun Heo if (waitqueue_active(&cancel_waitq)) 30388603e1b3STejun Heo __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); 30398603e1b3STejun Heo 30401f1f642eSOleg Nesterov return ret; 30411f1f642eSOleg Nesterov } 30421f1f642eSOleg Nesterov 30436e84d644SOleg Nesterov /** 3044401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 3045401a8d04STejun Heo * @work: the work to cancel 30466e84d644SOleg Nesterov * 3047401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 3048401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 3049401a8d04STejun Heo * another workqueue. On return from this function, @work is 3050401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 30511f1f642eSOleg Nesterov * 3052401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 3053401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 30546e84d644SOleg Nesterov * 3055401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 30566e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 3057401a8d04STejun Heo * 3058d185af30SYacine Belkadi * Return: 3059401a8d04STejun Heo * %true if @work was pending, %false otherwise. 30606e84d644SOleg Nesterov */ 3061401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 30626e84d644SOleg Nesterov { 306336e227d2STejun Heo return __cancel_work_timer(work, false); 3064b89deed3SOleg Nesterov } 306528e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 3066b89deed3SOleg Nesterov 30676e84d644SOleg Nesterov /** 3068401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 3069401a8d04STejun Heo * @dwork: the delayed work to flush 30706e84d644SOleg Nesterov * 3071401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 3072401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 3073401a8d04STejun Heo * considers the last queueing instance of @dwork. 30741f1f642eSOleg Nesterov * 3075d185af30SYacine Belkadi * Return: 3076401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3077401a8d04STejun Heo * %false if it was already idle. 30786e84d644SOleg Nesterov */ 3079401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 3080401a8d04STejun Heo { 30818930cabaSTejun Heo local_irq_disable(); 3082401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 308360c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 30848930cabaSTejun Heo local_irq_enable(); 3085401a8d04STejun Heo return flush_work(&dwork->work); 3086401a8d04STejun Heo } 3087401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 3088401a8d04STejun Heo 308905f0fe6bSTejun Heo /** 309005f0fe6bSTejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 309105f0fe6bSTejun Heo * @rwork: the rcu work to flush 309205f0fe6bSTejun Heo * 309305f0fe6bSTejun Heo * Return: 309405f0fe6bSTejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 309505f0fe6bSTejun Heo * %false if it was already idle. 309605f0fe6bSTejun Heo */ 309705f0fe6bSTejun Heo bool flush_rcu_work(struct rcu_work *rwork) 309805f0fe6bSTejun Heo { 309905f0fe6bSTejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 310005f0fe6bSTejun Heo rcu_barrier(); 310105f0fe6bSTejun Heo flush_work(&rwork->work); 310205f0fe6bSTejun Heo return true; 310305f0fe6bSTejun Heo } else { 310405f0fe6bSTejun Heo return flush_work(&rwork->work); 310505f0fe6bSTejun Heo } 310605f0fe6bSTejun Heo } 310705f0fe6bSTejun Heo EXPORT_SYMBOL(flush_rcu_work); 310805f0fe6bSTejun Heo 3109f72b8792SJens Axboe static bool __cancel_work(struct work_struct *work, bool is_dwork) 3110f72b8792SJens Axboe { 3111f72b8792SJens Axboe unsigned long flags; 3112f72b8792SJens Axboe int ret; 3113f72b8792SJens Axboe 3114f72b8792SJens Axboe do { 3115f72b8792SJens Axboe ret = try_to_grab_pending(work, is_dwork, &flags); 3116f72b8792SJens Axboe } while (unlikely(ret == -EAGAIN)); 3117f72b8792SJens Axboe 3118f72b8792SJens Axboe if (unlikely(ret < 0)) 3119f72b8792SJens Axboe return false; 3120f72b8792SJens Axboe 3121f72b8792SJens Axboe set_work_pool_and_clear_pending(work, get_work_pool_id(work)); 3122f72b8792SJens Axboe local_irq_restore(flags); 3123f72b8792SJens Axboe return ret; 3124f72b8792SJens Axboe } 3125f72b8792SJens Axboe 3126401a8d04STejun Heo /** 312757b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 312857b30ae7STejun Heo * @dwork: delayed_work to cancel 312909383498STejun Heo * 3130d185af30SYacine Belkadi * Kill off a pending delayed_work. 3131d185af30SYacine Belkadi * 3132d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 3133d185af30SYacine Belkadi * pending. 3134d185af30SYacine Belkadi * 3135d185af30SYacine Belkadi * Note: 3136d185af30SYacine Belkadi * The work callback function may still be running on return, unless 3137d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 3138d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 313909383498STejun Heo * 314057b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 314109383498STejun Heo */ 314257b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 314309383498STejun Heo { 3144f72b8792SJens Axboe return __cancel_work(&dwork->work, true); 314509383498STejun Heo } 314657b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 314709383498STejun Heo 314809383498STejun Heo /** 3149401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 3150401a8d04STejun Heo * @dwork: the delayed work cancel 3151401a8d04STejun Heo * 3152401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 3153401a8d04STejun Heo * 3154d185af30SYacine Belkadi * Return: 3155401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 3156401a8d04STejun Heo */ 3157401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 31586e84d644SOleg Nesterov { 315936e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 31606e84d644SOleg Nesterov } 3161f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 31621da177e4SLinus Torvalds 31630fcb78c2SRolf Eike Beer /** 316431ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 3165b6136773SAndrew Morton * @func: the function to call 3166b6136773SAndrew Morton * 316731ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 316831ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 3169b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 317031ddd871STejun Heo * 3171d185af30SYacine Belkadi * Return: 317231ddd871STejun Heo * 0 on success, -errno on failure. 3173b6136773SAndrew Morton */ 317465f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 317515316ba8SChristoph Lameter { 317615316ba8SChristoph Lameter int cpu; 317738f51568SNamhyung Kim struct work_struct __percpu *works; 317815316ba8SChristoph Lameter 3179b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 3180b6136773SAndrew Morton if (!works) 318115316ba8SChristoph Lameter return -ENOMEM; 3182b6136773SAndrew Morton 318395402b38SGautham R Shenoy get_online_cpus(); 318493981800STejun Heo 318515316ba8SChristoph Lameter for_each_online_cpu(cpu) { 31869bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 31879bfb1839SIngo Molnar 31889bfb1839SIngo Molnar INIT_WORK(work, func); 31898de6d308SOleg Nesterov schedule_work_on(cpu, work); 319015316ba8SChristoph Lameter } 319193981800STejun Heo 319293981800STejun Heo for_each_online_cpu(cpu) 31938616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 319493981800STejun Heo 319595402b38SGautham R Shenoy put_online_cpus(); 3196b6136773SAndrew Morton free_percpu(works); 319715316ba8SChristoph Lameter return 0; 319815316ba8SChristoph Lameter } 319915316ba8SChristoph Lameter 3200eef6a7d5SAlan Stern /** 32011fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 32021fa44ecaSJames Bottomley * @fn: the function to execute 32031fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 32041fa44ecaSJames Bottomley * be available when the work executes) 32051fa44ecaSJames Bottomley * 32061fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 32071fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 32081fa44ecaSJames Bottomley * 3209d185af30SYacine Belkadi * Return: 0 - function was executed 32101fa44ecaSJames Bottomley * 1 - function was scheduled for execution 32111fa44ecaSJames Bottomley */ 321265f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 32131fa44ecaSJames Bottomley { 32141fa44ecaSJames Bottomley if (!in_interrupt()) { 321565f27f38SDavid Howells fn(&ew->work); 32161fa44ecaSJames Bottomley return 0; 32171fa44ecaSJames Bottomley } 32181fa44ecaSJames Bottomley 321965f27f38SDavid Howells INIT_WORK(&ew->work, fn); 32201fa44ecaSJames Bottomley schedule_work(&ew->work); 32211fa44ecaSJames Bottomley 32221fa44ecaSJames Bottomley return 1; 32231fa44ecaSJames Bottomley } 32241fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 32251fa44ecaSJames Bottomley 32267a4e344cSTejun Heo /** 32277a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 32287a4e344cSTejun Heo * @attrs: workqueue_attrs to free 32297a4e344cSTejun Heo * 32307a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 32317a4e344cSTejun Heo */ 32327a4e344cSTejun Heo void free_workqueue_attrs(struct workqueue_attrs *attrs) 32337a4e344cSTejun Heo { 32347a4e344cSTejun Heo if (attrs) { 32357a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 32367a4e344cSTejun Heo kfree(attrs); 32377a4e344cSTejun Heo } 32387a4e344cSTejun Heo } 32397a4e344cSTejun Heo 32407a4e344cSTejun Heo /** 32417a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 32427a4e344cSTejun Heo * @gfp_mask: allocation mask to use 32437a4e344cSTejun Heo * 32447a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 3245d185af30SYacine Belkadi * return it. 3246d185af30SYacine Belkadi * 3247d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 32487a4e344cSTejun Heo */ 32497a4e344cSTejun Heo struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask) 32507a4e344cSTejun Heo { 32517a4e344cSTejun Heo struct workqueue_attrs *attrs; 32527a4e344cSTejun Heo 32537a4e344cSTejun Heo attrs = kzalloc(sizeof(*attrs), gfp_mask); 32547a4e344cSTejun Heo if (!attrs) 32557a4e344cSTejun Heo goto fail; 32567a4e344cSTejun Heo if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask)) 32577a4e344cSTejun Heo goto fail; 32587a4e344cSTejun Heo 325913e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 32607a4e344cSTejun Heo return attrs; 32617a4e344cSTejun Heo fail: 32627a4e344cSTejun Heo free_workqueue_attrs(attrs); 32637a4e344cSTejun Heo return NULL; 32647a4e344cSTejun Heo } 32657a4e344cSTejun Heo 326629c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 326729c91e99STejun Heo const struct workqueue_attrs *from) 326829c91e99STejun Heo { 326929c91e99STejun Heo to->nice = from->nice; 327029c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 32712865a8fbSShaohua Li /* 32722865a8fbSShaohua Li * Unlike hash and equality test, this function doesn't ignore 32732865a8fbSShaohua Li * ->no_numa as it is used for both pool and wq attrs. Instead, 32742865a8fbSShaohua Li * get_unbound_pool() explicitly clears ->no_numa after copying. 32752865a8fbSShaohua Li */ 32762865a8fbSShaohua Li to->no_numa = from->no_numa; 327729c91e99STejun Heo } 327829c91e99STejun Heo 327929c91e99STejun Heo /* hash value of the content of @attr */ 328029c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 328129c91e99STejun Heo { 328229c91e99STejun Heo u32 hash = 0; 328329c91e99STejun Heo 328429c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 328513e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 328613e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 328729c91e99STejun Heo return hash; 328829c91e99STejun Heo } 328929c91e99STejun Heo 329029c91e99STejun Heo /* content equality test */ 329129c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 329229c91e99STejun Heo const struct workqueue_attrs *b) 329329c91e99STejun Heo { 329429c91e99STejun Heo if (a->nice != b->nice) 329529c91e99STejun Heo return false; 329629c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 329729c91e99STejun Heo return false; 329829c91e99STejun Heo return true; 329929c91e99STejun Heo } 330029c91e99STejun Heo 33017a4e344cSTejun Heo /** 33027a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 33037a4e344cSTejun Heo * @pool: worker_pool to initialize 33047a4e344cSTejun Heo * 3305402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 3306d185af30SYacine Belkadi * 3307d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 330829c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 330929c91e99STejun Heo * on @pool safely to release it. 33107a4e344cSTejun Heo */ 33117a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 33124e1a1f9aSTejun Heo { 33134e1a1f9aSTejun Heo spin_lock_init(&pool->lock); 331429c91e99STejun Heo pool->id = -1; 331529c91e99STejun Heo pool->cpu = -1; 3316f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 33174e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 331882607adcSTejun Heo pool->watchdog_ts = jiffies; 33194e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 33204e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 33214e1a1f9aSTejun Heo hash_init(pool->busy_hash); 33224e1a1f9aSTejun Heo 332332a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 33244e1a1f9aSTejun Heo 332532a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 33264e1a1f9aSTejun Heo 3327da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 33287a4e344cSTejun Heo 33297cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 333029c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 333129c91e99STejun Heo pool->refcnt = 1; 333229c91e99STejun Heo 333329c91e99STejun Heo /* shouldn't fail above this point */ 33347a4e344cSTejun Heo pool->attrs = alloc_workqueue_attrs(GFP_KERNEL); 33357a4e344cSTejun Heo if (!pool->attrs) 33367a4e344cSTejun Heo return -ENOMEM; 33377a4e344cSTejun Heo return 0; 33384e1a1f9aSTejun Heo } 33394e1a1f9aSTejun Heo 3340e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 3341e2dca7adSTejun Heo { 3342e2dca7adSTejun Heo struct workqueue_struct *wq = 3343e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 3344e2dca7adSTejun Heo 3345e2dca7adSTejun Heo if (!(wq->flags & WQ_UNBOUND)) 3346e2dca7adSTejun Heo free_percpu(wq->cpu_pwqs); 3347e2dca7adSTejun Heo else 3348e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 3349e2dca7adSTejun Heo 3350e2dca7adSTejun Heo kfree(wq->rescuer); 3351e2dca7adSTejun Heo kfree(wq); 3352e2dca7adSTejun Heo } 3353e2dca7adSTejun Heo 335429c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 335529c91e99STejun Heo { 335629c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 335729c91e99STejun Heo 33587cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 335929c91e99STejun Heo free_workqueue_attrs(pool->attrs); 336029c91e99STejun Heo kfree(pool); 336129c91e99STejun Heo } 336229c91e99STejun Heo 336329c91e99STejun Heo /** 336429c91e99STejun Heo * put_unbound_pool - put a worker_pool 336529c91e99STejun Heo * @pool: worker_pool to put 336629c91e99STejun Heo * 336729c91e99STejun Heo * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU 3368c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 3369c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 3370c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 3371a892caccSTejun Heo * 3372a892caccSTejun Heo * Should be called with wq_pool_mutex held. 337329c91e99STejun Heo */ 337429c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 337529c91e99STejun Heo { 337660f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 337729c91e99STejun Heo struct worker *worker; 337829c91e99STejun Heo 3379a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3380a892caccSTejun Heo 3381a892caccSTejun Heo if (--pool->refcnt) 338229c91e99STejun Heo return; 338329c91e99STejun Heo 338429c91e99STejun Heo /* sanity checks */ 338561d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 3386a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 338729c91e99STejun Heo return; 338829c91e99STejun Heo 338929c91e99STejun Heo /* release id and unhash */ 339029c91e99STejun Heo if (pool->id >= 0) 339129c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 339229c91e99STejun Heo hash_del(&pool->hash_node); 339329c91e99STejun Heo 3394c5aa87bbSTejun Heo /* 3395692b4825STejun Heo * Become the manager and destroy all workers. This prevents 3396692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 3397692b4825STejun Heo * manager and @pool gets freed with the flag set. 3398c5aa87bbSTejun Heo */ 339960f5a4bcSLai Jiangshan spin_lock_irq(&pool->lock); 3400692b4825STejun Heo wait_event_lock_irq(wq_manager_wait, 3401692b4825STejun Heo !(pool->flags & POOL_MANAGER_ACTIVE), pool->lock); 3402692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 3403692b4825STejun Heo 34041037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 340529c91e99STejun Heo destroy_worker(worker); 340629c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 340729c91e99STejun Heo spin_unlock_irq(&pool->lock); 340860f5a4bcSLai Jiangshan 34091258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 3410da028469SLai Jiangshan if (!list_empty(&pool->workers)) 341160f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 34121258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 341360f5a4bcSLai Jiangshan 341460f5a4bcSLai Jiangshan if (pool->detach_completion) 341560f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 341660f5a4bcSLai Jiangshan 341729c91e99STejun Heo /* shut down the timers */ 341829c91e99STejun Heo del_timer_sync(&pool->idle_timer); 341929c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 342029c91e99STejun Heo 342129c91e99STejun Heo /* sched-RCU protected to allow dereferences from get_work_pool() */ 342225b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 342329c91e99STejun Heo } 342429c91e99STejun Heo 342529c91e99STejun Heo /** 342629c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 342729c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 342829c91e99STejun Heo * 342929c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 343029c91e99STejun Heo * reference count and return it. If there already is a matching 343129c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 3432d185af30SYacine Belkadi * create a new one. 3433a892caccSTejun Heo * 3434a892caccSTejun Heo * Should be called with wq_pool_mutex held. 3435d185af30SYacine Belkadi * 3436d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 3437d185af30SYacine Belkadi * On failure, %NULL. 343829c91e99STejun Heo */ 343929c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 344029c91e99STejun Heo { 344129c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 344229c91e99STejun Heo struct worker_pool *pool; 3443f3f90ad4STejun Heo int node; 3444e2273584SXunlei Pang int target_node = NUMA_NO_NODE; 344529c91e99STejun Heo 3446a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 344729c91e99STejun Heo 344829c91e99STejun Heo /* do we already have a matching pool? */ 344929c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 345029c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 345129c91e99STejun Heo pool->refcnt++; 34523fb1823cSLai Jiangshan return pool; 345329c91e99STejun Heo } 345429c91e99STejun Heo } 345529c91e99STejun Heo 3456e2273584SXunlei Pang /* if cpumask is contained inside a NUMA node, we belong to that node */ 3457e2273584SXunlei Pang if (wq_numa_enabled) { 3458e2273584SXunlei Pang for_each_node(node) { 3459e2273584SXunlei Pang if (cpumask_subset(attrs->cpumask, 3460e2273584SXunlei Pang wq_numa_possible_cpumask[node])) { 3461e2273584SXunlei Pang target_node = node; 3462e2273584SXunlei Pang break; 3463e2273584SXunlei Pang } 3464e2273584SXunlei Pang } 3465e2273584SXunlei Pang } 3466e2273584SXunlei Pang 346729c91e99STejun Heo /* nope, create a new one */ 3468e2273584SXunlei Pang pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, target_node); 346929c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 347029c91e99STejun Heo goto fail; 347129c91e99STejun Heo 34728864b4e5STejun Heo lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */ 347329c91e99STejun Heo copy_workqueue_attrs(pool->attrs, attrs); 3474e2273584SXunlei Pang pool->node = target_node; 347529c91e99STejun Heo 34762865a8fbSShaohua Li /* 34772865a8fbSShaohua Li * no_numa isn't a worker_pool attribute, always clear it. See 34782865a8fbSShaohua Li * 'struct workqueue_attrs' comments for detail. 34792865a8fbSShaohua Li */ 34802865a8fbSShaohua Li pool->attrs->no_numa = false; 34812865a8fbSShaohua Li 348229c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 348329c91e99STejun Heo goto fail; 348429c91e99STejun Heo 348529c91e99STejun Heo /* create and start the initial worker */ 34863347fa09STejun Heo if (wq_online && !create_worker(pool)) 348729c91e99STejun Heo goto fail; 348829c91e99STejun Heo 348929c91e99STejun Heo /* install */ 349029c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 34913fb1823cSLai Jiangshan 349229c91e99STejun Heo return pool; 349329c91e99STejun Heo fail: 349429c91e99STejun Heo if (pool) 349529c91e99STejun Heo put_unbound_pool(pool); 349629c91e99STejun Heo return NULL; 349729c91e99STejun Heo } 349829c91e99STejun Heo 34998864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 35008864b4e5STejun Heo { 35018864b4e5STejun Heo kmem_cache_free(pwq_cache, 35028864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 35038864b4e5STejun Heo } 35048864b4e5STejun Heo 35058864b4e5STejun Heo /* 35068864b4e5STejun Heo * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt 35078864b4e5STejun Heo * and needs to be destroyed. 35088864b4e5STejun Heo */ 35098864b4e5STejun Heo static void pwq_unbound_release_workfn(struct work_struct *work) 35108864b4e5STejun Heo { 35118864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 35128864b4e5STejun Heo unbound_release_work); 35138864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 35148864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 3515bc0caf09STejun Heo bool is_last; 35168864b4e5STejun Heo 35178864b4e5STejun Heo if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND))) 35188864b4e5STejun Heo return; 35198864b4e5STejun Heo 35203c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 35218864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 3522bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 35233c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 35248864b4e5STejun Heo 3525a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 35268864b4e5STejun Heo put_unbound_pool(pool); 3527a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 3528a892caccSTejun Heo 352925b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 35308864b4e5STejun Heo 35318864b4e5STejun Heo /* 35328864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 3533e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 35348864b4e5STejun Heo */ 3535e2dca7adSTejun Heo if (is_last) 353625b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 35376029a918STejun Heo } 35388864b4e5STejun Heo 35390fbd95aaSTejun Heo /** 3540699ce097STejun Heo * pwq_adjust_max_active - update a pwq's max_active to the current setting 35410fbd95aaSTejun Heo * @pwq: target pool_workqueue 35420fbd95aaSTejun Heo * 3543699ce097STejun Heo * If @pwq isn't freezing, set @pwq->max_active to the associated 3544699ce097STejun Heo * workqueue's saved_max_active and activate delayed work items 3545699ce097STejun Heo * accordingly. If @pwq is freezing, clear @pwq->max_active to zero. 35460fbd95aaSTejun Heo */ 3547699ce097STejun Heo static void pwq_adjust_max_active(struct pool_workqueue *pwq) 35480fbd95aaSTejun Heo { 3549699ce097STejun Heo struct workqueue_struct *wq = pwq->wq; 3550699ce097STejun Heo bool freezable = wq->flags & WQ_FREEZABLE; 35513347fa09STejun Heo unsigned long flags; 3552699ce097STejun Heo 3553699ce097STejun Heo /* for @wq->saved_max_active */ 3554a357fc03SLai Jiangshan lockdep_assert_held(&wq->mutex); 3555699ce097STejun Heo 3556699ce097STejun Heo /* fast exit for non-freezable wqs */ 3557699ce097STejun Heo if (!freezable && pwq->max_active == wq->saved_max_active) 3558699ce097STejun Heo return; 3559699ce097STejun Heo 35603347fa09STejun Heo /* this function can be called during early boot w/ irq disabled */ 35613347fa09STejun Heo spin_lock_irqsave(&pwq->pool->lock, flags); 3562699ce097STejun Heo 356374b414eaSLai Jiangshan /* 356474b414eaSLai Jiangshan * During [un]freezing, the caller is responsible for ensuring that 356574b414eaSLai Jiangshan * this function is called at least once after @workqueue_freezing 356674b414eaSLai Jiangshan * is updated and visible. 356774b414eaSLai Jiangshan */ 356874b414eaSLai Jiangshan if (!freezable || !workqueue_freezing) { 3569699ce097STejun Heo pwq->max_active = wq->saved_max_active; 35700fbd95aaSTejun Heo 35710fbd95aaSTejun Heo while (!list_empty(&pwq->delayed_works) && 35720fbd95aaSTejun Heo pwq->nr_active < pwq->max_active) 35730fbd95aaSTejun Heo pwq_activate_first_delayed(pwq); 3574951a078aSLai Jiangshan 3575951a078aSLai Jiangshan /* 3576951a078aSLai Jiangshan * Need to kick a worker after thawed or an unbound wq's 3577951a078aSLai Jiangshan * max_active is bumped. It's a slow path. Do it always. 3578951a078aSLai Jiangshan */ 3579951a078aSLai Jiangshan wake_up_worker(pwq->pool); 3580699ce097STejun Heo } else { 3581699ce097STejun Heo pwq->max_active = 0; 3582699ce097STejun Heo } 3583699ce097STejun Heo 35843347fa09STejun Heo spin_unlock_irqrestore(&pwq->pool->lock, flags); 35850fbd95aaSTejun Heo } 35860fbd95aaSTejun Heo 3587e50aba9aSTejun Heo /* initialize newly alloced @pwq which is associated with @wq and @pool */ 3588f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 3589f147f29eSTejun Heo struct worker_pool *pool) 3590d2c1d404STejun Heo { 3591d2c1d404STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 3592d2c1d404STejun Heo 3593e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 3594e50aba9aSTejun Heo 3595d2c1d404STejun Heo pwq->pool = pool; 3596d2c1d404STejun Heo pwq->wq = wq; 3597d2c1d404STejun Heo pwq->flush_color = -1; 35988864b4e5STejun Heo pwq->refcnt = 1; 3599d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->delayed_works); 36001befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 3601d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 36028864b4e5STejun Heo INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn); 3603f147f29eSTejun Heo } 3604d2c1d404STejun Heo 3605f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 36061befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 3607f147f29eSTejun Heo { 3608f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 3609f147f29eSTejun Heo 3610f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 361175ccf595STejun Heo 36121befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 36131befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 36141befcf30STejun Heo return; 36151befcf30STejun Heo 361629b1cb41SLai Jiangshan /* set the matching work_color */ 361775ccf595STejun Heo pwq->work_color = wq->work_color; 3618983ca25eSTejun Heo 3619983ca25eSTejun Heo /* sync max_active to the current setting */ 3620983ca25eSTejun Heo pwq_adjust_max_active(pwq); 3621983ca25eSTejun Heo 3622983ca25eSTejun Heo /* link in @pwq */ 36239e8cd2f5STejun Heo list_add_rcu(&pwq->pwqs_node, &wq->pwqs); 3624df2d5ae4STejun Heo } 36256029a918STejun Heo 3626f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 3627f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 3628f147f29eSTejun Heo const struct workqueue_attrs *attrs) 3629f147f29eSTejun Heo { 3630f147f29eSTejun Heo struct worker_pool *pool; 3631f147f29eSTejun Heo struct pool_workqueue *pwq; 3632f147f29eSTejun Heo 3633f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3634f147f29eSTejun Heo 3635f147f29eSTejun Heo pool = get_unbound_pool(attrs); 3636f147f29eSTejun Heo if (!pool) 3637f147f29eSTejun Heo return NULL; 3638f147f29eSTejun Heo 3639e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 3640f147f29eSTejun Heo if (!pwq) { 3641f147f29eSTejun Heo put_unbound_pool(pool); 3642f147f29eSTejun Heo return NULL; 3643f147f29eSTejun Heo } 3644f147f29eSTejun Heo 3645f147f29eSTejun Heo init_pwq(pwq, wq, pool); 3646f147f29eSTejun Heo return pwq; 3647d2c1d404STejun Heo } 3648d2c1d404STejun Heo 36494c16bd32STejun Heo /** 365030186c6fSGong Zhaogang * wq_calc_node_cpumask - calculate a wq_attrs' cpumask for the specified node 3651042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 36524c16bd32STejun Heo * @node: the target NUMA node 36534c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 36544c16bd32STejun Heo * @cpumask: outarg, the resulting cpumask 36554c16bd32STejun Heo * 36564c16bd32STejun Heo * Calculate the cpumask a workqueue with @attrs should use on @node. If 36574c16bd32STejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during 3658d185af30SYacine Belkadi * calculation. The result is stored in @cpumask. 36594c16bd32STejun Heo * 36604c16bd32STejun Heo * If NUMA affinity is not enabled, @attrs->cpumask is always used. If 36614c16bd32STejun Heo * enabled and @node has online CPUs requested by @attrs, the returned 36624c16bd32STejun Heo * cpumask is the intersection of the possible CPUs of @node and 36634c16bd32STejun Heo * @attrs->cpumask. 36644c16bd32STejun Heo * 36654c16bd32STejun Heo * The caller is responsible for ensuring that the cpumask of @node stays 36664c16bd32STejun Heo * stable. 3667d185af30SYacine Belkadi * 3668d185af30SYacine Belkadi * Return: %true if the resulting @cpumask is different from @attrs->cpumask, 3669d185af30SYacine Belkadi * %false if equal. 36704c16bd32STejun Heo */ 36714c16bd32STejun Heo static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node, 36724c16bd32STejun Heo int cpu_going_down, cpumask_t *cpumask) 36734c16bd32STejun Heo { 3674d55262c4STejun Heo if (!wq_numa_enabled || attrs->no_numa) 36754c16bd32STejun Heo goto use_dfl; 36764c16bd32STejun Heo 36774c16bd32STejun Heo /* does @node have any online CPUs @attrs wants? */ 36784c16bd32STejun Heo cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask); 36794c16bd32STejun Heo if (cpu_going_down >= 0) 36804c16bd32STejun Heo cpumask_clear_cpu(cpu_going_down, cpumask); 36814c16bd32STejun Heo 36824c16bd32STejun Heo if (cpumask_empty(cpumask)) 36834c16bd32STejun Heo goto use_dfl; 36844c16bd32STejun Heo 36854c16bd32STejun Heo /* yeap, return possible CPUs in @node that @attrs wants */ 36864c16bd32STejun Heo cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]); 36871ad0f0a7SMichael Bringmann 36881ad0f0a7SMichael Bringmann if (cpumask_empty(cpumask)) { 36891ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 36901ad0f0a7SMichael Bringmann "possible intersect\n"); 36911ad0f0a7SMichael Bringmann return false; 36921ad0f0a7SMichael Bringmann } 36931ad0f0a7SMichael Bringmann 36944c16bd32STejun Heo return !cpumask_equal(cpumask, attrs->cpumask); 36954c16bd32STejun Heo 36964c16bd32STejun Heo use_dfl: 36974c16bd32STejun Heo cpumask_copy(cpumask, attrs->cpumask); 36984c16bd32STejun Heo return false; 36994c16bd32STejun Heo } 37004c16bd32STejun Heo 37011befcf30STejun Heo /* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */ 37021befcf30STejun Heo static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq, 37031befcf30STejun Heo int node, 37041befcf30STejun Heo struct pool_workqueue *pwq) 37051befcf30STejun Heo { 37061befcf30STejun Heo struct pool_workqueue *old_pwq; 37071befcf30STejun Heo 37085b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 37091befcf30STejun Heo lockdep_assert_held(&wq->mutex); 37101befcf30STejun Heo 37111befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 37121befcf30STejun Heo link_pwq(pwq); 37131befcf30STejun Heo 37141befcf30STejun Heo old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]); 37151befcf30STejun Heo rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq); 37161befcf30STejun Heo return old_pwq; 37171befcf30STejun Heo } 37181befcf30STejun Heo 37192d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 37202d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 37212d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 37222d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 3723042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 37242d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 37252d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 37262d5f0764SLai Jiangshan }; 37272d5f0764SLai Jiangshan 37282d5f0764SLai Jiangshan /* free the resources after success or abort */ 37292d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 37302d5f0764SLai Jiangshan { 37312d5f0764SLai Jiangshan if (ctx) { 37322d5f0764SLai Jiangshan int node; 37332d5f0764SLai Jiangshan 37342d5f0764SLai Jiangshan for_each_node(node) 37352d5f0764SLai Jiangshan put_pwq_unlocked(ctx->pwq_tbl[node]); 37362d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 37372d5f0764SLai Jiangshan 37382d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 37392d5f0764SLai Jiangshan 37402d5f0764SLai Jiangshan kfree(ctx); 37412d5f0764SLai Jiangshan } 37422d5f0764SLai Jiangshan } 37432d5f0764SLai Jiangshan 37442d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 37452d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 37462d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 37472d5f0764SLai Jiangshan const struct workqueue_attrs *attrs) 37482d5f0764SLai Jiangshan { 37492d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 37502d5f0764SLai Jiangshan struct workqueue_attrs *new_attrs, *tmp_attrs; 37512d5f0764SLai Jiangshan int node; 37522d5f0764SLai Jiangshan 37532d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 37542d5f0764SLai Jiangshan 3755acafe7e3SKees Cook ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_node_ids), GFP_KERNEL); 37562d5f0764SLai Jiangshan 37572d5f0764SLai Jiangshan new_attrs = alloc_workqueue_attrs(GFP_KERNEL); 37582d5f0764SLai Jiangshan tmp_attrs = alloc_workqueue_attrs(GFP_KERNEL); 37592d5f0764SLai Jiangshan if (!ctx || !new_attrs || !tmp_attrs) 37602d5f0764SLai Jiangshan goto out_free; 37612d5f0764SLai Jiangshan 3762042f7df1SLai Jiangshan /* 3763042f7df1SLai Jiangshan * Calculate the attrs of the default pwq. 3764042f7df1SLai Jiangshan * If the user configured cpumask doesn't overlap with the 3765042f7df1SLai Jiangshan * wq_unbound_cpumask, we fallback to the wq_unbound_cpumask. 3766042f7df1SLai Jiangshan */ 37672d5f0764SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 3768b05a7928SFrederic Weisbecker cpumask_and(new_attrs->cpumask, new_attrs->cpumask, wq_unbound_cpumask); 3769042f7df1SLai Jiangshan if (unlikely(cpumask_empty(new_attrs->cpumask))) 3770042f7df1SLai Jiangshan cpumask_copy(new_attrs->cpumask, wq_unbound_cpumask); 37712d5f0764SLai Jiangshan 37722d5f0764SLai Jiangshan /* 37732d5f0764SLai Jiangshan * We may create multiple pwqs with differing cpumasks. Make a 37742d5f0764SLai Jiangshan * copy of @new_attrs which will be modified and used to obtain 37752d5f0764SLai Jiangshan * pools. 37762d5f0764SLai Jiangshan */ 37772d5f0764SLai Jiangshan copy_workqueue_attrs(tmp_attrs, new_attrs); 37782d5f0764SLai Jiangshan 37792d5f0764SLai Jiangshan /* 37802d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 37812d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 37822d5f0764SLai Jiangshan * it even if we don't use it immediately. 37832d5f0764SLai Jiangshan */ 37842d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 37852d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 37862d5f0764SLai Jiangshan goto out_free; 37872d5f0764SLai Jiangshan 37882d5f0764SLai Jiangshan for_each_node(node) { 3789042f7df1SLai Jiangshan if (wq_calc_node_cpumask(new_attrs, node, -1, tmp_attrs->cpumask)) { 37902d5f0764SLai Jiangshan ctx->pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs); 37912d5f0764SLai Jiangshan if (!ctx->pwq_tbl[node]) 37922d5f0764SLai Jiangshan goto out_free; 37932d5f0764SLai Jiangshan } else { 37942d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 37952d5f0764SLai Jiangshan ctx->pwq_tbl[node] = ctx->dfl_pwq; 37962d5f0764SLai Jiangshan } 37972d5f0764SLai Jiangshan } 37982d5f0764SLai Jiangshan 3799042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 3800042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 3801042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 38022d5f0764SLai Jiangshan ctx->attrs = new_attrs; 3803042f7df1SLai Jiangshan 38042d5f0764SLai Jiangshan ctx->wq = wq; 38052d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 38062d5f0764SLai Jiangshan return ctx; 38072d5f0764SLai Jiangshan 38082d5f0764SLai Jiangshan out_free: 38092d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 38102d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 38112d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 38122d5f0764SLai Jiangshan return NULL; 38132d5f0764SLai Jiangshan } 38142d5f0764SLai Jiangshan 38152d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 38162d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 38172d5f0764SLai Jiangshan { 38182d5f0764SLai Jiangshan int node; 38192d5f0764SLai Jiangshan 38202d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 38212d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 38222d5f0764SLai Jiangshan 38232d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 38242d5f0764SLai Jiangshan 38252d5f0764SLai Jiangshan /* save the previous pwq and install the new one */ 38262d5f0764SLai Jiangshan for_each_node(node) 38272d5f0764SLai Jiangshan ctx->pwq_tbl[node] = numa_pwq_tbl_install(ctx->wq, node, 38282d5f0764SLai Jiangshan ctx->pwq_tbl[node]); 38292d5f0764SLai Jiangshan 38302d5f0764SLai Jiangshan /* @dfl_pwq might not have been used, ensure it's linked */ 38312d5f0764SLai Jiangshan link_pwq(ctx->dfl_pwq); 38322d5f0764SLai Jiangshan swap(ctx->wq->dfl_pwq, ctx->dfl_pwq); 38332d5f0764SLai Jiangshan 38342d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 38352d5f0764SLai Jiangshan } 38362d5f0764SLai Jiangshan 3837a0111cf6SLai Jiangshan static void apply_wqattrs_lock(void) 3838a0111cf6SLai Jiangshan { 3839a0111cf6SLai Jiangshan /* CPUs should stay stable across pwq creations and installations */ 3840a0111cf6SLai Jiangshan get_online_cpus(); 3841a0111cf6SLai Jiangshan mutex_lock(&wq_pool_mutex); 3842a0111cf6SLai Jiangshan } 3843a0111cf6SLai Jiangshan 3844a0111cf6SLai Jiangshan static void apply_wqattrs_unlock(void) 3845a0111cf6SLai Jiangshan { 3846a0111cf6SLai Jiangshan mutex_unlock(&wq_pool_mutex); 3847a0111cf6SLai Jiangshan put_online_cpus(); 3848a0111cf6SLai Jiangshan } 3849a0111cf6SLai Jiangshan 3850a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 3851a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 3852a0111cf6SLai Jiangshan { 3853a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 3854a0111cf6SLai Jiangshan 3855a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 3856a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 3857a0111cf6SLai Jiangshan return -EINVAL; 3858a0111cf6SLai Jiangshan 3859a0111cf6SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 38600a94efb5STejun Heo if (!list_empty(&wq->pwqs)) { 38610a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 3862a0111cf6SLai Jiangshan return -EINVAL; 3863a0111cf6SLai Jiangshan 38640a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 38650a94efb5STejun Heo } 38660a94efb5STejun Heo 3867a0111cf6SLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs); 38686201171eSwanghaibin if (!ctx) 38696201171eSwanghaibin return -ENOMEM; 3870a0111cf6SLai Jiangshan 3871a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 3872a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 3873a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 3874a0111cf6SLai Jiangshan 38756201171eSwanghaibin return 0; 3876a0111cf6SLai Jiangshan } 3877a0111cf6SLai Jiangshan 38789e8cd2f5STejun Heo /** 38799e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 38809e8cd2f5STejun Heo * @wq: the target workqueue 38819e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 38829e8cd2f5STejun Heo * 38834c16bd32STejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA 38844c16bd32STejun Heo * machines, this function maps a separate pwq to each NUMA node with 38854c16bd32STejun Heo * possibles CPUs in @attrs->cpumask so that work items are affine to the 38864c16bd32STejun Heo * NUMA node it was issued on. Older pwqs are released as in-flight work 38874c16bd32STejun Heo * items finish. Note that a work item which repeatedly requeues itself 38884c16bd32STejun Heo * back-to-back will stay on its current pwq. 38899e8cd2f5STejun Heo * 3890d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 3891d185af30SYacine Belkadi * 3892d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 38939e8cd2f5STejun Heo */ 38949e8cd2f5STejun Heo int apply_workqueue_attrs(struct workqueue_struct *wq, 38959e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 38969e8cd2f5STejun Heo { 3897a0111cf6SLai Jiangshan int ret; 38989e8cd2f5STejun Heo 3899a0111cf6SLai Jiangshan apply_wqattrs_lock(); 3900a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 3901a0111cf6SLai Jiangshan apply_wqattrs_unlock(); 39022d5f0764SLai Jiangshan 39032d5f0764SLai Jiangshan return ret; 39049e8cd2f5STejun Heo } 39056106c0f8SNeilBrown EXPORT_SYMBOL_GPL(apply_workqueue_attrs); 39069e8cd2f5STejun Heo 39074c16bd32STejun Heo /** 39084c16bd32STejun Heo * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug 39094c16bd32STejun Heo * @wq: the target workqueue 39104c16bd32STejun Heo * @cpu: the CPU coming up or going down 39114c16bd32STejun Heo * @online: whether @cpu is coming up or going down 39124c16bd32STejun Heo * 39134c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 39144c16bd32STejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of 39154c16bd32STejun Heo * @wq accordingly. 39164c16bd32STejun Heo * 39174c16bd32STejun Heo * If NUMA affinity can't be adjusted due to memory allocation failure, it 39184c16bd32STejun Heo * falls back to @wq->dfl_pwq which may not be optimal but is always 39194c16bd32STejun Heo * correct. 39204c16bd32STejun Heo * 39214c16bd32STejun Heo * Note that when the last allowed CPU of a NUMA node goes offline for a 39224c16bd32STejun Heo * workqueue with a cpumask spanning multiple nodes, the workers which were 39234c16bd32STejun Heo * already executing the work items for the workqueue will lose their CPU 39244c16bd32STejun Heo * affinity and may execute on any CPU. This is similar to how per-cpu 39254c16bd32STejun Heo * workqueues behave on CPU_DOWN. If a workqueue user wants strict 39264c16bd32STejun Heo * affinity, it's the user's responsibility to flush the work item from 39274c16bd32STejun Heo * CPU_DOWN_PREPARE. 39284c16bd32STejun Heo */ 39294c16bd32STejun Heo static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu, 39304c16bd32STejun Heo bool online) 39314c16bd32STejun Heo { 39324c16bd32STejun Heo int node = cpu_to_node(cpu); 39334c16bd32STejun Heo int cpu_off = online ? -1 : cpu; 39344c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 39354c16bd32STejun Heo struct workqueue_attrs *target_attrs; 39364c16bd32STejun Heo cpumask_t *cpumask; 39374c16bd32STejun Heo 39384c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 39394c16bd32STejun Heo 3940f7142ed4SLai Jiangshan if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND) || 3941f7142ed4SLai Jiangshan wq->unbound_attrs->no_numa) 39424c16bd32STejun Heo return; 39434c16bd32STejun Heo 39444c16bd32STejun Heo /* 39454c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 39464c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 39474c16bd32STejun Heo * CPU hotplug exclusion. 39484c16bd32STejun Heo */ 39494c16bd32STejun Heo target_attrs = wq_update_unbound_numa_attrs_buf; 39504c16bd32STejun Heo cpumask = target_attrs->cpumask; 39514c16bd32STejun Heo 39524c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 39534c16bd32STejun Heo pwq = unbound_pwq_by_node(wq, node); 39544c16bd32STejun Heo 39554c16bd32STejun Heo /* 39564c16bd32STejun Heo * Let's determine what needs to be done. If the target cpumask is 3957042f7df1SLai Jiangshan * different from the default pwq's, we need to compare it to @pwq's 3958042f7df1SLai Jiangshan * and create a new one if they don't match. If the target cpumask 3959042f7df1SLai Jiangshan * equals the default pwq's, the default pwq should be used. 39604c16bd32STejun Heo */ 3961042f7df1SLai Jiangshan if (wq_calc_node_cpumask(wq->dfl_pwq->pool->attrs, node, cpu_off, cpumask)) { 39624c16bd32STejun Heo if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask)) 3963f7142ed4SLai Jiangshan return; 39644c16bd32STejun Heo } else { 39654c16bd32STejun Heo goto use_dfl_pwq; 39664c16bd32STejun Heo } 39674c16bd32STejun Heo 39684c16bd32STejun Heo /* create a new pwq */ 39694c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 39704c16bd32STejun Heo if (!pwq) { 39712d916033SFabian Frederick pr_warn("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n", 39724c16bd32STejun Heo wq->name); 397377f300b1SDaeseok Youn goto use_dfl_pwq; 39744c16bd32STejun Heo } 39754c16bd32STejun Heo 3976f7142ed4SLai Jiangshan /* Install the new pwq. */ 39774c16bd32STejun Heo mutex_lock(&wq->mutex); 39784c16bd32STejun Heo old_pwq = numa_pwq_tbl_install(wq, node, pwq); 39794c16bd32STejun Heo goto out_unlock; 39804c16bd32STejun Heo 39814c16bd32STejun Heo use_dfl_pwq: 3982f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 39834c16bd32STejun Heo spin_lock_irq(&wq->dfl_pwq->pool->lock); 39844c16bd32STejun Heo get_pwq(wq->dfl_pwq); 39854c16bd32STejun Heo spin_unlock_irq(&wq->dfl_pwq->pool->lock); 39864c16bd32STejun Heo old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq); 39874c16bd32STejun Heo out_unlock: 39884c16bd32STejun Heo mutex_unlock(&wq->mutex); 39894c16bd32STejun Heo put_pwq_unlocked(old_pwq); 39904c16bd32STejun Heo } 39914c16bd32STejun Heo 399230cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 39931da177e4SLinus Torvalds { 399449e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 39958a2b7538STejun Heo int cpu, ret; 3996e1d8aa9fSFrederic Weisbecker 399730cdf249STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 3998420c0ddbSTejun Heo wq->cpu_pwqs = alloc_percpu(struct pool_workqueue); 3999420c0ddbSTejun Heo if (!wq->cpu_pwqs) 400030cdf249STejun Heo return -ENOMEM; 400130cdf249STejun Heo 400230cdf249STejun Heo for_each_possible_cpu(cpu) { 40037fb98ea7STejun Heo struct pool_workqueue *pwq = 40047fb98ea7STejun Heo per_cpu_ptr(wq->cpu_pwqs, cpu); 40057a62c2c8STejun Heo struct worker_pool *cpu_pools = 4006f02ae73aSTejun Heo per_cpu(cpu_worker_pools, cpu); 400730cdf249STejun Heo 4008f147f29eSTejun Heo init_pwq(pwq, wq, &cpu_pools[highpri]); 4009f147f29eSTejun Heo 4010f147f29eSTejun Heo mutex_lock(&wq->mutex); 40111befcf30STejun Heo link_pwq(pwq); 4012f147f29eSTejun Heo mutex_unlock(&wq->mutex); 401330cdf249STejun Heo } 401430cdf249STejun Heo return 0; 40158a2b7538STejun Heo } else if (wq->flags & __WQ_ORDERED) { 40168a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 40178a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 40188a2b7538STejun Heo WARN(!ret && (wq->pwqs.next != &wq->dfl_pwq->pwqs_node || 40198a2b7538STejun Heo wq->pwqs.prev != &wq->dfl_pwq->pwqs_node), 40208a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 40218a2b7538STejun Heo return ret; 40229e8cd2f5STejun Heo } else { 40239e8cd2f5STejun Heo return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 40249e8cd2f5STejun Heo } 40250f900049STejun Heo } 40260f900049STejun Heo 4027f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 4028f3421797STejun Heo const char *name) 4029b71ab8c2STejun Heo { 4030f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 4031f3421797STejun Heo 4032f3421797STejun Heo if (max_active < 1 || max_active > lim) 4033044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 4034f3421797STejun Heo max_active, name, 1, lim); 4035b71ab8c2STejun Heo 4036f3421797STejun Heo return clamp_val(max_active, 1, lim); 4037b71ab8c2STejun Heo } 4038b71ab8c2STejun Heo 4039983c7515STejun Heo /* 4040983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 4041983c7515STejun Heo * to guarantee forward progress. 4042983c7515STejun Heo */ 4043983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 4044983c7515STejun Heo { 4045983c7515STejun Heo struct worker *rescuer; 4046983c7515STejun Heo int ret; 4047983c7515STejun Heo 4048983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 4049983c7515STejun Heo return 0; 4050983c7515STejun Heo 4051983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 4052983c7515STejun Heo if (!rescuer) 4053983c7515STejun Heo return -ENOMEM; 4054983c7515STejun Heo 4055983c7515STejun Heo rescuer->rescue_wq = wq; 4056983c7515STejun Heo rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", wq->name); 4057983c7515STejun Heo ret = PTR_ERR_OR_ZERO(rescuer->task); 4058983c7515STejun Heo if (ret) { 4059983c7515STejun Heo kfree(rescuer); 4060983c7515STejun Heo return ret; 4061983c7515STejun Heo } 4062983c7515STejun Heo 4063983c7515STejun Heo wq->rescuer = rescuer; 4064983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 4065983c7515STejun Heo wake_up_process(rescuer->task); 4066983c7515STejun Heo 4067983c7515STejun Heo return 0; 4068983c7515STejun Heo } 4069983c7515STejun Heo 4070b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt, 407197e37d7bSTejun Heo unsigned int flags, 40721e19ffc6STejun Heo int max_active, 4073eb13ba87SJohannes Berg struct lock_class_key *key, 4074b196be89STejun Heo const char *lock_name, ...) 40753af24433SOleg Nesterov { 4076df2d5ae4STejun Heo size_t tbl_size = 0; 4077ecf6881fSTejun Heo va_list args; 40783af24433SOleg Nesterov struct workqueue_struct *wq; 407949e3cf44STejun Heo struct pool_workqueue *pwq; 4080b196be89STejun Heo 40815c0338c6STejun Heo /* 40825c0338c6STejun Heo * Unbound && max_active == 1 used to imply ordered, which is no 40835c0338c6STejun Heo * longer the case on NUMA machines due to per-node pools. While 40845c0338c6STejun Heo * alloc_ordered_workqueue() is the right way to create an ordered 40855c0338c6STejun Heo * workqueue, keep the previous behavior to avoid subtle breakages 40865c0338c6STejun Heo * on NUMA. 40875c0338c6STejun Heo */ 40885c0338c6STejun Heo if ((flags & WQ_UNBOUND) && max_active == 1) 40895c0338c6STejun Heo flags |= __WQ_ORDERED; 40905c0338c6STejun Heo 4091cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 4092cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 4093cee22a15SViresh Kumar flags |= WQ_UNBOUND; 4094cee22a15SViresh Kumar 4095ecf6881fSTejun Heo /* allocate wq and format name */ 4096df2d5ae4STejun Heo if (flags & WQ_UNBOUND) 4097ddcb57e2SLai Jiangshan tbl_size = nr_node_ids * sizeof(wq->numa_pwq_tbl[0]); 4098df2d5ae4STejun Heo 4099df2d5ae4STejun Heo wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL); 4100b196be89STejun Heo if (!wq) 4101d2c1d404STejun Heo return NULL; 4102b196be89STejun Heo 41036029a918STejun Heo if (flags & WQ_UNBOUND) { 41046029a918STejun Heo wq->unbound_attrs = alloc_workqueue_attrs(GFP_KERNEL); 41056029a918STejun Heo if (!wq->unbound_attrs) 41066029a918STejun Heo goto err_free_wq; 41076029a918STejun Heo } 41086029a918STejun Heo 4109ecf6881fSTejun Heo va_start(args, lock_name); 4110ecf6881fSTejun Heo vsnprintf(wq->name, sizeof(wq->name), fmt, args); 4111b196be89STejun Heo va_end(args); 41123af24433SOleg Nesterov 4113d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 4114b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 41153af24433SOleg Nesterov 4116b196be89STejun Heo /* init wq */ 411797e37d7bSTejun Heo wq->flags = flags; 4118a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 41193c25a55dSLai Jiangshan mutex_init(&wq->mutex); 4120112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 412130cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 412273f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 412373f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 4124493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 41253af24433SOleg Nesterov 4126eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 4127cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 41283af24433SOleg Nesterov 412930cdf249STejun Heo if (alloc_and_link_pwqs(wq) < 0) 4130d2c1d404STejun Heo goto err_free_wq; 41311537663fSTejun Heo 413240c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 4133d2c1d404STejun Heo goto err_destroy; 4134e22bee78STejun Heo 4135226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 4136226223abSTejun Heo goto err_destroy; 4137226223abSTejun Heo 41386af8bf3dSOleg Nesterov /* 413968e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 414068e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 414168e13a67SLai Jiangshan * list. 41426af8bf3dSOleg Nesterov */ 414368e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4144a0a1a5fdSTejun Heo 4145a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 414649e3cf44STejun Heo for_each_pwq(pwq, wq) 4147699ce097STejun Heo pwq_adjust_max_active(pwq); 4148a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4149a0a1a5fdSTejun Heo 4150e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 4151a0a1a5fdSTejun Heo 415268e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 41533af24433SOleg Nesterov 41543af24433SOleg Nesterov return wq; 4155d2c1d404STejun Heo 4156d2c1d404STejun Heo err_free_wq: 41576029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 41584690c4abSTejun Heo kfree(wq); 4159d2c1d404STejun Heo return NULL; 4160d2c1d404STejun Heo err_destroy: 4161d2c1d404STejun Heo destroy_workqueue(wq); 41624690c4abSTejun Heo return NULL; 41631da177e4SLinus Torvalds } 4164d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key); 41651da177e4SLinus Torvalds 41663af24433SOleg Nesterov /** 41673af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 41683af24433SOleg Nesterov * @wq: target workqueue 41693af24433SOleg Nesterov * 41703af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 41713af24433SOleg Nesterov */ 41723af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 41733af24433SOleg Nesterov { 417449e3cf44STejun Heo struct pool_workqueue *pwq; 41754c16bd32STejun Heo int node; 41763af24433SOleg Nesterov 41779c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 41789c5a2ba7STejun Heo drain_workqueue(wq); 4179c8efcc25STejun Heo 41806183c009STejun Heo /* sanity checks */ 4181b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 418249e3cf44STejun Heo for_each_pwq(pwq, wq) { 41836183c009STejun Heo int i; 41846183c009STejun Heo 418576af4d93STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) { 418676af4d93STejun Heo if (WARN_ON(pwq->nr_in_flight[i])) { 4187b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 4188fa07fb6aSTejun Heo show_workqueue_state(); 41896183c009STejun Heo return; 419076af4d93STejun Heo } 419176af4d93STejun Heo } 419276af4d93STejun Heo 41935c529597SLai Jiangshan if (WARN_ON((pwq != wq->dfl_pwq) && (pwq->refcnt > 1)) || 41948864b4e5STejun Heo WARN_ON(pwq->nr_active) || 419576af4d93STejun Heo WARN_ON(!list_empty(&pwq->delayed_works))) { 4196b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 4197fa07fb6aSTejun Heo show_workqueue_state(); 41986183c009STejun Heo return; 41996183c009STejun Heo } 420076af4d93STejun Heo } 4201b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 42026183c009STejun Heo 4203a0a1a5fdSTejun Heo /* 4204a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 4205a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 4206a0a1a5fdSTejun Heo */ 420768e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4208e2dca7adSTejun Heo list_del_rcu(&wq->list); 420968e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 42103af24433SOleg Nesterov 4211226223abSTejun Heo workqueue_sysfs_unregister(wq); 4212226223abSTejun Heo 4213e2dca7adSTejun Heo if (wq->rescuer) 4214e22bee78STejun Heo kthread_stop(wq->rescuer->task); 4215e22bee78STejun Heo 42168864b4e5STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 421729c91e99STejun Heo /* 42188864b4e5STejun Heo * The base ref is never dropped on per-cpu pwqs. Directly 4219e2dca7adSTejun Heo * schedule RCU free. 422029c91e99STejun Heo */ 422125b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 42228864b4e5STejun Heo } else { 42238864b4e5STejun Heo /* 42248864b4e5STejun Heo * We're the sole accessor of @wq at this point. Directly 42254c16bd32STejun Heo * access numa_pwq_tbl[] and dfl_pwq to put the base refs. 42264c16bd32STejun Heo * @wq will be freed when the last pwq is released. 42278864b4e5STejun Heo */ 42284c16bd32STejun Heo for_each_node(node) { 42294c16bd32STejun Heo pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]); 42304c16bd32STejun Heo RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL); 42314c16bd32STejun Heo put_pwq_unlocked(pwq); 42324c16bd32STejun Heo } 42334c16bd32STejun Heo 42344c16bd32STejun Heo /* 42354c16bd32STejun Heo * Put dfl_pwq. @wq may be freed any time after dfl_pwq is 42364c16bd32STejun Heo * put. Don't access it afterwards. 42374c16bd32STejun Heo */ 42384c16bd32STejun Heo pwq = wq->dfl_pwq; 42394c16bd32STejun Heo wq->dfl_pwq = NULL; 4240dce90d47STejun Heo put_pwq_unlocked(pwq); 424129c91e99STejun Heo } 42423af24433SOleg Nesterov } 42433af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 42443af24433SOleg Nesterov 4245dcd989cbSTejun Heo /** 4246dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 4247dcd989cbSTejun Heo * @wq: target workqueue 4248dcd989cbSTejun Heo * @max_active: new max_active value. 4249dcd989cbSTejun Heo * 4250dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 4251dcd989cbSTejun Heo * 4252dcd989cbSTejun Heo * CONTEXT: 4253dcd989cbSTejun Heo * Don't call from IRQ context. 4254dcd989cbSTejun Heo */ 4255dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 4256dcd989cbSTejun Heo { 425749e3cf44STejun Heo struct pool_workqueue *pwq; 4258dcd989cbSTejun Heo 42598719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 42600a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 42618719dceaSTejun Heo return; 42628719dceaSTejun Heo 4263f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 4264dcd989cbSTejun Heo 4265a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4266dcd989cbSTejun Heo 42670a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 4268dcd989cbSTejun Heo wq->saved_max_active = max_active; 4269dcd989cbSTejun Heo 4270699ce097STejun Heo for_each_pwq(pwq, wq) 4271699ce097STejun Heo pwq_adjust_max_active(pwq); 4272dcd989cbSTejun Heo 4273a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4274dcd989cbSTejun Heo } 4275dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 4276dcd989cbSTejun Heo 4277dcd989cbSTejun Heo /** 427827d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 427927d4ee03SLukas Wunner * 428027d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 428127d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 428227d4ee03SLukas Wunner * 428327d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 428427d4ee03SLukas Wunner */ 428527d4ee03SLukas Wunner struct work_struct *current_work(void) 428627d4ee03SLukas Wunner { 428727d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 428827d4ee03SLukas Wunner 428927d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 429027d4ee03SLukas Wunner } 429127d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 429227d4ee03SLukas Wunner 429327d4ee03SLukas Wunner /** 4294e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 4295e6267616STejun Heo * 4296e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 4297e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 4298d185af30SYacine Belkadi * 4299d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 4300e6267616STejun Heo */ 4301e6267616STejun Heo bool current_is_workqueue_rescuer(void) 4302e6267616STejun Heo { 4303e6267616STejun Heo struct worker *worker = current_wq_worker(); 4304e6267616STejun Heo 43056a092dfdSLai Jiangshan return worker && worker->rescue_wq; 4306e6267616STejun Heo } 4307e6267616STejun Heo 4308e6267616STejun Heo /** 4309dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 4310dcd989cbSTejun Heo * @cpu: CPU in question 4311dcd989cbSTejun Heo * @wq: target workqueue 4312dcd989cbSTejun Heo * 4313dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 4314dcd989cbSTejun Heo * no synchronization around this function and the test result is 4315dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4316dcd989cbSTejun Heo * 4317d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 4318d3251859STejun Heo * Note that both per-cpu and unbound workqueues may be associated with 4319d3251859STejun Heo * multiple pool_workqueues which have separate congested states. A 4320d3251859STejun Heo * workqueue being congested on one CPU doesn't mean the workqueue is also 4321d3251859STejun Heo * contested on other CPUs / NUMA nodes. 4322d3251859STejun Heo * 4323d185af30SYacine Belkadi * Return: 4324dcd989cbSTejun Heo * %true if congested, %false otherwise. 4325dcd989cbSTejun Heo */ 4326d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 4327dcd989cbSTejun Heo { 43287fb98ea7STejun Heo struct pool_workqueue *pwq; 432976af4d93STejun Heo bool ret; 433076af4d93STejun Heo 433188109453SLai Jiangshan rcu_read_lock_sched(); 43327fb98ea7STejun Heo 4333d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 4334d3251859STejun Heo cpu = smp_processor_id(); 4335d3251859STejun Heo 43367fb98ea7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 43377fb98ea7STejun Heo pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 43387fb98ea7STejun Heo else 4339df2d5ae4STejun Heo pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu)); 4340dcd989cbSTejun Heo 434176af4d93STejun Heo ret = !list_empty(&pwq->delayed_works); 434288109453SLai Jiangshan rcu_read_unlock_sched(); 434376af4d93STejun Heo 434476af4d93STejun Heo return ret; 4345dcd989cbSTejun Heo } 4346dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 4347dcd989cbSTejun Heo 4348dcd989cbSTejun Heo /** 4349dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 4350dcd989cbSTejun Heo * @work: the work to be tested 4351dcd989cbSTejun Heo * 4352dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 4353dcd989cbSTejun Heo * synchronization around this function and the test result is 4354dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4355dcd989cbSTejun Heo * 4356d185af30SYacine Belkadi * Return: 4357dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 4358dcd989cbSTejun Heo */ 4359dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 4360dcd989cbSTejun Heo { 4361fa1b54e6STejun Heo struct worker_pool *pool; 4362dcd989cbSTejun Heo unsigned long flags; 4363dcd989cbSTejun Heo unsigned int ret = 0; 4364dcd989cbSTejun Heo 4365dcd989cbSTejun Heo if (work_pending(work)) 4366dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 4367038366c5SLai Jiangshan 4368fa1b54e6STejun Heo local_irq_save(flags); 4369fa1b54e6STejun Heo pool = get_work_pool(work); 4370038366c5SLai Jiangshan if (pool) { 4371fa1b54e6STejun Heo spin_lock(&pool->lock); 4372c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 4373dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 4374fa1b54e6STejun Heo spin_unlock(&pool->lock); 4375038366c5SLai Jiangshan } 4376fa1b54e6STejun Heo local_irq_restore(flags); 4377dcd989cbSTejun Heo 4378dcd989cbSTejun Heo return ret; 4379dcd989cbSTejun Heo } 4380dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 4381dcd989cbSTejun Heo 43823d1cb205STejun Heo /** 43833d1cb205STejun Heo * set_worker_desc - set description for the current work item 43843d1cb205STejun Heo * @fmt: printf-style format string 43853d1cb205STejun Heo * @...: arguments for the format string 43863d1cb205STejun Heo * 43873d1cb205STejun Heo * This function can be called by a running work function to describe what 43883d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 43893d1cb205STejun Heo * information will be printed out together to help debugging. The 43903d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 43913d1cb205STejun Heo */ 43923d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 43933d1cb205STejun Heo { 43943d1cb205STejun Heo struct worker *worker = current_wq_worker(); 43953d1cb205STejun Heo va_list args; 43963d1cb205STejun Heo 43973d1cb205STejun Heo if (worker) { 43983d1cb205STejun Heo va_start(args, fmt); 43993d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 44003d1cb205STejun Heo va_end(args); 44013d1cb205STejun Heo } 44023d1cb205STejun Heo } 44035c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 44043d1cb205STejun Heo 44053d1cb205STejun Heo /** 44063d1cb205STejun Heo * print_worker_info - print out worker information and description 44073d1cb205STejun Heo * @log_lvl: the log level to use when printing 44083d1cb205STejun Heo * @task: target task 44093d1cb205STejun Heo * 44103d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 44113d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 44123d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 44133d1cb205STejun Heo * 44143d1cb205STejun Heo * This function can be safely called on any task as long as the 44153d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 44163d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 44173d1cb205STejun Heo */ 44183d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 44193d1cb205STejun Heo { 44203d1cb205STejun Heo work_func_t *fn = NULL; 44213d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 44223d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 44233d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 44243d1cb205STejun Heo struct workqueue_struct *wq = NULL; 44253d1cb205STejun Heo struct worker *worker; 44263d1cb205STejun Heo 44273d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 44283d1cb205STejun Heo return; 44293d1cb205STejun Heo 44303d1cb205STejun Heo /* 44313d1cb205STejun Heo * This function is called without any synchronization and @task 44323d1cb205STejun Heo * could be in any state. Be careful with dereferences. 44333d1cb205STejun Heo */ 4434e700591aSPetr Mladek worker = kthread_probe_data(task); 44353d1cb205STejun Heo 44363d1cb205STejun Heo /* 44378bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 44388bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 44393d1cb205STejun Heo */ 44403d1cb205STejun Heo probe_kernel_read(&fn, &worker->current_func, sizeof(fn)); 44413d1cb205STejun Heo probe_kernel_read(&pwq, &worker->current_pwq, sizeof(pwq)); 44423d1cb205STejun Heo probe_kernel_read(&wq, &pwq->wq, sizeof(wq)); 44433d1cb205STejun Heo probe_kernel_read(name, wq->name, sizeof(name) - 1); 44443d1cb205STejun Heo probe_kernel_read(desc, worker->desc, sizeof(desc) - 1); 44453d1cb205STejun Heo 44463d1cb205STejun Heo if (fn || name[0] || desc[0]) { 44473d1cb205STejun Heo printk("%sWorkqueue: %s %pf", log_lvl, name, fn); 44488bf89593STejun Heo if (strcmp(name, desc)) 44493d1cb205STejun Heo pr_cont(" (%s)", desc); 44503d1cb205STejun Heo pr_cont("\n"); 44513d1cb205STejun Heo } 44523d1cb205STejun Heo } 44533d1cb205STejun Heo 44543494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 44553494fc30STejun Heo { 44563494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 44573494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 44583494fc30STejun Heo pr_cont(" node=%d", pool->node); 44593494fc30STejun Heo pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice); 44603494fc30STejun Heo } 44613494fc30STejun Heo 44623494fc30STejun Heo static void pr_cont_work(bool comma, struct work_struct *work) 44633494fc30STejun Heo { 44643494fc30STejun Heo if (work->func == wq_barrier_func) { 44653494fc30STejun Heo struct wq_barrier *barr; 44663494fc30STejun Heo 44673494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 44683494fc30STejun Heo 44693494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 44703494fc30STejun Heo task_pid_nr(barr->task)); 44713494fc30STejun Heo } else { 44723494fc30STejun Heo pr_cont("%s %pf", comma ? "," : "", work->func); 44733494fc30STejun Heo } 44743494fc30STejun Heo } 44753494fc30STejun Heo 44763494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 44773494fc30STejun Heo { 44783494fc30STejun Heo struct worker_pool *pool = pwq->pool; 44793494fc30STejun Heo struct work_struct *work; 44803494fc30STejun Heo struct worker *worker; 44813494fc30STejun Heo bool has_in_flight = false, has_pending = false; 44823494fc30STejun Heo int bkt; 44833494fc30STejun Heo 44843494fc30STejun Heo pr_info(" pwq %d:", pool->id); 44853494fc30STejun Heo pr_cont_pool_info(pool); 44863494fc30STejun Heo 44873494fc30STejun Heo pr_cont(" active=%d/%d%s\n", pwq->nr_active, pwq->max_active, 44883494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 44893494fc30STejun Heo 44903494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 44913494fc30STejun Heo if (worker->current_pwq == pwq) { 44923494fc30STejun Heo has_in_flight = true; 44933494fc30STejun Heo break; 44943494fc30STejun Heo } 44953494fc30STejun Heo } 44963494fc30STejun Heo if (has_in_flight) { 44973494fc30STejun Heo bool comma = false; 44983494fc30STejun Heo 44993494fc30STejun Heo pr_info(" in-flight:"); 45003494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 45013494fc30STejun Heo if (worker->current_pwq != pwq) 45023494fc30STejun Heo continue; 45033494fc30STejun Heo 45043494fc30STejun Heo pr_cont("%s %d%s:%pf", comma ? "," : "", 45053494fc30STejun Heo task_pid_nr(worker->task), 45063494fc30STejun Heo worker == pwq->wq->rescuer ? "(RESCUER)" : "", 45073494fc30STejun Heo worker->current_func); 45083494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 45093494fc30STejun Heo pr_cont_work(false, work); 45103494fc30STejun Heo comma = true; 45113494fc30STejun Heo } 45123494fc30STejun Heo pr_cont("\n"); 45133494fc30STejun Heo } 45143494fc30STejun Heo 45153494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 45163494fc30STejun Heo if (get_work_pwq(work) == pwq) { 45173494fc30STejun Heo has_pending = true; 45183494fc30STejun Heo break; 45193494fc30STejun Heo } 45203494fc30STejun Heo } 45213494fc30STejun Heo if (has_pending) { 45223494fc30STejun Heo bool comma = false; 45233494fc30STejun Heo 45243494fc30STejun Heo pr_info(" pending:"); 45253494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 45263494fc30STejun Heo if (get_work_pwq(work) != pwq) 45273494fc30STejun Heo continue; 45283494fc30STejun Heo 45293494fc30STejun Heo pr_cont_work(comma, work); 45303494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 45313494fc30STejun Heo } 45323494fc30STejun Heo pr_cont("\n"); 45333494fc30STejun Heo } 45343494fc30STejun Heo 45353494fc30STejun Heo if (!list_empty(&pwq->delayed_works)) { 45363494fc30STejun Heo bool comma = false; 45373494fc30STejun Heo 45383494fc30STejun Heo pr_info(" delayed:"); 45393494fc30STejun Heo list_for_each_entry(work, &pwq->delayed_works, entry) { 45403494fc30STejun Heo pr_cont_work(comma, work); 45413494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 45423494fc30STejun Heo } 45433494fc30STejun Heo pr_cont("\n"); 45443494fc30STejun Heo } 45453494fc30STejun Heo } 45463494fc30STejun Heo 45473494fc30STejun Heo /** 45483494fc30STejun Heo * show_workqueue_state - dump workqueue state 45493494fc30STejun Heo * 45507b776af6SRoger Lu * Called from a sysrq handler or try_to_freeze_tasks() and prints out 45517b776af6SRoger Lu * all busy workqueues and pools. 45523494fc30STejun Heo */ 45533494fc30STejun Heo void show_workqueue_state(void) 45543494fc30STejun Heo { 45553494fc30STejun Heo struct workqueue_struct *wq; 45563494fc30STejun Heo struct worker_pool *pool; 45573494fc30STejun Heo unsigned long flags; 45583494fc30STejun Heo int pi; 45593494fc30STejun Heo 45603494fc30STejun Heo rcu_read_lock_sched(); 45613494fc30STejun Heo 45623494fc30STejun Heo pr_info("Showing busy workqueues and worker pools:\n"); 45633494fc30STejun Heo 45643494fc30STejun Heo list_for_each_entry_rcu(wq, &workqueues, list) { 45653494fc30STejun Heo struct pool_workqueue *pwq; 45663494fc30STejun Heo bool idle = true; 45673494fc30STejun Heo 45683494fc30STejun Heo for_each_pwq(pwq, wq) { 45693494fc30STejun Heo if (pwq->nr_active || !list_empty(&pwq->delayed_works)) { 45703494fc30STejun Heo idle = false; 45713494fc30STejun Heo break; 45723494fc30STejun Heo } 45733494fc30STejun Heo } 45743494fc30STejun Heo if (idle) 45753494fc30STejun Heo continue; 45763494fc30STejun Heo 45773494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 45783494fc30STejun Heo 45793494fc30STejun Heo for_each_pwq(pwq, wq) { 45803494fc30STejun Heo spin_lock_irqsave(&pwq->pool->lock, flags); 45813494fc30STejun Heo if (pwq->nr_active || !list_empty(&pwq->delayed_works)) 45823494fc30STejun Heo show_pwq(pwq); 45833494fc30STejun Heo spin_unlock_irqrestore(&pwq->pool->lock, flags); 458462635ea8SSergey Senozhatsky /* 458562635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 458662635ea8SSergey Senozhatsky * sysrq-t -> show_workqueue_state(). Avoid triggering 458762635ea8SSergey Senozhatsky * hard lockup. 458862635ea8SSergey Senozhatsky */ 458962635ea8SSergey Senozhatsky touch_nmi_watchdog(); 45903494fc30STejun Heo } 45913494fc30STejun Heo } 45923494fc30STejun Heo 45933494fc30STejun Heo for_each_pool(pool, pi) { 45943494fc30STejun Heo struct worker *worker; 45953494fc30STejun Heo bool first = true; 45963494fc30STejun Heo 45973494fc30STejun Heo spin_lock_irqsave(&pool->lock, flags); 45983494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 45993494fc30STejun Heo goto next_pool; 46003494fc30STejun Heo 46013494fc30STejun Heo pr_info("pool %d:", pool->id); 46023494fc30STejun Heo pr_cont_pool_info(pool); 460382607adcSTejun Heo pr_cont(" hung=%us workers=%d", 460482607adcSTejun Heo jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000, 460582607adcSTejun Heo pool->nr_workers); 46063494fc30STejun Heo if (pool->manager) 46073494fc30STejun Heo pr_cont(" manager: %d", 46083494fc30STejun Heo task_pid_nr(pool->manager->task)); 46093494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 46103494fc30STejun Heo pr_cont(" %s%d", first ? "idle: " : "", 46113494fc30STejun Heo task_pid_nr(worker->task)); 46123494fc30STejun Heo first = false; 46133494fc30STejun Heo } 46143494fc30STejun Heo pr_cont("\n"); 46153494fc30STejun Heo next_pool: 46163494fc30STejun Heo spin_unlock_irqrestore(&pool->lock, flags); 461762635ea8SSergey Senozhatsky /* 461862635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 461962635ea8SSergey Senozhatsky * sysrq-t -> show_workqueue_state(). Avoid triggering 462062635ea8SSergey Senozhatsky * hard lockup. 462162635ea8SSergey Senozhatsky */ 462262635ea8SSergey Senozhatsky touch_nmi_watchdog(); 46233494fc30STejun Heo } 46243494fc30STejun Heo 46253494fc30STejun Heo rcu_read_unlock_sched(); 46263494fc30STejun Heo } 46273494fc30STejun Heo 46286b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 46296b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 46306b59808bSTejun Heo { 46316b59808bSTejun Heo int off; 46326b59808bSTejun Heo 46336b59808bSTejun Heo /* always show the actual comm */ 46346b59808bSTejun Heo off = strscpy(buf, task->comm, size); 46356b59808bSTejun Heo if (off < 0) 46366b59808bSTejun Heo return; 46376b59808bSTejun Heo 4638197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 46396b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 46406b59808bSTejun Heo 4641197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 4642197f6accSTejun Heo struct worker *worker = kthread_data(task); 4643197f6accSTejun Heo struct worker_pool *pool = worker->pool; 46446b59808bSTejun Heo 46456b59808bSTejun Heo if (pool) { 46466b59808bSTejun Heo spin_lock_irq(&pool->lock); 46476b59808bSTejun Heo /* 4648197f6accSTejun Heo * ->desc tracks information (wq name or 4649197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 4650197f6accSTejun Heo * current, prepend '+', otherwise '-'. 46516b59808bSTejun Heo */ 46526b59808bSTejun Heo if (worker->desc[0] != '\0') { 46536b59808bSTejun Heo if (worker->current_work) 46546b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 46556b59808bSTejun Heo worker->desc); 46566b59808bSTejun Heo else 46576b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 46586b59808bSTejun Heo worker->desc); 46596b59808bSTejun Heo } 46606b59808bSTejun Heo spin_unlock_irq(&pool->lock); 46616b59808bSTejun Heo } 4662197f6accSTejun Heo } 46636b59808bSTejun Heo 46646b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 46656b59808bSTejun Heo } 46666b59808bSTejun Heo 466766448bc2SMathieu Malaterre #ifdef CONFIG_SMP 466866448bc2SMathieu Malaterre 4669db7bccf4STejun Heo /* 4670db7bccf4STejun Heo * CPU hotplug. 4671db7bccf4STejun Heo * 4672e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 4673112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 4674706026c2STejun Heo * pool which make migrating pending and scheduled works very 4675e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 467694cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 4677e22bee78STejun Heo * blocked draining impractical. 4678e22bee78STejun Heo * 467924647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 4680628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 4681628c78e7STejun Heo * cpu comes back online. 4682db7bccf4STejun Heo */ 4683db7bccf4STejun Heo 4684e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 4685db7bccf4STejun Heo { 46864ce62e9eSTejun Heo struct worker_pool *pool; 4687db7bccf4STejun Heo struct worker *worker; 4688db7bccf4STejun Heo 4689f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 46901258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 469194cf58bbSTejun Heo spin_lock_irq(&pool->lock); 4692e22bee78STejun Heo 4693f2d5a0eeSTejun Heo /* 469492f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 469594cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 469694cf58bbSTejun Heo * except for the ones which are still executing works from 469794cf58bbSTejun Heo * before the last CPU down must be on the cpu. After 469894cf58bbSTejun Heo * this, they may become diasporas. 4699f2d5a0eeSTejun Heo */ 4700da028469SLai Jiangshan for_each_pool_worker(worker, pool) 4701403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 4702db7bccf4STejun Heo 470324647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 4704f2d5a0eeSTejun Heo 470594cf58bbSTejun Heo spin_unlock_irq(&pool->lock); 47061258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 4707e22bee78STejun Heo 4708e22bee78STejun Heo /* 4709eb283428SLai Jiangshan * Call schedule() so that we cross rq->lock and thus can 4710eb283428SLai Jiangshan * guarantee sched callbacks see the %WORKER_UNBOUND flag. 4711eb283428SLai Jiangshan * This is necessary as scheduler callbacks may be invoked 4712eb283428SLai Jiangshan * from other cpus. 4713628c78e7STejun Heo */ 4714628c78e7STejun Heo schedule(); 4715628c78e7STejun Heo 4716628c78e7STejun Heo /* 4717eb283428SLai Jiangshan * Sched callbacks are disabled now. Zap nr_running. 4718eb283428SLai Jiangshan * After this, nr_running stays zero and need_more_worker() 4719eb283428SLai Jiangshan * and keep_working() are always true as long as the 4720eb283428SLai Jiangshan * worklist is not empty. This pool now behaves as an 4721eb283428SLai Jiangshan * unbound (in terms of concurrency management) pool which 4722eb283428SLai Jiangshan * are served by workers tied to the pool. 4723e22bee78STejun Heo */ 4724e19e397aSTejun Heo atomic_set(&pool->nr_running, 0); 4725eb283428SLai Jiangshan 4726eb283428SLai Jiangshan /* 4727eb283428SLai Jiangshan * With concurrency management just turned off, a busy 4728eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 4729eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 4730eb283428SLai Jiangshan */ 4731eb283428SLai Jiangshan spin_lock_irq(&pool->lock); 4732eb283428SLai Jiangshan wake_up_worker(pool); 4733eb283428SLai Jiangshan spin_unlock_irq(&pool->lock); 4734eb283428SLai Jiangshan } 4735db7bccf4STejun Heo } 4736db7bccf4STejun Heo 4737bd7c089eSTejun Heo /** 4738bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 4739bd7c089eSTejun Heo * @pool: pool of interest 4740bd7c089eSTejun Heo * 4741a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 4742bd7c089eSTejun Heo */ 4743bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 4744bd7c089eSTejun Heo { 4745a9ab775bSTejun Heo struct worker *worker; 4746bd7c089eSTejun Heo 47471258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 4748bd7c089eSTejun Heo 4749bd7c089eSTejun Heo /* 4750a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 4751a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 4752402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 4753a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 4754a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 4755bd7c089eSTejun Heo */ 4756da028469SLai Jiangshan for_each_pool_worker(worker, pool) 4757a9ab775bSTejun Heo WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 4758a9ab775bSTejun Heo pool->attrs->cpumask) < 0); 4759a9ab775bSTejun Heo 4760a9ab775bSTejun Heo spin_lock_irq(&pool->lock); 4761f7c17d26SWanpeng Li 47623de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 4763a9ab775bSTejun Heo 4764da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 4765a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 4766a9ab775bSTejun Heo 4767a9ab775bSTejun Heo /* 4768a9ab775bSTejun Heo * A bound idle worker should actually be on the runqueue 4769a9ab775bSTejun Heo * of the associated CPU for local wake-ups targeting it to 4770a9ab775bSTejun Heo * work. Kick all idle workers so that they migrate to the 4771a9ab775bSTejun Heo * associated CPU. Doing this in the same loop as 4772a9ab775bSTejun Heo * replacing UNBOUND with REBOUND is safe as no worker will 4773a9ab775bSTejun Heo * be bound before @pool->lock is released. 4774a9ab775bSTejun Heo */ 4775a9ab775bSTejun Heo if (worker_flags & WORKER_IDLE) 4776bd7c089eSTejun Heo wake_up_process(worker->task); 4777bd7c089eSTejun Heo 4778bd7c089eSTejun Heo /* 4779a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 4780a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 4781a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 4782a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 4783a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 4784a9ab775bSTejun Heo * concurrency management. Note that when or whether 4785a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 4786a9ab775bSTejun Heo * 4787c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 4788a9ab775bSTejun Heo * tested without holding any lock in 4789a9ab775bSTejun Heo * wq_worker_waking_up(). Without it, NOT_RUNNING test may 4790a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 4791a9ab775bSTejun Heo * management operations. 4792bd7c089eSTejun Heo */ 4793a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 4794a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 4795a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 4796c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 4797bd7c089eSTejun Heo } 4798a9ab775bSTejun Heo 4799a9ab775bSTejun Heo spin_unlock_irq(&pool->lock); 4800bd7c089eSTejun Heo } 4801bd7c089eSTejun Heo 48027dbc725eSTejun Heo /** 48037dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 48047dbc725eSTejun Heo * @pool: unbound pool of interest 48057dbc725eSTejun Heo * @cpu: the CPU which is coming up 48067dbc725eSTejun Heo * 48077dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 48087dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 48097dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 48107dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 48117dbc725eSTejun Heo */ 48127dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 48137dbc725eSTejun Heo { 48147dbc725eSTejun Heo static cpumask_t cpumask; 48157dbc725eSTejun Heo struct worker *worker; 48167dbc725eSTejun Heo 48171258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 48187dbc725eSTejun Heo 48197dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 48207dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 48217dbc725eSTejun Heo return; 48227dbc725eSTejun Heo 48237dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 48247dbc725eSTejun Heo 48257dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 4826da028469SLai Jiangshan for_each_pool_worker(worker, pool) 4827d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 48287dbc725eSTejun Heo } 48297dbc725eSTejun Heo 48307ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 48311da177e4SLinus Torvalds { 48324ce62e9eSTejun Heo struct worker_pool *pool; 48331da177e4SLinus Torvalds 4834f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 48353ce63377STejun Heo if (pool->nr_workers) 48363ce63377STejun Heo continue; 4837051e1850SLai Jiangshan if (!create_worker(pool)) 48387ee681b2SThomas Gleixner return -ENOMEM; 48393af24433SOleg Nesterov } 48407ee681b2SThomas Gleixner return 0; 48417ee681b2SThomas Gleixner } 48421da177e4SLinus Torvalds 48437ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 48447ee681b2SThomas Gleixner { 48457ee681b2SThomas Gleixner struct worker_pool *pool; 48467ee681b2SThomas Gleixner struct workqueue_struct *wq; 48477ee681b2SThomas Gleixner int pi; 48487ee681b2SThomas Gleixner 484968e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 48507dbc725eSTejun Heo 48517dbc725eSTejun Heo for_each_pool(pool, pi) { 48521258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 485394cf58bbSTejun Heo 4854f05b558dSLai Jiangshan if (pool->cpu == cpu) 485594cf58bbSTejun Heo rebind_workers(pool); 4856f05b558dSLai Jiangshan else if (pool->cpu < 0) 48577dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 485894cf58bbSTejun Heo 48591258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 486094cf58bbSTejun Heo } 48617dbc725eSTejun Heo 48624c16bd32STejun Heo /* update NUMA affinity of unbound workqueues */ 48634c16bd32STejun Heo list_for_each_entry(wq, &workqueues, list) 48644c16bd32STejun Heo wq_update_unbound_numa(wq, cpu, true); 48654c16bd32STejun Heo 486668e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 48677ee681b2SThomas Gleixner return 0; 486865758202STejun Heo } 486965758202STejun Heo 48707ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 487165758202STejun Heo { 48724c16bd32STejun Heo struct workqueue_struct *wq; 48738db25e78STejun Heo 48744c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 4875e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 4876e8b3f8dbSLai Jiangshan return -1; 4877e8b3f8dbSLai Jiangshan 4878e8b3f8dbSLai Jiangshan unbind_workers(cpu); 48794c16bd32STejun Heo 48804c16bd32STejun Heo /* update NUMA affinity of unbound workqueues */ 48814c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 48824c16bd32STejun Heo list_for_each_entry(wq, &workqueues, list) 48834c16bd32STejun Heo wq_update_unbound_numa(wq, cpu, false); 48844c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 48854c16bd32STejun Heo 48867ee681b2SThomas Gleixner return 0; 488765758202STejun Heo } 488865758202STejun Heo 48892d3854a3SRusty Russell struct work_for_cpu { 4890ed48ece2STejun Heo struct work_struct work; 48912d3854a3SRusty Russell long (*fn)(void *); 48922d3854a3SRusty Russell void *arg; 48932d3854a3SRusty Russell long ret; 48942d3854a3SRusty Russell }; 48952d3854a3SRusty Russell 4896ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 48972d3854a3SRusty Russell { 4898ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 4899ed48ece2STejun Heo 49002d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 49012d3854a3SRusty Russell } 49022d3854a3SRusty Russell 49032d3854a3SRusty Russell /** 490422aceb31SAnna-Maria Gleixner * work_on_cpu - run a function in thread context on a particular cpu 49052d3854a3SRusty Russell * @cpu: the cpu to run on 49062d3854a3SRusty Russell * @fn: the function to run 49072d3854a3SRusty Russell * @arg: the function arg 49082d3854a3SRusty Russell * 490931ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 49106b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 4911d185af30SYacine Belkadi * 4912d185af30SYacine Belkadi * Return: The value @fn returns. 49132d3854a3SRusty Russell */ 4914d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg) 49152d3854a3SRusty Russell { 4916ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 49172d3854a3SRusty Russell 4918ed48ece2STejun Heo INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn); 4919ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 492012997d1aSBjorn Helgaas flush_work(&wfc.work); 4921440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 49222d3854a3SRusty Russell return wfc.ret; 49232d3854a3SRusty Russell } 49242d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 49250e8d6a93SThomas Gleixner 49260e8d6a93SThomas Gleixner /** 49270e8d6a93SThomas Gleixner * work_on_cpu_safe - run a function in thread context on a particular cpu 49280e8d6a93SThomas Gleixner * @cpu: the cpu to run on 49290e8d6a93SThomas Gleixner * @fn: the function to run 49300e8d6a93SThomas Gleixner * @arg: the function argument 49310e8d6a93SThomas Gleixner * 49320e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 49330e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 49340e8d6a93SThomas Gleixner * 49350e8d6a93SThomas Gleixner * Return: The value @fn returns. 49360e8d6a93SThomas Gleixner */ 49370e8d6a93SThomas Gleixner long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg) 49380e8d6a93SThomas Gleixner { 49390e8d6a93SThomas Gleixner long ret = -ENODEV; 49400e8d6a93SThomas Gleixner 49410e8d6a93SThomas Gleixner get_online_cpus(); 49420e8d6a93SThomas Gleixner if (cpu_online(cpu)) 49430e8d6a93SThomas Gleixner ret = work_on_cpu(cpu, fn, arg); 49440e8d6a93SThomas Gleixner put_online_cpus(); 49450e8d6a93SThomas Gleixner return ret; 49460e8d6a93SThomas Gleixner } 49470e8d6a93SThomas Gleixner EXPORT_SYMBOL_GPL(work_on_cpu_safe); 49482d3854a3SRusty Russell #endif /* CONFIG_SMP */ 49492d3854a3SRusty Russell 4950a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 4951e7577c50SRusty Russell 4952a0a1a5fdSTejun Heo /** 4953a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 4954a0a1a5fdSTejun Heo * 495558a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 4956c5aa87bbSTejun Heo * workqueues will queue new works to their delayed_works list instead of 4957706026c2STejun Heo * pool->worklist. 4958a0a1a5fdSTejun Heo * 4959a0a1a5fdSTejun Heo * CONTEXT: 4960a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 4961a0a1a5fdSTejun Heo */ 4962a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 4963a0a1a5fdSTejun Heo { 496424b8a847STejun Heo struct workqueue_struct *wq; 496524b8a847STejun Heo struct pool_workqueue *pwq; 4966a0a1a5fdSTejun Heo 496768e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4968a0a1a5fdSTejun Heo 49696183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 4970a0a1a5fdSTejun Heo workqueue_freezing = true; 4971a0a1a5fdSTejun Heo 497224b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 4973a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4974699ce097STejun Heo for_each_pwq(pwq, wq) 4975699ce097STejun Heo pwq_adjust_max_active(pwq); 4976a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4977a1056305STejun Heo } 49785bcab335STejun Heo 497968e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 4980a0a1a5fdSTejun Heo } 4981a0a1a5fdSTejun Heo 4982a0a1a5fdSTejun Heo /** 498358a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 4984a0a1a5fdSTejun Heo * 4985a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 4986a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 4987a0a1a5fdSTejun Heo * 4988a0a1a5fdSTejun Heo * CONTEXT: 498968e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 4990a0a1a5fdSTejun Heo * 4991d185af30SYacine Belkadi * Return: 499258a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 499358a69cb4STejun Heo * is complete. 4994a0a1a5fdSTejun Heo */ 4995a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 4996a0a1a5fdSTejun Heo { 4997a0a1a5fdSTejun Heo bool busy = false; 499824b8a847STejun Heo struct workqueue_struct *wq; 499924b8a847STejun Heo struct pool_workqueue *pwq; 5000a0a1a5fdSTejun Heo 500168e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5002a0a1a5fdSTejun Heo 50036183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 5004a0a1a5fdSTejun Heo 500524b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 500624b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 500724b8a847STejun Heo continue; 5008a0a1a5fdSTejun Heo /* 5009a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 5010a0a1a5fdSTejun Heo * to peek without lock. 5011a0a1a5fdSTejun Heo */ 501288109453SLai Jiangshan rcu_read_lock_sched(); 501324b8a847STejun Heo for_each_pwq(pwq, wq) { 50146183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 5015112202d9STejun Heo if (pwq->nr_active) { 5016a0a1a5fdSTejun Heo busy = true; 501788109453SLai Jiangshan rcu_read_unlock_sched(); 5018a0a1a5fdSTejun Heo goto out_unlock; 5019a0a1a5fdSTejun Heo } 5020a0a1a5fdSTejun Heo } 502188109453SLai Jiangshan rcu_read_unlock_sched(); 5022a0a1a5fdSTejun Heo } 5023a0a1a5fdSTejun Heo out_unlock: 502468e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5025a0a1a5fdSTejun Heo return busy; 5026a0a1a5fdSTejun Heo } 5027a0a1a5fdSTejun Heo 5028a0a1a5fdSTejun Heo /** 5029a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 5030a0a1a5fdSTejun Heo * 5031a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 5032706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 5033a0a1a5fdSTejun Heo * 5034a0a1a5fdSTejun Heo * CONTEXT: 5035a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5036a0a1a5fdSTejun Heo */ 5037a0a1a5fdSTejun Heo void thaw_workqueues(void) 5038a0a1a5fdSTejun Heo { 503924b8a847STejun Heo struct workqueue_struct *wq; 504024b8a847STejun Heo struct pool_workqueue *pwq; 5041a0a1a5fdSTejun Heo 504268e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5043a0a1a5fdSTejun Heo 5044a0a1a5fdSTejun Heo if (!workqueue_freezing) 5045a0a1a5fdSTejun Heo goto out_unlock; 5046a0a1a5fdSTejun Heo 504774b414eaSLai Jiangshan workqueue_freezing = false; 504824b8a847STejun Heo 504924b8a847STejun Heo /* restore max_active and repopulate worklist */ 505024b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 5051a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5052699ce097STejun Heo for_each_pwq(pwq, wq) 5053699ce097STejun Heo pwq_adjust_max_active(pwq); 5054a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 505524b8a847STejun Heo } 505624b8a847STejun Heo 5057a0a1a5fdSTejun Heo out_unlock: 505868e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5059a0a1a5fdSTejun Heo } 5060a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 5061a0a1a5fdSTejun Heo 5062042f7df1SLai Jiangshan static int workqueue_apply_unbound_cpumask(void) 5063042f7df1SLai Jiangshan { 5064042f7df1SLai Jiangshan LIST_HEAD(ctxs); 5065042f7df1SLai Jiangshan int ret = 0; 5066042f7df1SLai Jiangshan struct workqueue_struct *wq; 5067042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 5068042f7df1SLai Jiangshan 5069042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5070042f7df1SLai Jiangshan 5071042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 5072042f7df1SLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 5073042f7df1SLai Jiangshan continue; 5074042f7df1SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 5075042f7df1SLai Jiangshan if (wq->flags & __WQ_ORDERED) 5076042f7df1SLai Jiangshan continue; 5077042f7df1SLai Jiangshan 5078042f7df1SLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs); 5079042f7df1SLai Jiangshan if (!ctx) { 5080042f7df1SLai Jiangshan ret = -ENOMEM; 5081042f7df1SLai Jiangshan break; 5082042f7df1SLai Jiangshan } 5083042f7df1SLai Jiangshan 5084042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 5085042f7df1SLai Jiangshan } 5086042f7df1SLai Jiangshan 5087042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 5088042f7df1SLai Jiangshan if (!ret) 5089042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 5090042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 5091042f7df1SLai Jiangshan } 5092042f7df1SLai Jiangshan 5093042f7df1SLai Jiangshan return ret; 5094042f7df1SLai Jiangshan } 5095042f7df1SLai Jiangshan 5096042f7df1SLai Jiangshan /** 5097042f7df1SLai Jiangshan * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 5098042f7df1SLai Jiangshan * @cpumask: the cpumask to set 5099042f7df1SLai Jiangshan * 5100042f7df1SLai Jiangshan * The low-level workqueues cpumask is a global cpumask that limits 5101042f7df1SLai Jiangshan * the affinity of all unbound workqueues. This function check the @cpumask 5102042f7df1SLai Jiangshan * and apply it to all unbound workqueues and updates all pwqs of them. 5103042f7df1SLai Jiangshan * 5104042f7df1SLai Jiangshan * Retun: 0 - Success 5105042f7df1SLai Jiangshan * -EINVAL - Invalid @cpumask 5106042f7df1SLai Jiangshan * -ENOMEM - Failed to allocate memory for attrs or pwqs. 5107042f7df1SLai Jiangshan */ 5108042f7df1SLai Jiangshan int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 5109042f7df1SLai Jiangshan { 5110042f7df1SLai Jiangshan int ret = -EINVAL; 5111042f7df1SLai Jiangshan cpumask_var_t saved_cpumask; 5112042f7df1SLai Jiangshan 5113042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&saved_cpumask, GFP_KERNEL)) 5114042f7df1SLai Jiangshan return -ENOMEM; 5115042f7df1SLai Jiangshan 5116c98a9805STal Shorer /* 5117c98a9805STal Shorer * Not excluding isolated cpus on purpose. 5118c98a9805STal Shorer * If the user wishes to include them, we allow that. 5119c98a9805STal Shorer */ 5120042f7df1SLai Jiangshan cpumask_and(cpumask, cpumask, cpu_possible_mask); 5121042f7df1SLai Jiangshan if (!cpumask_empty(cpumask)) { 5122a0111cf6SLai Jiangshan apply_wqattrs_lock(); 5123042f7df1SLai Jiangshan 5124042f7df1SLai Jiangshan /* save the old wq_unbound_cpumask. */ 5125042f7df1SLai Jiangshan cpumask_copy(saved_cpumask, wq_unbound_cpumask); 5126042f7df1SLai Jiangshan 5127042f7df1SLai Jiangshan /* update wq_unbound_cpumask at first and apply it to wqs. */ 5128042f7df1SLai Jiangshan cpumask_copy(wq_unbound_cpumask, cpumask); 5129042f7df1SLai Jiangshan ret = workqueue_apply_unbound_cpumask(); 5130042f7df1SLai Jiangshan 5131042f7df1SLai Jiangshan /* restore the wq_unbound_cpumask when failed. */ 5132042f7df1SLai Jiangshan if (ret < 0) 5133042f7df1SLai Jiangshan cpumask_copy(wq_unbound_cpumask, saved_cpumask); 5134042f7df1SLai Jiangshan 5135a0111cf6SLai Jiangshan apply_wqattrs_unlock(); 5136042f7df1SLai Jiangshan } 5137042f7df1SLai Jiangshan 5138042f7df1SLai Jiangshan free_cpumask_var(saved_cpumask); 5139042f7df1SLai Jiangshan return ret; 5140042f7df1SLai Jiangshan } 5141042f7df1SLai Jiangshan 51426ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 51436ba94429SFrederic Weisbecker /* 51446ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 51456ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 51466ba94429SFrederic Weisbecker * following attributes. 51476ba94429SFrederic Weisbecker * 51486ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 51496ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 51506ba94429SFrederic Weisbecker * 51516ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 51526ba94429SFrederic Weisbecker * 51539a19b463SWang Long * pool_ids RO int : the associated pool IDs for each node 51546ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 51556ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 51569a19b463SWang Long * numa RW bool : whether enable NUMA affinity 51576ba94429SFrederic Weisbecker */ 51586ba94429SFrederic Weisbecker struct wq_device { 51596ba94429SFrederic Weisbecker struct workqueue_struct *wq; 51606ba94429SFrederic Weisbecker struct device dev; 51616ba94429SFrederic Weisbecker }; 51626ba94429SFrederic Weisbecker 51636ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 51646ba94429SFrederic Weisbecker { 51656ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 51666ba94429SFrederic Weisbecker 51676ba94429SFrederic Weisbecker return wq_dev->wq; 51686ba94429SFrederic Weisbecker } 51696ba94429SFrederic Weisbecker 51706ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 51716ba94429SFrederic Weisbecker char *buf) 51726ba94429SFrederic Weisbecker { 51736ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 51746ba94429SFrederic Weisbecker 51756ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 51766ba94429SFrederic Weisbecker } 51776ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 51786ba94429SFrederic Weisbecker 51796ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 51806ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 51816ba94429SFrederic Weisbecker { 51826ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 51836ba94429SFrederic Weisbecker 51846ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 51856ba94429SFrederic Weisbecker } 51866ba94429SFrederic Weisbecker 51876ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 51886ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 51896ba94429SFrederic Weisbecker size_t count) 51906ba94429SFrederic Weisbecker { 51916ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 51926ba94429SFrederic Weisbecker int val; 51936ba94429SFrederic Weisbecker 51946ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 51956ba94429SFrederic Weisbecker return -EINVAL; 51966ba94429SFrederic Weisbecker 51976ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 51986ba94429SFrederic Weisbecker return count; 51996ba94429SFrederic Weisbecker } 52006ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 52016ba94429SFrederic Weisbecker 52026ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 52036ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 52046ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 52056ba94429SFrederic Weisbecker NULL, 52066ba94429SFrederic Weisbecker }; 52076ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 52086ba94429SFrederic Weisbecker 52096ba94429SFrederic Weisbecker static ssize_t wq_pool_ids_show(struct device *dev, 52106ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 52116ba94429SFrederic Weisbecker { 52126ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 52136ba94429SFrederic Weisbecker const char *delim = ""; 52146ba94429SFrederic Weisbecker int node, written = 0; 52156ba94429SFrederic Weisbecker 52166ba94429SFrederic Weisbecker rcu_read_lock_sched(); 52176ba94429SFrederic Weisbecker for_each_node(node) { 52186ba94429SFrederic Weisbecker written += scnprintf(buf + written, PAGE_SIZE - written, 52196ba94429SFrederic Weisbecker "%s%d:%d", delim, node, 52206ba94429SFrederic Weisbecker unbound_pwq_by_node(wq, node)->pool->id); 52216ba94429SFrederic Weisbecker delim = " "; 52226ba94429SFrederic Weisbecker } 52236ba94429SFrederic Weisbecker written += scnprintf(buf + written, PAGE_SIZE - written, "\n"); 52246ba94429SFrederic Weisbecker rcu_read_unlock_sched(); 52256ba94429SFrederic Weisbecker 52266ba94429SFrederic Weisbecker return written; 52276ba94429SFrederic Weisbecker } 52286ba94429SFrederic Weisbecker 52296ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 52306ba94429SFrederic Weisbecker char *buf) 52316ba94429SFrederic Weisbecker { 52326ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 52336ba94429SFrederic Weisbecker int written; 52346ba94429SFrederic Weisbecker 52356ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 52366ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 52376ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 52386ba94429SFrederic Weisbecker 52396ba94429SFrederic Weisbecker return written; 52406ba94429SFrederic Weisbecker } 52416ba94429SFrederic Weisbecker 52426ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 52436ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 52446ba94429SFrederic Weisbecker { 52456ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 52466ba94429SFrederic Weisbecker 5247899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5248899a94feSLai Jiangshan 52496ba94429SFrederic Weisbecker attrs = alloc_workqueue_attrs(GFP_KERNEL); 52506ba94429SFrederic Weisbecker if (!attrs) 52516ba94429SFrederic Weisbecker return NULL; 52526ba94429SFrederic Weisbecker 52536ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 52546ba94429SFrederic Weisbecker return attrs; 52556ba94429SFrederic Weisbecker } 52566ba94429SFrederic Weisbecker 52576ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 52586ba94429SFrederic Weisbecker const char *buf, size_t count) 52596ba94429SFrederic Weisbecker { 52606ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 52616ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5262d4d3e257SLai Jiangshan int ret = -ENOMEM; 5263d4d3e257SLai Jiangshan 5264d4d3e257SLai Jiangshan apply_wqattrs_lock(); 52656ba94429SFrederic Weisbecker 52666ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 52676ba94429SFrederic Weisbecker if (!attrs) 5268d4d3e257SLai Jiangshan goto out_unlock; 52696ba94429SFrederic Weisbecker 52706ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 52716ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 5272d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 52736ba94429SFrederic Weisbecker else 52746ba94429SFrederic Weisbecker ret = -EINVAL; 52756ba94429SFrederic Weisbecker 5276d4d3e257SLai Jiangshan out_unlock: 5277d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 52786ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 52796ba94429SFrederic Weisbecker return ret ?: count; 52806ba94429SFrederic Weisbecker } 52816ba94429SFrederic Weisbecker 52826ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 52836ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 52846ba94429SFrederic Weisbecker { 52856ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 52866ba94429SFrederic Weisbecker int written; 52876ba94429SFrederic Weisbecker 52886ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 52896ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 52906ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 52916ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 52926ba94429SFrederic Weisbecker return written; 52936ba94429SFrederic Weisbecker } 52946ba94429SFrederic Weisbecker 52956ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 52966ba94429SFrederic Weisbecker struct device_attribute *attr, 52976ba94429SFrederic Weisbecker const char *buf, size_t count) 52986ba94429SFrederic Weisbecker { 52996ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 53006ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5301d4d3e257SLai Jiangshan int ret = -ENOMEM; 5302d4d3e257SLai Jiangshan 5303d4d3e257SLai Jiangshan apply_wqattrs_lock(); 53046ba94429SFrederic Weisbecker 53056ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 53066ba94429SFrederic Weisbecker if (!attrs) 5307d4d3e257SLai Jiangshan goto out_unlock; 53086ba94429SFrederic Weisbecker 53096ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 53106ba94429SFrederic Weisbecker if (!ret) 5311d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 53126ba94429SFrederic Weisbecker 5313d4d3e257SLai Jiangshan out_unlock: 5314d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 53156ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 53166ba94429SFrederic Weisbecker return ret ?: count; 53176ba94429SFrederic Weisbecker } 53186ba94429SFrederic Weisbecker 53196ba94429SFrederic Weisbecker static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr, 53206ba94429SFrederic Weisbecker char *buf) 53216ba94429SFrederic Weisbecker { 53226ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 53236ba94429SFrederic Weisbecker int written; 53246ba94429SFrederic Weisbecker 53256ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 53266ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", 53276ba94429SFrederic Weisbecker !wq->unbound_attrs->no_numa); 53286ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 53296ba94429SFrederic Weisbecker 53306ba94429SFrederic Weisbecker return written; 53316ba94429SFrederic Weisbecker } 53326ba94429SFrederic Weisbecker 53336ba94429SFrederic Weisbecker static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr, 53346ba94429SFrederic Weisbecker const char *buf, size_t count) 53356ba94429SFrederic Weisbecker { 53366ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 53376ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5338d4d3e257SLai Jiangshan int v, ret = -ENOMEM; 5339d4d3e257SLai Jiangshan 5340d4d3e257SLai Jiangshan apply_wqattrs_lock(); 53416ba94429SFrederic Weisbecker 53426ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 53436ba94429SFrederic Weisbecker if (!attrs) 5344d4d3e257SLai Jiangshan goto out_unlock; 53456ba94429SFrederic Weisbecker 53466ba94429SFrederic Weisbecker ret = -EINVAL; 53476ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &v) == 1) { 53486ba94429SFrederic Weisbecker attrs->no_numa = !v; 5349d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 53506ba94429SFrederic Weisbecker } 53516ba94429SFrederic Weisbecker 5352d4d3e257SLai Jiangshan out_unlock: 5353d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 53546ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 53556ba94429SFrederic Weisbecker return ret ?: count; 53566ba94429SFrederic Weisbecker } 53576ba94429SFrederic Weisbecker 53586ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 53596ba94429SFrederic Weisbecker __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL), 53606ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 53616ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 53626ba94429SFrederic Weisbecker __ATTR(numa, 0644, wq_numa_show, wq_numa_store), 53636ba94429SFrederic Weisbecker __ATTR_NULL, 53646ba94429SFrederic Weisbecker }; 53656ba94429SFrederic Weisbecker 53666ba94429SFrederic Weisbecker static struct bus_type wq_subsys = { 53676ba94429SFrederic Weisbecker .name = "workqueue", 53686ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 53696ba94429SFrederic Weisbecker }; 53706ba94429SFrederic Weisbecker 5371b05a7928SFrederic Weisbecker static ssize_t wq_unbound_cpumask_show(struct device *dev, 5372b05a7928SFrederic Weisbecker struct device_attribute *attr, char *buf) 5373b05a7928SFrederic Weisbecker { 5374b05a7928SFrederic Weisbecker int written; 5375b05a7928SFrederic Weisbecker 5376042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 5377b05a7928SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 5378b05a7928SFrederic Weisbecker cpumask_pr_args(wq_unbound_cpumask)); 5379042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5380b05a7928SFrederic Weisbecker 5381b05a7928SFrederic Weisbecker return written; 5382b05a7928SFrederic Weisbecker } 5383b05a7928SFrederic Weisbecker 5384042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 5385042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 5386042f7df1SLai Jiangshan { 5387042f7df1SLai Jiangshan cpumask_var_t cpumask; 5388042f7df1SLai Jiangshan int ret; 5389042f7df1SLai Jiangshan 5390042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 5391042f7df1SLai Jiangshan return -ENOMEM; 5392042f7df1SLai Jiangshan 5393042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 5394042f7df1SLai Jiangshan if (!ret) 5395042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 5396042f7df1SLai Jiangshan 5397042f7df1SLai Jiangshan free_cpumask_var(cpumask); 5398042f7df1SLai Jiangshan return ret ? ret : count; 5399042f7df1SLai Jiangshan } 5400042f7df1SLai Jiangshan 5401b05a7928SFrederic Weisbecker static struct device_attribute wq_sysfs_cpumask_attr = 5402042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 5403042f7df1SLai Jiangshan wq_unbound_cpumask_store); 5404b05a7928SFrederic Weisbecker 54056ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 54066ba94429SFrederic Weisbecker { 5407b05a7928SFrederic Weisbecker int err; 5408b05a7928SFrederic Weisbecker 5409b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 5410b05a7928SFrederic Weisbecker if (err) 5411b05a7928SFrederic Weisbecker return err; 5412b05a7928SFrederic Weisbecker 5413b05a7928SFrederic Weisbecker return device_create_file(wq_subsys.dev_root, &wq_sysfs_cpumask_attr); 54146ba94429SFrederic Weisbecker } 54156ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 54166ba94429SFrederic Weisbecker 54176ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 54186ba94429SFrederic Weisbecker { 54196ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 54206ba94429SFrederic Weisbecker 54216ba94429SFrederic Weisbecker kfree(wq_dev); 54226ba94429SFrederic Weisbecker } 54236ba94429SFrederic Weisbecker 54246ba94429SFrederic Weisbecker /** 54256ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 54266ba94429SFrederic Weisbecker * @wq: the workqueue to register 54276ba94429SFrederic Weisbecker * 54286ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 54296ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 54306ba94429SFrederic Weisbecker * which is the preferred method. 54316ba94429SFrederic Weisbecker * 54326ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 54336ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 54346ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 54356ba94429SFrederic Weisbecker * attributes. 54366ba94429SFrederic Weisbecker * 54376ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 54386ba94429SFrederic Weisbecker */ 54396ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 54406ba94429SFrederic Weisbecker { 54416ba94429SFrederic Weisbecker struct wq_device *wq_dev; 54426ba94429SFrederic Weisbecker int ret; 54436ba94429SFrederic Weisbecker 54446ba94429SFrederic Weisbecker /* 5445402dd89dSShailendra Verma * Adjusting max_active or creating new pwqs by applying 54466ba94429SFrederic Weisbecker * attributes breaks ordering guarantee. Disallow exposing ordered 54476ba94429SFrederic Weisbecker * workqueues. 54486ba94429SFrederic Weisbecker */ 54490a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 54506ba94429SFrederic Weisbecker return -EINVAL; 54516ba94429SFrederic Weisbecker 54526ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 54536ba94429SFrederic Weisbecker if (!wq_dev) 54546ba94429SFrederic Weisbecker return -ENOMEM; 54556ba94429SFrederic Weisbecker 54566ba94429SFrederic Weisbecker wq_dev->wq = wq; 54576ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 54586ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 545923217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 54606ba94429SFrederic Weisbecker 54616ba94429SFrederic Weisbecker /* 54626ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 54636ba94429SFrederic Weisbecker * everything is ready. 54646ba94429SFrederic Weisbecker */ 54656ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 54666ba94429SFrederic Weisbecker 54676ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 54686ba94429SFrederic Weisbecker if (ret) { 5469537f4146SArvind Yadav put_device(&wq_dev->dev); 54706ba94429SFrederic Weisbecker wq->wq_dev = NULL; 54716ba94429SFrederic Weisbecker return ret; 54726ba94429SFrederic Weisbecker } 54736ba94429SFrederic Weisbecker 54746ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 54756ba94429SFrederic Weisbecker struct device_attribute *attr; 54766ba94429SFrederic Weisbecker 54776ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 54786ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 54796ba94429SFrederic Weisbecker if (ret) { 54806ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 54816ba94429SFrederic Weisbecker wq->wq_dev = NULL; 54826ba94429SFrederic Weisbecker return ret; 54836ba94429SFrederic Weisbecker } 54846ba94429SFrederic Weisbecker } 54856ba94429SFrederic Weisbecker } 54866ba94429SFrederic Weisbecker 54876ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 54886ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 54896ba94429SFrederic Weisbecker return 0; 54906ba94429SFrederic Weisbecker } 54916ba94429SFrederic Weisbecker 54926ba94429SFrederic Weisbecker /** 54936ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 54946ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 54956ba94429SFrederic Weisbecker * 54966ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 54976ba94429SFrederic Weisbecker */ 54986ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 54996ba94429SFrederic Weisbecker { 55006ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 55016ba94429SFrederic Weisbecker 55026ba94429SFrederic Weisbecker if (!wq->wq_dev) 55036ba94429SFrederic Weisbecker return; 55046ba94429SFrederic Weisbecker 55056ba94429SFrederic Weisbecker wq->wq_dev = NULL; 55066ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 55076ba94429SFrederic Weisbecker } 55086ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 55096ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 55106ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 55116ba94429SFrederic Weisbecker 551282607adcSTejun Heo /* 551382607adcSTejun Heo * Workqueue watchdog. 551482607adcSTejun Heo * 551582607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 551682607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 551782607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 551882607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 551982607adcSTejun Heo * largely opaque. 552082607adcSTejun Heo * 552182607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 552282607adcSTejun Heo * state if some pools failed to make forward progress for a while where 552382607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 552482607adcSTejun Heo * 552582607adcSTejun Heo * This mechanism is controlled through the kernel parameter 552682607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 552782607adcSTejun Heo * corresponding sysfs parameter file. 552882607adcSTejun Heo */ 552982607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 553082607adcSTejun Heo 553182607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 55325cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 553382607adcSTejun Heo 553482607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 553582607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 553682607adcSTejun Heo 553782607adcSTejun Heo static void wq_watchdog_reset_touched(void) 553882607adcSTejun Heo { 553982607adcSTejun Heo int cpu; 554082607adcSTejun Heo 554182607adcSTejun Heo wq_watchdog_touched = jiffies; 554282607adcSTejun Heo for_each_possible_cpu(cpu) 554382607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 554482607adcSTejun Heo } 554582607adcSTejun Heo 55465cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 554782607adcSTejun Heo { 554882607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 554982607adcSTejun Heo bool lockup_detected = false; 555082607adcSTejun Heo struct worker_pool *pool; 555182607adcSTejun Heo int pi; 555282607adcSTejun Heo 555382607adcSTejun Heo if (!thresh) 555482607adcSTejun Heo return; 555582607adcSTejun Heo 555682607adcSTejun Heo rcu_read_lock(); 555782607adcSTejun Heo 555882607adcSTejun Heo for_each_pool(pool, pi) { 555982607adcSTejun Heo unsigned long pool_ts, touched, ts; 556082607adcSTejun Heo 556182607adcSTejun Heo if (list_empty(&pool->worklist)) 556282607adcSTejun Heo continue; 556382607adcSTejun Heo 556482607adcSTejun Heo /* get the latest of pool and touched timestamps */ 556582607adcSTejun Heo pool_ts = READ_ONCE(pool->watchdog_ts); 556682607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 556782607adcSTejun Heo 556882607adcSTejun Heo if (time_after(pool_ts, touched)) 556982607adcSTejun Heo ts = pool_ts; 557082607adcSTejun Heo else 557182607adcSTejun Heo ts = touched; 557282607adcSTejun Heo 557382607adcSTejun Heo if (pool->cpu >= 0) { 557482607adcSTejun Heo unsigned long cpu_touched = 557582607adcSTejun Heo READ_ONCE(per_cpu(wq_watchdog_touched_cpu, 557682607adcSTejun Heo pool->cpu)); 557782607adcSTejun Heo if (time_after(cpu_touched, ts)) 557882607adcSTejun Heo ts = cpu_touched; 557982607adcSTejun Heo } 558082607adcSTejun Heo 558182607adcSTejun Heo /* did we stall? */ 558282607adcSTejun Heo if (time_after(jiffies, ts + thresh)) { 558382607adcSTejun Heo lockup_detected = true; 558482607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 558582607adcSTejun Heo pr_cont_pool_info(pool); 558682607adcSTejun Heo pr_cont(" stuck for %us!\n", 558782607adcSTejun Heo jiffies_to_msecs(jiffies - pool_ts) / 1000); 558882607adcSTejun Heo } 558982607adcSTejun Heo } 559082607adcSTejun Heo 559182607adcSTejun Heo rcu_read_unlock(); 559282607adcSTejun Heo 559382607adcSTejun Heo if (lockup_detected) 559482607adcSTejun Heo show_workqueue_state(); 559582607adcSTejun Heo 559682607adcSTejun Heo wq_watchdog_reset_touched(); 559782607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 559882607adcSTejun Heo } 559982607adcSTejun Heo 5600cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 560182607adcSTejun Heo { 560282607adcSTejun Heo if (cpu >= 0) 560382607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 560482607adcSTejun Heo else 560582607adcSTejun Heo wq_watchdog_touched = jiffies; 560682607adcSTejun Heo } 560782607adcSTejun Heo 560882607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 560982607adcSTejun Heo { 561082607adcSTejun Heo wq_watchdog_thresh = 0; 561182607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 561282607adcSTejun Heo 561382607adcSTejun Heo if (thresh) { 561482607adcSTejun Heo wq_watchdog_thresh = thresh; 561582607adcSTejun Heo wq_watchdog_reset_touched(); 561682607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 561782607adcSTejun Heo } 561882607adcSTejun Heo } 561982607adcSTejun Heo 562082607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 562182607adcSTejun Heo const struct kernel_param *kp) 562282607adcSTejun Heo { 562382607adcSTejun Heo unsigned long thresh; 562482607adcSTejun Heo int ret; 562582607adcSTejun Heo 562682607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 562782607adcSTejun Heo if (ret) 562882607adcSTejun Heo return ret; 562982607adcSTejun Heo 563082607adcSTejun Heo if (system_wq) 563182607adcSTejun Heo wq_watchdog_set_thresh(thresh); 563282607adcSTejun Heo else 563382607adcSTejun Heo wq_watchdog_thresh = thresh; 563482607adcSTejun Heo 563582607adcSTejun Heo return 0; 563682607adcSTejun Heo } 563782607adcSTejun Heo 563882607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 563982607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 564082607adcSTejun Heo .get = param_get_ulong, 564182607adcSTejun Heo }; 564282607adcSTejun Heo 564382607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 564482607adcSTejun Heo 0644); 564582607adcSTejun Heo 564682607adcSTejun Heo static void wq_watchdog_init(void) 564782607adcSTejun Heo { 56485cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 564982607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 565082607adcSTejun Heo } 565182607adcSTejun Heo 565282607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 565382607adcSTejun Heo 565482607adcSTejun Heo static inline void wq_watchdog_init(void) { } 565582607adcSTejun Heo 565682607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 565782607adcSTejun Heo 5658bce90380STejun Heo static void __init wq_numa_init(void) 5659bce90380STejun Heo { 5660bce90380STejun Heo cpumask_var_t *tbl; 5661bce90380STejun Heo int node, cpu; 5662bce90380STejun Heo 5663bce90380STejun Heo if (num_possible_nodes() <= 1) 5664bce90380STejun Heo return; 5665bce90380STejun Heo 5666d55262c4STejun Heo if (wq_disable_numa) { 5667d55262c4STejun Heo pr_info("workqueue: NUMA affinity support disabled\n"); 5668d55262c4STejun Heo return; 5669d55262c4STejun Heo } 5670d55262c4STejun Heo 56714c16bd32STejun Heo wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(GFP_KERNEL); 56724c16bd32STejun Heo BUG_ON(!wq_update_unbound_numa_attrs_buf); 56734c16bd32STejun Heo 5674bce90380STejun Heo /* 5675bce90380STejun Heo * We want masks of possible CPUs of each node which isn't readily 5676bce90380STejun Heo * available. Build one from cpu_to_node() which should have been 5677bce90380STejun Heo * fully initialized by now. 5678bce90380STejun Heo */ 56796396bb22SKees Cook tbl = kcalloc(nr_node_ids, sizeof(tbl[0]), GFP_KERNEL); 5680bce90380STejun Heo BUG_ON(!tbl); 5681bce90380STejun Heo 5682bce90380STejun Heo for_each_node(node) 56835a6024f1SYasuaki Ishimatsu BUG_ON(!zalloc_cpumask_var_node(&tbl[node], GFP_KERNEL, 56841be0c25dSTejun Heo node_online(node) ? node : NUMA_NO_NODE)); 5685bce90380STejun Heo 5686bce90380STejun Heo for_each_possible_cpu(cpu) { 5687bce90380STejun Heo node = cpu_to_node(cpu); 5688bce90380STejun Heo if (WARN_ON(node == NUMA_NO_NODE)) { 5689bce90380STejun Heo pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu); 5690bce90380STejun Heo /* happens iff arch is bonkers, let's just proceed */ 5691bce90380STejun Heo return; 5692bce90380STejun Heo } 5693bce90380STejun Heo cpumask_set_cpu(cpu, tbl[node]); 5694bce90380STejun Heo } 5695bce90380STejun Heo 5696bce90380STejun Heo wq_numa_possible_cpumask = tbl; 5697bce90380STejun Heo wq_numa_enabled = true; 5698bce90380STejun Heo } 5699bce90380STejun Heo 57003347fa09STejun Heo /** 57013347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 57023347fa09STejun Heo * 57033347fa09STejun Heo * This is the first half of two-staged workqueue subsystem initialization 57043347fa09STejun Heo * and invoked as soon as the bare basics - memory allocation, cpumasks and 57053347fa09STejun Heo * idr are up. It sets up all the data structures and system workqueues 57063347fa09STejun Heo * and allows early boot code to create workqueues and queue/cancel work 57073347fa09STejun Heo * items. Actual work item execution starts only after kthreads can be 57083347fa09STejun Heo * created and scheduled right before early initcalls. 57093347fa09STejun Heo */ 57103347fa09STejun Heo int __init workqueue_init_early(void) 57111da177e4SLinus Torvalds { 57127a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 57131bda3f80SFrederic Weisbecker int hk_flags = HK_FLAG_DOMAIN | HK_FLAG_WQ; 57147a4e344cSTejun Heo int i, cpu; 5715c34056a3STejun Heo 5716e904e6c2STejun Heo WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 5717e904e6c2STejun Heo 5718b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 57191bda3f80SFrederic Weisbecker cpumask_copy(wq_unbound_cpumask, housekeeping_cpumask(hk_flags)); 5720b05a7928SFrederic Weisbecker 5721e904e6c2STejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 5722e904e6c2STejun Heo 5723706026c2STejun Heo /* initialize CPU pools */ 572429c91e99STejun Heo for_each_possible_cpu(cpu) { 57254ce62e9eSTejun Heo struct worker_pool *pool; 57268b03ae3cSTejun Heo 57277a4e344cSTejun Heo i = 0; 5728f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 57297a4e344cSTejun Heo BUG_ON(init_worker_pool(pool)); 5730ec22ca5eSTejun Heo pool->cpu = cpu; 57317a4e344cSTejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 57327a4e344cSTejun Heo pool->attrs->nice = std_nice[i++]; 5733f3f90ad4STejun Heo pool->node = cpu_to_node(cpu); 57347a4e344cSTejun Heo 57359daf9e67STejun Heo /* alloc pool ID */ 573668e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 57379daf9e67STejun Heo BUG_ON(worker_pool_assign_id(pool)); 573868e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 57394ce62e9eSTejun Heo } 57408b03ae3cSTejun Heo } 57418b03ae3cSTejun Heo 57428a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 574329c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 574429c91e99STejun Heo struct workqueue_attrs *attrs; 574529c91e99STejun Heo 574629c91e99STejun Heo BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL))); 574729c91e99STejun Heo attrs->nice = std_nice[i]; 574829c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 57498a2b7538STejun Heo 57508a2b7538STejun Heo /* 57518a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 57528a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 57538a2b7538STejun Heo * Turn off NUMA so that dfl_pwq is used for all nodes. 57548a2b7538STejun Heo */ 57558a2b7538STejun Heo BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL))); 57568a2b7538STejun Heo attrs->nice = std_nice[i]; 57578a2b7538STejun Heo attrs->no_numa = true; 57588a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 575929c91e99STejun Heo } 576029c91e99STejun Heo 5761d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 57621aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 5763d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 5764f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 5765f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 576624d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 576724d51addSTejun Heo WQ_FREEZABLE, 0); 57680668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 57690668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 57700668106cSViresh Kumar system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient", 57710668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 57720668106cSViresh Kumar 0); 57731aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 57740668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 57750668106cSViresh Kumar !system_power_efficient_wq || 57760668106cSViresh Kumar !system_freezable_power_efficient_wq); 577782607adcSTejun Heo 57783347fa09STejun Heo return 0; 57793347fa09STejun Heo } 57803347fa09STejun Heo 57813347fa09STejun Heo /** 57823347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 57833347fa09STejun Heo * 57843347fa09STejun Heo * This is the latter half of two-staged workqueue subsystem initialization 57853347fa09STejun Heo * and invoked as soon as kthreads can be created and scheduled. 57863347fa09STejun Heo * Workqueues have been created and work items queued on them, but there 57873347fa09STejun Heo * are no kworkers executing the work items yet. Populate the worker pools 57883347fa09STejun Heo * with the initial workers and enable future kworker creations. 57893347fa09STejun Heo */ 57903347fa09STejun Heo int __init workqueue_init(void) 57913347fa09STejun Heo { 57922186d9f9STejun Heo struct workqueue_struct *wq; 57933347fa09STejun Heo struct worker_pool *pool; 57943347fa09STejun Heo int cpu, bkt; 57953347fa09STejun Heo 57962186d9f9STejun Heo /* 57972186d9f9STejun Heo * It'd be simpler to initialize NUMA in workqueue_init_early() but 57982186d9f9STejun Heo * CPU to node mapping may not be available that early on some 57992186d9f9STejun Heo * archs such as power and arm64. As per-cpu pools created 58002186d9f9STejun Heo * previously could be missing node hint and unbound pools NUMA 58012186d9f9STejun Heo * affinity, fix them up. 580240c17f75STejun Heo * 580340c17f75STejun Heo * Also, while iterating workqueues, create rescuers if requested. 58042186d9f9STejun Heo */ 58052186d9f9STejun Heo wq_numa_init(); 58062186d9f9STejun Heo 58072186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 58082186d9f9STejun Heo 58092186d9f9STejun Heo for_each_possible_cpu(cpu) { 58102186d9f9STejun Heo for_each_cpu_worker_pool(pool, cpu) { 58112186d9f9STejun Heo pool->node = cpu_to_node(cpu); 58122186d9f9STejun Heo } 58132186d9f9STejun Heo } 58142186d9f9STejun Heo 581540c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 58162186d9f9STejun Heo wq_update_unbound_numa(wq, smp_processor_id(), true); 581740c17f75STejun Heo WARN(init_rescuer(wq), 581840c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 581940c17f75STejun Heo wq->name); 582040c17f75STejun Heo } 58212186d9f9STejun Heo 58222186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 58232186d9f9STejun Heo 58243347fa09STejun Heo /* create the initial workers */ 58253347fa09STejun Heo for_each_online_cpu(cpu) { 58263347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 58273347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 58283347fa09STejun Heo BUG_ON(!create_worker(pool)); 58293347fa09STejun Heo } 58303347fa09STejun Heo } 58313347fa09STejun Heo 58323347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 58333347fa09STejun Heo BUG_ON(!create_worker(pool)); 58343347fa09STejun Heo 58353347fa09STejun Heo wq_online = true; 583682607adcSTejun Heo wq_watchdog_init(); 583782607adcSTejun Heo 58386ee0578bSSuresh Siddha return 0; 58391da177e4SLinus Torvalds } 5840