1 // SPDX-License-Identifier: GPL-2.0-only
3 * kernel/workqueue.c - generic async execution with shared worker pool
5 * Copyright (C) 2002 Ingo Molnar
7 * Derived from the taskqueue/keventd code by:
8 * David Woodhouse <dwmw2@infradead.org>
10 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
11 * Theodore Ts'o <tytso@mit.edu>
13 * Made to use alloc_percpu by Christoph Lameter.
15 * Copyright (C) 2010 SUSE Linux Products GmbH
16 * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
18 * This is the generic async execution mechanism. Work items as are
19 * executed in process context. The worker pool is shared and
20 * automatically managed. There are two worker pools for each CPU (one for
21 * normal work items and the other for high priority ones) and some extra
22 * pools for workqueues which are not bound to any specific CPU - the
23 * number of these backing pools is dynamic.
25 * Please read Documentation/core-api/workqueue.rst for details.
28 #include <linux/export.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/init.h>
32 #include <linux/signal.h>
33 #include <linux/completion.h>
34 #include <linux/workqueue.h>
35 #include <linux/slab.h>
36 #include <linux/cpu.h>
37 #include <linux/notifier.h>
38 #include <linux/kthread.h>
39 #include <linux/hardirq.h>
40 #include <linux/mempolicy.h>
41 #include <linux/freezer.h>
42 #include <linux/debug_locks.h>
43 #include <linux/lockdep.h>
44 #include <linux/idr.h>
45 #include <linux/jhash.h>
46 #include <linux/hashtable.h>
47 #include <linux/rculist.h>
48 #include <linux/nodemask.h>
49 #include <linux/moduleparam.h>
50 #include <linux/uaccess.h>
51 #include <linux/sched/isolation.h>
52 #include <linux/sched/debug.h>
53 #include <linux/nmi.h>
54 #include <linux/kvm_para.h>
56 #include "workqueue_internal.h"
62 * A bound pool is either associated or disassociated with its CPU.
63 * While associated (!DISASSOCIATED), all workers are bound to the
64 * CPU and none has %WORKER_UNBOUND set and concurrency management
67 * While DISASSOCIATED, the cpu may be offline and all workers have
68 * %WORKER_UNBOUND set and concurrency management disabled, and may
69 * be executing on any CPU. The pool behaves as an unbound one.
71 * Note that DISASSOCIATED should be flipped only while holding
72 * wq_pool_attach_mutex to avoid changing binding state while
73 * worker_attach_to_pool() is in progress.
75 POOL_MANAGER_ACTIVE = 1 << 0, /* being managed */
76 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
79 WORKER_DIE = 1 << 1, /* die die die */
80 WORKER_IDLE = 1 << 2, /* is idle */
81 WORKER_PREP = 1 << 3, /* preparing to run works */
82 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
83 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
84 WORKER_REBOUND = 1 << 8, /* worker was rebound */
86 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE |
87 WORKER_UNBOUND | WORKER_REBOUND,
89 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
91 UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */
92 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
94 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
95 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
97 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
98 /* call for help after 10ms
100 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
101 CREATE_COOLDOWN = HZ, /* time to breath after fail */
104 * Rescue workers are used only on emergencies and shared by
105 * all cpus. Give MIN_NICE.
107 RESCUER_NICE_LEVEL = MIN_NICE,
108 HIGHPRI_NICE_LEVEL = MIN_NICE,
114 * Structure fields follow one of the following exclusion rules.
116 * I: Modifiable by initialization/destruction paths and read-only for
119 * P: Preemption protected. Disabling preemption is enough and should
120 * only be modified and accessed from the local cpu.
122 * L: pool->lock protected. Access with pool->lock held.
124 * X: During normal operation, modification requires pool->lock and should
125 * be done only from local cpu. Either disabling preemption on local
126 * cpu or grabbing pool->lock is enough for read access. If
127 * POOL_DISASSOCIATED is set, it's identical to L.
129 * A: wq_pool_attach_mutex protected.
131 * PL: wq_pool_mutex protected.
133 * PR: wq_pool_mutex protected for writes. RCU protected for reads.
135 * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads.
137 * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or
140 * WQ: wq->mutex protected.
142 * WR: wq->mutex protected for writes. RCU protected for reads.
144 * MD: wq_mayday_lock protected.
146 * WD: Used internally by the watchdog.
149 /* struct worker is defined in workqueue_internal.h */
152 raw_spinlock_t lock; /* the pool lock */
153 int cpu; /* I: the associated cpu */
154 int node; /* I: the associated node ID */
155 int id; /* I: pool ID */
156 unsigned int flags; /* X: flags */
158 unsigned long watchdog_ts; /* L: watchdog timestamp */
159 bool cpu_stall; /* WD: stalled cpu bound pool */
162 * The counter is incremented in a process context on the associated CPU
163 * w/ preemption disabled, and decremented or reset in the same context
164 * but w/ pool->lock held. The readers grab pool->lock and are
165 * guaranteed to see if the counter reached zero.
169 struct list_head worklist; /* L: list of pending works */
171 int nr_workers; /* L: total number of workers */
172 int nr_idle; /* L: currently idle workers */
174 struct list_head idle_list; /* L: list of idle workers */
175 struct timer_list idle_timer; /* L: worker idle timeout */
176 struct work_struct idle_cull_work; /* L: worker idle cleanup */
178 struct timer_list mayday_timer; /* L: SOS timer for workers */
180 /* a workers is either on busy_hash or idle_list, or the manager */
181 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
182 /* L: hash of busy workers */
184 struct worker *manager; /* L: purely informational */
185 struct list_head workers; /* A: attached workers */
186 struct list_head dying_workers; /* A: workers about to die */
187 struct completion *detach_completion; /* all workers detached */
189 struct ida worker_ida; /* worker IDs for task name */
191 struct workqueue_attrs *attrs; /* I: worker attributes */
192 struct hlist_node hash_node; /* PL: unbound_pool_hash node */
193 int refcnt; /* PL: refcnt for unbound pools */
196 * Destruction of pool is RCU protected to allow dereferences
197 * from get_work_pool().
203 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
204 * of work_struct->data are used for flags and the remaining high bits
205 * point to the pwq; thus, pwqs need to be aligned at two's power of the
206 * number of flag bits.
208 struct pool_workqueue {
209 struct worker_pool *pool; /* I: the associated pool */
210 struct workqueue_struct *wq; /* I: the owning workqueue */
211 int work_color; /* L: current color */
212 int flush_color; /* L: flushing color */
213 int refcnt; /* L: reference count */
214 int nr_in_flight[WORK_NR_COLORS];
215 /* L: nr of in_flight works */
218 * nr_active management and WORK_STRUCT_INACTIVE:
220 * When pwq->nr_active >= max_active, new work item is queued to
221 * pwq->inactive_works instead of pool->worklist and marked with
222 * WORK_STRUCT_INACTIVE.
224 * All work items marked with WORK_STRUCT_INACTIVE do not participate
225 * in pwq->nr_active and all work items in pwq->inactive_works are
226 * marked with WORK_STRUCT_INACTIVE. But not all WORK_STRUCT_INACTIVE
227 * work items are in pwq->inactive_works. Some of them are ready to
228 * run in pool->worklist or worker->scheduled. Those work itmes are
229 * only struct wq_barrier which is used for flush_work() and should
230 * not participate in pwq->nr_active. For non-barrier work item, it
231 * is marked with WORK_STRUCT_INACTIVE iff it is in pwq->inactive_works.
233 int nr_active; /* L: nr of active works */
234 int max_active; /* L: max active works */
235 struct list_head inactive_works; /* L: inactive works */
236 struct list_head pwqs_node; /* WR: node on wq->pwqs */
237 struct list_head mayday_node; /* MD: node on wq->maydays */
240 * Release of unbound pwq is punted to system_wq. See put_pwq()
241 * and pwq_unbound_release_workfn() for details. pool_workqueue
242 * itself is also RCU protected so that the first pwq can be
243 * determined without grabbing wq->mutex.
245 struct work_struct unbound_release_work;
247 } __aligned(1 << WORK_STRUCT_FLAG_BITS);
250 * Structure used to wait for workqueue flush.
253 struct list_head list; /* WQ: list of flushers */
254 int flush_color; /* WQ: flush color waiting for */
255 struct completion done; /* flush completion */
261 * The externally visible workqueue. It relays the issued work items to
262 * the appropriate worker_pool through its pool_workqueues.
264 struct workqueue_struct {
265 struct list_head pwqs; /* WR: all pwqs of this wq */
266 struct list_head list; /* PR: list of all workqueues */
268 struct mutex mutex; /* protects this wq */
269 int work_color; /* WQ: current work color */
270 int flush_color; /* WQ: current flush color */
271 atomic_t nr_pwqs_to_flush; /* flush in progress */
272 struct wq_flusher *first_flusher; /* WQ: first flusher */
273 struct list_head flusher_queue; /* WQ: flush waiters */
274 struct list_head flusher_overflow; /* WQ: flush overflow list */
276 struct list_head maydays; /* MD: pwqs requesting rescue */
277 struct worker *rescuer; /* MD: rescue worker */
279 int nr_drainers; /* WQ: drain in progress */
280 int saved_max_active; /* WQ: saved pwq max_active */
282 struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */
283 struct pool_workqueue *dfl_pwq; /* PW: only for unbound wqs */
286 struct wq_device *wq_dev; /* I: for sysfs interface */
288 #ifdef CONFIG_LOCKDEP
290 struct lock_class_key key;
291 struct lockdep_map lockdep_map;
293 char name[WQ_NAME_LEN]; /* I: workqueue name */
296 * Destruction of workqueue_struct is RCU protected to allow walking
297 * the workqueues list without grabbing wq_pool_mutex.
298 * This is used to dump all workqueues from sysrq.
302 /* hot fields used during command issue, aligned to cacheline */
303 unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */
304 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */
305 struct pool_workqueue __rcu *numa_pwq_tbl[]; /* PWR: unbound pwqs indexed by node */
308 static struct kmem_cache *pwq_cache;
310 static cpumask_var_t *wq_numa_possible_cpumask;
311 /* possible CPUs of each node */
313 static bool wq_disable_numa;
314 module_param_named(disable_numa, wq_disable_numa, bool, 0444);
316 /* see the comment above the definition of WQ_POWER_EFFICIENT */
317 static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT);
318 module_param_named(power_efficient, wq_power_efficient, bool, 0444);
320 static bool wq_online; /* can kworkers be created yet? */
322 static bool wq_numa_enabled; /* unbound NUMA affinity enabled */
324 /* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */
325 static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf;
327 static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */
328 static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */
329 static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */
330 /* wait for manager to go away */
331 static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait);
333 static LIST_HEAD(workqueues); /* PR: list of all workqueues */
334 static bool workqueue_freezing; /* PL: have wqs started freezing? */
336 /* PL&A: allowable cpus for unbound wqs and work items */
337 static cpumask_var_t wq_unbound_cpumask;
339 /* CPU where unbound work was last round robin scheduled from this CPU */
340 static DEFINE_PER_CPU(int, wq_rr_cpu_last);
343 * Local execution of unbound work items is no longer guaranteed. The
344 * following always forces round-robin CPU selection on unbound work items
345 * to uncover usages which depend on it.
347 #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU
348 static bool wq_debug_force_rr_cpu = true;
350 static bool wq_debug_force_rr_cpu = false;
352 module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644);
354 /* the per-cpu worker pools */
355 static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools);
357 static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */
359 /* PL: hash of all unbound pools keyed by pool->attrs */
360 static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
362 /* I: attributes used when instantiating standard unbound pools on demand */
363 static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
365 /* I: attributes used when instantiating ordered pools on demand */
366 static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS];
368 struct workqueue_struct *system_wq __read_mostly;
369 EXPORT_SYMBOL(system_wq);
370 struct workqueue_struct *system_highpri_wq __read_mostly;
371 EXPORT_SYMBOL_GPL(system_highpri_wq);
372 struct workqueue_struct *system_long_wq __read_mostly;
373 EXPORT_SYMBOL_GPL(system_long_wq);
374 struct workqueue_struct *system_unbound_wq __read_mostly;
375 EXPORT_SYMBOL_GPL(system_unbound_wq);
376 struct workqueue_struct *system_freezable_wq __read_mostly;
377 EXPORT_SYMBOL_GPL(system_freezable_wq);
378 struct workqueue_struct *system_power_efficient_wq __read_mostly;
379 EXPORT_SYMBOL_GPL(system_power_efficient_wq);
380 struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly;
381 EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
383 static int worker_thread(void *__worker);
384 static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
385 static void show_pwq(struct pool_workqueue *pwq);
386 static void show_one_worker_pool(struct worker_pool *pool);
388 #define CREATE_TRACE_POINTS
389 #include <trace/events/workqueue.h>
391 #define assert_rcu_or_pool_mutex() \
392 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
393 !lockdep_is_held(&wq_pool_mutex), \
394 "RCU or wq_pool_mutex should be held")
396 #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \
397 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
398 !lockdep_is_held(&wq->mutex) && \
399 !lockdep_is_held(&wq_pool_mutex), \
400 "RCU, wq->mutex or wq_pool_mutex should be held")
402 #define for_each_cpu_worker_pool(pool, cpu) \
403 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
404 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
408 * for_each_pool - iterate through all worker_pools in the system
409 * @pool: iteration cursor
410 * @pi: integer used for iteration
412 * This must be called either with wq_pool_mutex held or RCU read
413 * locked. If the pool needs to be used beyond the locking in effect, the
414 * caller is responsible for guaranteeing that the pool stays online.
416 * The if/else clause exists only for the lockdep assertion and can be
419 #define for_each_pool(pool, pi) \
420 idr_for_each_entry(&worker_pool_idr, pool, pi) \
421 if (({ assert_rcu_or_pool_mutex(); false; })) { } \
425 * for_each_pool_worker - iterate through all workers of a worker_pool
426 * @worker: iteration cursor
427 * @pool: worker_pool to iterate workers of
429 * This must be called with wq_pool_attach_mutex.
431 * The if/else clause exists only for the lockdep assertion and can be
434 #define for_each_pool_worker(worker, pool) \
435 list_for_each_entry((worker), &(pool)->workers, node) \
436 if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \
440 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
441 * @pwq: iteration cursor
442 * @wq: the target workqueue
444 * This must be called either with wq->mutex held or RCU read locked.
445 * If the pwq needs to be used beyond the locking in effect, the caller is
446 * responsible for guaranteeing that the pwq stays online.
448 * The if/else clause exists only for the lockdep assertion and can be
451 #define for_each_pwq(pwq, wq) \
452 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \
453 lockdep_is_held(&(wq->mutex)))
455 #ifdef CONFIG_DEBUG_OBJECTS_WORK
457 static const struct debug_obj_descr work_debug_descr;
459 static void *work_debug_hint(void *addr)
461 return ((struct work_struct *) addr)->func;
464 static bool work_is_static_object(void *addr)
466 struct work_struct *work = addr;
468 return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work));
472 * fixup_init is called when:
473 * - an active object is initialized
475 static bool work_fixup_init(void *addr, enum debug_obj_state state)
477 struct work_struct *work = addr;
480 case ODEBUG_STATE_ACTIVE:
481 cancel_work_sync(work);
482 debug_object_init(work, &work_debug_descr);
490 * fixup_free is called when:
491 * - an active object is freed
493 static bool work_fixup_free(void *addr, enum debug_obj_state state)
495 struct work_struct *work = addr;
498 case ODEBUG_STATE_ACTIVE:
499 cancel_work_sync(work);
500 debug_object_free(work, &work_debug_descr);
507 static const struct debug_obj_descr work_debug_descr = {
508 .name = "work_struct",
509 .debug_hint = work_debug_hint,
510 .is_static_object = work_is_static_object,
511 .fixup_init = work_fixup_init,
512 .fixup_free = work_fixup_free,
515 static inline void debug_work_activate(struct work_struct *work)
517 debug_object_activate(work, &work_debug_descr);
520 static inline void debug_work_deactivate(struct work_struct *work)
522 debug_object_deactivate(work, &work_debug_descr);
525 void __init_work(struct work_struct *work, int onstack)
528 debug_object_init_on_stack(work, &work_debug_descr);
530 debug_object_init(work, &work_debug_descr);
532 EXPORT_SYMBOL_GPL(__init_work);
534 void destroy_work_on_stack(struct work_struct *work)
536 debug_object_free(work, &work_debug_descr);
538 EXPORT_SYMBOL_GPL(destroy_work_on_stack);
540 void destroy_delayed_work_on_stack(struct delayed_work *work)
542 destroy_timer_on_stack(&work->timer);
543 debug_object_free(&work->work, &work_debug_descr);
545 EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack);
548 static inline void debug_work_activate(struct work_struct *work) { }
549 static inline void debug_work_deactivate(struct work_struct *work) { }
553 * worker_pool_assign_id - allocate ID and assign it to @pool
554 * @pool: the pool pointer of interest
556 * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned
557 * successfully, -errno on failure.
559 static int worker_pool_assign_id(struct worker_pool *pool)
563 lockdep_assert_held(&wq_pool_mutex);
565 ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE,
575 * unbound_pwq_by_node - return the unbound pool_workqueue for the given node
576 * @wq: the target workqueue
579 * This must be called with any of wq_pool_mutex, wq->mutex or RCU
581 * If the pwq needs to be used beyond the locking in effect, the caller is
582 * responsible for guaranteeing that the pwq stays online.
584 * Return: The unbound pool_workqueue for @node.
586 static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq,
589 assert_rcu_or_wq_mutex_or_pool_mutex(wq);
592 * XXX: @node can be NUMA_NO_NODE if CPU goes offline while a
593 * delayed item is pending. The plan is to keep CPU -> NODE
594 * mapping valid and stable across CPU on/offlines. Once that
595 * happens, this workaround can be removed.
597 if (unlikely(node == NUMA_NO_NODE))
600 return rcu_dereference_raw(wq->numa_pwq_tbl[node]);
603 static unsigned int work_color_to_flags(int color)
605 return color << WORK_STRUCT_COLOR_SHIFT;
608 static int get_work_color(unsigned long work_data)
610 return (work_data >> WORK_STRUCT_COLOR_SHIFT) &
611 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
614 static int work_next_color(int color)
616 return (color + 1) % WORK_NR_COLORS;
620 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
621 * contain the pointer to the queued pwq. Once execution starts, the flag
622 * is cleared and the high bits contain OFFQ flags and pool ID.
624 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
625 * and clear_work_data() can be used to set the pwq, pool or clear
626 * work->data. These functions should only be called while the work is
627 * owned - ie. while the PENDING bit is set.
629 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
630 * corresponding to a work. Pool is available once the work has been
631 * queued anywhere after initialization until it is sync canceled. pwq is
632 * available only while the work item is queued.
634 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
635 * canceled. While being canceled, a work item may have its PENDING set
636 * but stay off timer and worklist for arbitrarily long and nobody should
637 * try to steal the PENDING bit.
639 static inline void set_work_data(struct work_struct *work, unsigned long data,
642 WARN_ON_ONCE(!work_pending(work));
643 atomic_long_set(&work->data, data | flags | work_static(work));
646 static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
647 unsigned long extra_flags)
649 set_work_data(work, (unsigned long)pwq,
650 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
653 static void set_work_pool_and_keep_pending(struct work_struct *work,
656 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
657 WORK_STRUCT_PENDING);
660 static void set_work_pool_and_clear_pending(struct work_struct *work,
664 * The following wmb is paired with the implied mb in
665 * test_and_set_bit(PENDING) and ensures all updates to @work made
666 * here are visible to and precede any updates by the next PENDING
670 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
672 * The following mb guarantees that previous clear of a PENDING bit
673 * will not be reordered with any speculative LOADS or STORES from
674 * work->current_func, which is executed afterwards. This possible
675 * reordering can lead to a missed execution on attempt to queue
676 * the same @work. E.g. consider this case:
679 * ---------------------------- --------------------------------
681 * 1 STORE event_indicated
682 * 2 queue_work_on() {
683 * 3 test_and_set_bit(PENDING)
684 * 4 } set_..._and_clear_pending() {
685 * 5 set_work_data() # clear bit
687 * 7 work->current_func() {
688 * 8 LOAD event_indicated
691 * Without an explicit full barrier speculative LOAD on line 8 can
692 * be executed before CPU#0 does STORE on line 1. If that happens,
693 * CPU#0 observes the PENDING bit is still set and new execution of
694 * a @work is not queued in a hope, that CPU#1 will eventually
695 * finish the queued @work. Meanwhile CPU#1 does not see
696 * event_indicated is set, because speculative LOAD was executed
697 * before actual STORE.
702 static void clear_work_data(struct work_struct *work)
704 smp_wmb(); /* see set_work_pool_and_clear_pending() */
705 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
708 static struct pool_workqueue *get_work_pwq(struct work_struct *work)
710 unsigned long data = atomic_long_read(&work->data);
712 if (data & WORK_STRUCT_PWQ)
713 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
719 * get_work_pool - return the worker_pool a given work was associated with
720 * @work: the work item of interest
722 * Pools are created and destroyed under wq_pool_mutex, and allows read
723 * access under RCU read lock. As such, this function should be
724 * called under wq_pool_mutex or inside of a rcu_read_lock() region.
726 * All fields of the returned pool are accessible as long as the above
727 * mentioned locking is in effect. If the returned pool needs to be used
728 * beyond the critical section, the caller is responsible for ensuring the
729 * returned pool is and stays online.
731 * Return: The worker_pool @work was last associated with. %NULL if none.
733 static struct worker_pool *get_work_pool(struct work_struct *work)
735 unsigned long data = atomic_long_read(&work->data);
738 assert_rcu_or_pool_mutex();
740 if (data & WORK_STRUCT_PWQ)
741 return ((struct pool_workqueue *)
742 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
744 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
745 if (pool_id == WORK_OFFQ_POOL_NONE)
748 return idr_find(&worker_pool_idr, pool_id);
752 * get_work_pool_id - return the worker pool ID a given work is associated with
753 * @work: the work item of interest
755 * Return: The worker_pool ID @work was last associated with.
756 * %WORK_OFFQ_POOL_NONE if none.
758 static int get_work_pool_id(struct work_struct *work)
760 unsigned long data = atomic_long_read(&work->data);
762 if (data & WORK_STRUCT_PWQ)
763 return ((struct pool_workqueue *)
764 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
766 return data >> WORK_OFFQ_POOL_SHIFT;
769 static void mark_work_canceling(struct work_struct *work)
771 unsigned long pool_id = get_work_pool_id(work);
773 pool_id <<= WORK_OFFQ_POOL_SHIFT;
774 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
777 static bool work_is_canceling(struct work_struct *work)
779 unsigned long data = atomic_long_read(&work->data);
781 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
785 * Policy functions. These define the policies on how the global worker
786 * pools are managed. Unless noted otherwise, these functions assume that
787 * they're being called with pool->lock held.
790 static bool __need_more_worker(struct worker_pool *pool)
792 return !pool->nr_running;
796 * Need to wake up a worker? Called from anything but currently
799 * Note that, because unbound workers never contribute to nr_running, this
800 * function will always return %true for unbound pools as long as the
801 * worklist isn't empty.
803 static bool need_more_worker(struct worker_pool *pool)
805 return !list_empty(&pool->worklist) && __need_more_worker(pool);
808 /* Can I start working? Called from busy but !running workers. */
809 static bool may_start_working(struct worker_pool *pool)
811 return pool->nr_idle;
814 /* Do I need to keep working? Called from currently running workers. */
815 static bool keep_working(struct worker_pool *pool)
817 return !list_empty(&pool->worklist) && (pool->nr_running <= 1);
820 /* Do we need a new worker? Called from manager. */
821 static bool need_to_create_worker(struct worker_pool *pool)
823 return need_more_worker(pool) && !may_start_working(pool);
826 /* Do we have too many workers and should some go away? */
827 static bool too_many_workers(struct worker_pool *pool)
829 bool managing = pool->flags & POOL_MANAGER_ACTIVE;
830 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
831 int nr_busy = pool->nr_workers - nr_idle;
833 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
840 /* Return the first idle worker. Called with pool->lock held. */
841 static struct worker *first_idle_worker(struct worker_pool *pool)
843 if (unlikely(list_empty(&pool->idle_list)))
846 return list_first_entry(&pool->idle_list, struct worker, entry);
850 * wake_up_worker - wake up an idle worker
851 * @pool: worker pool to wake worker from
853 * Wake up the first idle worker of @pool.
856 * raw_spin_lock_irq(pool->lock).
858 static void wake_up_worker(struct worker_pool *pool)
860 struct worker *worker = first_idle_worker(pool);
863 wake_up_process(worker->task);
867 * wq_worker_running - a worker is running again
868 * @task: task waking up
870 * This function is called when a worker returns from schedule()
872 void wq_worker_running(struct task_struct *task)
874 struct worker *worker = kthread_data(task);
876 if (!worker->sleeping)
880 * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check
881 * and the nr_running increment below, we may ruin the nr_running reset
882 * and leave with an unexpected pool->nr_running == 1 on the newly unbound
883 * pool. Protect against such race.
886 if (!(worker->flags & WORKER_NOT_RUNNING))
887 worker->pool->nr_running++;
889 worker->sleeping = 0;
893 * wq_worker_sleeping - a worker is going to sleep
894 * @task: task going to sleep
896 * This function is called from schedule() when a busy worker is
899 void wq_worker_sleeping(struct task_struct *task)
901 struct worker *worker = kthread_data(task);
902 struct worker_pool *pool;
905 * Rescuers, which may not have all the fields set up like normal
906 * workers, also reach here, let's not access anything before
907 * checking NOT_RUNNING.
909 if (worker->flags & WORKER_NOT_RUNNING)
914 /* Return if preempted before wq_worker_running() was reached */
915 if (worker->sleeping)
918 worker->sleeping = 1;
919 raw_spin_lock_irq(&pool->lock);
922 * Recheck in case unbind_workers() preempted us. We don't
923 * want to decrement nr_running after the worker is unbound
924 * and nr_running has been reset.
926 if (worker->flags & WORKER_NOT_RUNNING) {
927 raw_spin_unlock_irq(&pool->lock);
932 if (need_more_worker(pool))
933 wake_up_worker(pool);
934 raw_spin_unlock_irq(&pool->lock);
938 * wq_worker_last_func - retrieve worker's last work function
939 * @task: Task to retrieve last work function of.
941 * Determine the last function a worker executed. This is called from
942 * the scheduler to get a worker's last known identity.
945 * raw_spin_lock_irq(rq->lock)
947 * This function is called during schedule() when a kworker is going
948 * to sleep. It's used by psi to identify aggregation workers during
949 * dequeuing, to allow periodic aggregation to shut-off when that
950 * worker is the last task in the system or cgroup to go to sleep.
952 * As this function doesn't involve any workqueue-related locking, it
953 * only returns stable values when called from inside the scheduler's
954 * queuing and dequeuing paths, when @task, which must be a kworker,
955 * is guaranteed to not be processing any works.
958 * The last work function %current executed as a worker, NULL if it
959 * hasn't executed any work yet.
961 work_func_t wq_worker_last_func(struct task_struct *task)
963 struct worker *worker = kthread_data(task);
965 return worker->last_func;
969 * worker_set_flags - set worker flags and adjust nr_running accordingly
971 * @flags: flags to set
973 * Set @flags in @worker->flags and adjust nr_running accordingly.
976 * raw_spin_lock_irq(pool->lock)
978 static inline void worker_set_flags(struct worker *worker, unsigned int flags)
980 struct worker_pool *pool = worker->pool;
982 WARN_ON_ONCE(worker->task != current);
984 /* If transitioning into NOT_RUNNING, adjust nr_running. */
985 if ((flags & WORKER_NOT_RUNNING) &&
986 !(worker->flags & WORKER_NOT_RUNNING)) {
990 worker->flags |= flags;
994 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
996 * @flags: flags to clear
998 * Clear @flags in @worker->flags and adjust nr_running accordingly.
1001 * raw_spin_lock_irq(pool->lock)
1003 static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
1005 struct worker_pool *pool = worker->pool;
1006 unsigned int oflags = worker->flags;
1008 WARN_ON_ONCE(worker->task != current);
1010 worker->flags &= ~flags;
1013 * If transitioning out of NOT_RUNNING, increment nr_running. Note
1014 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
1015 * of multiple flags, not a single flag.
1017 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
1018 if (!(worker->flags & WORKER_NOT_RUNNING))
1023 * find_worker_executing_work - find worker which is executing a work
1024 * @pool: pool of interest
1025 * @work: work to find worker for
1027 * Find a worker which is executing @work on @pool by searching
1028 * @pool->busy_hash which is keyed by the address of @work. For a worker
1029 * to match, its current execution should match the address of @work and
1030 * its work function. This is to avoid unwanted dependency between
1031 * unrelated work executions through a work item being recycled while still
1034 * This is a bit tricky. A work item may be freed once its execution
1035 * starts and nothing prevents the freed area from being recycled for
1036 * another work item. If the same work item address ends up being reused
1037 * before the original execution finishes, workqueue will identify the
1038 * recycled work item as currently executing and make it wait until the
1039 * current execution finishes, introducing an unwanted dependency.
1041 * This function checks the work item address and work function to avoid
1042 * false positives. Note that this isn't complete as one may construct a
1043 * work function which can introduce dependency onto itself through a
1044 * recycled work item. Well, if somebody wants to shoot oneself in the
1045 * foot that badly, there's only so much we can do, and if such deadlock
1046 * actually occurs, it should be easy to locate the culprit work function.
1049 * raw_spin_lock_irq(pool->lock).
1052 * Pointer to worker which is executing @work if found, %NULL
1055 static struct worker *find_worker_executing_work(struct worker_pool *pool,
1056 struct work_struct *work)
1058 struct worker *worker;
1060 hash_for_each_possible(pool->busy_hash, worker, hentry,
1061 (unsigned long)work)
1062 if (worker->current_work == work &&
1063 worker->current_func == work->func)
1070 * move_linked_works - move linked works to a list
1071 * @work: start of series of works to be scheduled
1072 * @head: target list to append @work to
1073 * @nextp: out parameter for nested worklist walking
1075 * Schedule linked works starting from @work to @head. Work series to
1076 * be scheduled starts at @work and includes any consecutive work with
1077 * WORK_STRUCT_LINKED set in its predecessor.
1079 * If @nextp is not NULL, it's updated to point to the next work of
1080 * the last scheduled work. This allows move_linked_works() to be
1081 * nested inside outer list_for_each_entry_safe().
1084 * raw_spin_lock_irq(pool->lock).
1086 static void move_linked_works(struct work_struct *work, struct list_head *head,
1087 struct work_struct **nextp)
1089 struct work_struct *n;
1092 * Linked worklist will always end before the end of the list,
1093 * use NULL for list head.
1095 list_for_each_entry_safe_from(work, n, NULL, entry) {
1096 list_move_tail(&work->entry, head);
1097 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1102 * If we're already inside safe list traversal and have moved
1103 * multiple works to the scheduled queue, the next position
1104 * needs to be updated.
1111 * get_pwq - get an extra reference on the specified pool_workqueue
1112 * @pwq: pool_workqueue to get
1114 * Obtain an extra reference on @pwq. The caller should guarantee that
1115 * @pwq has positive refcnt and be holding the matching pool->lock.
1117 static void get_pwq(struct pool_workqueue *pwq)
1119 lockdep_assert_held(&pwq->pool->lock);
1120 WARN_ON_ONCE(pwq->refcnt <= 0);
1125 * put_pwq - put a pool_workqueue reference
1126 * @pwq: pool_workqueue to put
1128 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
1129 * destruction. The caller should be holding the matching pool->lock.
1131 static void put_pwq(struct pool_workqueue *pwq)
1133 lockdep_assert_held(&pwq->pool->lock);
1134 if (likely(--pwq->refcnt))
1136 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
1139 * @pwq can't be released under pool->lock, bounce to
1140 * pwq_unbound_release_workfn(). This never recurses on the same
1141 * pool->lock as this path is taken only for unbound workqueues and
1142 * the release work item is scheduled on a per-cpu workqueue. To
1143 * avoid lockdep warning, unbound pool->locks are given lockdep
1144 * subclass of 1 in get_unbound_pool().
1146 schedule_work(&pwq->unbound_release_work);
1150 * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock
1151 * @pwq: pool_workqueue to put (can be %NULL)
1153 * put_pwq() with locking. This function also allows %NULL @pwq.
1155 static void put_pwq_unlocked(struct pool_workqueue *pwq)
1159 * As both pwqs and pools are RCU protected, the
1160 * following lock operations are safe.
1162 raw_spin_lock_irq(&pwq->pool->lock);
1164 raw_spin_unlock_irq(&pwq->pool->lock);
1168 static void pwq_activate_inactive_work(struct work_struct *work)
1170 struct pool_workqueue *pwq = get_work_pwq(work);
1172 trace_workqueue_activate_work(work);
1173 if (list_empty(&pwq->pool->worklist))
1174 pwq->pool->watchdog_ts = jiffies;
1175 move_linked_works(work, &pwq->pool->worklist, NULL);
1176 __clear_bit(WORK_STRUCT_INACTIVE_BIT, work_data_bits(work));
1180 static void pwq_activate_first_inactive(struct pool_workqueue *pwq)
1182 struct work_struct *work = list_first_entry(&pwq->inactive_works,
1183 struct work_struct, entry);
1185 pwq_activate_inactive_work(work);
1189 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1190 * @pwq: pwq of interest
1191 * @work_data: work_data of work which left the queue
1193 * A work either has completed or is removed from pending queue,
1194 * decrement nr_in_flight of its pwq and handle workqueue flushing.
1197 * raw_spin_lock_irq(pool->lock).
1199 static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data)
1201 int color = get_work_color(work_data);
1203 if (!(work_data & WORK_STRUCT_INACTIVE)) {
1205 if (!list_empty(&pwq->inactive_works)) {
1206 /* one down, submit an inactive one */
1207 if (pwq->nr_active < pwq->max_active)
1208 pwq_activate_first_inactive(pwq);
1212 pwq->nr_in_flight[color]--;
1214 /* is flush in progress and are we at the flushing tip? */
1215 if (likely(pwq->flush_color != color))
1218 /* are there still in-flight works? */
1219 if (pwq->nr_in_flight[color])
1222 /* this pwq is done, clear flush_color */
1223 pwq->flush_color = -1;
1226 * If this was the last pwq, wake up the first flusher. It
1227 * will handle the rest.
1229 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1230 complete(&pwq->wq->first_flusher->done);
1236 * try_to_grab_pending - steal work item from worklist and disable irq
1237 * @work: work item to steal
1238 * @is_dwork: @work is a delayed_work
1239 * @flags: place to store irq state
1241 * Try to grab PENDING bit of @work. This function can handle @work in any
1242 * stable state - idle, on timer or on worklist.
1246 * ======== ================================================================
1247 * 1 if @work was pending and we successfully stole PENDING
1248 * 0 if @work was idle and we claimed PENDING
1249 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
1250 * -ENOENT if someone else is canceling @work, this state may persist
1251 * for arbitrarily long
1252 * ======== ================================================================
1255 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
1256 * interrupted while holding PENDING and @work off queue, irq must be
1257 * disabled on entry. This, combined with delayed_work->timer being
1258 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
1260 * On successful return, >= 0, irq is disabled and the caller is
1261 * responsible for releasing it using local_irq_restore(*@flags).
1263 * This function is safe to call from any context including IRQ handler.
1265 static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1266 unsigned long *flags)
1268 struct worker_pool *pool;
1269 struct pool_workqueue *pwq;
1271 local_irq_save(*flags);
1273 /* try to steal the timer if it exists */
1275 struct delayed_work *dwork = to_delayed_work(work);
1278 * dwork->timer is irqsafe. If del_timer() fails, it's
1279 * guaranteed that the timer is not queued anywhere and not
1280 * running on the local CPU.
1282 if (likely(del_timer(&dwork->timer)))
1286 /* try to claim PENDING the normal way */
1287 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1292 * The queueing is in progress, or it is already queued. Try to
1293 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1295 pool = get_work_pool(work);
1299 raw_spin_lock(&pool->lock);
1301 * work->data is guaranteed to point to pwq only while the work
1302 * item is queued on pwq->wq, and both updating work->data to point
1303 * to pwq on queueing and to pool on dequeueing are done under
1304 * pwq->pool->lock. This in turn guarantees that, if work->data
1305 * points to pwq which is associated with a locked pool, the work
1306 * item is currently queued on that pool.
1308 pwq = get_work_pwq(work);
1309 if (pwq && pwq->pool == pool) {
1310 debug_work_deactivate(work);
1313 * A cancelable inactive work item must be in the
1314 * pwq->inactive_works since a queued barrier can't be
1315 * canceled (see the comments in insert_wq_barrier()).
1317 * An inactive work item cannot be grabbed directly because
1318 * it might have linked barrier work items which, if left
1319 * on the inactive_works list, will confuse pwq->nr_active
1320 * management later on and cause stall. Make sure the work
1321 * item is activated before grabbing.
1323 if (*work_data_bits(work) & WORK_STRUCT_INACTIVE)
1324 pwq_activate_inactive_work(work);
1326 list_del_init(&work->entry);
1327 pwq_dec_nr_in_flight(pwq, *work_data_bits(work));
1329 /* work->data points to pwq iff queued, point to pool */
1330 set_work_pool_and_keep_pending(work, pool->id);
1332 raw_spin_unlock(&pool->lock);
1336 raw_spin_unlock(&pool->lock);
1339 local_irq_restore(*flags);
1340 if (work_is_canceling(work))
1347 * insert_work - insert a work into a pool
1348 * @pwq: pwq @work belongs to
1349 * @work: work to insert
1350 * @head: insertion point
1351 * @extra_flags: extra WORK_STRUCT_* flags to set
1353 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
1354 * work_struct flags.
1357 * raw_spin_lock_irq(pool->lock).
1359 static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1360 struct list_head *head, unsigned int extra_flags)
1362 struct worker_pool *pool = pwq->pool;
1364 /* record the work call stack in order to print it in KASAN reports */
1365 kasan_record_aux_stack_noalloc(work);
1367 /* we own @work, set data and link */
1368 set_work_pwq(work, pwq, extra_flags);
1369 list_add_tail(&work->entry, head);
1372 if (__need_more_worker(pool))
1373 wake_up_worker(pool);
1377 * Test whether @work is being queued from another work executing on the
1380 static bool is_chained_work(struct workqueue_struct *wq)
1382 struct worker *worker;
1384 worker = current_wq_worker();
1386 * Return %true iff I'm a worker executing a work item on @wq. If
1387 * I'm @worker, it's safe to dereference it without locking.
1389 return worker && worker->current_pwq->wq == wq;
1393 * When queueing an unbound work item to a wq, prefer local CPU if allowed
1394 * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to
1395 * avoid perturbing sensitive tasks.
1397 static int wq_select_unbound_cpu(int cpu)
1401 if (likely(!wq_debug_force_rr_cpu)) {
1402 if (cpumask_test_cpu(cpu, wq_unbound_cpumask))
1405 pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n");
1408 if (cpumask_empty(wq_unbound_cpumask))
1411 new_cpu = __this_cpu_read(wq_rr_cpu_last);
1412 new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask);
1413 if (unlikely(new_cpu >= nr_cpu_ids)) {
1414 new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask);
1415 if (unlikely(new_cpu >= nr_cpu_ids))
1418 __this_cpu_write(wq_rr_cpu_last, new_cpu);
1423 static void __queue_work(int cpu, struct workqueue_struct *wq,
1424 struct work_struct *work)
1426 struct pool_workqueue *pwq;
1427 struct worker_pool *last_pool;
1428 struct list_head *worklist;
1429 unsigned int work_flags;
1430 unsigned int req_cpu = cpu;
1433 * While a work item is PENDING && off queue, a task trying to
1434 * steal the PENDING will busy-loop waiting for it to either get
1435 * queued or lose PENDING. Grabbing PENDING and queueing should
1436 * happen with IRQ disabled.
1438 lockdep_assert_irqs_disabled();
1442 * For a draining wq, only works from the same workqueue are
1443 * allowed. The __WQ_DESTROYING helps to spot the issue that
1444 * queues a new work item to a wq after destroy_workqueue(wq).
1446 if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) &&
1447 WARN_ON_ONCE(!is_chained_work(wq))))
1451 /* pwq which will be used unless @work is executing elsewhere */
1452 if (wq->flags & WQ_UNBOUND) {
1453 if (req_cpu == WORK_CPU_UNBOUND)
1454 cpu = wq_select_unbound_cpu(raw_smp_processor_id());
1455 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
1457 if (req_cpu == WORK_CPU_UNBOUND)
1458 cpu = raw_smp_processor_id();
1459 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
1463 * If @work was previously on a different pool, it might still be
1464 * running there, in which case the work needs to be queued on that
1465 * pool to guarantee non-reentrancy.
1467 last_pool = get_work_pool(work);
1468 if (last_pool && last_pool != pwq->pool) {
1469 struct worker *worker;
1471 raw_spin_lock(&last_pool->lock);
1473 worker = find_worker_executing_work(last_pool, work);
1475 if (worker && worker->current_pwq->wq == wq) {
1476 pwq = worker->current_pwq;
1478 /* meh... not running there, queue here */
1479 raw_spin_unlock(&last_pool->lock);
1480 raw_spin_lock(&pwq->pool->lock);
1483 raw_spin_lock(&pwq->pool->lock);
1487 * pwq is determined and locked. For unbound pools, we could have
1488 * raced with pwq release and it could already be dead. If its
1489 * refcnt is zero, repeat pwq selection. Note that pwqs never die
1490 * without another pwq replacing it in the numa_pwq_tbl or while
1491 * work items are executing on it, so the retrying is guaranteed to
1492 * make forward-progress.
1494 if (unlikely(!pwq->refcnt)) {
1495 if (wq->flags & WQ_UNBOUND) {
1496 raw_spin_unlock(&pwq->pool->lock);
1501 WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
1505 /* pwq determined, queue */
1506 trace_workqueue_queue_work(req_cpu, pwq, work);
1508 if (WARN_ON(!list_empty(&work->entry)))
1511 pwq->nr_in_flight[pwq->work_color]++;
1512 work_flags = work_color_to_flags(pwq->work_color);
1514 if (likely(pwq->nr_active < pwq->max_active)) {
1515 trace_workqueue_activate_work(work);
1517 worklist = &pwq->pool->worklist;
1518 if (list_empty(worklist))
1519 pwq->pool->watchdog_ts = jiffies;
1521 work_flags |= WORK_STRUCT_INACTIVE;
1522 worklist = &pwq->inactive_works;
1525 debug_work_activate(work);
1526 insert_work(pwq, work, worklist, work_flags);
1529 raw_spin_unlock(&pwq->pool->lock);
1534 * queue_work_on - queue work on specific cpu
1535 * @cpu: CPU number to execute work on
1536 * @wq: workqueue to use
1537 * @work: work to queue
1539 * We queue the work to a specific CPU, the caller must ensure it
1540 * can't go away. Callers that fail to ensure that the specified
1541 * CPU cannot go away will execute on a randomly chosen CPU.
1543 * Return: %false if @work was already on a queue, %true otherwise.
1545 bool queue_work_on(int cpu, struct workqueue_struct *wq,
1546 struct work_struct *work)
1549 unsigned long flags;
1551 local_irq_save(flags);
1553 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
1554 __queue_work(cpu, wq, work);
1558 local_irq_restore(flags);
1561 EXPORT_SYMBOL(queue_work_on);
1564 * workqueue_select_cpu_near - Select a CPU based on NUMA node
1565 * @node: NUMA node ID that we want to select a CPU from
1567 * This function will attempt to find a "random" cpu available on a given
1568 * node. If there are no CPUs available on the given node it will return
1569 * WORK_CPU_UNBOUND indicating that we should just schedule to any
1570 * available CPU if we need to schedule this work.
1572 static int workqueue_select_cpu_near(int node)
1576 /* No point in doing this if NUMA isn't enabled for workqueues */
1577 if (!wq_numa_enabled)
1578 return WORK_CPU_UNBOUND;
1580 /* Delay binding to CPU if node is not valid or online */
1581 if (node < 0 || node >= MAX_NUMNODES || !node_online(node))
1582 return WORK_CPU_UNBOUND;
1584 /* Use local node/cpu if we are already there */
1585 cpu = raw_smp_processor_id();
1586 if (node == cpu_to_node(cpu))
1589 /* Use "random" otherwise know as "first" online CPU of node */
1590 cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask);
1592 /* If CPU is valid return that, otherwise just defer */
1593 return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND;
1597 * queue_work_node - queue work on a "random" cpu for a given NUMA node
1598 * @node: NUMA node that we are targeting the work for
1599 * @wq: workqueue to use
1600 * @work: work to queue
1602 * We queue the work to a "random" CPU within a given NUMA node. The basic
1603 * idea here is to provide a way to somehow associate work with a given
1606 * This function will only make a best effort attempt at getting this onto
1607 * the right NUMA node. If no node is requested or the requested node is
1608 * offline then we just fall back to standard queue_work behavior.
1610 * Currently the "random" CPU ends up being the first available CPU in the
1611 * intersection of cpu_online_mask and the cpumask of the node, unless we
1612 * are running on the node. In that case we just use the current CPU.
1614 * Return: %false if @work was already on a queue, %true otherwise.
1616 bool queue_work_node(int node, struct workqueue_struct *wq,
1617 struct work_struct *work)
1619 unsigned long flags;
1623 * This current implementation is specific to unbound workqueues.
1624 * Specifically we only return the first available CPU for a given
1625 * node instead of cycling through individual CPUs within the node.
1627 * If this is used with a per-cpu workqueue then the logic in
1628 * workqueue_select_cpu_near would need to be updated to allow for
1629 * some round robin type logic.
1631 WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND));
1633 local_irq_save(flags);
1635 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
1636 int cpu = workqueue_select_cpu_near(node);
1638 __queue_work(cpu, wq, work);
1642 local_irq_restore(flags);
1645 EXPORT_SYMBOL_GPL(queue_work_node);
1647 void delayed_work_timer_fn(struct timer_list *t)
1649 struct delayed_work *dwork = from_timer(dwork, t, timer);
1651 /* should have been called from irqsafe timer with irq already off */
1652 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
1654 EXPORT_SYMBOL(delayed_work_timer_fn);
1656 static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1657 struct delayed_work *dwork, unsigned long delay)
1659 struct timer_list *timer = &dwork->timer;
1660 struct work_struct *work = &dwork->work;
1663 WARN_ON_ONCE(timer->function != delayed_work_timer_fn);
1664 WARN_ON_ONCE(timer_pending(timer));
1665 WARN_ON_ONCE(!list_empty(&work->entry));
1668 * If @delay is 0, queue @dwork->work immediately. This is for
1669 * both optimization and correctness. The earliest @timer can
1670 * expire is on the closest next tick and delayed_work users depend
1671 * on that there's no such delay when @delay is 0.
1674 __queue_work(cpu, wq, &dwork->work);
1680 timer->expires = jiffies + delay;
1682 if (unlikely(cpu != WORK_CPU_UNBOUND))
1683 add_timer_on(timer, cpu);
1689 * queue_delayed_work_on - queue work on specific CPU after delay
1690 * @cpu: CPU number to execute work on
1691 * @wq: workqueue to use
1692 * @dwork: work to queue
1693 * @delay: number of jiffies to wait before queueing
1695 * Return: %false if @work was already on a queue, %true otherwise. If
1696 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1699 bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1700 struct delayed_work *dwork, unsigned long delay)
1702 struct work_struct *work = &dwork->work;
1704 unsigned long flags;
1706 /* read the comment in __queue_work() */
1707 local_irq_save(flags);
1709 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
1710 __queue_delayed_work(cpu, wq, dwork, delay);
1714 local_irq_restore(flags);
1717 EXPORT_SYMBOL(queue_delayed_work_on);
1720 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1721 * @cpu: CPU number to execute work on
1722 * @wq: workqueue to use
1723 * @dwork: work to queue
1724 * @delay: number of jiffies to wait before queueing
1726 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1727 * modify @dwork's timer so that it expires after @delay. If @delay is
1728 * zero, @work is guaranteed to be scheduled immediately regardless of its
1731 * Return: %false if @dwork was idle and queued, %true if @dwork was
1732 * pending and its timer was modified.
1734 * This function is safe to call from any context including IRQ handler.
1735 * See try_to_grab_pending() for details.
1737 bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1738 struct delayed_work *dwork, unsigned long delay)
1740 unsigned long flags;
1744 ret = try_to_grab_pending(&dwork->work, true, &flags);
1745 } while (unlikely(ret == -EAGAIN));
1747 if (likely(ret >= 0)) {
1748 __queue_delayed_work(cpu, wq, dwork, delay);
1749 local_irq_restore(flags);
1752 /* -ENOENT from try_to_grab_pending() becomes %true */
1755 EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1757 static void rcu_work_rcufn(struct rcu_head *rcu)
1759 struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu);
1761 /* read the comment in __queue_work() */
1762 local_irq_disable();
1763 __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work);
1768 * queue_rcu_work - queue work after a RCU grace period
1769 * @wq: workqueue to use
1770 * @rwork: work to queue
1772 * Return: %false if @rwork was already pending, %true otherwise. Note
1773 * that a full RCU grace period is guaranteed only after a %true return.
1774 * While @rwork is guaranteed to be executed after a %false return, the
1775 * execution may happen before a full RCU grace period has passed.
1777 bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork)
1779 struct work_struct *work = &rwork->work;
1781 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
1783 call_rcu_hurry(&rwork->rcu, rcu_work_rcufn);
1789 EXPORT_SYMBOL(queue_rcu_work);
1792 * worker_enter_idle - enter idle state
1793 * @worker: worker which is entering idle state
1795 * @worker is entering idle state. Update stats and idle timer if
1799 * raw_spin_lock_irq(pool->lock).
1801 static void worker_enter_idle(struct worker *worker)
1803 struct worker_pool *pool = worker->pool;
1805 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1806 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1807 (worker->hentry.next || worker->hentry.pprev)))
1810 /* can't use worker_set_flags(), also called from create_worker() */
1811 worker->flags |= WORKER_IDLE;
1813 worker->last_active = jiffies;
1815 /* idle_list is LIFO */
1816 list_add(&worker->entry, &pool->idle_list);
1818 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1819 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
1821 /* Sanity check nr_running. */
1822 WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running);
1826 * worker_leave_idle - leave idle state
1827 * @worker: worker which is leaving idle state
1829 * @worker is leaving idle state. Update stats.
1832 * raw_spin_lock_irq(pool->lock).
1834 static void worker_leave_idle(struct worker *worker)
1836 struct worker_pool *pool = worker->pool;
1838 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1840 worker_clr_flags(worker, WORKER_IDLE);
1842 list_del_init(&worker->entry);
1845 static struct worker *alloc_worker(int node)
1847 struct worker *worker;
1849 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node);
1851 INIT_LIST_HEAD(&worker->entry);
1852 INIT_LIST_HEAD(&worker->scheduled);
1853 INIT_LIST_HEAD(&worker->node);
1854 /* on creation a worker is in !idle && prep state */
1855 worker->flags = WORKER_PREP;
1861 * worker_attach_to_pool() - attach a worker to a pool
1862 * @worker: worker to be attached
1863 * @pool: the target pool
1865 * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and
1866 * cpu-binding of @worker are kept coordinated with the pool across
1869 static void worker_attach_to_pool(struct worker *worker,
1870 struct worker_pool *pool)
1872 mutex_lock(&wq_pool_attach_mutex);
1875 * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains
1876 * stable across this function. See the comments above the flag
1877 * definition for details.
1879 if (pool->flags & POOL_DISASSOCIATED)
1880 worker->flags |= WORKER_UNBOUND;
1882 kthread_set_per_cpu(worker->task, pool->cpu);
1884 if (worker->rescue_wq)
1885 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
1887 list_add_tail(&worker->node, &pool->workers);
1888 worker->pool = pool;
1890 mutex_unlock(&wq_pool_attach_mutex);
1894 * worker_detach_from_pool() - detach a worker from its pool
1895 * @worker: worker which is attached to its pool
1897 * Undo the attaching which had been done in worker_attach_to_pool(). The
1898 * caller worker shouldn't access to the pool after detached except it has
1899 * other reference to the pool.
1901 static void worker_detach_from_pool(struct worker *worker)
1903 struct worker_pool *pool = worker->pool;
1904 struct completion *detach_completion = NULL;
1906 mutex_lock(&wq_pool_attach_mutex);
1908 kthread_set_per_cpu(worker->task, -1);
1909 list_del(&worker->node);
1910 worker->pool = NULL;
1912 if (list_empty(&pool->workers) && list_empty(&pool->dying_workers))
1913 detach_completion = pool->detach_completion;
1914 mutex_unlock(&wq_pool_attach_mutex);
1916 /* clear leftover flags without pool->lock after it is detached */
1917 worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND);
1919 if (detach_completion)
1920 complete(detach_completion);
1924 * create_worker - create a new workqueue worker
1925 * @pool: pool the new worker will belong to
1927 * Create and start a new worker which is attached to @pool.
1930 * Might sleep. Does GFP_KERNEL allocations.
1933 * Pointer to the newly created worker.
1935 static struct worker *create_worker(struct worker_pool *pool)
1937 struct worker *worker;
1941 /* ID is needed to determine kthread name */
1942 id = ida_alloc(&pool->worker_ida, GFP_KERNEL);
1944 pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n",
1949 worker = alloc_worker(pool->node);
1951 pr_err_once("workqueue: Failed to allocate a worker\n");
1958 snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,
1959 pool->attrs->nice < 0 ? "H" : "");
1961 snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id);
1963 worker->task = kthread_create_on_node(worker_thread, worker, pool->node,
1964 "kworker/%s", id_buf);
1965 if (IS_ERR(worker->task)) {
1966 if (PTR_ERR(worker->task) == -EINTR) {
1967 pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n",
1970 pr_err_once("workqueue: Failed to create a worker thread: %pe",
1976 set_user_nice(worker->task, pool->attrs->nice);
1977 kthread_bind_mask(worker->task, pool->attrs->cpumask);
1979 /* successful, attach the worker to the pool */
1980 worker_attach_to_pool(worker, pool);
1982 /* start the newly created worker */
1983 raw_spin_lock_irq(&pool->lock);
1984 worker->pool->nr_workers++;
1985 worker_enter_idle(worker);
1986 wake_up_process(worker->task);
1987 raw_spin_unlock_irq(&pool->lock);
1992 ida_free(&pool->worker_ida, id);
1997 static void unbind_worker(struct worker *worker)
1999 lockdep_assert_held(&wq_pool_attach_mutex);
2001 kthread_set_per_cpu(worker->task, -1);
2002 if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask))
2003 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0);
2005 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0);
2008 static void wake_dying_workers(struct list_head *cull_list)
2010 struct worker *worker, *tmp;
2012 list_for_each_entry_safe(worker, tmp, cull_list, entry) {
2013 list_del_init(&worker->entry);
2014 unbind_worker(worker);
2016 * If the worker was somehow already running, then it had to be
2017 * in pool->idle_list when set_worker_dying() happened or we
2018 * wouldn't have gotten here.
2020 * Thus, the worker must either have observed the WORKER_DIE
2021 * flag, or have set its state to TASK_IDLE. Either way, the
2022 * below will be observed by the worker and is safe to do
2023 * outside of pool->lock.
2025 wake_up_process(worker->task);
2030 * set_worker_dying - Tag a worker for destruction
2031 * @worker: worker to be destroyed
2032 * @list: transfer worker away from its pool->idle_list and into list
2034 * Tag @worker for destruction and adjust @pool stats accordingly. The worker
2038 * raw_spin_lock_irq(pool->lock).
2040 static void set_worker_dying(struct worker *worker, struct list_head *list)
2042 struct worker_pool *pool = worker->pool;
2044 lockdep_assert_held(&pool->lock);
2045 lockdep_assert_held(&wq_pool_attach_mutex);
2047 /* sanity check frenzy */
2048 if (WARN_ON(worker->current_work) ||
2049 WARN_ON(!list_empty(&worker->scheduled)) ||
2050 WARN_ON(!(worker->flags & WORKER_IDLE)))
2056 worker->flags |= WORKER_DIE;
2058 list_move(&worker->entry, list);
2059 list_move(&worker->node, &pool->dying_workers);
2063 * idle_worker_timeout - check if some idle workers can now be deleted.
2064 * @t: The pool's idle_timer that just expired
2066 * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in
2067 * worker_leave_idle(), as a worker flicking between idle and active while its
2068 * pool is at the too_many_workers() tipping point would cause too much timer
2069 * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let
2070 * it expire and re-evaluate things from there.
2072 static void idle_worker_timeout(struct timer_list *t)
2074 struct worker_pool *pool = from_timer(pool, t, idle_timer);
2075 bool do_cull = false;
2077 if (work_pending(&pool->idle_cull_work))
2080 raw_spin_lock_irq(&pool->lock);
2082 if (too_many_workers(pool)) {
2083 struct worker *worker;
2084 unsigned long expires;
2086 /* idle_list is kept in LIFO order, check the last one */
2087 worker = list_entry(pool->idle_list.prev, struct worker, entry);
2088 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2089 do_cull = !time_before(jiffies, expires);
2092 mod_timer(&pool->idle_timer, expires);
2094 raw_spin_unlock_irq(&pool->lock);
2097 queue_work(system_unbound_wq, &pool->idle_cull_work);
2101 * idle_cull_fn - cull workers that have been idle for too long.
2102 * @work: the pool's work for handling these idle workers
2104 * This goes through a pool's idle workers and gets rid of those that have been
2105 * idle for at least IDLE_WORKER_TIMEOUT seconds.
2107 * We don't want to disturb isolated CPUs because of a pcpu kworker being
2108 * culled, so this also resets worker affinity. This requires a sleepable
2109 * context, hence the split between timer callback and work item.
2111 static void idle_cull_fn(struct work_struct *work)
2113 struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work);
2114 struct list_head cull_list;
2116 INIT_LIST_HEAD(&cull_list);
2118 * Grabbing wq_pool_attach_mutex here ensures an already-running worker
2119 * cannot proceed beyong worker_detach_from_pool() in its self-destruct
2120 * path. This is required as a previously-preempted worker could run after
2121 * set_worker_dying() has happened but before wake_dying_workers() did.
2123 mutex_lock(&wq_pool_attach_mutex);
2124 raw_spin_lock_irq(&pool->lock);
2126 while (too_many_workers(pool)) {
2127 struct worker *worker;
2128 unsigned long expires;
2130 worker = list_entry(pool->idle_list.prev, struct worker, entry);
2131 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2133 if (time_before(jiffies, expires)) {
2134 mod_timer(&pool->idle_timer, expires);
2138 set_worker_dying(worker, &cull_list);
2141 raw_spin_unlock_irq(&pool->lock);
2142 wake_dying_workers(&cull_list);
2143 mutex_unlock(&wq_pool_attach_mutex);
2146 static void send_mayday(struct work_struct *work)
2148 struct pool_workqueue *pwq = get_work_pwq(work);
2149 struct workqueue_struct *wq = pwq->wq;
2151 lockdep_assert_held(&wq_mayday_lock);
2156 /* mayday mayday mayday */
2157 if (list_empty(&pwq->mayday_node)) {
2159 * If @pwq is for an unbound wq, its base ref may be put at
2160 * any time due to an attribute change. Pin @pwq until the
2161 * rescuer is done with it.
2164 list_add_tail(&pwq->mayday_node, &wq->maydays);
2165 wake_up_process(wq->rescuer->task);
2169 static void pool_mayday_timeout(struct timer_list *t)
2171 struct worker_pool *pool = from_timer(pool, t, mayday_timer);
2172 struct work_struct *work;
2174 raw_spin_lock_irq(&pool->lock);
2175 raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */
2177 if (need_to_create_worker(pool)) {
2179 * We've been trying to create a new worker but
2180 * haven't been successful. We might be hitting an
2181 * allocation deadlock. Send distress signals to
2184 list_for_each_entry(work, &pool->worklist, entry)
2188 raw_spin_unlock(&wq_mayday_lock);
2189 raw_spin_unlock_irq(&pool->lock);
2191 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
2195 * maybe_create_worker - create a new worker if necessary
2196 * @pool: pool to create a new worker for
2198 * Create a new worker for @pool if necessary. @pool is guaranteed to
2199 * have at least one idle worker on return from this function. If
2200 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
2201 * sent to all rescuers with works scheduled on @pool to resolve
2202 * possible allocation deadlock.
2204 * On return, need_to_create_worker() is guaranteed to be %false and
2205 * may_start_working() %true.
2208 * raw_spin_lock_irq(pool->lock) which may be released and regrabbed
2209 * multiple times. Does GFP_KERNEL allocations. Called only from
2212 static void maybe_create_worker(struct worker_pool *pool)
2213 __releases(&pool->lock)
2214 __acquires(&pool->lock)
2217 raw_spin_unlock_irq(&pool->lock);
2219 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
2220 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
2223 if (create_worker(pool) || !need_to_create_worker(pool))
2226 schedule_timeout_interruptible(CREATE_COOLDOWN);
2228 if (!need_to_create_worker(pool))
2232 del_timer_sync(&pool->mayday_timer);
2233 raw_spin_lock_irq(&pool->lock);
2235 * This is necessary even after a new worker was just successfully
2236 * created as @pool->lock was dropped and the new worker might have
2237 * already become busy.
2239 if (need_to_create_worker(pool))
2244 * manage_workers - manage worker pool
2247 * Assume the manager role and manage the worker pool @worker belongs
2248 * to. At any given time, there can be only zero or one manager per
2249 * pool. The exclusion is handled automatically by this function.
2251 * The caller can safely start processing works on false return. On
2252 * true return, it's guaranteed that need_to_create_worker() is false
2253 * and may_start_working() is true.
2256 * raw_spin_lock_irq(pool->lock) which may be released and regrabbed
2257 * multiple times. Does GFP_KERNEL allocations.
2260 * %false if the pool doesn't need management and the caller can safely
2261 * start processing works, %true if management function was performed and
2262 * the conditions that the caller verified before calling the function may
2263 * no longer be true.
2265 static bool manage_workers(struct worker *worker)
2267 struct worker_pool *pool = worker->pool;
2269 if (pool->flags & POOL_MANAGER_ACTIVE)
2272 pool->flags |= POOL_MANAGER_ACTIVE;
2273 pool->manager = worker;
2275 maybe_create_worker(pool);
2277 pool->manager = NULL;
2278 pool->flags &= ~POOL_MANAGER_ACTIVE;
2279 rcuwait_wake_up(&manager_wait);
2284 * process_one_work - process single work
2286 * @work: work to process
2288 * Process @work. This function contains all the logics necessary to
2289 * process a single work including synchronization against and
2290 * interaction with other workers on the same cpu, queueing and
2291 * flushing. As long as context requirement is met, any worker can
2292 * call this function to process a work.
2295 * raw_spin_lock_irq(pool->lock) which is released and regrabbed.
2297 static void process_one_work(struct worker *worker, struct work_struct *work)
2298 __releases(&pool->lock)
2299 __acquires(&pool->lock)
2301 struct pool_workqueue *pwq = get_work_pwq(work);
2302 struct worker_pool *pool = worker->pool;
2303 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
2304 unsigned long work_data;
2305 struct worker *collision;
2306 #ifdef CONFIG_LOCKDEP
2308 * It is permissible to free the struct work_struct from
2309 * inside the function that is called from it, this we need to
2310 * take into account for lockdep too. To avoid bogus "held
2311 * lock freed" warnings as well as problems when looking into
2312 * work->lockdep_map, make a copy and use that here.
2314 struct lockdep_map lockdep_map;
2316 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
2318 /* ensure we're on the correct CPU */
2319 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
2320 raw_smp_processor_id() != pool->cpu);
2323 * A single work shouldn't be executed concurrently by
2324 * multiple workers on a single cpu. Check whether anyone is
2325 * already processing the work. If so, defer the work to the
2326 * currently executing one.
2328 collision = find_worker_executing_work(pool, work);
2329 if (unlikely(collision)) {
2330 move_linked_works(work, &collision->scheduled, NULL);
2334 /* claim and dequeue */
2335 debug_work_deactivate(work);
2336 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
2337 worker->current_work = work;
2338 worker->current_func = work->func;
2339 worker->current_pwq = pwq;
2340 work_data = *work_data_bits(work);
2341 worker->current_color = get_work_color(work_data);
2344 * Record wq name for cmdline and debug reporting, may get
2345 * overridden through set_worker_desc().
2347 strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN);
2349 list_del_init(&work->entry);
2352 * CPU intensive works don't participate in concurrency management.
2353 * They're the scheduler's responsibility. This takes @worker out
2354 * of concurrency management and the next code block will chain
2355 * execution of the pending work items.
2357 if (unlikely(cpu_intensive))
2358 worker_set_flags(worker, WORKER_CPU_INTENSIVE);
2361 * Wake up another worker if necessary. The condition is always
2362 * false for normal per-cpu workers since nr_running would always
2363 * be >= 1 at this point. This is used to chain execution of the
2364 * pending work items for WORKER_NOT_RUNNING workers such as the
2365 * UNBOUND and CPU_INTENSIVE ones.
2367 if (need_more_worker(pool))
2368 wake_up_worker(pool);
2371 * Record the last pool and clear PENDING which should be the last
2372 * update to @work. Also, do this inside @pool->lock so that
2373 * PENDING and queued state changes happen together while IRQ is
2376 set_work_pool_and_clear_pending(work, pool->id);
2378 raw_spin_unlock_irq(&pool->lock);
2380 lock_map_acquire(&pwq->wq->lockdep_map);
2381 lock_map_acquire(&lockdep_map);
2383 * Strictly speaking we should mark the invariant state without holding
2384 * any locks, that is, before these two lock_map_acquire()'s.
2386 * However, that would result in:
2393 * Which would create W1->C->W1 dependencies, even though there is no
2394 * actual deadlock possible. There are two solutions, using a
2395 * read-recursive acquire on the work(queue) 'locks', but this will then
2396 * hit the lockdep limitation on recursive locks, or simply discard
2399 * AFAICT there is no possible deadlock scenario between the
2400 * flush_work() and complete() primitives (except for single-threaded
2401 * workqueues), so hiding them isn't a problem.
2403 lockdep_invariant_state(true);
2404 trace_workqueue_execute_start(work);
2405 worker->current_func(work);
2407 * While we must be careful to not use "work" after this, the trace
2408 * point will only record its address.
2410 trace_workqueue_execute_end(work, worker->current_func);
2411 lock_map_release(&lockdep_map);
2412 lock_map_release(&pwq->wq->lockdep_map);
2414 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
2415 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2416 " last function: %ps\n",
2417 current->comm, preempt_count(), task_pid_nr(current),
2418 worker->current_func);
2419 debug_show_held_locks(current);
2424 * The following prevents a kworker from hogging CPU on !PREEMPTION
2425 * kernels, where a requeueing work item waiting for something to
2426 * happen could deadlock with stop_machine as such work item could
2427 * indefinitely requeue itself while all other CPUs are trapped in
2428 * stop_machine. At the same time, report a quiescent RCU state so
2429 * the same condition doesn't freeze RCU.
2433 raw_spin_lock_irq(&pool->lock);
2435 /* clear cpu intensive status */
2436 if (unlikely(cpu_intensive))
2437 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2439 /* tag the worker for identification in schedule() */
2440 worker->last_func = worker->current_func;
2442 /* we're done with it, release */
2443 hash_del(&worker->hentry);
2444 worker->current_work = NULL;
2445 worker->current_func = NULL;
2446 worker->current_pwq = NULL;
2447 worker->current_color = INT_MAX;
2448 pwq_dec_nr_in_flight(pwq, work_data);
2452 * process_scheduled_works - process scheduled works
2455 * Process all scheduled works. Please note that the scheduled list
2456 * may change while processing a work, so this function repeatedly
2457 * fetches a work from the top and executes it.
2460 * raw_spin_lock_irq(pool->lock) which may be released and regrabbed
2463 static void process_scheduled_works(struct worker *worker)
2465 while (!list_empty(&worker->scheduled)) {
2466 struct work_struct *work = list_first_entry(&worker->scheduled,
2467 struct work_struct, entry);
2468 process_one_work(worker, work);
2472 static void set_pf_worker(bool val)
2474 mutex_lock(&wq_pool_attach_mutex);
2476 current->flags |= PF_WQ_WORKER;
2478 current->flags &= ~PF_WQ_WORKER;
2479 mutex_unlock(&wq_pool_attach_mutex);
2483 * worker_thread - the worker thread function
2486 * The worker thread function. All workers belong to a worker_pool -
2487 * either a per-cpu one or dynamic unbound one. These workers process all
2488 * work items regardless of their specific target workqueue. The only
2489 * exception is work items which belong to workqueues with a rescuer which
2490 * will be explained in rescuer_thread().
2494 static int worker_thread(void *__worker)
2496 struct worker *worker = __worker;
2497 struct worker_pool *pool = worker->pool;
2499 /* tell the scheduler that this is a workqueue worker */
2500 set_pf_worker(true);
2502 raw_spin_lock_irq(&pool->lock);
2504 /* am I supposed to die? */
2505 if (unlikely(worker->flags & WORKER_DIE)) {
2506 raw_spin_unlock_irq(&pool->lock);
2507 set_pf_worker(false);
2509 set_task_comm(worker->task, "kworker/dying");
2510 ida_free(&pool->worker_ida, worker->id);
2511 worker_detach_from_pool(worker);
2512 WARN_ON_ONCE(!list_empty(&worker->entry));
2517 worker_leave_idle(worker);
2519 /* no more worker necessary? */
2520 if (!need_more_worker(pool))
2523 /* do we need to manage? */
2524 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
2528 * ->scheduled list can only be filled while a worker is
2529 * preparing to process a work or actually processing it.
2530 * Make sure nobody diddled with it while I was sleeping.
2532 WARN_ON_ONCE(!list_empty(&worker->scheduled));
2535 * Finish PREP stage. We're guaranteed to have at least one idle
2536 * worker or that someone else has already assumed the manager
2537 * role. This is where @worker starts participating in concurrency
2538 * management if applicable and concurrency management is restored
2539 * after being rebound. See rebind_workers() for details.
2541 worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
2544 struct work_struct *work =
2545 list_first_entry(&pool->worklist,
2546 struct work_struct, entry);
2548 pool->watchdog_ts = jiffies;
2550 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2551 /* optimization path, not strictly necessary */
2552 process_one_work(worker, work);
2553 if (unlikely(!list_empty(&worker->scheduled)))
2554 process_scheduled_works(worker);
2556 move_linked_works(work, &worker->scheduled, NULL);
2557 process_scheduled_works(worker);
2559 } while (keep_working(pool));
2561 worker_set_flags(worker, WORKER_PREP);
2564 * pool->lock is held and there's no work to process and no need to
2565 * manage, sleep. Workers are woken up only while holding
2566 * pool->lock or from local cpu, so setting the current state
2567 * before releasing pool->lock is enough to prevent losing any
2570 worker_enter_idle(worker);
2571 __set_current_state(TASK_IDLE);
2572 raw_spin_unlock_irq(&pool->lock);
2578 * rescuer_thread - the rescuer thread function
2581 * Workqueue rescuer thread function. There's one rescuer for each
2582 * workqueue which has WQ_MEM_RECLAIM set.
2584 * Regular work processing on a pool may block trying to create a new
2585 * worker which uses GFP_KERNEL allocation which has slight chance of
2586 * developing into deadlock if some works currently on the same queue
2587 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2588 * the problem rescuer solves.
2590 * When such condition is possible, the pool summons rescuers of all
2591 * workqueues which have works queued on the pool and let them process
2592 * those works so that forward progress can be guaranteed.
2594 * This should happen rarely.
2598 static int rescuer_thread(void *__rescuer)
2600 struct worker *rescuer = __rescuer;
2601 struct workqueue_struct *wq = rescuer->rescue_wq;
2602 struct list_head *scheduled = &rescuer->scheduled;
2605 set_user_nice(current, RESCUER_NICE_LEVEL);
2608 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2609 * doesn't participate in concurrency management.
2611 set_pf_worker(true);
2613 set_current_state(TASK_IDLE);
2616 * By the time the rescuer is requested to stop, the workqueue
2617 * shouldn't have any work pending, but @wq->maydays may still have
2618 * pwq(s) queued. This can happen by non-rescuer workers consuming
2619 * all the work items before the rescuer got to them. Go through
2620 * @wq->maydays processing before acting on should_stop so that the
2621 * list is always empty on exit.
2623 should_stop = kthread_should_stop();
2625 /* see whether any pwq is asking for help */
2626 raw_spin_lock_irq(&wq_mayday_lock);
2628 while (!list_empty(&wq->maydays)) {
2629 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2630 struct pool_workqueue, mayday_node);
2631 struct worker_pool *pool = pwq->pool;
2632 struct work_struct *work, *n;
2635 __set_current_state(TASK_RUNNING);
2636 list_del_init(&pwq->mayday_node);
2638 raw_spin_unlock_irq(&wq_mayday_lock);
2640 worker_attach_to_pool(rescuer, pool);
2642 raw_spin_lock_irq(&pool->lock);
2645 * Slurp in all works issued via this workqueue and
2648 WARN_ON_ONCE(!list_empty(scheduled));
2649 list_for_each_entry_safe(work, n, &pool->worklist, entry) {
2650 if (get_work_pwq(work) == pwq) {
2652 pool->watchdog_ts = jiffies;
2653 move_linked_works(work, scheduled, &n);
2658 if (!list_empty(scheduled)) {
2659 process_scheduled_works(rescuer);
2662 * The above execution of rescued work items could
2663 * have created more to rescue through
2664 * pwq_activate_first_inactive() or chained
2665 * queueing. Let's put @pwq back on mayday list so
2666 * that such back-to-back work items, which may be
2667 * being used to relieve memory pressure, don't
2668 * incur MAYDAY_INTERVAL delay inbetween.
2670 if (pwq->nr_active && need_to_create_worker(pool)) {
2671 raw_spin_lock(&wq_mayday_lock);
2673 * Queue iff we aren't racing destruction
2674 * and somebody else hasn't queued it already.
2676 if (wq->rescuer && list_empty(&pwq->mayday_node)) {
2678 list_add_tail(&pwq->mayday_node, &wq->maydays);
2680 raw_spin_unlock(&wq_mayday_lock);
2685 * Put the reference grabbed by send_mayday(). @pool won't
2686 * go away while we're still attached to it.
2691 * Leave this pool. If need_more_worker() is %true, notify a
2692 * regular worker; otherwise, we end up with 0 concurrency
2693 * and stalling the execution.
2695 if (need_more_worker(pool))
2696 wake_up_worker(pool);
2698 raw_spin_unlock_irq(&pool->lock);
2700 worker_detach_from_pool(rescuer);
2702 raw_spin_lock_irq(&wq_mayday_lock);
2705 raw_spin_unlock_irq(&wq_mayday_lock);
2708 __set_current_state(TASK_RUNNING);
2709 set_pf_worker(false);
2713 /* rescuers should never participate in concurrency management */
2714 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
2720 * check_flush_dependency - check for flush dependency sanity
2721 * @target_wq: workqueue being flushed
2722 * @target_work: work item being flushed (NULL for workqueue flushes)
2724 * %current is trying to flush the whole @target_wq or @target_work on it.
2725 * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not
2726 * reclaiming memory or running on a workqueue which doesn't have
2727 * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to
2730 static void check_flush_dependency(struct workqueue_struct *target_wq,
2731 struct work_struct *target_work)
2733 work_func_t target_func = target_work ? target_work->func : NULL;
2734 struct worker *worker;
2736 if (target_wq->flags & WQ_MEM_RECLAIM)
2739 worker = current_wq_worker();
2741 WARN_ONCE(current->flags & PF_MEMALLOC,
2742 "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps",
2743 current->pid, current->comm, target_wq->name, target_func);
2744 WARN_ONCE(worker && ((worker->current_pwq->wq->flags &
2745 (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM),
2746 "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps",
2747 worker->current_pwq->wq->name, worker->current_func,
2748 target_wq->name, target_func);
2752 struct work_struct work;
2753 struct completion done;
2754 struct task_struct *task; /* purely informational */
2757 static void wq_barrier_func(struct work_struct *work)
2759 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2760 complete(&barr->done);
2764 * insert_wq_barrier - insert a barrier work
2765 * @pwq: pwq to insert barrier into
2766 * @barr: wq_barrier to insert
2767 * @target: target work to attach @barr to
2768 * @worker: worker currently executing @target, NULL if @target is not executing
2770 * @barr is linked to @target such that @barr is completed only after
2771 * @target finishes execution. Please note that the ordering
2772 * guarantee is observed only with respect to @target and on the local
2775 * Currently, a queued barrier can't be canceled. This is because
2776 * try_to_grab_pending() can't determine whether the work to be
2777 * grabbed is at the head of the queue and thus can't clear LINKED
2778 * flag of the previous work while there must be a valid next work
2779 * after a work with LINKED flag set.
2781 * Note that when @worker is non-NULL, @target may be modified
2782 * underneath us, so we can't reliably determine pwq from @target.
2785 * raw_spin_lock_irq(pool->lock).
2787 static void insert_wq_barrier(struct pool_workqueue *pwq,
2788 struct wq_barrier *barr,
2789 struct work_struct *target, struct worker *worker)
2791 unsigned int work_flags = 0;
2792 unsigned int work_color;
2793 struct list_head *head;
2796 * debugobject calls are safe here even with pool->lock locked
2797 * as we know for sure that this will not trigger any of the
2798 * checks and call back into the fixup functions where we
2801 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
2802 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
2804 init_completion_map(&barr->done, &target->lockdep_map);
2806 barr->task = current;
2808 /* The barrier work item does not participate in pwq->nr_active. */
2809 work_flags |= WORK_STRUCT_INACTIVE;
2812 * If @target is currently being executed, schedule the
2813 * barrier to the worker; otherwise, put it after @target.
2816 head = worker->scheduled.next;
2817 work_color = worker->current_color;
2819 unsigned long *bits = work_data_bits(target);
2821 head = target->entry.next;
2822 /* there can already be other linked works, inherit and set */
2823 work_flags |= *bits & WORK_STRUCT_LINKED;
2824 work_color = get_work_color(*bits);
2825 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2828 pwq->nr_in_flight[work_color]++;
2829 work_flags |= work_color_to_flags(work_color);
2831 debug_work_activate(&barr->work);
2832 insert_work(pwq, &barr->work, head, work_flags);
2836 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
2837 * @wq: workqueue being flushed
2838 * @flush_color: new flush color, < 0 for no-op
2839 * @work_color: new work color, < 0 for no-op
2841 * Prepare pwqs for workqueue flushing.
2843 * If @flush_color is non-negative, flush_color on all pwqs should be
2844 * -1. If no pwq has in-flight commands at the specified color, all
2845 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2846 * has in flight commands, its pwq->flush_color is set to
2847 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
2848 * wakeup logic is armed and %true is returned.
2850 * The caller should have initialized @wq->first_flusher prior to
2851 * calling this function with non-negative @flush_color. If
2852 * @flush_color is negative, no flush color update is done and %false
2855 * If @work_color is non-negative, all pwqs should have the same
2856 * work_color which is previous to @work_color and all will be
2857 * advanced to @work_color.
2860 * mutex_lock(wq->mutex).
2863 * %true if @flush_color >= 0 and there's something to flush. %false
2866 static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
2867 int flush_color, int work_color)
2870 struct pool_workqueue *pwq;
2872 if (flush_color >= 0) {
2873 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
2874 atomic_set(&wq->nr_pwqs_to_flush, 1);
2877 for_each_pwq(pwq, wq) {
2878 struct worker_pool *pool = pwq->pool;
2880 raw_spin_lock_irq(&pool->lock);
2882 if (flush_color >= 0) {
2883 WARN_ON_ONCE(pwq->flush_color != -1);
2885 if (pwq->nr_in_flight[flush_color]) {
2886 pwq->flush_color = flush_color;
2887 atomic_inc(&wq->nr_pwqs_to_flush);
2892 if (work_color >= 0) {
2893 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
2894 pwq->work_color = work_color;
2897 raw_spin_unlock_irq(&pool->lock);
2900 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
2901 complete(&wq->first_flusher->done);
2907 * __flush_workqueue - ensure that any scheduled work has run to completion.
2908 * @wq: workqueue to flush
2910 * This function sleeps until all work items which were queued on entry
2911 * have finished execution, but it is not livelocked by new incoming ones.
2913 void __flush_workqueue(struct workqueue_struct *wq)
2915 struct wq_flusher this_flusher = {
2916 .list = LIST_HEAD_INIT(this_flusher.list),
2918 .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map),
2922 if (WARN_ON(!wq_online))
2925 lock_map_acquire(&wq->lockdep_map);
2926 lock_map_release(&wq->lockdep_map);
2928 mutex_lock(&wq->mutex);
2931 * Start-to-wait phase
2933 next_color = work_next_color(wq->work_color);
2935 if (next_color != wq->flush_color) {
2937 * Color space is not full. The current work_color
2938 * becomes our flush_color and work_color is advanced
2941 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
2942 this_flusher.flush_color = wq->work_color;
2943 wq->work_color = next_color;
2945 if (!wq->first_flusher) {
2946 /* no flush in progress, become the first flusher */
2947 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
2949 wq->first_flusher = &this_flusher;
2951 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
2953 /* nothing to flush, done */
2954 wq->flush_color = next_color;
2955 wq->first_flusher = NULL;
2960 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
2961 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2962 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
2966 * Oops, color space is full, wait on overflow queue.
2967 * The next flush completion will assign us
2968 * flush_color and transfer to flusher_queue.
2970 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2973 check_flush_dependency(wq, NULL);
2975 mutex_unlock(&wq->mutex);
2977 wait_for_completion(&this_flusher.done);
2980 * Wake-up-and-cascade phase
2982 * First flushers are responsible for cascading flushes and
2983 * handling overflow. Non-first flushers can simply return.
2985 if (READ_ONCE(wq->first_flusher) != &this_flusher)
2988 mutex_lock(&wq->mutex);
2990 /* we might have raced, check again with mutex held */
2991 if (wq->first_flusher != &this_flusher)
2994 WRITE_ONCE(wq->first_flusher, NULL);
2996 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2997 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
3000 struct wq_flusher *next, *tmp;
3002 /* complete all the flushers sharing the current flush color */
3003 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
3004 if (next->flush_color != wq->flush_color)
3006 list_del_init(&next->list);
3007 complete(&next->done);
3010 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
3011 wq->flush_color != work_next_color(wq->work_color));
3013 /* this flush_color is finished, advance by one */
3014 wq->flush_color = work_next_color(wq->flush_color);
3016 /* one color has been freed, handle overflow queue */
3017 if (!list_empty(&wq->flusher_overflow)) {
3019 * Assign the same color to all overflowed
3020 * flushers, advance work_color and append to
3021 * flusher_queue. This is the start-to-wait
3022 * phase for these overflowed flushers.
3024 list_for_each_entry(tmp, &wq->flusher_overflow, list)
3025 tmp->flush_color = wq->work_color;
3027 wq->work_color = work_next_color(wq->work_color);
3029 list_splice_tail_init(&wq->flusher_overflow,
3030 &wq->flusher_queue);
3031 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
3034 if (list_empty(&wq->flusher_queue)) {
3035 WARN_ON_ONCE(wq->flush_color != wq->work_color);
3040 * Need to flush more colors. Make the next flusher
3041 * the new first flusher and arm pwqs.
3043 WARN_ON_ONCE(wq->flush_color == wq->work_color);
3044 WARN_ON_ONCE(wq->flush_color != next->flush_color);
3046 list_del_init(&next->list);
3047 wq->first_flusher = next;
3049 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
3053 * Meh... this color is already done, clear first
3054 * flusher and repeat cascading.
3056 wq->first_flusher = NULL;
3060 mutex_unlock(&wq->mutex);
3062 EXPORT_SYMBOL(__flush_workqueue);
3065 * drain_workqueue - drain a workqueue
3066 * @wq: workqueue to drain
3068 * Wait until the workqueue becomes empty. While draining is in progress,
3069 * only chain queueing is allowed. IOW, only currently pending or running
3070 * work items on @wq can queue further work items on it. @wq is flushed
3071 * repeatedly until it becomes empty. The number of flushing is determined
3072 * by the depth of chaining and should be relatively short. Whine if it
3075 void drain_workqueue(struct workqueue_struct *wq)
3077 unsigned int flush_cnt = 0;
3078 struct pool_workqueue *pwq;
3081 * __queue_work() needs to test whether there are drainers, is much
3082 * hotter than drain_workqueue() and already looks at @wq->flags.
3083 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
3085 mutex_lock(&wq->mutex);
3086 if (!wq->nr_drainers++)
3087 wq->flags |= __WQ_DRAINING;
3088 mutex_unlock(&wq->mutex);
3090 __flush_workqueue(wq);
3092 mutex_lock(&wq->mutex);
3094 for_each_pwq(pwq, wq) {
3097 raw_spin_lock_irq(&pwq->pool->lock);
3098 drained = !pwq->nr_active && list_empty(&pwq->inactive_works);
3099 raw_spin_unlock_irq(&pwq->pool->lock);
3104 if (++flush_cnt == 10 ||
3105 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
3106 pr_warn("workqueue %s: %s() isn't complete after %u tries\n",
3107 wq->name, __func__, flush_cnt);
3109 mutex_unlock(&wq->mutex);
3113 if (!--wq->nr_drainers)
3114 wq->flags &= ~__WQ_DRAINING;
3115 mutex_unlock(&wq->mutex);
3117 EXPORT_SYMBOL_GPL(drain_workqueue);
3119 static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
3122 struct worker *worker = NULL;
3123 struct worker_pool *pool;
3124 struct pool_workqueue *pwq;
3129 pool = get_work_pool(work);
3135 raw_spin_lock_irq(&pool->lock);
3136 /* see the comment in try_to_grab_pending() with the same code */
3137 pwq = get_work_pwq(work);
3139 if (unlikely(pwq->pool != pool))
3142 worker = find_worker_executing_work(pool, work);
3145 pwq = worker->current_pwq;
3148 check_flush_dependency(pwq->wq, work);
3150 insert_wq_barrier(pwq, barr, work, worker);
3151 raw_spin_unlock_irq(&pool->lock);
3154 * Force a lock recursion deadlock when using flush_work() inside a
3155 * single-threaded or rescuer equipped workqueue.
3157 * For single threaded workqueues the deadlock happens when the work
3158 * is after the work issuing the flush_work(). For rescuer equipped
3159 * workqueues the deadlock happens when the rescuer stalls, blocking
3163 (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)) {
3164 lock_map_acquire(&pwq->wq->lockdep_map);
3165 lock_map_release(&pwq->wq->lockdep_map);
3170 raw_spin_unlock_irq(&pool->lock);
3175 static bool __flush_work(struct work_struct *work, bool from_cancel)
3177 struct wq_barrier barr;
3179 if (WARN_ON(!wq_online))
3182 if (WARN_ON(!work->func))
3185 lock_map_acquire(&work->lockdep_map);
3186 lock_map_release(&work->lockdep_map);
3188 if (start_flush_work(work, &barr, from_cancel)) {
3189 wait_for_completion(&barr.done);
3190 destroy_work_on_stack(&barr.work);
3198 * flush_work - wait for a work to finish executing the last queueing instance
3199 * @work: the work to flush
3201 * Wait until @work has finished execution. @work is guaranteed to be idle
3202 * on return if it hasn't been requeued since flush started.
3205 * %true if flush_work() waited for the work to finish execution,
3206 * %false if it was already idle.
3208 bool flush_work(struct work_struct *work)
3210 return __flush_work(work, false);
3212 EXPORT_SYMBOL_GPL(flush_work);
3215 wait_queue_entry_t wait;
3216 struct work_struct *work;
3219 static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
3221 struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait);
3223 if (cwait->work != key)
3225 return autoremove_wake_function(wait, mode, sync, key);
3228 static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
3230 static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq);
3231 unsigned long flags;
3235 ret = try_to_grab_pending(work, is_dwork, &flags);
3237 * If someone else is already canceling, wait for it to
3238 * finish. flush_work() doesn't work for PREEMPT_NONE
3239 * because we may get scheduled between @work's completion
3240 * and the other canceling task resuming and clearing
3241 * CANCELING - flush_work() will return false immediately
3242 * as @work is no longer busy, try_to_grab_pending() will
3243 * return -ENOENT as @work is still being canceled and the
3244 * other canceling task won't be able to clear CANCELING as
3245 * we're hogging the CPU.
3247 * Let's wait for completion using a waitqueue. As this
3248 * may lead to the thundering herd problem, use a custom
3249 * wake function which matches @work along with exclusive
3252 if (unlikely(ret == -ENOENT)) {
3253 struct cwt_wait cwait;
3255 init_wait(&cwait.wait);
3256 cwait.wait.func = cwt_wakefn;
3259 prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait,
3260 TASK_UNINTERRUPTIBLE);
3261 if (work_is_canceling(work))
3263 finish_wait(&cancel_waitq, &cwait.wait);
3265 } while (unlikely(ret < 0));
3267 /* tell other tasks trying to grab @work to back off */
3268 mark_work_canceling(work);
3269 local_irq_restore(flags);
3272 * This allows canceling during early boot. We know that @work
3276 __flush_work(work, true);
3278 clear_work_data(work);
3281 * Paired with prepare_to_wait() above so that either
3282 * waitqueue_active() is visible here or !work_is_canceling() is
3286 if (waitqueue_active(&cancel_waitq))
3287 __wake_up(&cancel_waitq, TASK_NORMAL, 1, work);
3293 * cancel_work_sync - cancel a work and wait for it to finish
3294 * @work: the work to cancel
3296 * Cancel @work and wait for its execution to finish. This function
3297 * can be used even if the work re-queues itself or migrates to
3298 * another workqueue. On return from this function, @work is
3299 * guaranteed to be not pending or executing on any CPU.
3301 * cancel_work_sync(&delayed_work->work) must not be used for
3302 * delayed_work's. Use cancel_delayed_work_sync() instead.
3304 * The caller must ensure that the workqueue on which @work was last
3305 * queued can't be destroyed before this function returns.
3308 * %true if @work was pending, %false otherwise.
3310 bool cancel_work_sync(struct work_struct *work)
3312 return __cancel_work_timer(work, false);
3314 EXPORT_SYMBOL_GPL(cancel_work_sync);
3317 * flush_delayed_work - wait for a dwork to finish executing the last queueing
3318 * @dwork: the delayed work to flush
3320 * Delayed timer is cancelled and the pending work is queued for
3321 * immediate execution. Like flush_work(), this function only
3322 * considers the last queueing instance of @dwork.
3325 * %true if flush_work() waited for the work to finish execution,
3326 * %false if it was already idle.
3328 bool flush_delayed_work(struct delayed_work *dwork)
3330 local_irq_disable();
3331 if (del_timer_sync(&dwork->timer))
3332 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
3334 return flush_work(&dwork->work);
3336 EXPORT_SYMBOL(flush_delayed_work);
3339 * flush_rcu_work - wait for a rwork to finish executing the last queueing
3340 * @rwork: the rcu work to flush
3343 * %true if flush_rcu_work() waited for the work to finish execution,
3344 * %false if it was already idle.
3346 bool flush_rcu_work(struct rcu_work *rwork)
3348 if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) {
3350 flush_work(&rwork->work);
3353 return flush_work(&rwork->work);
3356 EXPORT_SYMBOL(flush_rcu_work);
3358 static bool __cancel_work(struct work_struct *work, bool is_dwork)
3360 unsigned long flags;
3364 ret = try_to_grab_pending(work, is_dwork, &flags);
3365 } while (unlikely(ret == -EAGAIN));
3367 if (unlikely(ret < 0))
3370 set_work_pool_and_clear_pending(work, get_work_pool_id(work));
3371 local_irq_restore(flags);
3376 * See cancel_delayed_work()
3378 bool cancel_work(struct work_struct *work)
3380 return __cancel_work(work, false);
3382 EXPORT_SYMBOL(cancel_work);
3385 * cancel_delayed_work - cancel a delayed work
3386 * @dwork: delayed_work to cancel
3388 * Kill off a pending delayed_work.
3390 * Return: %true if @dwork was pending and canceled; %false if it wasn't
3394 * The work callback function may still be running on return, unless
3395 * it returns %true and the work doesn't re-arm itself. Explicitly flush or
3396 * use cancel_delayed_work_sync() to wait on it.
3398 * This function is safe to call from any context including IRQ handler.
3400 bool cancel_delayed_work(struct delayed_work *dwork)
3402 return __cancel_work(&dwork->work, true);
3404 EXPORT_SYMBOL(cancel_delayed_work);
3407 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
3408 * @dwork: the delayed work cancel
3410 * This is cancel_work_sync() for delayed works.
3413 * %true if @dwork was pending, %false otherwise.
3415 bool cancel_delayed_work_sync(struct delayed_work *dwork)
3417 return __cancel_work_timer(&dwork->work, true);
3419 EXPORT_SYMBOL(cancel_delayed_work_sync);
3422 * schedule_on_each_cpu - execute a function synchronously on each online CPU
3423 * @func: the function to call
3425 * schedule_on_each_cpu() executes @func on each online CPU using the
3426 * system workqueue and blocks until all CPUs have completed.
3427 * schedule_on_each_cpu() is very slow.
3430 * 0 on success, -errno on failure.
3432 int schedule_on_each_cpu(work_func_t func)
3435 struct work_struct __percpu *works;
3437 works = alloc_percpu(struct work_struct);
3443 for_each_online_cpu(cpu) {
3444 struct work_struct *work = per_cpu_ptr(works, cpu);
3446 INIT_WORK(work, func);
3447 schedule_work_on(cpu, work);
3450 for_each_online_cpu(cpu)
3451 flush_work(per_cpu_ptr(works, cpu));
3459 * execute_in_process_context - reliably execute the routine with user context
3460 * @fn: the function to execute
3461 * @ew: guaranteed storage for the execute work structure (must
3462 * be available when the work executes)
3464 * Executes the function immediately if process context is available,
3465 * otherwise schedules the function for delayed execution.
3467 * Return: 0 - function was executed
3468 * 1 - function was scheduled for execution
3470 int execute_in_process_context(work_func_t fn, struct execute_work *ew)
3472 if (!in_interrupt()) {
3477 INIT_WORK(&ew->work, fn);
3478 schedule_work(&ew->work);
3482 EXPORT_SYMBOL_GPL(execute_in_process_context);
3485 * free_workqueue_attrs - free a workqueue_attrs
3486 * @attrs: workqueue_attrs to free
3488 * Undo alloc_workqueue_attrs().
3490 void free_workqueue_attrs(struct workqueue_attrs *attrs)
3493 free_cpumask_var(attrs->cpumask);
3499 * alloc_workqueue_attrs - allocate a workqueue_attrs
3501 * Allocate a new workqueue_attrs, initialize with default settings and
3504 * Return: The allocated new workqueue_attr on success. %NULL on failure.
3506 struct workqueue_attrs *alloc_workqueue_attrs(void)
3508 struct workqueue_attrs *attrs;
3510 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
3513 if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL))
3516 cpumask_copy(attrs->cpumask, cpu_possible_mask);
3519 free_workqueue_attrs(attrs);
3523 static void copy_workqueue_attrs(struct workqueue_attrs *to,
3524 const struct workqueue_attrs *from)
3526 to->nice = from->nice;
3527 cpumask_copy(to->cpumask, from->cpumask);
3529 * Unlike hash and equality test, this function doesn't ignore
3530 * ->no_numa as it is used for both pool and wq attrs. Instead,
3531 * get_unbound_pool() explicitly clears ->no_numa after copying.
3533 to->no_numa = from->no_numa;
3536 /* hash value of the content of @attr */
3537 static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3541 hash = jhash_1word(attrs->nice, hash);
3542 hash = jhash(cpumask_bits(attrs->cpumask),
3543 BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash);
3547 /* content equality test */
3548 static bool wqattrs_equal(const struct workqueue_attrs *a,
3549 const struct workqueue_attrs *b)
3551 if (a->nice != b->nice)
3553 if (!cpumask_equal(a->cpumask, b->cpumask))
3559 * init_worker_pool - initialize a newly zalloc'd worker_pool
3560 * @pool: worker_pool to initialize
3562 * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs.
3564 * Return: 0 on success, -errno on failure. Even on failure, all fields
3565 * inside @pool proper are initialized and put_unbound_pool() can be called
3566 * on @pool safely to release it.
3568 static int init_worker_pool(struct worker_pool *pool)
3570 raw_spin_lock_init(&pool->lock);
3573 pool->node = NUMA_NO_NODE;
3574 pool->flags |= POOL_DISASSOCIATED;
3575 pool->watchdog_ts = jiffies;
3576 INIT_LIST_HEAD(&pool->worklist);
3577 INIT_LIST_HEAD(&pool->idle_list);
3578 hash_init(pool->busy_hash);
3580 timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE);
3581 INIT_WORK(&pool->idle_cull_work, idle_cull_fn);
3583 timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0);
3585 INIT_LIST_HEAD(&pool->workers);
3586 INIT_LIST_HEAD(&pool->dying_workers);
3588 ida_init(&pool->worker_ida);
3589 INIT_HLIST_NODE(&pool->hash_node);
3592 /* shouldn't fail above this point */
3593 pool->attrs = alloc_workqueue_attrs();
3599 #ifdef CONFIG_LOCKDEP
3600 static void wq_init_lockdep(struct workqueue_struct *wq)
3604 lockdep_register_key(&wq->key);
3605 lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name);
3607 lock_name = wq->name;
3609 wq->lock_name = lock_name;
3610 lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0);
3613 static void wq_unregister_lockdep(struct workqueue_struct *wq)
3615 lockdep_unregister_key(&wq->key);
3618 static void wq_free_lockdep(struct workqueue_struct *wq)
3620 if (wq->lock_name != wq->name)
3621 kfree(wq->lock_name);
3624 static void wq_init_lockdep(struct workqueue_struct *wq)
3628 static void wq_unregister_lockdep(struct workqueue_struct *wq)
3632 static void wq_free_lockdep(struct workqueue_struct *wq)
3637 static void rcu_free_wq(struct rcu_head *rcu)
3639 struct workqueue_struct *wq =
3640 container_of(rcu, struct workqueue_struct, rcu);
3642 wq_free_lockdep(wq);
3644 if (!(wq->flags & WQ_UNBOUND))
3645 free_percpu(wq->cpu_pwqs);
3647 free_workqueue_attrs(wq->unbound_attrs);
3652 static void rcu_free_pool(struct rcu_head *rcu)
3654 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3656 ida_destroy(&pool->worker_ida);
3657 free_workqueue_attrs(pool->attrs);
3662 * put_unbound_pool - put a worker_pool
3663 * @pool: worker_pool to put
3665 * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU
3666 * safe manner. get_unbound_pool() calls this function on its failure path
3667 * and this function should be able to release pools which went through,
3668 * successfully or not, init_worker_pool().
3670 * Should be called with wq_pool_mutex held.
3672 static void put_unbound_pool(struct worker_pool *pool)
3674 DECLARE_COMPLETION_ONSTACK(detach_completion);
3675 struct list_head cull_list;
3676 struct worker *worker;
3678 INIT_LIST_HEAD(&cull_list);
3680 lockdep_assert_held(&wq_pool_mutex);
3686 if (WARN_ON(!(pool->cpu < 0)) ||
3687 WARN_ON(!list_empty(&pool->worklist)))
3690 /* release id and unhash */
3692 idr_remove(&worker_pool_idr, pool->id);
3693 hash_del(&pool->hash_node);
3696 * Become the manager and destroy all workers. This prevents
3697 * @pool's workers from blocking on attach_mutex. We're the last
3698 * manager and @pool gets freed with the flag set.
3700 * Having a concurrent manager is quite unlikely to happen as we can
3701 * only get here with
3702 * pwq->refcnt == pool->refcnt == 0
3703 * which implies no work queued to the pool, which implies no worker can
3704 * become the manager. However a worker could have taken the role of
3705 * manager before the refcnts dropped to 0, since maybe_create_worker()
3709 rcuwait_wait_event(&manager_wait,
3710 !(pool->flags & POOL_MANAGER_ACTIVE),
3711 TASK_UNINTERRUPTIBLE);
3713 mutex_lock(&wq_pool_attach_mutex);
3714 raw_spin_lock_irq(&pool->lock);
3715 if (!(pool->flags & POOL_MANAGER_ACTIVE)) {
3716 pool->flags |= POOL_MANAGER_ACTIVE;
3719 raw_spin_unlock_irq(&pool->lock);
3720 mutex_unlock(&wq_pool_attach_mutex);
3723 while ((worker = first_idle_worker(pool)))
3724 set_worker_dying(worker, &cull_list);
3725 WARN_ON(pool->nr_workers || pool->nr_idle);
3726 raw_spin_unlock_irq(&pool->lock);
3728 wake_dying_workers(&cull_list);
3730 if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers))
3731 pool->detach_completion = &detach_completion;
3732 mutex_unlock(&wq_pool_attach_mutex);
3734 if (pool->detach_completion)
3735 wait_for_completion(pool->detach_completion);
3737 /* shut down the timers */
3738 del_timer_sync(&pool->idle_timer);
3739 cancel_work_sync(&pool->idle_cull_work);
3740 del_timer_sync(&pool->mayday_timer);
3742 /* RCU protected to allow dereferences from get_work_pool() */
3743 call_rcu(&pool->rcu, rcu_free_pool);
3747 * get_unbound_pool - get a worker_pool with the specified attributes
3748 * @attrs: the attributes of the worker_pool to get
3750 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3751 * reference count and return it. If there already is a matching
3752 * worker_pool, it will be used; otherwise, this function attempts to
3755 * Should be called with wq_pool_mutex held.
3757 * Return: On success, a worker_pool with the same attributes as @attrs.
3758 * On failure, %NULL.
3760 static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3762 u32 hash = wqattrs_hash(attrs);
3763 struct worker_pool *pool;
3765 int target_node = NUMA_NO_NODE;
3767 lockdep_assert_held(&wq_pool_mutex);
3769 /* do we already have a matching pool? */
3770 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3771 if (wqattrs_equal(pool->attrs, attrs)) {
3777 /* if cpumask is contained inside a NUMA node, we belong to that node */
3778 if (wq_numa_enabled) {
3779 for_each_node(node) {
3780 if (cpumask_subset(attrs->cpumask,
3781 wq_numa_possible_cpumask[node])) {
3788 /* nope, create a new one */
3789 pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, target_node);
3790 if (!pool || init_worker_pool(pool) < 0)
3793 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
3794 copy_workqueue_attrs(pool->attrs, attrs);
3795 pool->node = target_node;
3798 * no_numa isn't a worker_pool attribute, always clear it. See
3799 * 'struct workqueue_attrs' comments for detail.
3801 pool->attrs->no_numa = false;
3803 if (worker_pool_assign_id(pool) < 0)
3806 /* create and start the initial worker */
3807 if (wq_online && !create_worker(pool))
3811 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3816 put_unbound_pool(pool);
3820 static void rcu_free_pwq(struct rcu_head *rcu)
3822 kmem_cache_free(pwq_cache,
3823 container_of(rcu, struct pool_workqueue, rcu));
3827 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3828 * and needs to be destroyed.
3830 static void pwq_unbound_release_workfn(struct work_struct *work)
3832 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3833 unbound_release_work);
3834 struct workqueue_struct *wq = pwq->wq;
3835 struct worker_pool *pool = pwq->pool;
3836 bool is_last = false;
3839 * when @pwq is not linked, it doesn't hold any reference to the
3840 * @wq, and @wq is invalid to access.
3842 if (!list_empty(&pwq->pwqs_node)) {
3843 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3846 mutex_lock(&wq->mutex);
3847 list_del_rcu(&pwq->pwqs_node);
3848 is_last = list_empty(&wq->pwqs);
3849 mutex_unlock(&wq->mutex);
3852 mutex_lock(&wq_pool_mutex);
3853 put_unbound_pool(pool);
3854 mutex_unlock(&wq_pool_mutex);
3856 call_rcu(&pwq->rcu, rcu_free_pwq);
3859 * If we're the last pwq going away, @wq is already dead and no one
3860 * is gonna access it anymore. Schedule RCU free.
3863 wq_unregister_lockdep(wq);
3864 call_rcu(&wq->rcu, rcu_free_wq);
3869 * pwq_adjust_max_active - update a pwq's max_active to the current setting
3870 * @pwq: target pool_workqueue
3872 * If @pwq isn't freezing, set @pwq->max_active to the associated
3873 * workqueue's saved_max_active and activate inactive work items
3874 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
3876 static void pwq_adjust_max_active(struct pool_workqueue *pwq)
3878 struct workqueue_struct *wq = pwq->wq;
3879 bool freezable = wq->flags & WQ_FREEZABLE;
3880 unsigned long flags;
3882 /* for @wq->saved_max_active */
3883 lockdep_assert_held(&wq->mutex);
3885 /* fast exit for non-freezable wqs */
3886 if (!freezable && pwq->max_active == wq->saved_max_active)
3889 /* this function can be called during early boot w/ irq disabled */
3890 raw_spin_lock_irqsave(&pwq->pool->lock, flags);
3893 * During [un]freezing, the caller is responsible for ensuring that
3894 * this function is called at least once after @workqueue_freezing
3895 * is updated and visible.
3897 if (!freezable || !workqueue_freezing) {
3900 pwq->max_active = wq->saved_max_active;
3902 while (!list_empty(&pwq->inactive_works) &&
3903 pwq->nr_active < pwq->max_active) {
3904 pwq_activate_first_inactive(pwq);
3909 * Need to kick a worker after thawed or an unbound wq's
3910 * max_active is bumped. In realtime scenarios, always kicking a
3911 * worker will cause interference on the isolated cpu cores, so
3912 * let's kick iff work items were activated.
3915 wake_up_worker(pwq->pool);
3917 pwq->max_active = 0;
3920 raw_spin_unlock_irqrestore(&pwq->pool->lock, flags);
3923 /* initialize newly allocated @pwq which is associated with @wq and @pool */
3924 static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq,
3925 struct worker_pool *pool)
3927 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3929 memset(pwq, 0, sizeof(*pwq));
3933 pwq->flush_color = -1;
3935 INIT_LIST_HEAD(&pwq->inactive_works);
3936 INIT_LIST_HEAD(&pwq->pwqs_node);
3937 INIT_LIST_HEAD(&pwq->mayday_node);
3938 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
3941 /* sync @pwq with the current state of its associated wq and link it */
3942 static void link_pwq(struct pool_workqueue *pwq)
3944 struct workqueue_struct *wq = pwq->wq;
3946 lockdep_assert_held(&wq->mutex);
3948 /* may be called multiple times, ignore if already linked */
3949 if (!list_empty(&pwq->pwqs_node))
3952 /* set the matching work_color */
3953 pwq->work_color = wq->work_color;
3955 /* sync max_active to the current setting */
3956 pwq_adjust_max_active(pwq);
3959 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
3962 /* obtain a pool matching @attr and create a pwq associating the pool and @wq */
3963 static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,
3964 const struct workqueue_attrs *attrs)
3966 struct worker_pool *pool;
3967 struct pool_workqueue *pwq;
3969 lockdep_assert_held(&wq_pool_mutex);
3971 pool = get_unbound_pool(attrs);
3975 pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node);
3977 put_unbound_pool(pool);
3981 init_pwq(pwq, wq, pool);
3986 * wq_calc_node_cpumask - calculate a wq_attrs' cpumask for the specified node
3987 * @attrs: the wq_attrs of the default pwq of the target workqueue
3988 * @node: the target NUMA node
3989 * @cpu_going_down: if >= 0, the CPU to consider as offline
3990 * @cpumask: outarg, the resulting cpumask
3992 * Calculate the cpumask a workqueue with @attrs should use on @node. If
3993 * @cpu_going_down is >= 0, that cpu is considered offline during
3994 * calculation. The result is stored in @cpumask.
3996 * If NUMA affinity is not enabled, @attrs->cpumask is always used. If
3997 * enabled and @node has online CPUs requested by @attrs, the returned
3998 * cpumask is the intersection of the possible CPUs of @node and
4001 * The caller is responsible for ensuring that the cpumask of @node stays
4004 * Return: %true if the resulting @cpumask is different from @attrs->cpumask,
4007 static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
4008 int cpu_going_down, cpumask_t *cpumask)
4010 if (!wq_numa_enabled || attrs->no_numa)
4013 /* does @node have any online CPUs @attrs wants? */
4014 cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask);
4015 if (cpu_going_down >= 0)
4016 cpumask_clear_cpu(cpu_going_down, cpumask);
4018 if (cpumask_empty(cpumask))
4021 /* yeap, return possible CPUs in @node that @attrs wants */
4022 cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]);
4024 if (cpumask_empty(cpumask)) {
4025 pr_warn_once("WARNING: workqueue cpumask: online intersect > "
4026 "possible intersect\n");
4030 return !cpumask_equal(cpumask, attrs->cpumask);
4033 cpumask_copy(cpumask, attrs->cpumask);
4037 /* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */
4038 static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,
4040 struct pool_workqueue *pwq)
4042 struct pool_workqueue *old_pwq;
4044 lockdep_assert_held(&wq_pool_mutex);
4045 lockdep_assert_held(&wq->mutex);
4047 /* link_pwq() can handle duplicate calls */
4050 old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
4051 rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq);
4055 /* context to store the prepared attrs & pwqs before applying */
4056 struct apply_wqattrs_ctx {
4057 struct workqueue_struct *wq; /* target workqueue */
4058 struct workqueue_attrs *attrs; /* attrs to apply */
4059 struct list_head list; /* queued for batching commit */
4060 struct pool_workqueue *dfl_pwq;
4061 struct pool_workqueue *pwq_tbl[];
4064 /* free the resources after success or abort */
4065 static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx)
4071 put_pwq_unlocked(ctx->pwq_tbl[node]);
4072 put_pwq_unlocked(ctx->dfl_pwq);
4074 free_workqueue_attrs(ctx->attrs);
4080 /* allocate the attrs and pwqs for later installation */
4081 static struct apply_wqattrs_ctx *
4082 apply_wqattrs_prepare(struct workqueue_struct *wq,
4083 const struct workqueue_attrs *attrs,
4084 const cpumask_var_t unbound_cpumask)
4086 struct apply_wqattrs_ctx *ctx;
4087 struct workqueue_attrs *new_attrs, *tmp_attrs;
4090 lockdep_assert_held(&wq_pool_mutex);
4092 ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_node_ids), GFP_KERNEL);
4094 new_attrs = alloc_workqueue_attrs();
4095 tmp_attrs = alloc_workqueue_attrs();
4096 if (!ctx || !new_attrs || !tmp_attrs)
4100 * Calculate the attrs of the default pwq with unbound_cpumask
4101 * which is wq_unbound_cpumask or to set to wq_unbound_cpumask.
4102 * If the user configured cpumask doesn't overlap with the
4103 * wq_unbound_cpumask, we fallback to the wq_unbound_cpumask.
4105 copy_workqueue_attrs(new_attrs, attrs);
4106 cpumask_and(new_attrs->cpumask, new_attrs->cpumask, unbound_cpumask);
4107 if (unlikely(cpumask_empty(new_attrs->cpumask)))
4108 cpumask_copy(new_attrs->cpumask, unbound_cpumask);
4111 * We may create multiple pwqs with differing cpumasks. Make a
4112 * copy of @new_attrs which will be modified and used to obtain
4115 copy_workqueue_attrs(tmp_attrs, new_attrs);
4118 * If something goes wrong during CPU up/down, we'll fall back to
4119 * the default pwq covering whole @attrs->cpumask. Always create
4120 * it even if we don't use it immediately.
4122 ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs);
4126 for_each_node(node) {
4127 if (wq_calc_node_cpumask(new_attrs, node, -1, tmp_attrs->cpumask)) {
4128 ctx->pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs);
4129 if (!ctx->pwq_tbl[node])
4132 ctx->dfl_pwq->refcnt++;
4133 ctx->pwq_tbl[node] = ctx->dfl_pwq;
4137 /* save the user configured attrs and sanitize it. */
4138 copy_workqueue_attrs(new_attrs, attrs);
4139 cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);
4140 ctx->attrs = new_attrs;
4143 free_workqueue_attrs(tmp_attrs);
4147 free_workqueue_attrs(tmp_attrs);
4148 free_workqueue_attrs(new_attrs);
4149 apply_wqattrs_cleanup(ctx);
4153 /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */
4154 static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx)
4158 /* all pwqs have been created successfully, let's install'em */
4159 mutex_lock(&ctx->wq->mutex);
4161 copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs);
4163 /* save the previous pwq and install the new one */
4165 ctx->pwq_tbl[node] = numa_pwq_tbl_install(ctx->wq, node,
4166 ctx->pwq_tbl[node]);
4168 /* @dfl_pwq might not have been used, ensure it's linked */
4169 link_pwq(ctx->dfl_pwq);
4170 swap(ctx->wq->dfl_pwq, ctx->dfl_pwq);
4172 mutex_unlock(&ctx->wq->mutex);
4175 static void apply_wqattrs_lock(void)
4177 /* CPUs should stay stable across pwq creations and installations */
4179 mutex_lock(&wq_pool_mutex);
4182 static void apply_wqattrs_unlock(void)
4184 mutex_unlock(&wq_pool_mutex);
4188 static int apply_workqueue_attrs_locked(struct workqueue_struct *wq,
4189 const struct workqueue_attrs *attrs)
4191 struct apply_wqattrs_ctx *ctx;
4193 /* only unbound workqueues can change attributes */
4194 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
4197 /* creating multiple pwqs breaks ordering guarantee */
4198 if (!list_empty(&wq->pwqs)) {
4199 if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT))
4202 wq->flags &= ~__WQ_ORDERED;
4205 ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask);
4209 /* the ctx has been prepared successfully, let's commit it */
4210 apply_wqattrs_commit(ctx);
4211 apply_wqattrs_cleanup(ctx);
4217 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
4218 * @wq: the target workqueue
4219 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
4221 * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA
4222 * machines, this function maps a separate pwq to each NUMA node with
4223 * possibles CPUs in @attrs->cpumask so that work items are affine to the
4224 * NUMA node it was issued on. Older pwqs are released as in-flight work
4225 * items finish. Note that a work item which repeatedly requeues itself
4226 * back-to-back will stay on its current pwq.
4228 * Performs GFP_KERNEL allocations.
4230 * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock().
4232 * Return: 0 on success and -errno on failure.
4234 int apply_workqueue_attrs(struct workqueue_struct *wq,
4235 const struct workqueue_attrs *attrs)
4239 lockdep_assert_cpus_held();
4241 mutex_lock(&wq_pool_mutex);
4242 ret = apply_workqueue_attrs_locked(wq, attrs);
4243 mutex_unlock(&wq_pool_mutex);
4249 * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug
4250 * @wq: the target workqueue
4251 * @cpu: the CPU coming up or going down
4252 * @online: whether @cpu is coming up or going down
4254 * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and
4255 * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of
4258 * If NUMA affinity can't be adjusted due to memory allocation failure, it
4259 * falls back to @wq->dfl_pwq which may not be optimal but is always
4262 * Note that when the last allowed CPU of a NUMA node goes offline for a
4263 * workqueue with a cpumask spanning multiple nodes, the workers which were
4264 * already executing the work items for the workqueue will lose their CPU
4265 * affinity and may execute on any CPU. This is similar to how per-cpu
4266 * workqueues behave on CPU_DOWN. If a workqueue user wants strict
4267 * affinity, it's the user's responsibility to flush the work item from
4270 static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu,
4273 int node = cpu_to_node(cpu);
4274 int cpu_off = online ? -1 : cpu;
4275 struct pool_workqueue *old_pwq = NULL, *pwq;
4276 struct workqueue_attrs *target_attrs;
4279 lockdep_assert_held(&wq_pool_mutex);
4281 if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND) ||
4282 wq->unbound_attrs->no_numa)
4286 * We don't wanna alloc/free wq_attrs for each wq for each CPU.
4287 * Let's use a preallocated one. The following buf is protected by
4288 * CPU hotplug exclusion.
4290 target_attrs = wq_update_unbound_numa_attrs_buf;
4291 cpumask = target_attrs->cpumask;
4293 copy_workqueue_attrs(target_attrs, wq->unbound_attrs);
4294 pwq = unbound_pwq_by_node(wq, node);
4297 * Let's determine what needs to be done. If the target cpumask is
4298 * different from the default pwq's, we need to compare it to @pwq's
4299 * and create a new one if they don't match. If the target cpumask
4300 * equals the default pwq's, the default pwq should be used.
4302 if (wq_calc_node_cpumask(wq->dfl_pwq->pool->attrs, node, cpu_off, cpumask)) {
4303 if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask))
4309 /* create a new pwq */
4310 pwq = alloc_unbound_pwq(wq, target_attrs);
4312 pr_warn("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n",
4317 /* Install the new pwq. */
4318 mutex_lock(&wq->mutex);
4319 old_pwq = numa_pwq_tbl_install(wq, node, pwq);
4323 mutex_lock(&wq->mutex);
4324 raw_spin_lock_irq(&wq->dfl_pwq->pool->lock);
4325 get_pwq(wq->dfl_pwq);
4326 raw_spin_unlock_irq(&wq->dfl_pwq->pool->lock);
4327 old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq);
4329 mutex_unlock(&wq->mutex);
4330 put_pwq_unlocked(old_pwq);
4333 static int alloc_and_link_pwqs(struct workqueue_struct *wq)
4335 bool highpri = wq->flags & WQ_HIGHPRI;
4338 if (!(wq->flags & WQ_UNBOUND)) {
4339 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
4343 for_each_possible_cpu(cpu) {
4344 struct pool_workqueue *pwq =
4345 per_cpu_ptr(wq->cpu_pwqs, cpu);
4346 struct worker_pool *cpu_pools =
4347 per_cpu(cpu_worker_pools, cpu);
4349 init_pwq(pwq, wq, &cpu_pools[highpri]);
4351 mutex_lock(&wq->mutex);
4353 mutex_unlock(&wq->mutex);
4359 if (wq->flags & __WQ_ORDERED) {
4360 ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]);
4361 /* there should only be single pwq for ordering guarantee */
4362 WARN(!ret && (wq->pwqs.next != &wq->dfl_pwq->pwqs_node ||
4363 wq->pwqs.prev != &wq->dfl_pwq->pwqs_node),
4364 "ordering guarantee broken for workqueue %s\n", wq->name);
4366 ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
4373 static int wq_clamp_max_active(int max_active, unsigned int flags,
4376 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
4378 if (max_active < 1 || max_active > lim)
4379 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
4380 max_active, name, 1, lim);
4382 return clamp_val(max_active, 1, lim);
4386 * Workqueues which may be used during memory reclaim should have a rescuer
4387 * to guarantee forward progress.
4389 static int init_rescuer(struct workqueue_struct *wq)
4391 struct worker *rescuer;
4394 if (!(wq->flags & WQ_MEM_RECLAIM))
4397 rescuer = alloc_worker(NUMA_NO_NODE);
4399 pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n",
4404 rescuer->rescue_wq = wq;
4405 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", wq->name);
4406 if (IS_ERR(rescuer->task)) {
4407 ret = PTR_ERR(rescuer->task);
4408 pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe",
4409 wq->name, ERR_PTR(ret));
4414 wq->rescuer = rescuer;
4415 kthread_bind_mask(rescuer->task, cpu_possible_mask);
4416 wake_up_process(rescuer->task);
4422 struct workqueue_struct *alloc_workqueue(const char *fmt,
4424 int max_active, ...)
4426 size_t tbl_size = 0;
4428 struct workqueue_struct *wq;
4429 struct pool_workqueue *pwq;
4432 * Unbound && max_active == 1 used to imply ordered, which is no
4433 * longer the case on NUMA machines due to per-node pools. While
4434 * alloc_ordered_workqueue() is the right way to create an ordered
4435 * workqueue, keep the previous behavior to avoid subtle breakages
4438 if ((flags & WQ_UNBOUND) && max_active == 1)
4439 flags |= __WQ_ORDERED;
4441 /* see the comment above the definition of WQ_POWER_EFFICIENT */
4442 if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient)
4443 flags |= WQ_UNBOUND;
4445 /* allocate wq and format name */
4446 if (flags & WQ_UNBOUND)
4447 tbl_size = nr_node_ids * sizeof(wq->numa_pwq_tbl[0]);
4449 wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL);
4453 if (flags & WQ_UNBOUND) {
4454 wq->unbound_attrs = alloc_workqueue_attrs();
4455 if (!wq->unbound_attrs)
4459 va_start(args, max_active);
4460 vsnprintf(wq->name, sizeof(wq->name), fmt, args);
4463 max_active = max_active ?: WQ_DFL_ACTIVE;
4464 max_active = wq_clamp_max_active(max_active, flags, wq->name);
4468 wq->saved_max_active = max_active;
4469 mutex_init(&wq->mutex);
4470 atomic_set(&wq->nr_pwqs_to_flush, 0);
4471 INIT_LIST_HEAD(&wq->pwqs);
4472 INIT_LIST_HEAD(&wq->flusher_queue);
4473 INIT_LIST_HEAD(&wq->flusher_overflow);
4474 INIT_LIST_HEAD(&wq->maydays);
4476 wq_init_lockdep(wq);
4477 INIT_LIST_HEAD(&wq->list);
4479 if (alloc_and_link_pwqs(wq) < 0)
4480 goto err_unreg_lockdep;
4482 if (wq_online && init_rescuer(wq) < 0)
4485 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
4489 * wq_pool_mutex protects global freeze state and workqueues list.
4490 * Grab it, adjust max_active and add the new @wq to workqueues
4493 mutex_lock(&wq_pool_mutex);
4495 mutex_lock(&wq->mutex);
4496 for_each_pwq(pwq, wq)
4497 pwq_adjust_max_active(pwq);
4498 mutex_unlock(&wq->mutex);
4500 list_add_tail_rcu(&wq->list, &workqueues);
4502 mutex_unlock(&wq_pool_mutex);
4507 wq_unregister_lockdep(wq);
4508 wq_free_lockdep(wq);
4510 free_workqueue_attrs(wq->unbound_attrs);
4514 destroy_workqueue(wq);
4517 EXPORT_SYMBOL_GPL(alloc_workqueue);
4519 static bool pwq_busy(struct pool_workqueue *pwq)
4523 for (i = 0; i < WORK_NR_COLORS; i++)
4524 if (pwq->nr_in_flight[i])
4527 if ((pwq != pwq->wq->dfl_pwq) && (pwq->refcnt > 1))
4529 if (pwq->nr_active || !list_empty(&pwq->inactive_works))
4536 * destroy_workqueue - safely terminate a workqueue
4537 * @wq: target workqueue
4539 * Safely destroy a workqueue. All work currently pending will be done first.
4541 void destroy_workqueue(struct workqueue_struct *wq)
4543 struct pool_workqueue *pwq;
4547 * Remove it from sysfs first so that sanity check failure doesn't
4548 * lead to sysfs name conflicts.
4550 workqueue_sysfs_unregister(wq);
4552 /* mark the workqueue destruction is in progress */
4553 mutex_lock(&wq->mutex);
4554 wq->flags |= __WQ_DESTROYING;
4555 mutex_unlock(&wq->mutex);
4557 /* drain it before proceeding with destruction */
4558 drain_workqueue(wq);
4560 /* kill rescuer, if sanity checks fail, leave it w/o rescuer */
4562 struct worker *rescuer = wq->rescuer;
4564 /* this prevents new queueing */
4565 raw_spin_lock_irq(&wq_mayday_lock);
4567 raw_spin_unlock_irq(&wq_mayday_lock);
4569 /* rescuer will empty maydays list before exiting */
4570 kthread_stop(rescuer->task);
4575 * Sanity checks - grab all the locks so that we wait for all
4576 * in-flight operations which may do put_pwq().
4578 mutex_lock(&wq_pool_mutex);
4579 mutex_lock(&wq->mutex);
4580 for_each_pwq(pwq, wq) {
4581 raw_spin_lock_irq(&pwq->pool->lock);
4582 if (WARN_ON(pwq_busy(pwq))) {
4583 pr_warn("%s: %s has the following busy pwq\n",
4584 __func__, wq->name);
4586 raw_spin_unlock_irq(&pwq->pool->lock);
4587 mutex_unlock(&wq->mutex);
4588 mutex_unlock(&wq_pool_mutex);
4589 show_one_workqueue(wq);
4592 raw_spin_unlock_irq(&pwq->pool->lock);
4594 mutex_unlock(&wq->mutex);
4597 * wq list is used to freeze wq, remove from list after
4598 * flushing is complete in case freeze races us.
4600 list_del_rcu(&wq->list);
4601 mutex_unlock(&wq_pool_mutex);
4603 if (!(wq->flags & WQ_UNBOUND)) {
4604 wq_unregister_lockdep(wq);
4606 * The base ref is never dropped on per-cpu pwqs. Directly
4607 * schedule RCU free.
4609 call_rcu(&wq->rcu, rcu_free_wq);
4612 * We're the sole accessor of @wq at this point. Directly
4613 * access numa_pwq_tbl[] and dfl_pwq to put the base refs.
4614 * @wq will be freed when the last pwq is released.
4616 for_each_node(node) {
4617 pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
4618 RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL);
4619 put_pwq_unlocked(pwq);
4623 * Put dfl_pwq. @wq may be freed any time after dfl_pwq is
4624 * put. Don't access it afterwards.
4628 put_pwq_unlocked(pwq);
4631 EXPORT_SYMBOL_GPL(destroy_workqueue);
4634 * workqueue_set_max_active - adjust max_active of a workqueue
4635 * @wq: target workqueue
4636 * @max_active: new max_active value.
4638 * Set max_active of @wq to @max_active.
4641 * Don't call from IRQ context.
4643 void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
4645 struct pool_workqueue *pwq;
4647 /* disallow meddling with max_active for ordered workqueues */
4648 if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT))
4651 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
4653 mutex_lock(&wq->mutex);
4655 wq->flags &= ~__WQ_ORDERED;
4656 wq->saved_max_active = max_active;
4658 for_each_pwq(pwq, wq)
4659 pwq_adjust_max_active(pwq);
4661 mutex_unlock(&wq->mutex);
4663 EXPORT_SYMBOL_GPL(workqueue_set_max_active);
4666 * current_work - retrieve %current task's work struct
4668 * Determine if %current task is a workqueue worker and what it's working on.
4669 * Useful to find out the context that the %current task is running in.
4671 * Return: work struct if %current task is a workqueue worker, %NULL otherwise.
4673 struct work_struct *current_work(void)
4675 struct worker *worker = current_wq_worker();
4677 return worker ? worker->current_work : NULL;
4679 EXPORT_SYMBOL(current_work);
4682 * current_is_workqueue_rescuer - is %current workqueue rescuer?
4684 * Determine whether %current is a workqueue rescuer. Can be used from
4685 * work functions to determine whether it's being run off the rescuer task.
4687 * Return: %true if %current is a workqueue rescuer. %false otherwise.
4689 bool current_is_workqueue_rescuer(void)
4691 struct worker *worker = current_wq_worker();
4693 return worker && worker->rescue_wq;
4697 * workqueue_congested - test whether a workqueue is congested
4698 * @cpu: CPU in question
4699 * @wq: target workqueue
4701 * Test whether @wq's cpu workqueue for @cpu is congested. There is
4702 * no synchronization around this function and the test result is
4703 * unreliable and only useful as advisory hints or for debugging.
4705 * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU.
4706 * Note that both per-cpu and unbound workqueues may be associated with
4707 * multiple pool_workqueues which have separate congested states. A
4708 * workqueue being congested on one CPU doesn't mean the workqueue is also
4709 * contested on other CPUs / NUMA nodes.
4712 * %true if congested, %false otherwise.
4714 bool workqueue_congested(int cpu, struct workqueue_struct *wq)
4716 struct pool_workqueue *pwq;
4722 if (cpu == WORK_CPU_UNBOUND)
4723 cpu = smp_processor_id();
4725 if (!(wq->flags & WQ_UNBOUND))
4726 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
4728 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
4730 ret = !list_empty(&pwq->inactive_works);
4736 EXPORT_SYMBOL_GPL(workqueue_congested);
4739 * work_busy - test whether a work is currently pending or running
4740 * @work: the work to be tested
4742 * Test whether @work is currently pending or running. There is no
4743 * synchronization around this function and the test result is
4744 * unreliable and only useful as advisory hints or for debugging.
4747 * OR'd bitmask of WORK_BUSY_* bits.
4749 unsigned int work_busy(struct work_struct *work)
4751 struct worker_pool *pool;
4752 unsigned long flags;
4753 unsigned int ret = 0;
4755 if (work_pending(work))
4756 ret |= WORK_BUSY_PENDING;
4759 pool = get_work_pool(work);
4761 raw_spin_lock_irqsave(&pool->lock, flags);
4762 if (find_worker_executing_work(pool, work))
4763 ret |= WORK_BUSY_RUNNING;
4764 raw_spin_unlock_irqrestore(&pool->lock, flags);
4770 EXPORT_SYMBOL_GPL(work_busy);
4773 * set_worker_desc - set description for the current work item
4774 * @fmt: printf-style format string
4775 * @...: arguments for the format string
4777 * This function can be called by a running work function to describe what
4778 * the work item is about. If the worker task gets dumped, this
4779 * information will be printed out together to help debugging. The
4780 * description can be at most WORKER_DESC_LEN including the trailing '\0'.
4782 void set_worker_desc(const char *fmt, ...)
4784 struct worker *worker = current_wq_worker();
4788 va_start(args, fmt);
4789 vsnprintf(worker->desc, sizeof(worker->desc), fmt, args);
4793 EXPORT_SYMBOL_GPL(set_worker_desc);
4796 * print_worker_info - print out worker information and description
4797 * @log_lvl: the log level to use when printing
4798 * @task: target task
4800 * If @task is a worker and currently executing a work item, print out the
4801 * name of the workqueue being serviced and worker description set with
4802 * set_worker_desc() by the currently executing work item.
4804 * This function can be safely called on any task as long as the
4805 * task_struct itself is accessible. While safe, this function isn't
4806 * synchronized and may print out mixups or garbages of limited length.
4808 void print_worker_info(const char *log_lvl, struct task_struct *task)
4810 work_func_t *fn = NULL;
4811 char name[WQ_NAME_LEN] = { };
4812 char desc[WORKER_DESC_LEN] = { };
4813 struct pool_workqueue *pwq = NULL;
4814 struct workqueue_struct *wq = NULL;
4815 struct worker *worker;
4817 if (!(task->flags & PF_WQ_WORKER))
4821 * This function is called without any synchronization and @task
4822 * could be in any state. Be careful with dereferences.
4824 worker = kthread_probe_data(task);
4827 * Carefully copy the associated workqueue's workfn, name and desc.
4828 * Keep the original last '\0' in case the original is garbage.
4830 copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn));
4831 copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq));
4832 copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq));
4833 copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1);
4834 copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1);
4836 if (fn || name[0] || desc[0]) {
4837 printk("%sWorkqueue: %s %ps", log_lvl, name, fn);
4838 if (strcmp(name, desc))
4839 pr_cont(" (%s)", desc);
4844 static void pr_cont_pool_info(struct worker_pool *pool)
4846 pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask);
4847 if (pool->node != NUMA_NO_NODE)
4848 pr_cont(" node=%d", pool->node);
4849 pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice);
4852 struct pr_cont_work_struct {
4858 static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp)
4862 if (func == pcwsp->func) {
4866 if (pcwsp->ctr == 1)
4867 pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func);
4869 pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func);
4872 if ((long)func == -1L)
4874 pcwsp->comma = comma;
4879 static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp)
4881 if (work->func == wq_barrier_func) {
4882 struct wq_barrier *barr;
4884 barr = container_of(work, struct wq_barrier, work);
4886 pr_cont_work_flush(comma, (work_func_t)-1, pcwsp);
4887 pr_cont("%s BAR(%d)", comma ? "," : "",
4888 task_pid_nr(barr->task));
4891 pr_cont_work_flush(comma, (work_func_t)-1, pcwsp);
4892 pr_cont_work_flush(comma, work->func, pcwsp);
4896 static void show_pwq(struct pool_workqueue *pwq)
4898 struct pr_cont_work_struct pcws = { .ctr = 0, };
4899 struct worker_pool *pool = pwq->pool;
4900 struct work_struct *work;
4901 struct worker *worker;
4902 bool has_in_flight = false, has_pending = false;
4905 pr_info(" pwq %d:", pool->id);
4906 pr_cont_pool_info(pool);
4908 pr_cont(" active=%d/%d refcnt=%d%s\n",
4909 pwq->nr_active, pwq->max_active, pwq->refcnt,
4910 !list_empty(&pwq->mayday_node) ? " MAYDAY" : "");
4912 hash_for_each(pool->busy_hash, bkt, worker, hentry) {
4913 if (worker->current_pwq == pwq) {
4914 has_in_flight = true;
4918 if (has_in_flight) {
4921 pr_info(" in-flight:");
4922 hash_for_each(pool->busy_hash, bkt, worker, hentry) {
4923 if (worker->current_pwq != pwq)
4926 pr_cont("%s %d%s:%ps", comma ? "," : "",
4927 task_pid_nr(worker->task),
4928 worker->rescue_wq ? "(RESCUER)" : "",
4929 worker->current_func);
4930 list_for_each_entry(work, &worker->scheduled, entry)
4931 pr_cont_work(false, work, &pcws);
4932 pr_cont_work_flush(comma, (work_func_t)-1L, &pcws);
4938 list_for_each_entry(work, &pool->worklist, entry) {
4939 if (get_work_pwq(work) == pwq) {
4947 pr_info(" pending:");
4948 list_for_each_entry(work, &pool->worklist, entry) {
4949 if (get_work_pwq(work) != pwq)
4952 pr_cont_work(comma, work, &pcws);
4953 comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED);
4955 pr_cont_work_flush(comma, (work_func_t)-1L, &pcws);
4959 if (!list_empty(&pwq->inactive_works)) {
4962 pr_info(" inactive:");
4963 list_for_each_entry(work, &pwq->inactive_works, entry) {
4964 pr_cont_work(comma, work, &pcws);
4965 comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED);
4967 pr_cont_work_flush(comma, (work_func_t)-1L, &pcws);
4973 * show_one_workqueue - dump state of specified workqueue
4974 * @wq: workqueue whose state will be printed
4976 void show_one_workqueue(struct workqueue_struct *wq)
4978 struct pool_workqueue *pwq;
4980 unsigned long flags;
4982 for_each_pwq(pwq, wq) {
4983 if (pwq->nr_active || !list_empty(&pwq->inactive_works)) {
4988 if (idle) /* Nothing to print for idle workqueue */
4991 pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags);
4993 for_each_pwq(pwq, wq) {
4994 raw_spin_lock_irqsave(&pwq->pool->lock, flags);
4995 if (pwq->nr_active || !list_empty(&pwq->inactive_works)) {
4997 * Defer printing to avoid deadlocks in console
4998 * drivers that queue work while holding locks
4999 * also taken in their write paths.
5001 printk_deferred_enter();
5003 printk_deferred_exit();
5005 raw_spin_unlock_irqrestore(&pwq->pool->lock, flags);
5007 * We could be printing a lot from atomic context, e.g.
5008 * sysrq-t -> show_all_workqueues(). Avoid triggering
5011 touch_nmi_watchdog();
5017 * show_one_worker_pool - dump state of specified worker pool
5018 * @pool: worker pool whose state will be printed
5020 static void show_one_worker_pool(struct worker_pool *pool)
5022 struct worker *worker;
5024 unsigned long flags;
5025 unsigned long hung = 0;
5027 raw_spin_lock_irqsave(&pool->lock, flags);
5028 if (pool->nr_workers == pool->nr_idle)
5031 /* How long the first pending work is waiting for a worker. */
5032 if (!list_empty(&pool->worklist))
5033 hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000;
5036 * Defer printing to avoid deadlocks in console drivers that
5037 * queue work while holding locks also taken in their write
5040 printk_deferred_enter();
5041 pr_info("pool %d:", pool->id);
5042 pr_cont_pool_info(pool);
5043 pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers);
5045 pr_cont(" manager: %d",
5046 task_pid_nr(pool->manager->task));
5047 list_for_each_entry(worker, &pool->idle_list, entry) {
5048 pr_cont(" %s%d", first ? "idle: " : "",
5049 task_pid_nr(worker->task));
5053 printk_deferred_exit();
5055 raw_spin_unlock_irqrestore(&pool->lock, flags);
5057 * We could be printing a lot from atomic context, e.g.
5058 * sysrq-t -> show_all_workqueues(). Avoid triggering
5061 touch_nmi_watchdog();
5066 * show_all_workqueues - dump workqueue state
5068 * Called from a sysrq handler and prints out all busy workqueues and pools.
5070 void show_all_workqueues(void)
5072 struct workqueue_struct *wq;
5073 struct worker_pool *pool;
5078 pr_info("Showing busy workqueues and worker pools:\n");
5080 list_for_each_entry_rcu(wq, &workqueues, list)
5081 show_one_workqueue(wq);
5083 for_each_pool(pool, pi)
5084 show_one_worker_pool(pool);
5090 * show_freezable_workqueues - dump freezable workqueue state
5092 * Called from try_to_freeze_tasks() and prints out all freezable workqueues
5095 void show_freezable_workqueues(void)
5097 struct workqueue_struct *wq;
5101 pr_info("Showing freezable workqueues that are still busy:\n");
5103 list_for_each_entry_rcu(wq, &workqueues, list) {
5104 if (!(wq->flags & WQ_FREEZABLE))
5106 show_one_workqueue(wq);
5112 /* used to show worker information through /proc/PID/{comm,stat,status} */
5113 void wq_worker_comm(char *buf, size_t size, struct task_struct *task)
5117 /* always show the actual comm */
5118 off = strscpy(buf, task->comm, size);
5122 /* stabilize PF_WQ_WORKER and worker pool association */
5123 mutex_lock(&wq_pool_attach_mutex);
5125 if (task->flags & PF_WQ_WORKER) {
5126 struct worker *worker = kthread_data(task);
5127 struct worker_pool *pool = worker->pool;
5130 raw_spin_lock_irq(&pool->lock);
5132 * ->desc tracks information (wq name or
5133 * set_worker_desc()) for the latest execution. If
5134 * current, prepend '+', otherwise '-'.
5136 if (worker->desc[0] != '\0') {
5137 if (worker->current_work)
5138 scnprintf(buf + off, size - off, "+%s",
5141 scnprintf(buf + off, size - off, "-%s",
5144 raw_spin_unlock_irq(&pool->lock);
5148 mutex_unlock(&wq_pool_attach_mutex);
5156 * There are two challenges in supporting CPU hotplug. Firstly, there
5157 * are a lot of assumptions on strong associations among work, pwq and
5158 * pool which make migrating pending and scheduled works very
5159 * difficult to implement without impacting hot paths. Secondly,
5160 * worker pools serve mix of short, long and very long running works making
5161 * blocked draining impractical.
5163 * This is solved by allowing the pools to be disassociated from the CPU
5164 * running as an unbound one and allowing it to be reattached later if the
5165 * cpu comes back online.
5168 static void unbind_workers(int cpu)
5170 struct worker_pool *pool;
5171 struct worker *worker;
5173 for_each_cpu_worker_pool(pool, cpu) {
5174 mutex_lock(&wq_pool_attach_mutex);
5175 raw_spin_lock_irq(&pool->lock);
5178 * We've blocked all attach/detach operations. Make all workers
5179 * unbound and set DISASSOCIATED. Before this, all workers
5180 * must be on the cpu. After this, they may become diasporas.
5181 * And the preemption disabled section in their sched callbacks
5182 * are guaranteed to see WORKER_UNBOUND since the code here
5183 * is on the same cpu.
5185 for_each_pool_worker(worker, pool)
5186 worker->flags |= WORKER_UNBOUND;
5188 pool->flags |= POOL_DISASSOCIATED;
5191 * The handling of nr_running in sched callbacks are disabled
5192 * now. Zap nr_running. After this, nr_running stays zero and
5193 * need_more_worker() and keep_working() are always true as
5194 * long as the worklist is not empty. This pool now behaves as
5195 * an unbound (in terms of concurrency management) pool which
5196 * are served by workers tied to the pool.
5198 pool->nr_running = 0;
5201 * With concurrency management just turned off, a busy
5202 * worker blocking could lead to lengthy stalls. Kick off
5203 * unbound chain execution of currently pending work items.
5205 wake_up_worker(pool);
5207 raw_spin_unlock_irq(&pool->lock);
5209 for_each_pool_worker(worker, pool)
5210 unbind_worker(worker);
5212 mutex_unlock(&wq_pool_attach_mutex);
5217 * rebind_workers - rebind all workers of a pool to the associated CPU
5218 * @pool: pool of interest
5220 * @pool->cpu is coming online. Rebind all workers to the CPU.
5222 static void rebind_workers(struct worker_pool *pool)
5224 struct worker *worker;
5226 lockdep_assert_held(&wq_pool_attach_mutex);
5229 * Restore CPU affinity of all workers. As all idle workers should
5230 * be on the run-queue of the associated CPU before any local
5231 * wake-ups for concurrency management happen, restore CPU affinity
5232 * of all workers first and then clear UNBOUND. As we're called
5233 * from CPU_ONLINE, the following shouldn't fail.
5235 for_each_pool_worker(worker, pool) {
5236 kthread_set_per_cpu(worker->task, pool->cpu);
5237 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
5238 pool->attrs->cpumask) < 0);
5241 raw_spin_lock_irq(&pool->lock);
5243 pool->flags &= ~POOL_DISASSOCIATED;
5245 for_each_pool_worker(worker, pool) {
5246 unsigned int worker_flags = worker->flags;
5249 * We want to clear UNBOUND but can't directly call
5250 * worker_clr_flags() or adjust nr_running. Atomically
5251 * replace UNBOUND with another NOT_RUNNING flag REBOUND.
5252 * @worker will clear REBOUND using worker_clr_flags() when
5253 * it initiates the next execution cycle thus restoring
5254 * concurrency management. Note that when or whether
5255 * @worker clears REBOUND doesn't affect correctness.
5257 * WRITE_ONCE() is necessary because @worker->flags may be
5258 * tested without holding any lock in
5259 * wq_worker_running(). Without it, NOT_RUNNING test may
5260 * fail incorrectly leading to premature concurrency
5261 * management operations.
5263 WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
5264 worker_flags |= WORKER_REBOUND;
5265 worker_flags &= ~WORKER_UNBOUND;
5266 WRITE_ONCE(worker->flags, worker_flags);
5269 raw_spin_unlock_irq(&pool->lock);
5273 * restore_unbound_workers_cpumask - restore cpumask of unbound workers
5274 * @pool: unbound pool of interest
5275 * @cpu: the CPU which is coming up
5277 * An unbound pool may end up with a cpumask which doesn't have any online
5278 * CPUs. When a worker of such pool get scheduled, the scheduler resets
5279 * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
5280 * online CPU before, cpus_allowed of all its workers should be restored.
5282 static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
5284 static cpumask_t cpumask;
5285 struct worker *worker;
5287 lockdep_assert_held(&wq_pool_attach_mutex);
5289 /* is @cpu allowed for @pool? */
5290 if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
5293 cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
5295 /* as we're called from CPU_ONLINE, the following shouldn't fail */
5296 for_each_pool_worker(worker, pool)
5297 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0);
5300 int workqueue_prepare_cpu(unsigned int cpu)
5302 struct worker_pool *pool;
5304 for_each_cpu_worker_pool(pool, cpu) {
5305 if (pool->nr_workers)
5307 if (!create_worker(pool))
5313 int workqueue_online_cpu(unsigned int cpu)
5315 struct worker_pool *pool;
5316 struct workqueue_struct *wq;
5319 mutex_lock(&wq_pool_mutex);
5321 for_each_pool(pool, pi) {
5322 mutex_lock(&wq_pool_attach_mutex);
5324 if (pool->cpu == cpu)
5325 rebind_workers(pool);
5326 else if (pool->cpu < 0)
5327 restore_unbound_workers_cpumask(pool, cpu);
5329 mutex_unlock(&wq_pool_attach_mutex);
5332 /* update NUMA affinity of unbound workqueues */
5333 list_for_each_entry(wq, &workqueues, list)
5334 wq_update_unbound_numa(wq, cpu, true);
5336 mutex_unlock(&wq_pool_mutex);
5340 int workqueue_offline_cpu(unsigned int cpu)
5342 struct workqueue_struct *wq;
5344 /* unbinding per-cpu workers should happen on the local CPU */
5345 if (WARN_ON(cpu != smp_processor_id()))
5348 unbind_workers(cpu);
5350 /* update NUMA affinity of unbound workqueues */
5351 mutex_lock(&wq_pool_mutex);
5352 list_for_each_entry(wq, &workqueues, list)
5353 wq_update_unbound_numa(wq, cpu, false);
5354 mutex_unlock(&wq_pool_mutex);
5359 struct work_for_cpu {
5360 struct work_struct work;
5366 static void work_for_cpu_fn(struct work_struct *work)
5368 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
5370 wfc->ret = wfc->fn(wfc->arg);
5374 * work_on_cpu - run a function in thread context on a particular cpu
5375 * @cpu: the cpu to run on
5376 * @fn: the function to run
5377 * @arg: the function arg
5379 * It is up to the caller to ensure that the cpu doesn't go offline.
5380 * The caller must not hold any locks which would prevent @fn from completing.
5382 * Return: The value @fn returns.
5384 long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
5386 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
5388 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
5389 schedule_work_on(cpu, &wfc.work);
5390 flush_work(&wfc.work);
5391 destroy_work_on_stack(&wfc.work);
5394 EXPORT_SYMBOL_GPL(work_on_cpu);
5397 * work_on_cpu_safe - run a function in thread context on a particular cpu
5398 * @cpu: the cpu to run on
5399 * @fn: the function to run
5400 * @arg: the function argument
5402 * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold
5403 * any locks which would prevent @fn from completing.
5405 * Return: The value @fn returns.
5407 long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg)
5412 if (cpu_online(cpu))
5413 ret = work_on_cpu(cpu, fn, arg);
5417 EXPORT_SYMBOL_GPL(work_on_cpu_safe);
5418 #endif /* CONFIG_SMP */
5420 #ifdef CONFIG_FREEZER
5423 * freeze_workqueues_begin - begin freezing workqueues
5425 * Start freezing workqueues. After this function returns, all freezable
5426 * workqueues will queue new works to their inactive_works list instead of
5430 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
5432 void freeze_workqueues_begin(void)
5434 struct workqueue_struct *wq;
5435 struct pool_workqueue *pwq;
5437 mutex_lock(&wq_pool_mutex);
5439 WARN_ON_ONCE(workqueue_freezing);
5440 workqueue_freezing = true;
5442 list_for_each_entry(wq, &workqueues, list) {
5443 mutex_lock(&wq->mutex);
5444 for_each_pwq(pwq, wq)
5445 pwq_adjust_max_active(pwq);
5446 mutex_unlock(&wq->mutex);
5449 mutex_unlock(&wq_pool_mutex);
5453 * freeze_workqueues_busy - are freezable workqueues still busy?
5455 * Check whether freezing is complete. This function must be called
5456 * between freeze_workqueues_begin() and thaw_workqueues().
5459 * Grabs and releases wq_pool_mutex.
5462 * %true if some freezable workqueues are still busy. %false if freezing
5465 bool freeze_workqueues_busy(void)
5468 struct workqueue_struct *wq;
5469 struct pool_workqueue *pwq;
5471 mutex_lock(&wq_pool_mutex);
5473 WARN_ON_ONCE(!workqueue_freezing);
5475 list_for_each_entry(wq, &workqueues, list) {
5476 if (!(wq->flags & WQ_FREEZABLE))
5479 * nr_active is monotonically decreasing. It's safe
5480 * to peek without lock.
5483 for_each_pwq(pwq, wq) {
5484 WARN_ON_ONCE(pwq->nr_active < 0);
5485 if (pwq->nr_active) {
5494 mutex_unlock(&wq_pool_mutex);
5499 * thaw_workqueues - thaw workqueues
5501 * Thaw workqueues. Normal queueing is restored and all collected
5502 * frozen works are transferred to their respective pool worklists.
5505 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
5507 void thaw_workqueues(void)
5509 struct workqueue_struct *wq;
5510 struct pool_workqueue *pwq;
5512 mutex_lock(&wq_pool_mutex);
5514 if (!workqueue_freezing)
5517 workqueue_freezing = false;
5519 /* restore max_active and repopulate worklist */
5520 list_for_each_entry(wq, &workqueues, list) {
5521 mutex_lock(&wq->mutex);
5522 for_each_pwq(pwq, wq)
5523 pwq_adjust_max_active(pwq);
5524 mutex_unlock(&wq->mutex);
5528 mutex_unlock(&wq_pool_mutex);
5530 #endif /* CONFIG_FREEZER */
5532 static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask)
5536 struct workqueue_struct *wq;
5537 struct apply_wqattrs_ctx *ctx, *n;
5539 lockdep_assert_held(&wq_pool_mutex);
5541 list_for_each_entry(wq, &workqueues, list) {
5542 if (!(wq->flags & WQ_UNBOUND))
5544 /* creating multiple pwqs breaks ordering guarantee */
5545 if (wq->flags & __WQ_ORDERED)
5548 ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask);
5554 list_add_tail(&ctx->list, &ctxs);
5557 list_for_each_entry_safe(ctx, n, &ctxs, list) {
5559 apply_wqattrs_commit(ctx);
5560 apply_wqattrs_cleanup(ctx);
5564 mutex_lock(&wq_pool_attach_mutex);
5565 cpumask_copy(wq_unbound_cpumask, unbound_cpumask);
5566 mutex_unlock(&wq_pool_attach_mutex);
5572 * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask
5573 * @cpumask: the cpumask to set
5575 * The low-level workqueues cpumask is a global cpumask that limits
5576 * the affinity of all unbound workqueues. This function check the @cpumask
5577 * and apply it to all unbound workqueues and updates all pwqs of them.
5579 * Return: 0 - Success
5580 * -EINVAL - Invalid @cpumask
5581 * -ENOMEM - Failed to allocate memory for attrs or pwqs.
5583 int workqueue_set_unbound_cpumask(cpumask_var_t cpumask)
5588 * Not excluding isolated cpus on purpose.
5589 * If the user wishes to include them, we allow that.
5591 cpumask_and(cpumask, cpumask, cpu_possible_mask);
5592 if (!cpumask_empty(cpumask)) {
5593 apply_wqattrs_lock();
5594 if (cpumask_equal(cpumask, wq_unbound_cpumask)) {
5599 ret = workqueue_apply_unbound_cpumask(cpumask);
5602 apply_wqattrs_unlock();
5610 * Workqueues with WQ_SYSFS flag set is visible to userland via
5611 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
5612 * following attributes.
5614 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
5615 * max_active RW int : maximum number of in-flight work items
5617 * Unbound workqueues have the following extra attributes.
5619 * pool_ids RO int : the associated pool IDs for each node
5620 * nice RW int : nice value of the workers
5621 * cpumask RW mask : bitmask of allowed CPUs for the workers
5622 * numa RW bool : whether enable NUMA affinity
5625 struct workqueue_struct *wq;
5629 static struct workqueue_struct *dev_to_wq(struct device *dev)
5631 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
5636 static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr,
5639 struct workqueue_struct *wq = dev_to_wq(dev);
5641 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
5643 static DEVICE_ATTR_RO(per_cpu);
5645 static ssize_t max_active_show(struct device *dev,
5646 struct device_attribute *attr, char *buf)
5648 struct workqueue_struct *wq = dev_to_wq(dev);
5650 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
5653 static ssize_t max_active_store(struct device *dev,
5654 struct device_attribute *attr, const char *buf,
5657 struct workqueue_struct *wq = dev_to_wq(dev);
5660 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
5663 workqueue_set_max_active(wq, val);
5666 static DEVICE_ATTR_RW(max_active);
5668 static struct attribute *wq_sysfs_attrs[] = {
5669 &dev_attr_per_cpu.attr,
5670 &dev_attr_max_active.attr,
5673 ATTRIBUTE_GROUPS(wq_sysfs);
5675 static ssize_t wq_pool_ids_show(struct device *dev,
5676 struct device_attribute *attr, char *buf)
5678 struct workqueue_struct *wq = dev_to_wq(dev);
5679 const char *delim = "";
5680 int node, written = 0;
5684 for_each_node(node) {
5685 written += scnprintf(buf + written, PAGE_SIZE - written,
5686 "%s%d:%d", delim, node,
5687 unbound_pwq_by_node(wq, node)->pool->id);
5690 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
5697 static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
5700 struct workqueue_struct *wq = dev_to_wq(dev);
5703 mutex_lock(&wq->mutex);
5704 written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice);
5705 mutex_unlock(&wq->mutex);
5710 /* prepare workqueue_attrs for sysfs store operations */
5711 static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
5713 struct workqueue_attrs *attrs;
5715 lockdep_assert_held(&wq_pool_mutex);
5717 attrs = alloc_workqueue_attrs();
5721 copy_workqueue_attrs(attrs, wq->unbound_attrs);
5725 static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
5726 const char *buf, size_t count)
5728 struct workqueue_struct *wq = dev_to_wq(dev);
5729 struct workqueue_attrs *attrs;
5732 apply_wqattrs_lock();
5734 attrs = wq_sysfs_prep_attrs(wq);
5738 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
5739 attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE)
5740 ret = apply_workqueue_attrs_locked(wq, attrs);
5745 apply_wqattrs_unlock();
5746 free_workqueue_attrs(attrs);
5747 return ret ?: count;
5750 static ssize_t wq_cpumask_show(struct device *dev,
5751 struct device_attribute *attr, char *buf)
5753 struct workqueue_struct *wq = dev_to_wq(dev);
5756 mutex_lock(&wq->mutex);
5757 written = scnprintf(buf, PAGE_SIZE, "%*pb\n",
5758 cpumask_pr_args(wq->unbound_attrs->cpumask));
5759 mutex_unlock(&wq->mutex);
5763 static ssize_t wq_cpumask_store(struct device *dev,
5764 struct device_attribute *attr,
5765 const char *buf, size_t count)
5767 struct workqueue_struct *wq = dev_to_wq(dev);
5768 struct workqueue_attrs *attrs;
5771 apply_wqattrs_lock();
5773 attrs = wq_sysfs_prep_attrs(wq);
5777 ret = cpumask_parse(buf, attrs->cpumask);
5779 ret = apply_workqueue_attrs_locked(wq, attrs);
5782 apply_wqattrs_unlock();
5783 free_workqueue_attrs(attrs);
5784 return ret ?: count;
5787 static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr,
5790 struct workqueue_struct *wq = dev_to_wq(dev);
5793 mutex_lock(&wq->mutex);
5794 written = scnprintf(buf, PAGE_SIZE, "%d\n",
5795 !wq->unbound_attrs->no_numa);
5796 mutex_unlock(&wq->mutex);
5801 static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr,
5802 const char *buf, size_t count)
5804 struct workqueue_struct *wq = dev_to_wq(dev);
5805 struct workqueue_attrs *attrs;
5806 int v, ret = -ENOMEM;
5808 apply_wqattrs_lock();
5810 attrs = wq_sysfs_prep_attrs(wq);
5815 if (sscanf(buf, "%d", &v) == 1) {
5816 attrs->no_numa = !v;
5817 ret = apply_workqueue_attrs_locked(wq, attrs);
5821 apply_wqattrs_unlock();
5822 free_workqueue_attrs(attrs);
5823 return ret ?: count;
5826 static struct device_attribute wq_sysfs_unbound_attrs[] = {
5827 __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL),
5828 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
5829 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
5830 __ATTR(numa, 0644, wq_numa_show, wq_numa_store),
5834 static struct bus_type wq_subsys = {
5835 .name = "workqueue",
5836 .dev_groups = wq_sysfs_groups,
5839 static ssize_t wq_unbound_cpumask_show(struct device *dev,
5840 struct device_attribute *attr, char *buf)
5844 mutex_lock(&wq_pool_mutex);
5845 written = scnprintf(buf, PAGE_SIZE, "%*pb\n",
5846 cpumask_pr_args(wq_unbound_cpumask));
5847 mutex_unlock(&wq_pool_mutex);
5852 static ssize_t wq_unbound_cpumask_store(struct device *dev,
5853 struct device_attribute *attr, const char *buf, size_t count)
5855 cpumask_var_t cpumask;
5858 if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL))
5861 ret = cpumask_parse(buf, cpumask);
5863 ret = workqueue_set_unbound_cpumask(cpumask);
5865 free_cpumask_var(cpumask);
5866 return ret ? ret : count;
5869 static struct device_attribute wq_sysfs_cpumask_attr =
5870 __ATTR(cpumask, 0644, wq_unbound_cpumask_show,
5871 wq_unbound_cpumask_store);
5873 static int __init wq_sysfs_init(void)
5875 struct device *dev_root;
5878 err = subsys_virtual_register(&wq_subsys, NULL);
5882 dev_root = bus_get_dev_root(&wq_subsys);
5884 err = device_create_file(dev_root, &wq_sysfs_cpumask_attr);
5885 put_device(dev_root);
5889 core_initcall(wq_sysfs_init);
5891 static void wq_device_release(struct device *dev)
5893 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
5899 * workqueue_sysfs_register - make a workqueue visible in sysfs
5900 * @wq: the workqueue to register
5902 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
5903 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
5904 * which is the preferred method.
5906 * Workqueue user should use this function directly iff it wants to apply
5907 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
5908 * apply_workqueue_attrs() may race against userland updating the
5911 * Return: 0 on success, -errno on failure.
5913 int workqueue_sysfs_register(struct workqueue_struct *wq)
5915 struct wq_device *wq_dev;
5919 * Adjusting max_active or creating new pwqs by applying
5920 * attributes breaks ordering guarantee. Disallow exposing ordered
5923 if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT))
5926 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
5931 wq_dev->dev.bus = &wq_subsys;
5932 wq_dev->dev.release = wq_device_release;
5933 dev_set_name(&wq_dev->dev, "%s", wq->name);
5936 * unbound_attrs are created separately. Suppress uevent until
5937 * everything is ready.
5939 dev_set_uevent_suppress(&wq_dev->dev, true);
5941 ret = device_register(&wq_dev->dev);
5943 put_device(&wq_dev->dev);
5948 if (wq->flags & WQ_UNBOUND) {
5949 struct device_attribute *attr;
5951 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
5952 ret = device_create_file(&wq_dev->dev, attr);
5954 device_unregister(&wq_dev->dev);
5961 dev_set_uevent_suppress(&wq_dev->dev, false);
5962 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
5967 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
5968 * @wq: the workqueue to unregister
5970 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
5972 static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
5974 struct wq_device *wq_dev = wq->wq_dev;
5980 device_unregister(&wq_dev->dev);
5982 #else /* CONFIG_SYSFS */
5983 static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
5984 #endif /* CONFIG_SYSFS */
5987 * Workqueue watchdog.
5989 * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal
5990 * flush dependency, a concurrency managed work item which stays RUNNING
5991 * indefinitely. Workqueue stalls can be very difficult to debug as the
5992 * usual warning mechanisms don't trigger and internal workqueue state is
5995 * Workqueue watchdog monitors all worker pools periodically and dumps
5996 * state if some pools failed to make forward progress for a while where
5997 * forward progress is defined as the first item on ->worklist changing.
5999 * This mechanism is controlled through the kernel parameter
6000 * "workqueue.watchdog_thresh" which can be updated at runtime through the
6001 * corresponding sysfs parameter file.
6003 #ifdef CONFIG_WQ_WATCHDOG
6005 static unsigned long wq_watchdog_thresh = 30;
6006 static struct timer_list wq_watchdog_timer;
6008 static unsigned long wq_watchdog_touched = INITIAL_JIFFIES;
6009 static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES;
6012 * Show workers that might prevent the processing of pending work items.
6013 * The only candidates are CPU-bound workers in the running state.
6014 * Pending work items should be handled by another idle worker
6015 * in all other situations.
6017 static void show_cpu_pool_hog(struct worker_pool *pool)
6019 struct worker *worker;
6020 unsigned long flags;
6023 raw_spin_lock_irqsave(&pool->lock, flags);
6025 hash_for_each(pool->busy_hash, bkt, worker, hentry) {
6026 if (task_is_running(worker->task)) {
6028 * Defer printing to avoid deadlocks in console
6029 * drivers that queue work while holding locks
6030 * also taken in their write paths.
6032 printk_deferred_enter();
6034 pr_info("pool %d:\n", pool->id);
6035 sched_show_task(worker->task);
6037 printk_deferred_exit();
6041 raw_spin_unlock_irqrestore(&pool->lock, flags);
6044 static void show_cpu_pools_hogs(void)
6046 struct worker_pool *pool;
6049 pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n");
6053 for_each_pool(pool, pi) {
6054 if (pool->cpu_stall)
6055 show_cpu_pool_hog(pool);
6062 static void wq_watchdog_reset_touched(void)
6066 wq_watchdog_touched = jiffies;
6067 for_each_possible_cpu(cpu)
6068 per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies;
6071 static void wq_watchdog_timer_fn(struct timer_list *unused)
6073 unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ;
6074 bool lockup_detected = false;
6075 bool cpu_pool_stall = false;
6076 unsigned long now = jiffies;
6077 struct worker_pool *pool;
6085 for_each_pool(pool, pi) {
6086 unsigned long pool_ts, touched, ts;
6088 pool->cpu_stall = false;
6089 if (list_empty(&pool->worklist))
6093 * If a virtual machine is stopped by the host it can look to
6094 * the watchdog like a stall.
6096 kvm_check_and_clear_guest_paused();
6098 /* get the latest of pool and touched timestamps */
6100 touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu));
6102 touched = READ_ONCE(wq_watchdog_touched);
6103 pool_ts = READ_ONCE(pool->watchdog_ts);
6105 if (time_after(pool_ts, touched))
6111 if (time_after(now, ts + thresh)) {
6112 lockup_detected = true;
6113 if (pool->cpu >= 0) {
6114 pool->cpu_stall = true;
6115 cpu_pool_stall = true;
6117 pr_emerg("BUG: workqueue lockup - pool");
6118 pr_cont_pool_info(pool);
6119 pr_cont(" stuck for %us!\n",
6120 jiffies_to_msecs(now - pool_ts) / 1000);
6128 if (lockup_detected)
6129 show_all_workqueues();
6132 show_cpu_pools_hogs();
6134 wq_watchdog_reset_touched();
6135 mod_timer(&wq_watchdog_timer, jiffies + thresh);
6138 notrace void wq_watchdog_touch(int cpu)
6141 per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies;
6143 wq_watchdog_touched = jiffies;
6146 static void wq_watchdog_set_thresh(unsigned long thresh)
6148 wq_watchdog_thresh = 0;
6149 del_timer_sync(&wq_watchdog_timer);
6152 wq_watchdog_thresh = thresh;
6153 wq_watchdog_reset_touched();
6154 mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ);
6158 static int wq_watchdog_param_set_thresh(const char *val,
6159 const struct kernel_param *kp)
6161 unsigned long thresh;
6164 ret = kstrtoul(val, 0, &thresh);
6169 wq_watchdog_set_thresh(thresh);
6171 wq_watchdog_thresh = thresh;
6176 static const struct kernel_param_ops wq_watchdog_thresh_ops = {
6177 .set = wq_watchdog_param_set_thresh,
6178 .get = param_get_ulong,
6181 module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh,
6184 static void wq_watchdog_init(void)
6186 timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE);
6187 wq_watchdog_set_thresh(wq_watchdog_thresh);
6190 #else /* CONFIG_WQ_WATCHDOG */
6192 static inline void wq_watchdog_init(void) { }
6194 #endif /* CONFIG_WQ_WATCHDOG */
6196 static void __init wq_numa_init(void)
6201 if (num_possible_nodes() <= 1)
6204 if (wq_disable_numa) {
6205 pr_info("workqueue: NUMA affinity support disabled\n");
6209 for_each_possible_cpu(cpu) {
6210 if (WARN_ON(cpu_to_node(cpu) == NUMA_NO_NODE)) {
6211 pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
6216 wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs();
6217 BUG_ON(!wq_update_unbound_numa_attrs_buf);
6220 * We want masks of possible CPUs of each node which isn't readily
6221 * available. Build one from cpu_to_node() which should have been
6222 * fully initialized by now.
6224 tbl = kcalloc(nr_node_ids, sizeof(tbl[0]), GFP_KERNEL);
6228 BUG_ON(!zalloc_cpumask_var_node(&tbl[node], GFP_KERNEL,
6229 node_online(node) ? node : NUMA_NO_NODE));
6231 for_each_possible_cpu(cpu) {
6232 node = cpu_to_node(cpu);
6233 cpumask_set_cpu(cpu, tbl[node]);
6236 wq_numa_possible_cpumask = tbl;
6237 wq_numa_enabled = true;
6241 * workqueue_init_early - early init for workqueue subsystem
6243 * This is the first half of two-staged workqueue subsystem initialization
6244 * and invoked as soon as the bare basics - memory allocation, cpumasks and
6245 * idr are up. It sets up all the data structures and system workqueues
6246 * and allows early boot code to create workqueues and queue/cancel work
6247 * items. Actual work item execution starts only after kthreads can be
6248 * created and scheduled right before early initcalls.
6250 void __init workqueue_init_early(void)
6252 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
6255 BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
6257 BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL));
6258 cpumask_copy(wq_unbound_cpumask, housekeeping_cpumask(HK_TYPE_WQ));
6259 cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, housekeeping_cpumask(HK_TYPE_DOMAIN));
6261 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
6263 /* initialize CPU pools */
6264 for_each_possible_cpu(cpu) {
6265 struct worker_pool *pool;
6268 for_each_cpu_worker_pool(pool, cpu) {
6269 BUG_ON(init_worker_pool(pool));
6271 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
6272 pool->attrs->nice = std_nice[i++];
6273 pool->node = cpu_to_node(cpu);
6276 mutex_lock(&wq_pool_mutex);
6277 BUG_ON(worker_pool_assign_id(pool));
6278 mutex_unlock(&wq_pool_mutex);
6282 /* create default unbound and ordered wq attrs */
6283 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
6284 struct workqueue_attrs *attrs;
6286 BUG_ON(!(attrs = alloc_workqueue_attrs()));
6287 attrs->nice = std_nice[i];
6288 unbound_std_wq_attrs[i] = attrs;
6291 * An ordered wq should have only one pwq as ordering is
6292 * guaranteed by max_active which is enforced by pwqs.
6293 * Turn off NUMA so that dfl_pwq is used for all nodes.
6295 BUG_ON(!(attrs = alloc_workqueue_attrs()));
6296 attrs->nice = std_nice[i];
6297 attrs->no_numa = true;
6298 ordered_wq_attrs[i] = attrs;
6301 system_wq = alloc_workqueue("events", 0, 0);
6302 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
6303 system_long_wq = alloc_workqueue("events_long", 0, 0);
6304 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
6305 WQ_UNBOUND_MAX_ACTIVE);
6306 system_freezable_wq = alloc_workqueue("events_freezable",
6308 system_power_efficient_wq = alloc_workqueue("events_power_efficient",
6309 WQ_POWER_EFFICIENT, 0);
6310 system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient",
6311 WQ_FREEZABLE | WQ_POWER_EFFICIENT,
6313 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
6314 !system_unbound_wq || !system_freezable_wq ||
6315 !system_power_efficient_wq ||
6316 !system_freezable_power_efficient_wq);
6320 * workqueue_init - bring workqueue subsystem fully online
6322 * This is the latter half of two-staged workqueue subsystem initialization
6323 * and invoked as soon as kthreads can be created and scheduled.
6324 * Workqueues have been created and work items queued on them, but there
6325 * are no kworkers executing the work items yet. Populate the worker pools
6326 * with the initial workers and enable future kworker creations.
6328 void __init workqueue_init(void)
6330 struct workqueue_struct *wq;
6331 struct worker_pool *pool;
6335 * It'd be simpler to initialize NUMA in workqueue_init_early() but
6336 * CPU to node mapping may not be available that early on some
6337 * archs such as power and arm64. As per-cpu pools created
6338 * previously could be missing node hint and unbound pools NUMA
6339 * affinity, fix them up.
6341 * Also, while iterating workqueues, create rescuers if requested.
6345 mutex_lock(&wq_pool_mutex);
6347 for_each_possible_cpu(cpu) {
6348 for_each_cpu_worker_pool(pool, cpu) {
6349 pool->node = cpu_to_node(cpu);
6353 list_for_each_entry(wq, &workqueues, list) {
6354 wq_update_unbound_numa(wq, smp_processor_id(), true);
6355 WARN(init_rescuer(wq),
6356 "workqueue: failed to create early rescuer for %s",
6360 mutex_unlock(&wq_pool_mutex);
6362 /* create the initial workers */
6363 for_each_online_cpu(cpu) {
6364 for_each_cpu_worker_pool(pool, cpu) {
6365 pool->flags &= ~POOL_DISASSOCIATED;
6366 BUG_ON(!create_worker(pool));
6370 hash_for_each(unbound_pool_hash, bkt, pool, hash_node)
6371 BUG_ON(!create_worker(pool));
6378 * Despite the naming, this is a no-op function which is here only for avoiding
6379 * link error. Since compile-time warning may fail to catch, we will need to
6380 * emit run-time warning from __flush_workqueue().
6382 void __warn_flushing_systemwide_wq(void) { }
6383 EXPORT_SYMBOL(__warn_flushing_systemwide_wq);