1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
3c54fce6eSTejun Heo * kernel/workqueue.c - generic async execution with shared worker pool
41da177e4SLinus Torvalds *
5c54fce6eSTejun Heo * Copyright (C) 2002 Ingo Molnar
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * Derived from the taskqueue/keventd code by:
81da177e4SLinus Torvalds * David Woodhouse <[email protected]>
9e1f8e874SFrancois Cami * Andrew Morton
101da177e4SLinus Torvalds * Kai Petzke <[email protected]>
111da177e4SLinus Torvalds * Theodore Ts'o <[email protected]>
1289ada679SChristoph Lameter *
13cde53535SChristoph Lameter * Made to use alloc_percpu by Christoph Lameter.
14c54fce6eSTejun Heo *
15c54fce6eSTejun Heo * Copyright (C) 2010 SUSE Linux Products GmbH
16c54fce6eSTejun Heo * Copyright (C) 2010 Tejun Heo <[email protected]>
17c54fce6eSTejun Heo *
18c54fce6eSTejun Heo * This is the generic async execution mechanism. Work items as are
19c54fce6eSTejun Heo * executed in process context. The worker pool is shared and
20b11895c4SLibin * automatically managed. There are two worker pools for each CPU (one for
21b11895c4SLibin * normal work items and the other for high priority ones) and some extra
22b11895c4SLibin * pools for workqueues which are not bound to any specific CPU - the
23b11895c4SLibin * number of these backing pools is dynamic.
24c54fce6eSTejun Heo *
259a261491SBenjamin Peterson * Please read Documentation/core-api/workqueue.rst for details.
261da177e4SLinus Torvalds */
271da177e4SLinus Torvalds
289984de1aSPaul Gortmaker #include <linux/export.h>
291da177e4SLinus Torvalds #include <linux/kernel.h>
301da177e4SLinus Torvalds #include <linux/sched.h>
311da177e4SLinus Torvalds #include <linux/init.h>
324cb1ef64STejun Heo #include <linux/interrupt.h>
331da177e4SLinus Torvalds #include <linux/signal.h>
341da177e4SLinus Torvalds #include <linux/completion.h>
351da177e4SLinus Torvalds #include <linux/workqueue.h>
361da177e4SLinus Torvalds #include <linux/slab.h>
371da177e4SLinus Torvalds #include <linux/cpu.h>
381da177e4SLinus Torvalds #include <linux/notifier.h>
391da177e4SLinus Torvalds #include <linux/kthread.h>
401fa44ecaSJames Bottomley #include <linux/hardirq.h>
4146934023SChristoph Lameter #include <linux/mempolicy.h>
42341a5958SRafael J. Wysocki #include <linux/freezer.h>
43d5abe669SPeter Zijlstra #include <linux/debug_locks.h>
444e6045f1SJohannes Berg #include <linux/lockdep.h>
45c34056a3STejun Heo #include <linux/idr.h>
4629c91e99STejun Heo #include <linux/jhash.h>
4742f8570fSSasha Levin #include <linux/hashtable.h>
4876af4d93STejun Heo #include <linux/rculist.h>
49bce90380STejun Heo #include <linux/nodemask.h>
504c16bd32STejun Heo #include <linux/moduleparam.h>
513d1cb205STejun Heo #include <linux/uaccess.h>
52c98a9805STal Shorer #include <linux/sched/isolation.h>
53cd2440d6SPetr Mladek #include <linux/sched/debug.h>
5462635ea8SSergey Senozhatsky #include <linux/nmi.h>
55940d71c6SSergey Senozhatsky #include <linux/kvm_para.h>
56aa6fde93STejun Heo #include <linux/delay.h>
572f34d733STejun Heo #include <linux/irq_work.h>
58e22bee78STejun Heo
59ea138446STejun Heo #include "workqueue_internal.h"
601da177e4SLinus Torvalds
61e563d0a7STejun Heo enum worker_pool_flags {
62bc2ae0f5STejun Heo /*
6324647570STejun Heo * worker_pool flags
64bc2ae0f5STejun Heo *
6524647570STejun Heo * A bound pool is either associated or disassociated with its CPU.
66bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the
67bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management
68bc2ae0f5STejun Heo * is in effect.
69bc2ae0f5STejun Heo *
70bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have
71bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may
7224647570STejun Heo * be executing on any CPU. The pool behaves as an unbound one.
73bc2ae0f5STejun Heo *
74bc3a1afcSTejun Heo * Note that DISASSOCIATED should be flipped only while holding
751258fae7STejun Heo * wq_pool_attach_mutex to avoid changing binding state while
764736cbf7SLai Jiangshan * worker_attach_to_pool() is in progress.
774cb1ef64STejun Heo *
784cb1ef64STejun Heo * As there can only be one concurrent BH execution context per CPU, a
794cb1ef64STejun Heo * BH pool is per-CPU and always DISASSOCIATED.
80bc2ae0f5STejun Heo */
814cb1ef64STejun Heo POOL_BH = 1 << 0, /* is a BH pool */
824cb1ef64STejun Heo POOL_MANAGER_ACTIVE = 1 << 1, /* being managed */
8324647570STejun Heo POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
841acd92d9STejun Heo POOL_BH_DRAINING = 1 << 3, /* draining after CPU offline */
85e563d0a7STejun Heo };
86db7bccf4STejun Heo
87e563d0a7STejun Heo enum worker_flags {
88c8e55f36STejun Heo /* worker flags */
89c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */
90c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */
91e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */
92fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
93f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */
94a9ab775bSTejun Heo WORKER_REBOUND = 1 << 8, /* worker was rebound */
95e22bee78STejun Heo
96a9ab775bSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE |
97a9ab775bSTejun Heo WORKER_UNBOUND | WORKER_REBOUND,
98e563d0a7STejun Heo };
99db7bccf4STejun Heo
100c5f5b942STejun Heo enum work_cancel_flags {
101c5f5b942STejun Heo WORK_CANCEL_DELAYED = 1 << 0, /* canceling a delayed_work */
10286898fa6STejun Heo WORK_CANCEL_DISABLE = 1 << 1, /* canceling to disable */
103c5f5b942STejun Heo };
104c5f5b942STejun Heo
105e563d0a7STejun Heo enum wq_internal_consts {
106e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
1074ce62e9eSTejun Heo
10829c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */
109c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
110db7bccf4STejun Heo
111e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
112e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
113e22bee78STejun Heo
1143233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
1153233cdbdSTejun Heo /* call for help after 10ms
1163233cdbdSTejun Heo (min two ticks) */
117e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
118e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */
1191da177e4SLinus Torvalds
1201da177e4SLinus Torvalds /*
121e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by
1228698a745SDongsheng Yang * all cpus. Give MIN_NICE.
123e22bee78STejun Heo */
1248698a745SDongsheng Yang RESCUER_NICE_LEVEL = MIN_NICE,
1258698a745SDongsheng Yang HIGHPRI_NICE_LEVEL = MIN_NICE,
126ecf6881fSTejun Heo
12731c89007SAudra Mitchell WQ_NAME_LEN = 32,
1282a1b02bcSTejun Heo WORKER_ID_LEN = 10 + WQ_NAME_LEN, /* "kworker/R-" + WQ_NAME_LEN */
129c8e55f36STejun Heo };
130c8e55f36STejun Heo
1311da177e4SLinus Torvalds /*
1324cb1ef64STejun Heo * We don't want to trap softirq for too long. See MAX_SOFTIRQ_TIME and
1334cb1ef64STejun Heo * MAX_SOFTIRQ_RESTART in kernel/softirq.c. These are macros because
1344cb1ef64STejun Heo * msecs_to_jiffies() can't be an initializer.
1354cb1ef64STejun Heo */
1364cb1ef64STejun Heo #define BH_WORKER_JIFFIES msecs_to_jiffies(2)
1374cb1ef64STejun Heo #define BH_WORKER_RESTARTS 10
1384cb1ef64STejun Heo
1394cb1ef64STejun Heo /*
1404690c4abSTejun Heo * Structure fields follow one of the following exclusion rules.
1414690c4abSTejun Heo *
142e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for
143e41e704bSTejun Heo * everyone else.
1444690c4abSTejun Heo *
145e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should
146e22bee78STejun Heo * only be modified and accessed from the local cpu.
147e22bee78STejun Heo *
148d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held.
1494690c4abSTejun Heo *
1505797b1c1STejun Heo * LN: pool->lock and wq_node_nr_active->lock protected for writes. Either for
1515797b1c1STejun Heo * reads.
1525797b1c1STejun Heo *
153bdf8b9bfSTejun Heo * K: Only modified by worker while holding pool->lock. Can be safely read by
154bdf8b9bfSTejun Heo * self, while holding pool->lock or from IRQ context if %current is the
155bdf8b9bfSTejun Heo * kworker.
156bdf8b9bfSTejun Heo *
157bdf8b9bfSTejun Heo * S: Only modified by worker self.
158bdf8b9bfSTejun Heo *
1591258fae7STejun Heo * A: wq_pool_attach_mutex protected.
160822d8405STejun Heo *
16168e13a67SLai Jiangshan * PL: wq_pool_mutex protected.
16276af4d93STejun Heo *
16324acfb71SThomas Gleixner * PR: wq_pool_mutex protected for writes. RCU protected for reads.
1645bcab335STejun Heo *
1655b95e1afSLai Jiangshan * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads.
1665b95e1afSLai Jiangshan *
1675b95e1afSLai Jiangshan * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or
16824acfb71SThomas Gleixner * RCU for reads.
1695b95e1afSLai Jiangshan *
1703c25a55dSLai Jiangshan * WQ: wq->mutex protected.
1713c25a55dSLai Jiangshan *
17224acfb71SThomas Gleixner * WR: wq->mutex protected for writes. RCU protected for reads.
1732e109a28STejun Heo *
174a045a272STejun Heo * WO: wq->mutex protected for writes. Updated with WRITE_ONCE() and can be read
175a045a272STejun Heo * with READ_ONCE() without locking.
176a045a272STejun Heo *
1772e109a28STejun Heo * MD: wq_mayday_lock protected.
178cd2440d6SPetr Mladek *
179cd2440d6SPetr Mladek * WD: Used internally by the watchdog.
1804690c4abSTejun Heo */
1814690c4abSTejun Heo
1822eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */
183c34056a3STejun Heo
184bd7bdd43STejun Heo struct worker_pool {
185a9b8a985SSebastian Andrzej Siewior raw_spinlock_t lock; /* the pool lock */
186d84ff051STejun Heo int cpu; /* I: the associated cpu */
187f3f90ad4STejun Heo int node; /* I: the associated node ID */
1889daf9e67STejun Heo int id; /* I: pool ID */
189bc8b50c2STejun Heo unsigned int flags; /* L: flags */
190bd7bdd43STejun Heo
19182607adcSTejun Heo unsigned long watchdog_ts; /* L: watchdog timestamp */
192cd2440d6SPetr Mladek bool cpu_stall; /* WD: stalled cpu bound pool */
19382607adcSTejun Heo
194bc35f7efSLai Jiangshan /*
195bc35f7efSLai Jiangshan * The counter is incremented in a process context on the associated CPU
196bc35f7efSLai Jiangshan * w/ preemption disabled, and decremented or reset in the same context
197bc35f7efSLai Jiangshan * but w/ pool->lock held. The readers grab pool->lock and are
198bc35f7efSLai Jiangshan * guaranteed to see if the counter reached zero.
199bc35f7efSLai Jiangshan */
200bc35f7efSLai Jiangshan int nr_running;
20184f91c62SLai Jiangshan
202bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */
203ea1abd61SLai Jiangshan
2045826cc8fSLai Jiangshan int nr_workers; /* L: total number of workers */
2055826cc8fSLai Jiangshan int nr_idle; /* L: currently idle workers */
206bd7bdd43STejun Heo
2072c1f1a91SLai Jiangshan struct list_head idle_list; /* L: list of idle workers */
208bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */
2093f959aa3SValentin Schneider struct work_struct idle_cull_work; /* L: worker idle cleanup */
2103f959aa3SValentin Schneider
211bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */
212bd7bdd43STejun Heo
213c5aa87bbSTejun Heo /* a workers is either on busy_hash or idle_list, or the manager */
214c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
215c9e7cf27STejun Heo /* L: hash of busy workers */
216c9e7cf27STejun Heo
2172607d7a6STejun Heo struct worker *manager; /* L: purely informational */
21892f9c5c4SLai Jiangshan struct list_head workers; /* A: attached workers */
219e19e397aSTejun Heo
2207cda9aaeSLai Jiangshan struct ida worker_ida; /* worker IDs for task name */
221e19e397aSTejun Heo
2227a4e344cSTejun Heo struct workqueue_attrs *attrs; /* I: worker attributes */
22368e13a67SLai Jiangshan struct hlist_node hash_node; /* PL: unbound_pool_hash node */
22468e13a67SLai Jiangshan int refcnt; /* PL: refcnt for unbound pools */
2257a4e344cSTejun Heo
226e19e397aSTejun Heo /*
22724acfb71SThomas Gleixner * Destruction of pool is RCU protected to allow dereferences
22829c91e99STejun Heo * from get_work_pool().
22929c91e99STejun Heo */
23029c91e99STejun Heo struct rcu_head rcu;
23184f91c62SLai Jiangshan };
2328b03ae3cSTejun Heo
2338b03ae3cSTejun Heo /*
234725e8ec5STejun Heo * Per-pool_workqueue statistics. These can be monitored using
235725e8ec5STejun Heo * tools/workqueue/wq_monitor.py.
236725e8ec5STejun Heo */
237725e8ec5STejun Heo enum pool_workqueue_stats {
238725e8ec5STejun Heo PWQ_STAT_STARTED, /* work items started execution */
239725e8ec5STejun Heo PWQ_STAT_COMPLETED, /* work items completed execution */
2408a1dd1e5STejun Heo PWQ_STAT_CPU_TIME, /* total CPU time consumed */
241616db877STejun Heo PWQ_STAT_CPU_INTENSIVE, /* wq_cpu_intensive_thresh_us violations */
242725e8ec5STejun Heo PWQ_STAT_CM_WAKEUP, /* concurrency-management worker wakeups */
2438639ecebSTejun Heo PWQ_STAT_REPATRIATED, /* unbound workers brought back into scope */
244725e8ec5STejun Heo PWQ_STAT_MAYDAY, /* maydays to rescuer */
245725e8ec5STejun Heo PWQ_STAT_RESCUED, /* linked work items executed by rescuer */
246725e8ec5STejun Heo
247725e8ec5STejun Heo PWQ_NR_STATS,
248725e8ec5STejun Heo };
249725e8ec5STejun Heo
250725e8ec5STejun Heo /*
251e9a8e01fSTejun Heo * The per-pool workqueue. While queued, bits below WORK_PWQ_SHIFT
252112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits
253112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the
254112202d9STejun Heo * number of flag bits.
2551da177e4SLinus Torvalds */
256112202d9STejun Heo struct pool_workqueue {
257bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */
2584690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */
25973f53c4aSTejun Heo int work_color; /* L: current color */
26073f53c4aSTejun Heo int flush_color; /* L: flushing color */
2618864b4e5STejun Heo int refcnt; /* L: reference count */
26273f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS];
26373f53c4aSTejun Heo /* L: nr of in_flight works */
2644c065dbcSWaiman Long bool plugged; /* L: execution suspended */
265018f3a13SLai Jiangshan
266018f3a13SLai Jiangshan /*
267018f3a13SLai Jiangshan * nr_active management and WORK_STRUCT_INACTIVE:
268018f3a13SLai Jiangshan *
269018f3a13SLai Jiangshan * When pwq->nr_active >= max_active, new work item is queued to
270018f3a13SLai Jiangshan * pwq->inactive_works instead of pool->worklist and marked with
271018f3a13SLai Jiangshan * WORK_STRUCT_INACTIVE.
272018f3a13SLai Jiangshan *
2735797b1c1STejun Heo * All work items marked with WORK_STRUCT_INACTIVE do not participate in
2745797b1c1STejun Heo * nr_active and all work items in pwq->inactive_works are marked with
2755797b1c1STejun Heo * WORK_STRUCT_INACTIVE. But not all WORK_STRUCT_INACTIVE work items are
2765797b1c1STejun Heo * in pwq->inactive_works. Some of them are ready to run in
2775797b1c1STejun Heo * pool->worklist or worker->scheduled. Those work itmes are only struct
2785797b1c1STejun Heo * wq_barrier which is used for flush_work() and should not participate
2795797b1c1STejun Heo * in nr_active. For non-barrier work item, it is marked with
2805797b1c1STejun Heo * WORK_STRUCT_INACTIVE iff it is in pwq->inactive_works.
281018f3a13SLai Jiangshan */
2821e19ffc6STejun Heo int nr_active; /* L: nr of active works */
283f97a4a1aSLai Jiangshan struct list_head inactive_works; /* L: inactive works */
2845797b1c1STejun Heo struct list_head pending_node; /* LN: node on wq_node_nr_active->pending_pwqs */
2853c25a55dSLai Jiangshan struct list_head pwqs_node; /* WR: node on wq->pwqs */
2862e109a28STejun Heo struct list_head mayday_node; /* MD: node on wq->maydays */
2878864b4e5STejun Heo
288725e8ec5STejun Heo u64 stats[PWQ_NR_STATS];
289725e8ec5STejun Heo
2908864b4e5STejun Heo /*
291967b494eSTejun Heo * Release of unbound pwq is punted to a kthread_worker. See put_pwq()
292687a9aa5STejun Heo * and pwq_release_workfn() for details. pool_workqueue itself is also
293687a9aa5STejun Heo * RCU protected so that the first pwq can be determined without
294967b494eSTejun Heo * grabbing wq->mutex.
2958864b4e5STejun Heo */
296687a9aa5STejun Heo struct kthread_work release_work;
2978864b4e5STejun Heo struct rcu_head rcu;
298e9a8e01fSTejun Heo } __aligned(1 << WORK_STRUCT_PWQ_SHIFT);
2991da177e4SLinus Torvalds
3001da177e4SLinus Torvalds /*
30173f53c4aSTejun Heo * Structure used to wait for workqueue flush.
30273f53c4aSTejun Heo */
30373f53c4aSTejun Heo struct wq_flusher {
3043c25a55dSLai Jiangshan struct list_head list; /* WQ: list of flushers */
3053c25a55dSLai Jiangshan int flush_color; /* WQ: flush color waiting for */
30673f53c4aSTejun Heo struct completion done; /* flush completion */
30773f53c4aSTejun Heo };
3081da177e4SLinus Torvalds
309226223abSTejun Heo struct wq_device;
310226223abSTejun Heo
31173f53c4aSTejun Heo /*
31291ccc6e7STejun Heo * Unlike in a per-cpu workqueue where max_active limits its concurrency level
31391ccc6e7STejun Heo * on each CPU, in an unbound workqueue, max_active applies to the whole system.
31491ccc6e7STejun Heo * As sharing a single nr_active across multiple sockets can be very expensive,
31591ccc6e7STejun Heo * the counting and enforcement is per NUMA node.
3165797b1c1STejun Heo *
3175797b1c1STejun Heo * The following struct is used to enforce per-node max_active. When a pwq wants
3185797b1c1STejun Heo * to start executing a work item, it should increment ->nr using
3195797b1c1STejun Heo * tryinc_node_nr_active(). If acquisition fails due to ->nr already being over
3205797b1c1STejun Heo * ->max, the pwq is queued on ->pending_pwqs. As in-flight work items finish
3215797b1c1STejun Heo * and decrement ->nr, node_activate_pending_pwq() activates the pending pwqs in
3225797b1c1STejun Heo * round-robin order.
32391ccc6e7STejun Heo */
32491ccc6e7STejun Heo struct wq_node_nr_active {
3255797b1c1STejun Heo int max; /* per-node max_active */
3265797b1c1STejun Heo atomic_t nr; /* per-node nr_active */
3275797b1c1STejun Heo raw_spinlock_t lock; /* nests inside pool locks */
3285797b1c1STejun Heo struct list_head pending_pwqs; /* LN: pwqs with inactive works */
32991ccc6e7STejun Heo };
33091ccc6e7STejun Heo
33191ccc6e7STejun Heo /*
332c5aa87bbSTejun Heo * The externally visible workqueue. It relays the issued work items to
333c5aa87bbSTejun Heo * the appropriate worker_pool through its pool_workqueues.
3341da177e4SLinus Torvalds */
3351da177e4SLinus Torvalds struct workqueue_struct {
3363c25a55dSLai Jiangshan struct list_head pwqs; /* WR: all pwqs of this wq */
337e2dca7adSTejun Heo struct list_head list; /* PR: list of all workqueues */
33873f53c4aSTejun Heo
3393c25a55dSLai Jiangshan struct mutex mutex; /* protects this wq */
3403c25a55dSLai Jiangshan int work_color; /* WQ: current work color */
3413c25a55dSLai Jiangshan int flush_color; /* WQ: current flush color */
342112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */
3433c25a55dSLai Jiangshan struct wq_flusher *first_flusher; /* WQ: first flusher */
3443c25a55dSLai Jiangshan struct list_head flusher_queue; /* WQ: flush waiters */
3453c25a55dSLai Jiangshan struct list_head flusher_overflow; /* WQ: flush overflow list */
34673f53c4aSTejun Heo
3472e109a28STejun Heo struct list_head maydays; /* MD: pwqs requesting rescue */
34830ae2fc0STejun Heo struct worker *rescuer; /* MD: rescue worker */
349e22bee78STejun Heo
35087fc741eSLai Jiangshan int nr_drainers; /* WQ: drain in progress */
3515797b1c1STejun Heo
3525797b1c1STejun Heo /* See alloc_workqueue() function comment for info on min/max_active */
353a045a272STejun Heo int max_active; /* WO: max active works */
3545797b1c1STejun Heo int min_active; /* WO: min active works */
355a045a272STejun Heo int saved_max_active; /* WQ: saved max_active */
3565797b1c1STejun Heo int saved_min_active; /* WQ: saved min_active */
357226223abSTejun Heo
3585b95e1afSLai Jiangshan struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */
3599f66cff2STejun Heo struct pool_workqueue __rcu *dfl_pwq; /* PW: only for unbound wqs */
3606029a918STejun Heo
361226223abSTejun Heo #ifdef CONFIG_SYSFS
362226223abSTejun Heo struct wq_device *wq_dev; /* I: for sysfs interface */
363226223abSTejun Heo #endif
3644e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
365669de8bdSBart Van Assche char *lock_name;
366669de8bdSBart Van Assche struct lock_class_key key;
3674f022f43SMatthew Brost struct lockdep_map __lockdep_map;
3684f022f43SMatthew Brost struct lockdep_map *lockdep_map;
3694e6045f1SJohannes Berg #endif
370ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */
3712728fd2fSTejun Heo
372e2dca7adSTejun Heo /*
37324acfb71SThomas Gleixner * Destruction of workqueue_struct is RCU protected to allow walking
37424acfb71SThomas Gleixner * the workqueues list without grabbing wq_pool_mutex.
375e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq.
376e2dca7adSTejun Heo */
377e2dca7adSTejun Heo struct rcu_head rcu;
378e2dca7adSTejun Heo
3792728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */
3802728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */
381c4c8f369SUros Bizjak struct pool_workqueue __rcu * __percpu *cpu_pwq; /* I: per-cpu pwqs */
38291ccc6e7STejun Heo struct wq_node_nr_active *node_nr_active[]; /* I: per-node nr_active */
3831da177e4SLinus Torvalds };
3841da177e4SLinus Torvalds
38584193c07STejun Heo /*
38684193c07STejun Heo * Each pod type describes how CPUs should be grouped for unbound workqueues.
38784193c07STejun Heo * See the comment above workqueue_attrs->affn_scope.
38884193c07STejun Heo */
38984193c07STejun Heo struct wq_pod_type {
39084193c07STejun Heo int nr_pods; /* number of pods */
39184193c07STejun Heo cpumask_var_t *pod_cpus; /* pod -> cpus */
39284193c07STejun Heo int *pod_node; /* pod -> node */
39384193c07STejun Heo int *cpu_pod; /* cpu -> pod */
39484193c07STejun Heo };
39584193c07STejun Heo
3961211f3b2STejun Heo struct work_offq_data {
3971211f3b2STejun Heo u32 pool_id;
39886898fa6STejun Heo u32 disable;
3991211f3b2STejun Heo u32 flags;
4001211f3b2STejun Heo };
4011211f3b2STejun Heo
40263c5484eSTejun Heo static const char *wq_affn_names[WQ_AFFN_NR_TYPES] = {
403523a301eSTejun Heo [WQ_AFFN_DFL] = "default",
40463c5484eSTejun Heo [WQ_AFFN_CPU] = "cpu",
40563c5484eSTejun Heo [WQ_AFFN_SMT] = "smt",
40663c5484eSTejun Heo [WQ_AFFN_CACHE] = "cache",
40763c5484eSTejun Heo [WQ_AFFN_NUMA] = "numa",
40863c5484eSTejun Heo [WQ_AFFN_SYSTEM] = "system",
40963c5484eSTejun Heo };
410bce90380STejun Heo
411616db877STejun Heo /*
412616db877STejun Heo * Per-cpu work items which run for longer than the following threshold are
413616db877STejun Heo * automatically considered CPU intensive and excluded from concurrency
414616db877STejun Heo * management to prevent them from noticeably delaying other per-cpu work items.
415aa6fde93STejun Heo * ULONG_MAX indicates that the user hasn't overridden it with a boot parameter.
416aa6fde93STejun Heo * The actual value is initialized in wq_cpu_intensive_thresh_init().
417616db877STejun Heo */
418aa6fde93STejun Heo static unsigned long wq_cpu_intensive_thresh_us = ULONG_MAX;
419616db877STejun Heo module_param_named(cpu_intensive_thresh_us, wq_cpu_intensive_thresh_us, ulong, 0644);
420ccdec921SXuewen Yan #ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT
421ccdec921SXuewen Yan static unsigned int wq_cpu_intensive_warning_thresh = 4;
422ccdec921SXuewen Yan module_param_named(cpu_intensive_warning_thresh, wq_cpu_intensive_warning_thresh, uint, 0644);
423ccdec921SXuewen Yan #endif
424616db877STejun Heo
425cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */
426552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT);
427cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444);
428cee22a15SViresh Kumar
429863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */
430c7a40c49STejun Heo static bool wq_topo_initialized __read_mostly = false;
431c7a40c49STejun Heo
432c7a40c49STejun Heo static struct kmem_cache *pwq_cache;
433c7a40c49STejun Heo
434c7a40c49STejun Heo static struct wq_pod_type wq_pod_types[WQ_AFFN_NR_TYPES];
435c7a40c49STejun Heo static enum wq_affn_scope wq_affn_dfl = WQ_AFFN_CACHE;
4363347fa09STejun Heo
437fef59c9cSTejun Heo /* buf for wq_update_unbound_pod_attrs(), protected by CPU hotplug exclusion */
438b2b1f933SLai Jiangshan static struct workqueue_attrs *unbound_wq_update_pwq_attrs_buf;
4394c16bd32STejun Heo
44068e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */
4411258fae7STejun Heo static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */
442a9b8a985SSebastian Andrzej Siewior static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */
443d8bb65abSSebastian Andrzej Siewior /* wait for manager to go away */
444d8bb65abSSebastian Andrzej Siewior static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait);
4455bcab335STejun Heo
446e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */
44768e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */
4487d19c5ceSTejun Heo
4498d84baf7SLai Jiangshan /* PL: mirror the cpu_online_mask excluding the CPU in the midst of hotplugging */
4508d84baf7SLai Jiangshan static cpumask_var_t wq_online_cpumask;
4518d84baf7SLai Jiangshan
45299c621efSLai Jiangshan /* PL&A: allowable cpus for unbound wqs and work items */
453ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask;
454ef557180SMike Galbraith
455fe28f631SWaiman Long /* PL: user requested unbound cpumask via sysfs */
456fe28f631SWaiman Long static cpumask_var_t wq_requested_unbound_cpumask;
457fe28f631SWaiman Long
458fe28f631SWaiman Long /* PL: isolated cpumask to be excluded from unbound cpumask */
459fe28f631SWaiman Long static cpumask_var_t wq_isolated_cpumask;
460fe28f631SWaiman Long
461ace3c549Stiozhang /* for further constrain wq_unbound_cpumask by cmdline parameter*/
462ace3c549Stiozhang static struct cpumask wq_cmdline_cpumask __initdata;
463ace3c549Stiozhang
464ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */
465ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last);
466b05a7928SFrederic Weisbecker
467f303fccbSTejun Heo /*
468f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The
469f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items
470f303fccbSTejun Heo * to uncover usages which depend on it.
471f303fccbSTejun Heo */
472f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU
473f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true;
474f303fccbSTejun Heo #else
475f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false;
476f303fccbSTejun Heo #endif
477f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644);
478f303fccbSTejun Heo
4792f34d733STejun Heo /* to raise softirq for the BH worker pools on other CPUs */
480b4722b85SBaoquan He static DEFINE_PER_CPU_SHARED_ALIGNED(struct irq_work [NR_STD_WORKER_POOLS], bh_pool_irq_works);
4812f34d733STejun Heo
4824cb1ef64STejun Heo /* the BH worker pools */
483b4722b85SBaoquan He static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], bh_worker_pools);
4844cb1ef64STejun Heo
4857d19c5ceSTejun Heo /* the per-cpu worker pools */
486b4722b85SBaoquan He static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools);
4877d19c5ceSTejun Heo
48868e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */
4897d19c5ceSTejun Heo
49068e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */
49129c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
49229c91e99STejun Heo
493c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */
49429c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
49529c91e99STejun Heo
4968a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */
4978a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS];
4988a2b7538STejun Heo
499967b494eSTejun Heo /*
500967b494eSTejun Heo * I: kthread_worker to release pwq's. pwq release needs to be bounced to a
501967b494eSTejun Heo * process context while holding a pool lock. Bounce to a dedicated kthread
502967b494eSTejun Heo * worker to avoid A-A deadlocks.
503967b494eSTejun Heo */
50468279f9cSAlexey Dobriyan static struct kthread_worker *pwq_release_worker __ro_after_init;
505967b494eSTejun Heo
50668279f9cSAlexey Dobriyan struct workqueue_struct *system_wq __ro_after_init;
507ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq);
50868279f9cSAlexey Dobriyan struct workqueue_struct *system_highpri_wq __ro_after_init;
5091aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq);
51068279f9cSAlexey Dobriyan struct workqueue_struct *system_long_wq __ro_after_init;
511d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq);
51268279f9cSAlexey Dobriyan struct workqueue_struct *system_unbound_wq __ro_after_init;
513f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq);
51468279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_wq __ro_after_init;
51524d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq);
51668279f9cSAlexey Dobriyan struct workqueue_struct *system_power_efficient_wq __ro_after_init;
5170668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq);
51868279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_power_efficient_wq __ro_after_init;
5190668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
5204cb1ef64STejun Heo struct workqueue_struct *system_bh_wq;
5214cb1ef64STejun Heo EXPORT_SYMBOL_GPL(system_bh_wq);
5224cb1ef64STejun Heo struct workqueue_struct *system_bh_highpri_wq;
5234cb1ef64STejun Heo EXPORT_SYMBOL_GPL(system_bh_highpri_wq);
524d320c038STejun Heo
5257d19c5ceSTejun Heo static int worker_thread(void *__worker);
5266ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
527c29eb853STejun Heo static void show_pwq(struct pool_workqueue *pwq);
52855df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool);
5297d19c5ceSTejun Heo
53097bd2347STejun Heo #define CREATE_TRACE_POINTS
53197bd2347STejun Heo #include <trace/events/workqueue.h>
53297bd2347STejun Heo
53368e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \
534d355001fSTejun Heo RCU_LOCKDEP_WARN(!rcu_read_lock_any_held() && \
535f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \
53624acfb71SThomas Gleixner "RCU or wq_pool_mutex should be held")
5375bcab335STejun Heo
5385b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \
539d355001fSTejun Heo RCU_LOCKDEP_WARN(!rcu_read_lock_any_held() && \
540f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \
541f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \
54224acfb71SThomas Gleixner "RCU, wq->mutex or wq_pool_mutex should be held")
5435b95e1afSLai Jiangshan
5444cb1ef64STejun Heo #define for_each_bh_worker_pool(pool, cpu) \
5454cb1ef64STejun Heo for ((pool) = &per_cpu(bh_worker_pools, cpu)[0]; \
5464cb1ef64STejun Heo (pool) < &per_cpu(bh_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
5474cb1ef64STejun Heo (pool)++)
5484cb1ef64STejun Heo
549f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \
550f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
551f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
5527a62c2c8STejun Heo (pool)++)
5534ce62e9eSTejun Heo
55449e3cf44STejun Heo /**
55517116969STejun Heo * for_each_pool - iterate through all worker_pools in the system
55617116969STejun Heo * @pool: iteration cursor
557611c92a0STejun Heo * @pi: integer used for iteration
558fa1b54e6STejun Heo *
55924acfb71SThomas Gleixner * This must be called either with wq_pool_mutex held or RCU read
56068e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the
56168e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online.
562fa1b54e6STejun Heo *
563fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be
564fa1b54e6STejun Heo * ignored.
56517116969STejun Heo */
566611c92a0STejun Heo #define for_each_pool(pool, pi) \
567611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \
56868e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \
569fa1b54e6STejun Heo else
57017116969STejun Heo
57117116969STejun Heo /**
572822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool
573822d8405STejun Heo * @worker: iteration cursor
574822d8405STejun Heo * @pool: worker_pool to iterate workers of
575822d8405STejun Heo *
5761258fae7STejun Heo * This must be called with wq_pool_attach_mutex.
577822d8405STejun Heo *
578822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be
579822d8405STejun Heo * ignored.
580822d8405STejun Heo */
581da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \
582da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \
5831258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \
584822d8405STejun Heo else
585822d8405STejun Heo
586822d8405STejun Heo /**
58749e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
58849e3cf44STejun Heo * @pwq: iteration cursor
58949e3cf44STejun Heo * @wq: the target workqueue
59076af4d93STejun Heo *
59124acfb71SThomas Gleixner * This must be called either with wq->mutex held or RCU read locked.
592794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is
593794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online.
59476af4d93STejun Heo *
59576af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be
59676af4d93STejun Heo * ignored.
59749e3cf44STejun Heo */
59849e3cf44STejun Heo #define for_each_pwq(pwq, wq) \
59949e9d1a9SSebastian Andrzej Siewior list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \
6005a644662SJoel Fernandes (Google) lockdep_is_held(&(wq->mutex)))
601f3421797STejun Heo
602dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK
603dc186ad7SThomas Gleixner
604f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr;
605dc186ad7SThomas Gleixner
work_debug_hint(void * addr)60699777288SStanislaw Gruszka static void *work_debug_hint(void *addr)
60799777288SStanislaw Gruszka {
60899777288SStanislaw Gruszka return ((struct work_struct *) addr)->func;
60999777288SStanislaw Gruszka }
61099777288SStanislaw Gruszka
work_is_static_object(void * addr)611b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr)
612b9fdac7fSDu, Changbin {
613b9fdac7fSDu, Changbin struct work_struct *work = addr;
614b9fdac7fSDu, Changbin
615b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work));
616b9fdac7fSDu, Changbin }
617b9fdac7fSDu, Changbin
618dc186ad7SThomas Gleixner /*
619dc186ad7SThomas Gleixner * fixup_init is called when:
620dc186ad7SThomas Gleixner * - an active object is initialized
621dc186ad7SThomas Gleixner */
work_fixup_init(void * addr,enum debug_obj_state state)62202a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state)
623dc186ad7SThomas Gleixner {
624dc186ad7SThomas Gleixner struct work_struct *work = addr;
625dc186ad7SThomas Gleixner
626dc186ad7SThomas Gleixner switch (state) {
627dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE:
628dc186ad7SThomas Gleixner cancel_work_sync(work);
629dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr);
63002a982a6SDu, Changbin return true;
631dc186ad7SThomas Gleixner default:
63202a982a6SDu, Changbin return false;
633dc186ad7SThomas Gleixner }
634dc186ad7SThomas Gleixner }
635dc186ad7SThomas Gleixner
636dc186ad7SThomas Gleixner /*
637dc186ad7SThomas Gleixner * fixup_free is called when:
638dc186ad7SThomas Gleixner * - an active object is freed
639dc186ad7SThomas Gleixner */
work_fixup_free(void * addr,enum debug_obj_state state)64002a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state)
641dc186ad7SThomas Gleixner {
642dc186ad7SThomas Gleixner struct work_struct *work = addr;
643dc186ad7SThomas Gleixner
644dc186ad7SThomas Gleixner switch (state) {
645dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE:
646dc186ad7SThomas Gleixner cancel_work_sync(work);
647dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr);
64802a982a6SDu, Changbin return true;
649dc186ad7SThomas Gleixner default:
65002a982a6SDu, Changbin return false;
651dc186ad7SThomas Gleixner }
652dc186ad7SThomas Gleixner }
653dc186ad7SThomas Gleixner
654f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr = {
655dc186ad7SThomas Gleixner .name = "work_struct",
65699777288SStanislaw Gruszka .debug_hint = work_debug_hint,
657b9fdac7fSDu, Changbin .is_static_object = work_is_static_object,
658dc186ad7SThomas Gleixner .fixup_init = work_fixup_init,
659dc186ad7SThomas Gleixner .fixup_free = work_fixup_free,
660dc186ad7SThomas Gleixner };
661dc186ad7SThomas Gleixner
debug_work_activate(struct work_struct * work)662dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work)
663dc186ad7SThomas Gleixner {
664dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr);
665dc186ad7SThomas Gleixner }
666dc186ad7SThomas Gleixner
debug_work_deactivate(struct work_struct * work)667dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work)
668dc186ad7SThomas Gleixner {
669dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr);
670dc186ad7SThomas Gleixner }
671dc186ad7SThomas Gleixner
__init_work(struct work_struct * work,int onstack)672dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack)
673dc186ad7SThomas Gleixner {
674dc186ad7SThomas Gleixner if (onstack)
675dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr);
676dc186ad7SThomas Gleixner else
677dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr);
678dc186ad7SThomas Gleixner }
679dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work);
680dc186ad7SThomas Gleixner
destroy_work_on_stack(struct work_struct * work)681dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work)
682dc186ad7SThomas Gleixner {
683dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr);
684dc186ad7SThomas Gleixner }
685dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack);
686dc186ad7SThomas Gleixner
destroy_delayed_work_on_stack(struct delayed_work * work)687ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work)
688ea2e64f2SThomas Gleixner {
689ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer);
690ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr);
691ea2e64f2SThomas Gleixner }
692ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack);
693ea2e64f2SThomas Gleixner
694dc186ad7SThomas Gleixner #else
debug_work_activate(struct work_struct * work)695dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { }
debug_work_deactivate(struct work_struct * work)696dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { }
697dc186ad7SThomas Gleixner #endif
698dc186ad7SThomas Gleixner
6994e8b22bdSLi Bin /**
70067dc8325SCai Huoqing * worker_pool_assign_id - allocate ID and assign it to @pool
7014e8b22bdSLi Bin * @pool: the pool pointer of interest
7024e8b22bdSLi Bin *
7034e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned
7044e8b22bdSLi Bin * successfully, -errno on failure.
7054e8b22bdSLi Bin */
worker_pool_assign_id(struct worker_pool * pool)7069daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool)
7079daf9e67STejun Heo {
7089daf9e67STejun Heo int ret;
7099daf9e67STejun Heo
71068e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex);
7115bcab335STejun Heo
7124e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE,
7134e8b22bdSLi Bin GFP_KERNEL);
714229641a6STejun Heo if (ret >= 0) {
715e68035fbSTejun Heo pool->id = ret;
716229641a6STejun Heo return 0;
717229641a6STejun Heo }
7189daf9e67STejun Heo return ret;
7199daf9e67STejun Heo }
7209daf9e67STejun Heo
7219f66cff2STejun Heo static struct pool_workqueue __rcu **
unbound_pwq_slot(struct workqueue_struct * wq,int cpu)7229f66cff2STejun Heo unbound_pwq_slot(struct workqueue_struct *wq, int cpu)
7239f66cff2STejun Heo {
7249f66cff2STejun Heo if (cpu >= 0)
7259f66cff2STejun Heo return per_cpu_ptr(wq->cpu_pwq, cpu);
7269f66cff2STejun Heo else
7279f66cff2STejun Heo return &wq->dfl_pwq;
7289f66cff2STejun Heo }
7299f66cff2STejun Heo
7309f66cff2STejun Heo /* @cpu < 0 for dfl_pwq */
unbound_pwq(struct workqueue_struct * wq,int cpu)7319f66cff2STejun Heo static struct pool_workqueue *unbound_pwq(struct workqueue_struct *wq, int cpu)
7329f66cff2STejun Heo {
7339f66cff2STejun Heo return rcu_dereference_check(*unbound_pwq_slot(wq, cpu),
7349f66cff2STejun Heo lockdep_is_held(&wq_pool_mutex) ||
7359f66cff2STejun Heo lockdep_is_held(&wq->mutex));
7369f66cff2STejun Heo }
7379f66cff2STejun Heo
7385797b1c1STejun Heo /**
7395797b1c1STejun Heo * unbound_effective_cpumask - effective cpumask of an unbound workqueue
7405797b1c1STejun Heo * @wq: workqueue of interest
7415797b1c1STejun Heo *
7425797b1c1STejun Heo * @wq->unbound_attrs->cpumask contains the cpumask requested by the user which
7435797b1c1STejun Heo * is masked with wq_unbound_cpumask to determine the effective cpumask. The
7445797b1c1STejun Heo * default pwq is always mapped to the pool with the current effective cpumask.
7455797b1c1STejun Heo */
unbound_effective_cpumask(struct workqueue_struct * wq)7465797b1c1STejun Heo static struct cpumask *unbound_effective_cpumask(struct workqueue_struct *wq)
7475797b1c1STejun Heo {
7485797b1c1STejun Heo return unbound_pwq(wq, -1)->pool->attrs->__pod_cpumask;
7495797b1c1STejun Heo }
7505797b1c1STejun Heo
work_color_to_flags(int color)75173f53c4aSTejun Heo static unsigned int work_color_to_flags(int color)
75273f53c4aSTejun Heo {
75373f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT;
75473f53c4aSTejun Heo }
75573f53c4aSTejun Heo
get_work_color(unsigned long work_data)756c4560c2cSLai Jiangshan static int get_work_color(unsigned long work_data)
75773f53c4aSTejun Heo {
758c4560c2cSLai Jiangshan return (work_data >> WORK_STRUCT_COLOR_SHIFT) &
75973f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1);
76073f53c4aSTejun Heo }
76173f53c4aSTejun Heo
work_next_color(int color)76273f53c4aSTejun Heo static int work_next_color(int color)
76373f53c4aSTejun Heo {
76473f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS;
765a848e3b6SOleg Nesterov }
766a848e3b6SOleg Nesterov
pool_offq_flags(struct worker_pool * pool)767456a78eeSTejun Heo static unsigned long pool_offq_flags(struct worker_pool *pool)
768456a78eeSTejun Heo {
769456a78eeSTejun Heo return (pool->flags & POOL_BH) ? WORK_OFFQ_BH : 0;
770456a78eeSTejun Heo }
771456a78eeSTejun Heo
7724594bf15SDavid Howells /*
773112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
774112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag
7757c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID.
7767a22ad75STejun Heo *
777afe928c1STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending() and mark_work_canceling()
778afe928c1STejun Heo * can be used to set the pwq, pool or clear work->data. These functions should
779afe928c1STejun Heo * only be called while the work is owned - ie. while the PENDING bit is set.
7807a22ad75STejun Heo *
781112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
7827c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been
783112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is
7847c3eed5cSTejun Heo * available only while the work item is queued.
7854594bf15SDavid Howells */
set_work_data(struct work_struct * work,unsigned long data)786bccdc1faSTejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data)
7877a22ad75STejun Heo {
7886183c009STejun Heo WARN_ON_ONCE(!work_pending(work));
789bccdc1faSTejun Heo atomic_long_set(&work->data, data | work_static(work));
7907a22ad75STejun Heo }
7917a22ad75STejun Heo
set_work_pwq(struct work_struct * work,struct pool_workqueue * pwq,unsigned long flags)792112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
793bccdc1faSTejun Heo unsigned long flags)
794365970a1SDavid Howells {
795bccdc1faSTejun Heo set_work_data(work, (unsigned long)pwq | WORK_STRUCT_PENDING |
796bccdc1faSTejun Heo WORK_STRUCT_PWQ | flags);
797365970a1SDavid Howells }
798365970a1SDavid Howells
set_work_pool_and_keep_pending(struct work_struct * work,int pool_id,unsigned long flags)7994468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work,
800bccdc1faSTejun Heo int pool_id, unsigned long flags)
8014468a00fSLai Jiangshan {
802bccdc1faSTejun Heo set_work_data(work, ((unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT) |
803bccdc1faSTejun Heo WORK_STRUCT_PENDING | flags);
8044468a00fSLai Jiangshan }
8054468a00fSLai Jiangshan
set_work_pool_and_clear_pending(struct work_struct * work,int pool_id,unsigned long flags)8067c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work,
807bccdc1faSTejun Heo int pool_id, unsigned long flags)
8084d707b9fSOleg Nesterov {
80923657bb1STejun Heo /*
81023657bb1STejun Heo * The following wmb is paired with the implied mb in
81123657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made
81223657bb1STejun Heo * here are visible to and precede any updates by the next PENDING
81323657bb1STejun Heo * owner.
81423657bb1STejun Heo */
81523657bb1STejun Heo smp_wmb();
816bccdc1faSTejun Heo set_work_data(work, ((unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT) |
817bccdc1faSTejun Heo flags);
818346c09f8SRoman Pen /*
819346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit
820346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from
821346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible
8228bdc6201SLiu Song * reordering can lead to a missed execution on attempt to queue
823346c09f8SRoman Pen * the same @work. E.g. consider this case:
824346c09f8SRoman Pen *
825346c09f8SRoman Pen * CPU#0 CPU#1
826346c09f8SRoman Pen * ---------------------------- --------------------------------
827346c09f8SRoman Pen *
828346c09f8SRoman Pen * 1 STORE event_indicated
829346c09f8SRoman Pen * 2 queue_work_on() {
830346c09f8SRoman Pen * 3 test_and_set_bit(PENDING)
831346c09f8SRoman Pen * 4 } set_..._and_clear_pending() {
832346c09f8SRoman Pen * 5 set_work_data() # clear bit
833346c09f8SRoman Pen * 6 smp_mb()
834346c09f8SRoman Pen * 7 work->current_func() {
835346c09f8SRoman Pen * 8 LOAD event_indicated
836346c09f8SRoman Pen * }
837346c09f8SRoman Pen *
838346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can
839346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens,
840346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of
841346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually
842346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see
843346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed
844346c09f8SRoman Pen * before actual STORE.
845346c09f8SRoman Pen */
846346c09f8SRoman Pen smp_mb();
8474d707b9fSOleg Nesterov }
8484d707b9fSOleg Nesterov
work_struct_pwq(unsigned long data)849afa4bb77SLinus Torvalds static inline struct pool_workqueue *work_struct_pwq(unsigned long data)
850afa4bb77SLinus Torvalds {
851e9a8e01fSTejun Heo return (struct pool_workqueue *)(data & WORK_STRUCT_PWQ_MASK);
852afa4bb77SLinus Torvalds }
853afa4bb77SLinus Torvalds
get_work_pwq(struct work_struct * work)854112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work)
8557a22ad75STejun Heo {
856e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data);
8577a22ad75STejun Heo
858112202d9STejun Heo if (data & WORK_STRUCT_PWQ)
859afa4bb77SLinus Torvalds return work_struct_pwq(data);
860e120153dSTejun Heo else
861e120153dSTejun Heo return NULL;
8627a22ad75STejun Heo }
8637a22ad75STejun Heo
8647c3eed5cSTejun Heo /**
8657c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with
8667c3eed5cSTejun Heo * @work: the work item of interest
8677c3eed5cSTejun Heo *
86868e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read
86924acfb71SThomas Gleixner * access under RCU read lock. As such, this function should be
87024acfb71SThomas Gleixner * called under wq_pool_mutex or inside of a rcu_read_lock() region.
871fa1b54e6STejun Heo *
872fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above
873fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used
874fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the
875fa1b54e6STejun Heo * returned pool is and stays online.
876d185af30SYacine Belkadi *
877d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none.
8787c3eed5cSTejun Heo */
get_work_pool(struct work_struct * work)8797c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work)
8807a22ad75STejun Heo {
881e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data);
8827c3eed5cSTejun Heo int pool_id;
8837a22ad75STejun Heo
88468e13a67SLai Jiangshan assert_rcu_or_pool_mutex();
885fa1b54e6STejun Heo
886112202d9STejun Heo if (data & WORK_STRUCT_PWQ)
887afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool;
8887a22ad75STejun Heo
8897c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT;
8907c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE)
8917a22ad75STejun Heo return NULL;
8927a22ad75STejun Heo
893fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id);
8947c3eed5cSTejun Heo }
8957c3eed5cSTejun Heo
shift_and_mask(unsigned long v,u32 shift,u32 bits)8961211f3b2STejun Heo static unsigned long shift_and_mask(unsigned long v, u32 shift, u32 bits)
8977c3eed5cSTejun Heo {
89838f7e145SWill Deacon return (v >> shift) & ((1U << bits) - 1);
8997c3eed5cSTejun Heo }
9007c3eed5cSTejun Heo
work_offqd_unpack(struct work_offq_data * offqd,unsigned long data)9011211f3b2STejun Heo static void work_offqd_unpack(struct work_offq_data *offqd, unsigned long data)
902bbb68dfaSTejun Heo {
9031211f3b2STejun Heo WARN_ON_ONCE(data & WORK_STRUCT_PWQ);
904bbb68dfaSTejun Heo
9051211f3b2STejun Heo offqd->pool_id = shift_and_mask(data, WORK_OFFQ_POOL_SHIFT,
9061211f3b2STejun Heo WORK_OFFQ_POOL_BITS);
90786898fa6STejun Heo offqd->disable = shift_and_mask(data, WORK_OFFQ_DISABLE_SHIFT,
90886898fa6STejun Heo WORK_OFFQ_DISABLE_BITS);
9091211f3b2STejun Heo offqd->flags = data & WORK_OFFQ_FLAG_MASK;
910bbb68dfaSTejun Heo }
911bbb68dfaSTejun Heo
work_offqd_pack_flags(struct work_offq_data * offqd)9121211f3b2STejun Heo static unsigned long work_offqd_pack_flags(struct work_offq_data *offqd)
913bbb68dfaSTejun Heo {
91486898fa6STejun Heo return ((unsigned long)offqd->disable << WORK_OFFQ_DISABLE_SHIFT) |
91586898fa6STejun Heo ((unsigned long)offqd->flags);
916bbb68dfaSTejun Heo }
917bbb68dfaSTejun Heo
918e22bee78STejun Heo /*
9193270476aSTejun Heo * Policy functions. These define the policies on how the global worker
9203270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that
921d565ed63STejun Heo * they're being called with pool->lock held.
922e22bee78STejun Heo */
923e22bee78STejun Heo
924e22bee78STejun Heo /*
925e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently
926e22bee78STejun Heo * running workers.
927974271c4STejun Heo *
928974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this
929706026c2STejun Heo * function will always return %true for unbound pools as long as the
930974271c4STejun Heo * worklist isn't empty.
931e22bee78STejun Heo */
need_more_worker(struct worker_pool * pool)93263d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool)
933e22bee78STejun Heo {
9340219a352STejun Heo return !list_empty(&pool->worklist) && !pool->nr_running;
935e22bee78STejun Heo }
936e22bee78STejun Heo
937e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */
may_start_working(struct worker_pool * pool)93863d95a91STejun Heo static bool may_start_working(struct worker_pool *pool)
939e22bee78STejun Heo {
94063d95a91STejun Heo return pool->nr_idle;
941e22bee78STejun Heo }
942e22bee78STejun Heo
943e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */
keep_working(struct worker_pool * pool)94463d95a91STejun Heo static bool keep_working(struct worker_pool *pool)
945e22bee78STejun Heo {
946bc35f7efSLai Jiangshan return !list_empty(&pool->worklist) && (pool->nr_running <= 1);
947e22bee78STejun Heo }
948e22bee78STejun Heo
949e22bee78STejun Heo /* Do we need a new worker? Called from manager. */
need_to_create_worker(struct worker_pool * pool)95063d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool)
951e22bee78STejun Heo {
95263d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool);
953e22bee78STejun Heo }
954e22bee78STejun Heo
955e22bee78STejun Heo /* Do we have too many workers and should some go away? */
too_many_workers(struct worker_pool * pool)95663d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool)
957e22bee78STejun Heo {
958692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE;
95963d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
96063d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle;
961e22bee78STejun Heo
962e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
963e22bee78STejun Heo }
964e22bee78STejun Heo
9654690c4abSTejun Heo /**
966e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly
967cb444766STejun Heo * @worker: self
968d302f017STejun Heo * @flags: flags to set
969d302f017STejun Heo *
970228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly.
971d302f017STejun Heo */
worker_set_flags(struct worker * worker,unsigned int flags)972228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags)
973d302f017STejun Heo {
974bd7bdd43STejun Heo struct worker_pool *pool = worker->pool;
975e22bee78STejun Heo
976bc8b50c2STejun Heo lockdep_assert_held(&pool->lock);
977cb444766STejun Heo
978228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */
979e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) &&
980e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) {
981bc35f7efSLai Jiangshan pool->nr_running--;
982e22bee78STejun Heo }
983e22bee78STejun Heo
984d302f017STejun Heo worker->flags |= flags;
985d302f017STejun Heo }
986d302f017STejun Heo
987d302f017STejun Heo /**
988e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly
989cb444766STejun Heo * @worker: self
990d302f017STejun Heo * @flags: flags to clear
991d302f017STejun Heo *
992e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly.
993d302f017STejun Heo */
worker_clr_flags(struct worker * worker,unsigned int flags)994d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
995d302f017STejun Heo {
99663d95a91STejun Heo struct worker_pool *pool = worker->pool;
997e22bee78STejun Heo unsigned int oflags = worker->flags;
998e22bee78STejun Heo
999bc8b50c2STejun Heo lockdep_assert_held(&pool->lock);
1000cb444766STejun Heo
1001d302f017STejun Heo worker->flags &= ~flags;
1002e22bee78STejun Heo
100342c025f3STejun Heo /*
100442c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note
100542c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
100642c025f3STejun Heo * of multiple flags, not a single flag.
100742c025f3STejun Heo */
1008e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
1009e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING))
1010bc35f7efSLai Jiangshan pool->nr_running++;
1011d302f017STejun Heo }
1012d302f017STejun Heo
1013797e8345STejun Heo /* Return the first idle worker. Called with pool->lock held. */
first_idle_worker(struct worker_pool * pool)1014797e8345STejun Heo static struct worker *first_idle_worker(struct worker_pool *pool)
1015797e8345STejun Heo {
1016797e8345STejun Heo if (unlikely(list_empty(&pool->idle_list)))
1017797e8345STejun Heo return NULL;
1018797e8345STejun Heo
1019797e8345STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry);
1020797e8345STejun Heo }
1021797e8345STejun Heo
1022797e8345STejun Heo /**
1023797e8345STejun Heo * worker_enter_idle - enter idle state
1024797e8345STejun Heo * @worker: worker which is entering idle state
1025797e8345STejun Heo *
1026797e8345STejun Heo * @worker is entering idle state. Update stats and idle timer if
1027797e8345STejun Heo * necessary.
1028797e8345STejun Heo *
1029797e8345STejun Heo * LOCKING:
1030797e8345STejun Heo * raw_spin_lock_irq(pool->lock).
1031797e8345STejun Heo */
worker_enter_idle(struct worker * worker)1032797e8345STejun Heo static void worker_enter_idle(struct worker *worker)
1033797e8345STejun Heo {
1034797e8345STejun Heo struct worker_pool *pool = worker->pool;
1035797e8345STejun Heo
1036797e8345STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1037797e8345STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) &&
1038797e8345STejun Heo (worker->hentry.next || worker->hentry.pprev)))
1039797e8345STejun Heo return;
1040797e8345STejun Heo
1041797e8345STejun Heo /* can't use worker_set_flags(), also called from create_worker() */
1042797e8345STejun Heo worker->flags |= WORKER_IDLE;
1043797e8345STejun Heo pool->nr_idle++;
1044797e8345STejun Heo worker->last_active = jiffies;
1045797e8345STejun Heo
1046797e8345STejun Heo /* idle_list is LIFO */
1047797e8345STejun Heo list_add(&worker->entry, &pool->idle_list);
1048797e8345STejun Heo
1049797e8345STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1050797e8345STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
1051797e8345STejun Heo
1052797e8345STejun Heo /* Sanity check nr_running. */
1053797e8345STejun Heo WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running);
1054797e8345STejun Heo }
1055797e8345STejun Heo
1056797e8345STejun Heo /**
1057797e8345STejun Heo * worker_leave_idle - leave idle state
1058797e8345STejun Heo * @worker: worker which is leaving idle state
1059797e8345STejun Heo *
1060797e8345STejun Heo * @worker is leaving idle state. Update stats.
1061797e8345STejun Heo *
1062797e8345STejun Heo * LOCKING:
1063797e8345STejun Heo * raw_spin_lock_irq(pool->lock).
1064797e8345STejun Heo */
worker_leave_idle(struct worker * worker)1065797e8345STejun Heo static void worker_leave_idle(struct worker *worker)
1066797e8345STejun Heo {
1067797e8345STejun Heo struct worker_pool *pool = worker->pool;
1068797e8345STejun Heo
1069797e8345STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1070797e8345STejun Heo return;
1071797e8345STejun Heo worker_clr_flags(worker, WORKER_IDLE);
1072797e8345STejun Heo pool->nr_idle--;
1073797e8345STejun Heo list_del_init(&worker->entry);
1074797e8345STejun Heo }
1075797e8345STejun Heo
1076797e8345STejun Heo /**
1077797e8345STejun Heo * find_worker_executing_work - find worker which is executing a work
1078797e8345STejun Heo * @pool: pool of interest
1079797e8345STejun Heo * @work: work to find worker for
1080797e8345STejun Heo *
1081797e8345STejun Heo * Find a worker which is executing @work on @pool by searching
1082797e8345STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker
1083797e8345STejun Heo * to match, its current execution should match the address of @work and
1084797e8345STejun Heo * its work function. This is to avoid unwanted dependency between
1085797e8345STejun Heo * unrelated work executions through a work item being recycled while still
1086797e8345STejun Heo * being executed.
1087797e8345STejun Heo *
1088797e8345STejun Heo * This is a bit tricky. A work item may be freed once its execution
1089797e8345STejun Heo * starts and nothing prevents the freed area from being recycled for
1090797e8345STejun Heo * another work item. If the same work item address ends up being reused
1091797e8345STejun Heo * before the original execution finishes, workqueue will identify the
1092797e8345STejun Heo * recycled work item as currently executing and make it wait until the
1093797e8345STejun Heo * current execution finishes, introducing an unwanted dependency.
1094797e8345STejun Heo *
1095797e8345STejun Heo * This function checks the work item address and work function to avoid
1096797e8345STejun Heo * false positives. Note that this isn't complete as one may construct a
1097797e8345STejun Heo * work function which can introduce dependency onto itself through a
1098797e8345STejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the
1099797e8345STejun Heo * foot that badly, there's only so much we can do, and if such deadlock
1100797e8345STejun Heo * actually occurs, it should be easy to locate the culprit work function.
1101797e8345STejun Heo *
1102797e8345STejun Heo * CONTEXT:
1103797e8345STejun Heo * raw_spin_lock_irq(pool->lock).
1104797e8345STejun Heo *
1105797e8345STejun Heo * Return:
1106797e8345STejun Heo * Pointer to worker which is executing @work if found, %NULL
1107797e8345STejun Heo * otherwise.
1108797e8345STejun Heo */
find_worker_executing_work(struct worker_pool * pool,struct work_struct * work)1109797e8345STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool,
1110797e8345STejun Heo struct work_struct *work)
1111797e8345STejun Heo {
1112797e8345STejun Heo struct worker *worker;
1113797e8345STejun Heo
1114797e8345STejun Heo hash_for_each_possible(pool->busy_hash, worker, hentry,
1115797e8345STejun Heo (unsigned long)work)
1116797e8345STejun Heo if (worker->current_work == work &&
1117797e8345STejun Heo worker->current_func == work->func)
1118797e8345STejun Heo return worker;
1119797e8345STejun Heo
1120797e8345STejun Heo return NULL;
1121797e8345STejun Heo }
1122797e8345STejun Heo
1123797e8345STejun Heo /**
1124797e8345STejun Heo * move_linked_works - move linked works to a list
1125797e8345STejun Heo * @work: start of series of works to be scheduled
1126797e8345STejun Heo * @head: target list to append @work to
1127797e8345STejun Heo * @nextp: out parameter for nested worklist walking
1128797e8345STejun Heo *
1129873eaca6STejun Heo * Schedule linked works starting from @work to @head. Work series to be
1130873eaca6STejun Heo * scheduled starts at @work and includes any consecutive work with
1131873eaca6STejun Heo * WORK_STRUCT_LINKED set in its predecessor. See assign_work() for details on
1132873eaca6STejun Heo * @nextp.
1133797e8345STejun Heo *
1134797e8345STejun Heo * CONTEXT:
1135797e8345STejun Heo * raw_spin_lock_irq(pool->lock).
1136797e8345STejun Heo */
move_linked_works(struct work_struct * work,struct list_head * head,struct work_struct ** nextp)1137797e8345STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head,
1138797e8345STejun Heo struct work_struct **nextp)
1139797e8345STejun Heo {
1140797e8345STejun Heo struct work_struct *n;
1141797e8345STejun Heo
1142797e8345STejun Heo /*
1143797e8345STejun Heo * Linked worklist will always end before the end of the list,
1144797e8345STejun Heo * use NULL for list head.
1145797e8345STejun Heo */
1146797e8345STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) {
1147797e8345STejun Heo list_move_tail(&work->entry, head);
1148797e8345STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1149797e8345STejun Heo break;
1150797e8345STejun Heo }
1151797e8345STejun Heo
1152797e8345STejun Heo /*
1153797e8345STejun Heo * If we're already inside safe list traversal and have moved
1154797e8345STejun Heo * multiple works to the scheduled queue, the next position
1155797e8345STejun Heo * needs to be updated.
1156797e8345STejun Heo */
1157797e8345STejun Heo if (nextp)
1158797e8345STejun Heo *nextp = n;
1159797e8345STejun Heo }
1160797e8345STejun Heo
1161797e8345STejun Heo /**
1162873eaca6STejun Heo * assign_work - assign a work item and its linked work items to a worker
1163873eaca6STejun Heo * @work: work to assign
1164873eaca6STejun Heo * @worker: worker to assign to
1165873eaca6STejun Heo * @nextp: out parameter for nested worklist walking
1166873eaca6STejun Heo *
1167873eaca6STejun Heo * Assign @work and its linked work items to @worker. If @work is already being
1168873eaca6STejun Heo * executed by another worker in the same pool, it'll be punted there.
1169873eaca6STejun Heo *
1170873eaca6STejun Heo * If @nextp is not NULL, it's updated to point to the next work of the last
1171873eaca6STejun Heo * scheduled work. This allows assign_work() to be nested inside
1172873eaca6STejun Heo * list_for_each_entry_safe().
1173873eaca6STejun Heo *
1174873eaca6STejun Heo * Returns %true if @work was successfully assigned to @worker. %false if @work
1175873eaca6STejun Heo * was punted to another worker already executing it.
1176873eaca6STejun Heo */
assign_work(struct work_struct * work,struct worker * worker,struct work_struct ** nextp)1177873eaca6STejun Heo static bool assign_work(struct work_struct *work, struct worker *worker,
1178873eaca6STejun Heo struct work_struct **nextp)
1179873eaca6STejun Heo {
1180873eaca6STejun Heo struct worker_pool *pool = worker->pool;
1181873eaca6STejun Heo struct worker *collision;
1182873eaca6STejun Heo
1183873eaca6STejun Heo lockdep_assert_held(&pool->lock);
1184873eaca6STejun Heo
1185873eaca6STejun Heo /*
1186873eaca6STejun Heo * A single work shouldn't be executed concurrently by multiple workers.
1187873eaca6STejun Heo * __queue_work() ensures that @work doesn't jump to a different pool
1188873eaca6STejun Heo * while still running in the previous pool. Here, we should ensure that
1189873eaca6STejun Heo * @work is not executed concurrently by multiple workers from the same
1190873eaca6STejun Heo * pool. Check whether anyone is already processing the work. If so,
1191873eaca6STejun Heo * defer the work to the currently executing one.
1192873eaca6STejun Heo */
1193873eaca6STejun Heo collision = find_worker_executing_work(pool, work);
1194873eaca6STejun Heo if (unlikely(collision)) {
1195873eaca6STejun Heo move_linked_works(work, &collision->scheduled, nextp);
1196873eaca6STejun Heo return false;
1197873eaca6STejun Heo }
1198873eaca6STejun Heo
1199873eaca6STejun Heo move_linked_works(work, &worker->scheduled, nextp);
1200873eaca6STejun Heo return true;
1201873eaca6STejun Heo }
1202873eaca6STejun Heo
bh_pool_irq_work(struct worker_pool * pool)12032f34d733STejun Heo static struct irq_work *bh_pool_irq_work(struct worker_pool *pool)
12042f34d733STejun Heo {
12052f34d733STejun Heo int high = pool->attrs->nice == HIGHPRI_NICE_LEVEL ? 1 : 0;
12062f34d733STejun Heo
12072f34d733STejun Heo return &per_cpu(bh_pool_irq_works, pool->cpu)[high];
12082f34d733STejun Heo }
12092f34d733STejun Heo
kick_bh_pool(struct worker_pool * pool)1210fd0a68a2STejun Heo static void kick_bh_pool(struct worker_pool *pool)
1211fd0a68a2STejun Heo {
1212fd0a68a2STejun Heo #ifdef CONFIG_SMP
12131acd92d9STejun Heo /* see drain_dead_softirq_workfn() for BH_DRAINING */
12141acd92d9STejun Heo if (unlikely(pool->cpu != smp_processor_id() &&
12151acd92d9STejun Heo !(pool->flags & POOL_BH_DRAINING))) {
1216fd0a68a2STejun Heo irq_work_queue_on(bh_pool_irq_work(pool), pool->cpu);
1217fd0a68a2STejun Heo return;
1218fd0a68a2STejun Heo }
1219fd0a68a2STejun Heo #endif
1220fd0a68a2STejun Heo if (pool->attrs->nice == HIGHPRI_NICE_LEVEL)
1221fd0a68a2STejun Heo raise_softirq_irqoff(HI_SOFTIRQ);
1222fd0a68a2STejun Heo else
1223fd0a68a2STejun Heo raise_softirq_irqoff(TASKLET_SOFTIRQ);
1224fd0a68a2STejun Heo }
1225fd0a68a2STejun Heo
1226873eaca6STejun Heo /**
12270219a352STejun Heo * kick_pool - wake up an idle worker if necessary
12280219a352STejun Heo * @pool: pool to kick
1229797e8345STejun Heo *
12300219a352STejun Heo * @pool may have pending work items. Wake up worker if necessary. Returns
12310219a352STejun Heo * whether a worker was woken up.
1232797e8345STejun Heo */
kick_pool(struct worker_pool * pool)12330219a352STejun Heo static bool kick_pool(struct worker_pool *pool)
1234797e8345STejun Heo {
1235797e8345STejun Heo struct worker *worker = first_idle_worker(pool);
12368639ecebSTejun Heo struct task_struct *p;
1237797e8345STejun Heo
12380219a352STejun Heo lockdep_assert_held(&pool->lock);
12390219a352STejun Heo
12400219a352STejun Heo if (!need_more_worker(pool) || !worker)
12410219a352STejun Heo return false;
12420219a352STejun Heo
12434cb1ef64STejun Heo if (pool->flags & POOL_BH) {
1244fd0a68a2STejun Heo kick_bh_pool(pool);
12454cb1ef64STejun Heo return true;
12464cb1ef64STejun Heo }
12474cb1ef64STejun Heo
12488639ecebSTejun Heo p = worker->task;
12498639ecebSTejun Heo
12508639ecebSTejun Heo #ifdef CONFIG_SMP
12518639ecebSTejun Heo /*
12528639ecebSTejun Heo * Idle @worker is about to execute @work and waking up provides an
12538639ecebSTejun Heo * opportunity to migrate @worker at a lower cost by setting the task's
12548639ecebSTejun Heo * wake_cpu field. Let's see if we want to move @worker to improve
12558639ecebSTejun Heo * execution locality.
12568639ecebSTejun Heo *
12578639ecebSTejun Heo * We're waking the worker that went idle the latest and there's some
12588639ecebSTejun Heo * chance that @worker is marked idle but hasn't gone off CPU yet. If
12598639ecebSTejun Heo * so, setting the wake_cpu won't do anything. As this is a best-effort
12608639ecebSTejun Heo * optimization and the race window is narrow, let's leave as-is for
12618639ecebSTejun Heo * now. If this becomes pronounced, we can skip over workers which are
12628639ecebSTejun Heo * still on cpu when picking an idle worker.
12638639ecebSTejun Heo *
12648639ecebSTejun Heo * If @pool has non-strict affinity, @worker might have ended up outside
12658639ecebSTejun Heo * its affinity scope. Repatriate.
12668639ecebSTejun Heo */
12678639ecebSTejun Heo if (!pool->attrs->affn_strict &&
12688639ecebSTejun Heo !cpumask_test_cpu(p->wake_cpu, pool->attrs->__pod_cpumask)) {
12698639ecebSTejun Heo struct work_struct *work = list_first_entry(&pool->worklist,
12708639ecebSTejun Heo struct work_struct, entry);
127157a01eafSSven Schnelle int wake_cpu = cpumask_any_and_distribute(pool->attrs->__pod_cpumask,
127257a01eafSSven Schnelle cpu_online_mask);
127357a01eafSSven Schnelle if (wake_cpu < nr_cpu_ids) {
127457a01eafSSven Schnelle p->wake_cpu = wake_cpu;
12758639ecebSTejun Heo get_work_pwq(work)->stats[PWQ_STAT_REPATRIATED]++;
12768639ecebSTejun Heo }
127757a01eafSSven Schnelle }
12788639ecebSTejun Heo #endif
12798639ecebSTejun Heo wake_up_process(p);
12800219a352STejun Heo return true;
1281797e8345STejun Heo }
1282797e8345STejun Heo
128363638450STejun Heo #ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT
128463638450STejun Heo
128563638450STejun Heo /*
128663638450STejun Heo * Concurrency-managed per-cpu work items that hog CPU for longer than
128763638450STejun Heo * wq_cpu_intensive_thresh_us trigger the automatic CPU_INTENSIVE mechanism,
128863638450STejun Heo * which prevents them from stalling other concurrency-managed work items. If a
128963638450STejun Heo * work function keeps triggering this mechanism, it's likely that the work item
129063638450STejun Heo * should be using an unbound workqueue instead.
129163638450STejun Heo *
129263638450STejun Heo * wq_cpu_intensive_report() tracks work functions which trigger such conditions
129363638450STejun Heo * and report them so that they can be examined and converted to use unbound
129463638450STejun Heo * workqueues as appropriate. To avoid flooding the console, each violating work
129563638450STejun Heo * function is tracked and reported with exponential backoff.
129663638450STejun Heo */
129763638450STejun Heo #define WCI_MAX_ENTS 128
129863638450STejun Heo
129963638450STejun Heo struct wci_ent {
130063638450STejun Heo work_func_t func;
130163638450STejun Heo atomic64_t cnt;
130263638450STejun Heo struct hlist_node hash_node;
130363638450STejun Heo };
130463638450STejun Heo
130563638450STejun Heo static struct wci_ent wci_ents[WCI_MAX_ENTS];
130663638450STejun Heo static int wci_nr_ents;
130763638450STejun Heo static DEFINE_RAW_SPINLOCK(wci_lock);
130863638450STejun Heo static DEFINE_HASHTABLE(wci_hash, ilog2(WCI_MAX_ENTS));
130963638450STejun Heo
wci_find_ent(work_func_t func)131063638450STejun Heo static struct wci_ent *wci_find_ent(work_func_t func)
131163638450STejun Heo {
131263638450STejun Heo struct wci_ent *ent;
131363638450STejun Heo
131463638450STejun Heo hash_for_each_possible_rcu(wci_hash, ent, hash_node,
131563638450STejun Heo (unsigned long)func) {
131663638450STejun Heo if (ent->func == func)
131763638450STejun Heo return ent;
131863638450STejun Heo }
131963638450STejun Heo return NULL;
132063638450STejun Heo }
132163638450STejun Heo
wq_cpu_intensive_report(work_func_t func)132263638450STejun Heo static void wq_cpu_intensive_report(work_func_t func)
132363638450STejun Heo {
132463638450STejun Heo struct wci_ent *ent;
132563638450STejun Heo
132663638450STejun Heo restart:
132763638450STejun Heo ent = wci_find_ent(func);
132863638450STejun Heo if (ent) {
132963638450STejun Heo u64 cnt;
133063638450STejun Heo
133163638450STejun Heo /*
1332ccdec921SXuewen Yan * Start reporting from the warning_thresh and back off
133363638450STejun Heo * exponentially.
133463638450STejun Heo */
133563638450STejun Heo cnt = atomic64_inc_return_relaxed(&ent->cnt);
1336ccdec921SXuewen Yan if (wq_cpu_intensive_warning_thresh &&
1337ccdec921SXuewen Yan cnt >= wq_cpu_intensive_warning_thresh &&
1338ccdec921SXuewen Yan is_power_of_2(cnt + 1 - wq_cpu_intensive_warning_thresh))
133963638450STejun Heo printk_deferred(KERN_WARNING "workqueue: %ps hogged CPU for >%luus %llu times, consider switching to WQ_UNBOUND\n",
134063638450STejun Heo ent->func, wq_cpu_intensive_thresh_us,
134163638450STejun Heo atomic64_read(&ent->cnt));
134263638450STejun Heo return;
134363638450STejun Heo }
134463638450STejun Heo
134563638450STejun Heo /*
134663638450STejun Heo * @func is a new violation. Allocate a new entry for it. If wcn_ents[]
134763638450STejun Heo * is exhausted, something went really wrong and we probably made enough
134863638450STejun Heo * noise already.
134963638450STejun Heo */
135063638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS)
135163638450STejun Heo return;
135263638450STejun Heo
135363638450STejun Heo raw_spin_lock(&wci_lock);
135463638450STejun Heo
135563638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) {
135663638450STejun Heo raw_spin_unlock(&wci_lock);
135763638450STejun Heo return;
135863638450STejun Heo }
135963638450STejun Heo
136063638450STejun Heo if (wci_find_ent(func)) {
136163638450STejun Heo raw_spin_unlock(&wci_lock);
136263638450STejun Heo goto restart;
136363638450STejun Heo }
136463638450STejun Heo
136563638450STejun Heo ent = &wci_ents[wci_nr_ents++];
136663638450STejun Heo ent->func = func;
1367ccdec921SXuewen Yan atomic64_set(&ent->cnt, 0);
136863638450STejun Heo hash_add_rcu(wci_hash, &ent->hash_node, (unsigned long)func);
136963638450STejun Heo
137063638450STejun Heo raw_spin_unlock(&wci_lock);
1371ccdec921SXuewen Yan
1372ccdec921SXuewen Yan goto restart;
137363638450STejun Heo }
137463638450STejun Heo
137563638450STejun Heo #else /* CONFIG_WQ_CPU_INTENSIVE_REPORT */
wq_cpu_intensive_report(work_func_t func)137663638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) {}
137763638450STejun Heo #endif /* CONFIG_WQ_CPU_INTENSIVE_REPORT */
137863638450STejun Heo
1379c54d5046STejun Heo /**
13801da177e4SLinus Torvalds * wq_worker_running - a worker is running again
13811da177e4SLinus Torvalds * @task: task waking up
13821da177e4SLinus Torvalds *
13831da177e4SLinus Torvalds * This function is called when a worker returns from schedule()
13841da177e4SLinus Torvalds */
wq_worker_running(struct task_struct * task)13851da177e4SLinus Torvalds void wq_worker_running(struct task_struct *task)
13861da177e4SLinus Torvalds {
13871da177e4SLinus Torvalds struct worker *worker = kthread_data(task);
13881da177e4SLinus Torvalds
1389c8f6219bSZqiang if (!READ_ONCE(worker->sleeping))
13901da177e4SLinus Torvalds return;
13911da177e4SLinus Torvalds
13921da177e4SLinus Torvalds /*
13931da177e4SLinus Torvalds * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check
13941da177e4SLinus Torvalds * and the nr_running increment below, we may ruin the nr_running reset
13951da177e4SLinus Torvalds * and leave with an unexpected pool->nr_running == 1 on the newly unbound
13961da177e4SLinus Torvalds * pool. Protect against such race.
13971da177e4SLinus Torvalds */
13981da177e4SLinus Torvalds preempt_disable();
13991da177e4SLinus Torvalds if (!(worker->flags & WORKER_NOT_RUNNING))
14001da177e4SLinus Torvalds worker->pool->nr_running++;
14011da177e4SLinus Torvalds preempt_enable();
1402616db877STejun Heo
1403616db877STejun Heo /*
1404616db877STejun Heo * CPU intensive auto-detection cares about how long a work item hogged
1405616db877STejun Heo * CPU without sleeping. Reset the starting timestamp on wakeup.
1406616db877STejun Heo */
1407616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime;
1408616db877STejun Heo
1409c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 0);
14101da177e4SLinus Torvalds }
14111da177e4SLinus Torvalds
14121da177e4SLinus Torvalds /**
14131da177e4SLinus Torvalds * wq_worker_sleeping - a worker is going to sleep
14141da177e4SLinus Torvalds * @task: task going to sleep
14151da177e4SLinus Torvalds *
14161da177e4SLinus Torvalds * This function is called from schedule() when a busy worker is
14171da177e4SLinus Torvalds * going to sleep.
14181da177e4SLinus Torvalds */
wq_worker_sleeping(struct task_struct * task)14191da177e4SLinus Torvalds void wq_worker_sleeping(struct task_struct *task)
14201da177e4SLinus Torvalds {
14211da177e4SLinus Torvalds struct worker *worker = kthread_data(task);
14221da177e4SLinus Torvalds struct worker_pool *pool;
14231da177e4SLinus Torvalds
14241da177e4SLinus Torvalds /*
14251da177e4SLinus Torvalds * Rescuers, which may not have all the fields set up like normal
14261da177e4SLinus Torvalds * workers, also reach here, let's not access anything before
14271da177e4SLinus Torvalds * checking NOT_RUNNING.
14281da177e4SLinus Torvalds */
14291da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING)
14301da177e4SLinus Torvalds return;
14311da177e4SLinus Torvalds
14321da177e4SLinus Torvalds pool = worker->pool;
14331da177e4SLinus Torvalds
14341da177e4SLinus Torvalds /* Return if preempted before wq_worker_running() was reached */
1435c8f6219bSZqiang if (READ_ONCE(worker->sleeping))
14361da177e4SLinus Torvalds return;
14371da177e4SLinus Torvalds
1438c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 1);
14391da177e4SLinus Torvalds raw_spin_lock_irq(&pool->lock);
14401da177e4SLinus Torvalds
14411da177e4SLinus Torvalds /*
14421da177e4SLinus Torvalds * Recheck in case unbind_workers() preempted us. We don't
14431da177e4SLinus Torvalds * want to decrement nr_running after the worker is unbound
14441da177e4SLinus Torvalds * and nr_running has been reset.
14451da177e4SLinus Torvalds */
14461da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) {
14471da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock);
14481da177e4SLinus Torvalds return;
14491da177e4SLinus Torvalds }
14501da177e4SLinus Torvalds
14511da177e4SLinus Torvalds pool->nr_running--;
14520219a352STejun Heo if (kick_pool(pool))
1453725e8ec5STejun Heo worker->current_pwq->stats[PWQ_STAT_CM_WAKEUP]++;
14540219a352STejun Heo
14551da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock);
14561da177e4SLinus Torvalds }
14571da177e4SLinus Torvalds
14581da177e4SLinus Torvalds /**
1459616db877STejun Heo * wq_worker_tick - a scheduler tick occurred while a kworker is running
1460616db877STejun Heo * @task: task currently running
1461616db877STejun Heo *
146286dd6c04SIngo Molnar * Called from sched_tick(). We're in the IRQ context and the current
1463616db877STejun Heo * worker's fields which follow the 'K' locking rule can be accessed safely.
1464616db877STejun Heo */
wq_worker_tick(struct task_struct * task)1465616db877STejun Heo void wq_worker_tick(struct task_struct *task)
1466616db877STejun Heo {
1467616db877STejun Heo struct worker *worker = kthread_data(task);
1468616db877STejun Heo struct pool_workqueue *pwq = worker->current_pwq;
1469616db877STejun Heo struct worker_pool *pool = worker->pool;
1470616db877STejun Heo
1471616db877STejun Heo if (!pwq)
1472616db877STejun Heo return;
1473616db877STejun Heo
14748a1dd1e5STejun Heo pwq->stats[PWQ_STAT_CPU_TIME] += TICK_USEC;
14758a1dd1e5STejun Heo
147618c8ae81SZqiang if (!wq_cpu_intensive_thresh_us)
147718c8ae81SZqiang return;
147818c8ae81SZqiang
1479616db877STejun Heo /*
1480616db877STejun Heo * If the current worker is concurrency managed and hogged the CPU for
1481616db877STejun Heo * longer than wq_cpu_intensive_thresh_us, it's automatically marked
1482616db877STejun Heo * CPU_INTENSIVE to avoid stalling other concurrency-managed work items.
1483c8f6219bSZqiang *
1484c8f6219bSZqiang * Set @worker->sleeping means that @worker is in the process of
1485c8f6219bSZqiang * switching out voluntarily and won't be contributing to
1486c8f6219bSZqiang * @pool->nr_running until it wakes up. As wq_worker_sleeping() also
1487c8f6219bSZqiang * decrements ->nr_running, setting CPU_INTENSIVE here can lead to
1488c8f6219bSZqiang * double decrements. The task is releasing the CPU anyway. Let's skip.
1489c8f6219bSZqiang * We probably want to make this prettier in the future.
1490616db877STejun Heo */
1491c8f6219bSZqiang if ((worker->flags & WORKER_NOT_RUNNING) || READ_ONCE(worker->sleeping) ||
1492616db877STejun Heo worker->task->se.sum_exec_runtime - worker->current_at <
1493616db877STejun Heo wq_cpu_intensive_thresh_us * NSEC_PER_USEC)
1494616db877STejun Heo return;
1495616db877STejun Heo
1496616db877STejun Heo raw_spin_lock(&pool->lock);
1497616db877STejun Heo
1498616db877STejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE);
149963638450STejun Heo wq_cpu_intensive_report(worker->current_func);
1500616db877STejun Heo pwq->stats[PWQ_STAT_CPU_INTENSIVE]++;
1501616db877STejun Heo
15020219a352STejun Heo if (kick_pool(pool))
1503616db877STejun Heo pwq->stats[PWQ_STAT_CM_WAKEUP]++;
1504616db877STejun Heo
1505616db877STejun Heo raw_spin_unlock(&pool->lock);
1506616db877STejun Heo }
1507616db877STejun Heo
1508616db877STejun Heo /**
15091da177e4SLinus Torvalds * wq_worker_last_func - retrieve worker's last work function
15101da177e4SLinus Torvalds * @task: Task to retrieve last work function of.
15111da177e4SLinus Torvalds *
15121da177e4SLinus Torvalds * Determine the last function a worker executed. This is called from
15131da177e4SLinus Torvalds * the scheduler to get a worker's last known identity.
15141da177e4SLinus Torvalds *
15159b41ea72SAndrew Morton * CONTEXT:
15161da177e4SLinus Torvalds * raw_spin_lock_irq(rq->lock)
15171da177e4SLinus Torvalds *
1518f756d5e2SNathan Lynch * This function is called during schedule() when a kworker is going
1519f756d5e2SNathan Lynch * to sleep. It's used by psi to identify aggregation workers during
15201da177e4SLinus Torvalds * dequeuing, to allow periodic aggregation to shut-off when that
15211da177e4SLinus Torvalds * worker is the last task in the system or cgroup to go to sleep.
15221da177e4SLinus Torvalds *
15231da177e4SLinus Torvalds * As this function doesn't involve any workqueue-related locking, it
15241da177e4SLinus Torvalds * only returns stable values when called from inside the scheduler's
15251da177e4SLinus Torvalds * queuing and dequeuing paths, when @task, which must be a kworker,
1526365970a1SDavid Howells * is guaranteed to not be processing any works.
1527365970a1SDavid Howells *
1528365970a1SDavid Howells * Return:
1529365970a1SDavid Howells * The last work function %current executed as a worker, NULL if it
1530365970a1SDavid Howells * hasn't executed any work yet.
1531365970a1SDavid Howells */
wq_worker_last_func(struct task_struct * task)1532365970a1SDavid Howells work_func_t wq_worker_last_func(struct task_struct *task)
1533365970a1SDavid Howells {
1534365970a1SDavid Howells struct worker *worker = kthread_data(task);
1535365970a1SDavid Howells
1536365970a1SDavid Howells return worker->last_func;
1537365970a1SDavid Howells }
1538365970a1SDavid Howells
1539d302f017STejun Heo /**
154091ccc6e7STejun Heo * wq_node_nr_active - Determine wq_node_nr_active to use
154191ccc6e7STejun Heo * @wq: workqueue of interest
154291ccc6e7STejun Heo * @node: NUMA node, can be %NUMA_NO_NODE
154391ccc6e7STejun Heo *
154491ccc6e7STejun Heo * Determine wq_node_nr_active to use for @wq on @node. Returns:
154591ccc6e7STejun Heo *
154691ccc6e7STejun Heo * - %NULL for per-cpu workqueues as they don't need to use shared nr_active.
154791ccc6e7STejun Heo *
154891ccc6e7STejun Heo * - node_nr_active[nr_node_ids] if @node is %NUMA_NO_NODE.
154991ccc6e7STejun Heo *
155091ccc6e7STejun Heo * - Otherwise, node_nr_active[@node].
155191ccc6e7STejun Heo */
wq_node_nr_active(struct workqueue_struct * wq,int node)155291ccc6e7STejun Heo static struct wq_node_nr_active *wq_node_nr_active(struct workqueue_struct *wq,
155391ccc6e7STejun Heo int node)
155491ccc6e7STejun Heo {
155591ccc6e7STejun Heo if (!(wq->flags & WQ_UNBOUND))
155691ccc6e7STejun Heo return NULL;
155791ccc6e7STejun Heo
155891ccc6e7STejun Heo if (node == NUMA_NO_NODE)
155991ccc6e7STejun Heo node = nr_node_ids;
156091ccc6e7STejun Heo
156191ccc6e7STejun Heo return wq->node_nr_active[node];
156291ccc6e7STejun Heo }
156391ccc6e7STejun Heo
156491ccc6e7STejun Heo /**
15655797b1c1STejun Heo * wq_update_node_max_active - Update per-node max_actives to use
15665797b1c1STejun Heo * @wq: workqueue to update
15675797b1c1STejun Heo * @off_cpu: CPU that's going down, -1 if a CPU is not going down
15685797b1c1STejun Heo *
15695797b1c1STejun Heo * Update @wq->node_nr_active[]->max. @wq must be unbound. max_active is
15705797b1c1STejun Heo * distributed among nodes according to the proportions of numbers of online
15715797b1c1STejun Heo * cpus. The result is always between @wq->min_active and max_active.
15725797b1c1STejun Heo */
wq_update_node_max_active(struct workqueue_struct * wq,int off_cpu)15735797b1c1STejun Heo static void wq_update_node_max_active(struct workqueue_struct *wq, int off_cpu)
15745797b1c1STejun Heo {
15755797b1c1STejun Heo struct cpumask *effective = unbound_effective_cpumask(wq);
15765797b1c1STejun Heo int min_active = READ_ONCE(wq->min_active);
15775797b1c1STejun Heo int max_active = READ_ONCE(wq->max_active);
15785797b1c1STejun Heo int total_cpus, node;
15795797b1c1STejun Heo
15805797b1c1STejun Heo lockdep_assert_held(&wq->mutex);
15815797b1c1STejun Heo
1582c5f8cd6cSTejun Heo if (!wq_topo_initialized)
1583c5f8cd6cSTejun Heo return;
1584c5f8cd6cSTejun Heo
158515930da4STejun Heo if (off_cpu >= 0 && !cpumask_test_cpu(off_cpu, effective))
15865797b1c1STejun Heo off_cpu = -1;
15875797b1c1STejun Heo
15885797b1c1STejun Heo total_cpus = cpumask_weight_and(effective, cpu_online_mask);
15895797b1c1STejun Heo if (off_cpu >= 0)
15905797b1c1STejun Heo total_cpus--;
15915797b1c1STejun Heo
159291f09870SLai Jiangshan /* If all CPUs of the wq get offline, use the default values */
159391f09870SLai Jiangshan if (unlikely(!total_cpus)) {
159491f09870SLai Jiangshan for_each_node(node)
159591f09870SLai Jiangshan wq_node_nr_active(wq, node)->max = min_active;
159691f09870SLai Jiangshan
159791f09870SLai Jiangshan wq_node_nr_active(wq, NUMA_NO_NODE)->max = max_active;
159891f09870SLai Jiangshan return;
159991f09870SLai Jiangshan }
160091f09870SLai Jiangshan
16015797b1c1STejun Heo for_each_node(node) {
16025797b1c1STejun Heo int node_cpus;
16035797b1c1STejun Heo
16045797b1c1STejun Heo node_cpus = cpumask_weight_and(effective, cpumask_of_node(node));
16055797b1c1STejun Heo if (off_cpu >= 0 && cpu_to_node(off_cpu) == node)
16065797b1c1STejun Heo node_cpus--;
16075797b1c1STejun Heo
16085797b1c1STejun Heo wq_node_nr_active(wq, node)->max =
16095797b1c1STejun Heo clamp(DIV_ROUND_UP(max_active * node_cpus, total_cpus),
16105797b1c1STejun Heo min_active, max_active);
16115797b1c1STejun Heo }
16125797b1c1STejun Heo
1613d40f9202STejun Heo wq_node_nr_active(wq, NUMA_NO_NODE)->max = max_active;
16145797b1c1STejun Heo }
16155797b1c1STejun Heo
16165797b1c1STejun Heo /**
16178864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue
16188864b4e5STejun Heo * @pwq: pool_workqueue to get
16198864b4e5STejun Heo *
16208864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that
16218864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock.
16228864b4e5STejun Heo */
get_pwq(struct pool_workqueue * pwq)16238864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq)
16248864b4e5STejun Heo {
16258864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock);
16268864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0);
16278864b4e5STejun Heo pwq->refcnt++;
16288864b4e5STejun Heo }
16298864b4e5STejun Heo
16308864b4e5STejun Heo /**
16318864b4e5STejun Heo * put_pwq - put a pool_workqueue reference
16328864b4e5STejun Heo * @pwq: pool_workqueue to put
16338864b4e5STejun Heo *
16348864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
16358864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock.
16368864b4e5STejun Heo */
put_pwq(struct pool_workqueue * pwq)16378864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq)
16388864b4e5STejun Heo {
16398864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock);
16408864b4e5STejun Heo if (likely(--pwq->refcnt))
16418864b4e5STejun Heo return;
16428864b4e5STejun Heo /*
1643967b494eSTejun Heo * @pwq can't be released under pool->lock, bounce to a dedicated
1644967b494eSTejun Heo * kthread_worker to avoid A-A deadlocks.
16458864b4e5STejun Heo */
1646687a9aa5STejun Heo kthread_queue_work(pwq_release_worker, &pwq->release_work);
16478864b4e5STejun Heo }
16488864b4e5STejun Heo
1649dce90d47STejun Heo /**
1650dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock
1651dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL)
1652dce90d47STejun Heo *
1653dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq.
1654dce90d47STejun Heo */
put_pwq_unlocked(struct pool_workqueue * pwq)1655dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq)
1656dce90d47STejun Heo {
1657dce90d47STejun Heo if (pwq) {
1658dce90d47STejun Heo /*
165924acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the
1660dce90d47STejun Heo * following lock operations are safe.
1661dce90d47STejun Heo */
1662a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock);
1663dce90d47STejun Heo put_pwq(pwq);
1664a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock);
1665dce90d47STejun Heo }
1666dce90d47STejun Heo }
1667dce90d47STejun Heo
pwq_is_empty(struct pool_workqueue * pwq)1668afa87ce8STejun Heo static bool pwq_is_empty(struct pool_workqueue *pwq)
1669afa87ce8STejun Heo {
1670afa87ce8STejun Heo return !pwq->nr_active && list_empty(&pwq->inactive_works);
1671afa87ce8STejun Heo }
1672afa87ce8STejun Heo
__pwq_activate_work(struct pool_workqueue * pwq,struct work_struct * work)16734c638030STejun Heo static void __pwq_activate_work(struct pool_workqueue *pwq,
16744c638030STejun Heo struct work_struct *work)
1675bf4ede01STejun Heo {
16761c270b79STejun Heo unsigned long *wdb = work_data_bits(work);
16771c270b79STejun Heo
16781c270b79STejun Heo WARN_ON_ONCE(!(*wdb & WORK_STRUCT_INACTIVE));
1679bf4ede01STejun Heo trace_workqueue_activate_work(work);
168082607adcSTejun Heo if (list_empty(&pwq->pool->worklist))
168182607adcSTejun Heo pwq->pool->watchdog_ts = jiffies;
1682112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL);
16831c270b79STejun Heo __clear_bit(WORK_STRUCT_INACTIVE_BIT, wdb);
16844c638030STejun Heo }
16854c638030STejun Heo
tryinc_node_nr_active(struct wq_node_nr_active * nna)16865797b1c1STejun Heo static bool tryinc_node_nr_active(struct wq_node_nr_active *nna)
16875797b1c1STejun Heo {
16885797b1c1STejun Heo int max = READ_ONCE(nna->max);
16895797b1c1STejun Heo
16905797b1c1STejun Heo while (true) {
16915797b1c1STejun Heo int old, tmp;
16925797b1c1STejun Heo
16935797b1c1STejun Heo old = atomic_read(&nna->nr);
16945797b1c1STejun Heo if (old >= max)
16955797b1c1STejun Heo return false;
16965797b1c1STejun Heo tmp = atomic_cmpxchg_relaxed(&nna->nr, old, old + 1);
16975797b1c1STejun Heo if (tmp == old)
16985797b1c1STejun Heo return true;
16995797b1c1STejun Heo }
17005797b1c1STejun Heo }
17015797b1c1STejun Heo
17021c270b79STejun Heo /**
17031c270b79STejun Heo * pwq_tryinc_nr_active - Try to increment nr_active for a pwq
17041c270b79STejun Heo * @pwq: pool_workqueue of interest
17055797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level
17061c270b79STejun Heo *
17071c270b79STejun Heo * Try to increment nr_active for @pwq. Returns %true if an nr_active count is
17081c270b79STejun Heo * successfully obtained. %false otherwise.
17091c270b79STejun Heo */
pwq_tryinc_nr_active(struct pool_workqueue * pwq,bool fill)17105797b1c1STejun Heo static bool pwq_tryinc_nr_active(struct pool_workqueue *pwq, bool fill)
17113aa62497SLai Jiangshan {
17121c270b79STejun Heo struct workqueue_struct *wq = pwq->wq;
17131c270b79STejun Heo struct worker_pool *pool = pwq->pool;
171491ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(wq, pool->node);
17155797b1c1STejun Heo bool obtained = false;
17161c270b79STejun Heo
17171c270b79STejun Heo lockdep_assert_held(&pool->lock);
17181c270b79STejun Heo
17195797b1c1STejun Heo if (!nna) {
17204cb1ef64STejun Heo /* BH or per-cpu workqueue, pwq->nr_active is sufficient */
17211c270b79STejun Heo obtained = pwq->nr_active < READ_ONCE(wq->max_active);
17225797b1c1STejun Heo goto out;
172391ccc6e7STejun Heo }
17245797b1c1STejun Heo
17254c065dbcSWaiman Long if (unlikely(pwq->plugged))
17264c065dbcSWaiman Long return false;
17274c065dbcSWaiman Long
17285797b1c1STejun Heo /*
17295797b1c1STejun Heo * Unbound workqueue uses per-node shared nr_active $nna. If @pwq is
17305797b1c1STejun Heo * already waiting on $nna, pwq_dec_nr_active() will maintain the
17315797b1c1STejun Heo * concurrency level. Don't jump the line.
17325797b1c1STejun Heo *
17335797b1c1STejun Heo * We need to ignore the pending test after max_active has increased as
17345797b1c1STejun Heo * pwq_dec_nr_active() can only maintain the concurrency level but not
17355797b1c1STejun Heo * increase it. This is indicated by @fill.
17365797b1c1STejun Heo */
17375797b1c1STejun Heo if (!list_empty(&pwq->pending_node) && likely(!fill))
17385797b1c1STejun Heo goto out;
17395797b1c1STejun Heo
17405797b1c1STejun Heo obtained = tryinc_node_nr_active(nna);
17415797b1c1STejun Heo if (obtained)
17425797b1c1STejun Heo goto out;
17435797b1c1STejun Heo
17445797b1c1STejun Heo /*
17455797b1c1STejun Heo * Lockless acquisition failed. Lock, add ourself to $nna->pending_pwqs
17465797b1c1STejun Heo * and try again. The smp_mb() is paired with the implied memory barrier
17475797b1c1STejun Heo * of atomic_dec_return() in pwq_dec_nr_active() to ensure that either
17485797b1c1STejun Heo * we see the decremented $nna->nr or they see non-empty
17495797b1c1STejun Heo * $nna->pending_pwqs.
17505797b1c1STejun Heo */
17515797b1c1STejun Heo raw_spin_lock(&nna->lock);
17525797b1c1STejun Heo
17535797b1c1STejun Heo if (list_empty(&pwq->pending_node))
17545797b1c1STejun Heo list_add_tail(&pwq->pending_node, &nna->pending_pwqs);
17555797b1c1STejun Heo else if (likely(!fill))
17565797b1c1STejun Heo goto out_unlock;
17575797b1c1STejun Heo
17585797b1c1STejun Heo smp_mb();
17595797b1c1STejun Heo
17605797b1c1STejun Heo obtained = tryinc_node_nr_active(nna);
17615797b1c1STejun Heo
17625797b1c1STejun Heo /*
17635797b1c1STejun Heo * If @fill, @pwq might have already been pending. Being spuriously
17645797b1c1STejun Heo * pending in cold paths doesn't affect anything. Let's leave it be.
17655797b1c1STejun Heo */
17665797b1c1STejun Heo if (obtained && likely(!fill))
17675797b1c1STejun Heo list_del_init(&pwq->pending_node);
17685797b1c1STejun Heo
17695797b1c1STejun Heo out_unlock:
17705797b1c1STejun Heo raw_spin_unlock(&nna->lock);
17715797b1c1STejun Heo out:
17725797b1c1STejun Heo if (obtained)
17735797b1c1STejun Heo pwq->nr_active++;
17741c270b79STejun Heo return obtained;
17751c270b79STejun Heo }
17761c270b79STejun Heo
17771c270b79STejun Heo /**
17781c270b79STejun Heo * pwq_activate_first_inactive - Activate the first inactive work item on a pwq
17791c270b79STejun Heo * @pwq: pool_workqueue of interest
17805797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level
17811c270b79STejun Heo *
17821c270b79STejun Heo * Activate the first inactive work item of @pwq if available and allowed by
17831c270b79STejun Heo * max_active limit.
17841c270b79STejun Heo *
17851c270b79STejun Heo * Returns %true if an inactive work item has been activated. %false if no
17861c270b79STejun Heo * inactive work item is found or max_active limit is reached.
17871c270b79STejun Heo */
pwq_activate_first_inactive(struct pool_workqueue * pwq,bool fill)17885797b1c1STejun Heo static bool pwq_activate_first_inactive(struct pool_workqueue *pwq, bool fill)
17891c270b79STejun Heo {
17901c270b79STejun Heo struct work_struct *work =
17911c270b79STejun Heo list_first_entry_or_null(&pwq->inactive_works,
17923aa62497SLai Jiangshan struct work_struct, entry);
17933aa62497SLai Jiangshan
17945797b1c1STejun Heo if (work && pwq_tryinc_nr_active(pwq, fill)) {
17951c270b79STejun Heo __pwq_activate_work(pwq, work);
17961c270b79STejun Heo return true;
17971c270b79STejun Heo } else {
17981c270b79STejun Heo return false;
17991c270b79STejun Heo }
18001c270b79STejun Heo }
18011c270b79STejun Heo
18021c270b79STejun Heo /**
1803516d3dc9SWaiman Long * unplug_oldest_pwq - unplug the oldest pool_workqueue
1804516d3dc9SWaiman Long * @wq: workqueue_struct where its oldest pwq is to be unplugged
18054c065dbcSWaiman Long *
1806516d3dc9SWaiman Long * This function should only be called for ordered workqueues where only the
1807516d3dc9SWaiman Long * oldest pwq is unplugged, the others are plugged to suspend execution to
1808516d3dc9SWaiman Long * ensure proper work item ordering::
18094c065dbcSWaiman Long *
18104c065dbcSWaiman Long * dfl_pwq --------------+ [P] - plugged
18114c065dbcSWaiman Long * |
18124c065dbcSWaiman Long * v
18134c065dbcSWaiman Long * pwqs -> A -> B [P] -> C [P] (newest)
18144c065dbcSWaiman Long * | | |
18154c065dbcSWaiman Long * 1 3 5
18164c065dbcSWaiman Long * | | |
18174c065dbcSWaiman Long * 2 4 6
1818516d3dc9SWaiman Long *
1819516d3dc9SWaiman Long * When the oldest pwq is drained and removed, this function should be called
1820516d3dc9SWaiman Long * to unplug the next oldest one to start its work item execution. Note that
1821516d3dc9SWaiman Long * pwq's are linked into wq->pwqs with the oldest first, so the first one in
1822516d3dc9SWaiman Long * the list is the oldest.
18234c065dbcSWaiman Long */
unplug_oldest_pwq(struct workqueue_struct * wq)18244c065dbcSWaiman Long static void unplug_oldest_pwq(struct workqueue_struct *wq)
18254c065dbcSWaiman Long {
18264c065dbcSWaiman Long struct pool_workqueue *pwq;
18274c065dbcSWaiman Long
18284c065dbcSWaiman Long lockdep_assert_held(&wq->mutex);
18294c065dbcSWaiman Long
18304c065dbcSWaiman Long /* Caller should make sure that pwqs isn't empty before calling */
18314c065dbcSWaiman Long pwq = list_first_entry_or_null(&wq->pwqs, struct pool_workqueue,
18324c065dbcSWaiman Long pwqs_node);
18334c065dbcSWaiman Long raw_spin_lock_irq(&pwq->pool->lock);
18344c065dbcSWaiman Long if (pwq->plugged) {
18354c065dbcSWaiman Long pwq->plugged = false;
18364c065dbcSWaiman Long if (pwq_activate_first_inactive(pwq, true))
18374c065dbcSWaiman Long kick_pool(pwq->pool);
18384c065dbcSWaiman Long }
18394c065dbcSWaiman Long raw_spin_unlock_irq(&pwq->pool->lock);
18404c065dbcSWaiman Long }
18414c065dbcSWaiman Long
18424c065dbcSWaiman Long /**
18435797b1c1STejun Heo * node_activate_pending_pwq - Activate a pending pwq on a wq_node_nr_active
18445797b1c1STejun Heo * @nna: wq_node_nr_active to activate a pending pwq for
18455797b1c1STejun Heo * @caller_pool: worker_pool the caller is locking
18465797b1c1STejun Heo *
18475797b1c1STejun Heo * Activate a pwq in @nna->pending_pwqs. Called with @caller_pool locked.
18485797b1c1STejun Heo * @caller_pool may be unlocked and relocked to lock other worker_pools.
18495797b1c1STejun Heo */
node_activate_pending_pwq(struct wq_node_nr_active * nna,struct worker_pool * caller_pool)18505797b1c1STejun Heo static void node_activate_pending_pwq(struct wq_node_nr_active *nna,
18515797b1c1STejun Heo struct worker_pool *caller_pool)
18525797b1c1STejun Heo {
18535797b1c1STejun Heo struct worker_pool *locked_pool = caller_pool;
18545797b1c1STejun Heo struct pool_workqueue *pwq;
18555797b1c1STejun Heo struct work_struct *work;
18565797b1c1STejun Heo
18575797b1c1STejun Heo lockdep_assert_held(&caller_pool->lock);
18585797b1c1STejun Heo
18595797b1c1STejun Heo raw_spin_lock(&nna->lock);
18605797b1c1STejun Heo retry:
18615797b1c1STejun Heo pwq = list_first_entry_or_null(&nna->pending_pwqs,
18625797b1c1STejun Heo struct pool_workqueue, pending_node);
18635797b1c1STejun Heo if (!pwq)
18645797b1c1STejun Heo goto out_unlock;
18655797b1c1STejun Heo
18665797b1c1STejun Heo /*
18675797b1c1STejun Heo * If @pwq is for a different pool than @locked_pool, we need to lock
18685797b1c1STejun Heo * @pwq->pool->lock. Let's trylock first. If unsuccessful, do the unlock
18695797b1c1STejun Heo * / lock dance. For that, we also need to release @nna->lock as it's
18705797b1c1STejun Heo * nested inside pool locks.
18715797b1c1STejun Heo */
18725797b1c1STejun Heo if (pwq->pool != locked_pool) {
18735797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock);
18745797b1c1STejun Heo locked_pool = pwq->pool;
18755797b1c1STejun Heo if (!raw_spin_trylock(&locked_pool->lock)) {
18765797b1c1STejun Heo raw_spin_unlock(&nna->lock);
18775797b1c1STejun Heo raw_spin_lock(&locked_pool->lock);
18785797b1c1STejun Heo raw_spin_lock(&nna->lock);
18795797b1c1STejun Heo goto retry;
18805797b1c1STejun Heo }
18815797b1c1STejun Heo }
18825797b1c1STejun Heo
18835797b1c1STejun Heo /*
18845797b1c1STejun Heo * $pwq may not have any inactive work items due to e.g. cancellations.
18855797b1c1STejun Heo * Drop it from pending_pwqs and see if there's another one.
18865797b1c1STejun Heo */
18875797b1c1STejun Heo work = list_first_entry_or_null(&pwq->inactive_works,
18885797b1c1STejun Heo struct work_struct, entry);
18895797b1c1STejun Heo if (!work) {
18905797b1c1STejun Heo list_del_init(&pwq->pending_node);
18915797b1c1STejun Heo goto retry;
18925797b1c1STejun Heo }
18935797b1c1STejun Heo
18945797b1c1STejun Heo /*
18955797b1c1STejun Heo * Acquire an nr_active count and activate the inactive work item. If
18965797b1c1STejun Heo * $pwq still has inactive work items, rotate it to the end of the
18975797b1c1STejun Heo * pending_pwqs so that we round-robin through them. This means that
18985797b1c1STejun Heo * inactive work items are not activated in queueing order which is fine
18995797b1c1STejun Heo * given that there has never been any ordering across different pwqs.
19005797b1c1STejun Heo */
19015797b1c1STejun Heo if (likely(tryinc_node_nr_active(nna))) {
19025797b1c1STejun Heo pwq->nr_active++;
19035797b1c1STejun Heo __pwq_activate_work(pwq, work);
19045797b1c1STejun Heo
19055797b1c1STejun Heo if (list_empty(&pwq->inactive_works))
19065797b1c1STejun Heo list_del_init(&pwq->pending_node);
19075797b1c1STejun Heo else
19085797b1c1STejun Heo list_move_tail(&pwq->pending_node, &nna->pending_pwqs);
19095797b1c1STejun Heo
19105797b1c1STejun Heo /* if activating a foreign pool, make sure it's running */
19115797b1c1STejun Heo if (pwq->pool != caller_pool)
19125797b1c1STejun Heo kick_pool(pwq->pool);
19135797b1c1STejun Heo }
19145797b1c1STejun Heo
19155797b1c1STejun Heo out_unlock:
19165797b1c1STejun Heo raw_spin_unlock(&nna->lock);
19175797b1c1STejun Heo if (locked_pool != caller_pool) {
19185797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock);
19195797b1c1STejun Heo raw_spin_lock(&caller_pool->lock);
19205797b1c1STejun Heo }
19215797b1c1STejun Heo }
19225797b1c1STejun Heo
19235797b1c1STejun Heo /**
19241c270b79STejun Heo * pwq_dec_nr_active - Retire an active count
19251c270b79STejun Heo * @pwq: pool_workqueue of interest
19261c270b79STejun Heo *
19271c270b79STejun Heo * Decrement @pwq's nr_active and try to activate the first inactive work item.
19285797b1c1STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock.
19291c270b79STejun Heo */
pwq_dec_nr_active(struct pool_workqueue * pwq)19301c270b79STejun Heo static void pwq_dec_nr_active(struct pool_workqueue *pwq)
19311c270b79STejun Heo {
19321c270b79STejun Heo struct worker_pool *pool = pwq->pool;
193391ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(pwq->wq, pool->node);
19341c270b79STejun Heo
19351c270b79STejun Heo lockdep_assert_held(&pool->lock);
19361c270b79STejun Heo
193791ccc6e7STejun Heo /*
193891ccc6e7STejun Heo * @pwq->nr_active should be decremented for both percpu and unbound
193991ccc6e7STejun Heo * workqueues.
194091ccc6e7STejun Heo */
19411c270b79STejun Heo pwq->nr_active--;
194291ccc6e7STejun Heo
194391ccc6e7STejun Heo /*
194491ccc6e7STejun Heo * For a percpu workqueue, it's simple. Just need to kick the first
194591ccc6e7STejun Heo * inactive work item on @pwq itself.
194691ccc6e7STejun Heo */
194791ccc6e7STejun Heo if (!nna) {
19485797b1c1STejun Heo pwq_activate_first_inactive(pwq, false);
194991ccc6e7STejun Heo return;
195091ccc6e7STejun Heo }
195191ccc6e7STejun Heo
19525797b1c1STejun Heo /*
19535797b1c1STejun Heo * If @pwq is for an unbound workqueue, it's more complicated because
19545797b1c1STejun Heo * multiple pwqs and pools may be sharing the nr_active count. When a
19555797b1c1STejun Heo * pwq needs to wait for an nr_active count, it puts itself on
19565797b1c1STejun Heo * $nna->pending_pwqs. The following atomic_dec_return()'s implied
19575797b1c1STejun Heo * memory barrier is paired with smp_mb() in pwq_tryinc_nr_active() to
19585797b1c1STejun Heo * guarantee that either we see non-empty pending_pwqs or they see
19595797b1c1STejun Heo * decremented $nna->nr.
19605797b1c1STejun Heo *
19615797b1c1STejun Heo * $nna->max may change as CPUs come online/offline and @pwq->wq's
19625797b1c1STejun Heo * max_active gets updated. However, it is guaranteed to be equal to or
19635797b1c1STejun Heo * larger than @pwq->wq->min_active which is above zero unless freezing.
19645797b1c1STejun Heo * This maintains the forward progress guarantee.
19655797b1c1STejun Heo */
19665797b1c1STejun Heo if (atomic_dec_return(&nna->nr) >= READ_ONCE(nna->max))
19675797b1c1STejun Heo return;
19685797b1c1STejun Heo
19695797b1c1STejun Heo if (!list_empty(&nna->pending_pwqs))
19705797b1c1STejun Heo node_activate_pending_pwq(nna, pool);
19713aa62497SLai Jiangshan }
19723aa62497SLai Jiangshan
1973bf4ede01STejun Heo /**
1974112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1975112202d9STejun Heo * @pwq: pwq of interest
1976c4560c2cSLai Jiangshan * @work_data: work_data of work which left the queue
1977bf4ede01STejun Heo *
1978bf4ede01STejun Heo * A work either has completed or is removed from pending queue,
1979112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing.
1980bf4ede01STejun Heo *
1981dd6c3c54STejun Heo * NOTE:
1982dd6c3c54STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock
1983dd6c3c54STejun Heo * and thus should be called after all other state updates for the in-flight
1984dd6c3c54STejun Heo * work item is complete.
1985dd6c3c54STejun Heo *
1986bf4ede01STejun Heo * CONTEXT:
1987a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock).
1988bf4ede01STejun Heo */
pwq_dec_nr_in_flight(struct pool_workqueue * pwq,unsigned long work_data)1989c4560c2cSLai Jiangshan static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data)
1990bf4ede01STejun Heo {
1991c4560c2cSLai Jiangshan int color = get_work_color(work_data);
1992c4560c2cSLai Jiangshan
19931c270b79STejun Heo if (!(work_data & WORK_STRUCT_INACTIVE))
19941c270b79STejun Heo pwq_dec_nr_active(pwq);
1995018f3a13SLai Jiangshan
1996018f3a13SLai Jiangshan pwq->nr_in_flight[color]--;
1997bf4ede01STejun Heo
1998bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */
1999112202d9STejun Heo if (likely(pwq->flush_color != color))
20008864b4e5STejun Heo goto out_put;
2001bf4ede01STejun Heo
2002bf4ede01STejun Heo /* are there still in-flight works? */
2003112202d9STejun Heo if (pwq->nr_in_flight[color])
20048864b4e5STejun Heo goto out_put;
2005bf4ede01STejun Heo
2006112202d9STejun Heo /* this pwq is done, clear flush_color */
2007112202d9STejun Heo pwq->flush_color = -1;
2008bf4ede01STejun Heo
2009bf4ede01STejun Heo /*
2010112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It
2011bf4ede01STejun Heo * will handle the rest.
2012bf4ede01STejun Heo */
2013112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
2014112202d9STejun Heo complete(&pwq->wq->first_flusher->done);
20158864b4e5STejun Heo out_put:
20168864b4e5STejun Heo put_pwq(pwq);
2017bf4ede01STejun Heo }
2018bf4ede01STejun Heo
201936e227d2STejun Heo /**
2020bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq
202136e227d2STejun Heo * @work: work item to steal
2022c5f5b942STejun Heo * @cflags: %WORK_CANCEL_ flags
2023c26e2f2eSTejun Heo * @irq_flags: place to store irq state
202436e227d2STejun Heo *
202536e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any
2026d185af30SYacine Belkadi * stable state - idle, on timer or on worklist.
202736e227d2STejun Heo *
2028d185af30SYacine Belkadi * Return:
20293eb6b31bSMauro Carvalho Chehab *
20303eb6b31bSMauro Carvalho Chehab * ======== ================================================================
203136e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING
203236e227d2STejun Heo * 0 if @work was idle and we claimed PENDING
203336e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
20343eb6b31bSMauro Carvalho Chehab * ======== ================================================================
203536e227d2STejun Heo *
2036d185af30SYacine Belkadi * Note:
2037bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
2038e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be
2039e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being
2040e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time.
2041bbb68dfaSTejun Heo *
2042bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is
2043c26e2f2eSTejun Heo * responsible for releasing it using local_irq_restore(*@irq_flags).
2044bbb68dfaSTejun Heo *
2045e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler.
2046bf4ede01STejun Heo */
try_to_grab_pending(struct work_struct * work,u32 cflags,unsigned long * irq_flags)2047c5f5b942STejun Heo static int try_to_grab_pending(struct work_struct *work, u32 cflags,
2048c26e2f2eSTejun Heo unsigned long *irq_flags)
2049bf4ede01STejun Heo {
2050d565ed63STejun Heo struct worker_pool *pool;
2051112202d9STejun Heo struct pool_workqueue *pwq;
2052bf4ede01STejun Heo
2053c26e2f2eSTejun Heo local_irq_save(*irq_flags);
2054bbb68dfaSTejun Heo
205536e227d2STejun Heo /* try to steal the timer if it exists */
2056c5f5b942STejun Heo if (cflags & WORK_CANCEL_DELAYED) {
205736e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work);
205836e227d2STejun Heo
2059e0aecdd8STejun Heo /*
2060*8fa7292fSThomas Gleixner * dwork->timer is irqsafe. If timer_delete() fails, it's
2061e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not
2062e0aecdd8STejun Heo * running on the local CPU.
2063e0aecdd8STejun Heo */
2064*8fa7292fSThomas Gleixner if (likely(timer_delete(&dwork->timer)))
206536e227d2STejun Heo return 1;
206636e227d2STejun Heo }
206736e227d2STejun Heo
206836e227d2STejun Heo /* try to claim PENDING the normal way */
2069bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
2070bf4ede01STejun Heo return 0;
2071bf4ede01STejun Heo
207224acfb71SThomas Gleixner rcu_read_lock();
2073bf4ede01STejun Heo /*
2074bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to
2075bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2076bf4ede01STejun Heo */
2077d565ed63STejun Heo pool = get_work_pool(work);
2078d565ed63STejun Heo if (!pool)
2079bbb68dfaSTejun Heo goto fail;
2080bf4ede01STejun Heo
2081a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pool->lock);
2082bf4ede01STejun Heo /*
2083112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work
2084112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point
2085112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under
2086112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data
2087112202d9STejun Heo * points to pwq which is associated with a locked pool, the work
20880b3dae68SLai Jiangshan * item is currently queued on that pool.
2089bf4ede01STejun Heo */
2090112202d9STejun Heo pwq = get_work_pwq(work);
2091112202d9STejun Heo if (pwq && pwq->pool == pool) {
2092b56c7207SLai Jiangshan unsigned long work_data = *work_data_bits(work);
2093c70e1779STejun Heo
2094bf4ede01STejun Heo debug_work_deactivate(work);
20953aa62497SLai Jiangshan
20963aa62497SLai Jiangshan /*
2097018f3a13SLai Jiangshan * A cancelable inactive work item must be in the
2098018f3a13SLai Jiangshan * pwq->inactive_works since a queued barrier can't be
2099018f3a13SLai Jiangshan * canceled (see the comments in insert_wq_barrier()).
2100018f3a13SLai Jiangshan *
2101b56c7207SLai Jiangshan * An inactive work item cannot be deleted directly because
2102d812796eSLai Jiangshan * it might have linked barrier work items which, if left
2103f97a4a1aSLai Jiangshan * on the inactive_works list, will confuse pwq->nr_active
2104b56c7207SLai Jiangshan * management later on and cause stall. Move the linked
2105b56c7207SLai Jiangshan * barrier work items to the worklist when deleting the grabbed
2106b56c7207SLai Jiangshan * item. Also keep WORK_STRUCT_INACTIVE in work_data, so that
2107b56c7207SLai Jiangshan * it doesn't participate in nr_active management in later
2108b56c7207SLai Jiangshan * pwq_dec_nr_in_flight().
21093aa62497SLai Jiangshan */
2110b56c7207SLai Jiangshan if (work_data & WORK_STRUCT_INACTIVE)
2111b56c7207SLai Jiangshan move_linked_works(work, &pwq->pool->worklist, NULL);
21123aa62497SLai Jiangshan
2113bf4ede01STejun Heo list_del_init(&work->entry);
211436e227d2STejun Heo
2115c70e1779STejun Heo /*
2116c70e1779STejun Heo * work->data points to pwq iff queued. Let's point to pool. As
2117c70e1779STejun Heo * this destroys work->data needed by the next step, stash it.
2118c70e1779STejun Heo */
2119456a78eeSTejun Heo set_work_pool_and_keep_pending(work, pool->id,
2120456a78eeSTejun Heo pool_offq_flags(pool));
21214468a00fSLai Jiangshan
2122dd6c3c54STejun Heo /* must be the last step, see the function comment */
2123c70e1779STejun Heo pwq_dec_nr_in_flight(pwq, work_data);
2124dd6c3c54STejun Heo
2125a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock);
212624acfb71SThomas Gleixner rcu_read_unlock();
212736e227d2STejun Heo return 1;
2128bf4ede01STejun Heo }
2129a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock);
2130bbb68dfaSTejun Heo fail:
213124acfb71SThomas Gleixner rcu_read_unlock();
2132c26e2f2eSTejun Heo local_irq_restore(*irq_flags);
213336e227d2STejun Heo return -EAGAIN;
2134bf4ede01STejun Heo }
2135bf4ede01STejun Heo
2136978b8409STejun Heo /**
2137978b8409STejun Heo * work_grab_pending - steal work item from worklist and disable irq
2138978b8409STejun Heo * @work: work item to steal
2139978b8409STejun Heo * @cflags: %WORK_CANCEL_ flags
2140978b8409STejun Heo * @irq_flags: place to store IRQ state
2141978b8409STejun Heo *
2142978b8409STejun Heo * Grab PENDING bit of @work. @work can be in any stable state - idle, on timer
2143978b8409STejun Heo * or on worklist.
2144978b8409STejun Heo *
2145f09b10b6STejun Heo * Can be called from any context. IRQ is disabled on return with IRQ state
2146978b8409STejun Heo * stored in *@irq_flags. The caller is responsible for re-enabling it using
2147978b8409STejun Heo * local_irq_restore().
2148978b8409STejun Heo *
2149978b8409STejun Heo * Returns %true if @work was pending. %false if idle.
2150978b8409STejun Heo */
work_grab_pending(struct work_struct * work,u32 cflags,unsigned long * irq_flags)2151978b8409STejun Heo static bool work_grab_pending(struct work_struct *work, u32 cflags,
2152978b8409STejun Heo unsigned long *irq_flags)
2153978b8409STejun Heo {
2154978b8409STejun Heo int ret;
2155978b8409STejun Heo
2156f09b10b6STejun Heo while (true) {
2157978b8409STejun Heo ret = try_to_grab_pending(work, cflags, irq_flags);
2158f09b10b6STejun Heo if (ret >= 0)
2159978b8409STejun Heo return ret;
2160f09b10b6STejun Heo cpu_relax();
2161f09b10b6STejun Heo }
2162978b8409STejun Heo }
2163978b8409STejun Heo
2164bf4ede01STejun Heo /**
2165706026c2STejun Heo * insert_work - insert a work into a pool
2166112202d9STejun Heo * @pwq: pwq @work belongs to
21674690c4abSTejun Heo * @work: work to insert
21684690c4abSTejun Heo * @head: insertion point
21694690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set
21704690c4abSTejun Heo *
2171112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
2172706026c2STejun Heo * work_struct flags.
21734690c4abSTejun Heo *
21744690c4abSTejun Heo * CONTEXT:
2175a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock).
2176365970a1SDavid Howells */
insert_work(struct pool_workqueue * pwq,struct work_struct * work,struct list_head * head,unsigned int extra_flags)2177112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
2178112202d9STejun Heo struct list_head *head, unsigned int extra_flags)
2179b89deed3SOleg Nesterov {
2180fe089f87STejun Heo debug_work_activate(work);
2181e1d8aa9fSFrederic Weisbecker
2182e89a85d6SWalter Wu /* record the work call stack in order to print it in KASAN reports */
2183d40797d6SPeter Zijlstra kasan_record_aux_stack(work);
2184e89a85d6SWalter Wu
21854690c4abSTejun Heo /* we own @work, set data and link */
2186112202d9STejun Heo set_work_pwq(work, pwq, extra_flags);
21871a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head);
21888864b4e5STejun Heo get_pwq(pwq);
2189b89deed3SOleg Nesterov }
2190b89deed3SOleg Nesterov
2191c8efcc25STejun Heo /*
2192c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the
21938d03ecfeSTejun Heo * same workqueue.
2194c8efcc25STejun Heo */
is_chained_work(struct workqueue_struct * wq)2195c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq)
2196c8efcc25STejun Heo {
2197c8efcc25STejun Heo struct worker *worker;
2198c8efcc25STejun Heo
21998d03ecfeSTejun Heo worker = current_wq_worker();
2200c8efcc25STejun Heo /*
2201bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If
22028d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking.
2203c8efcc25STejun Heo */
2204112202d9STejun Heo return worker && worker->current_pwq->wq == wq;
2205c8efcc25STejun Heo }
2206c8efcc25STejun Heo
2207ef557180SMike Galbraith /*
2208ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed
2209ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to
2210ef557180SMike Galbraith * avoid perturbing sensitive tasks.
2211ef557180SMike Galbraith */
wq_select_unbound_cpu(int cpu)2212ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu)
2213ef557180SMike Galbraith {
2214ef557180SMike Galbraith int new_cpu;
2215ef557180SMike Galbraith
2216f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) {
2217ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask))
2218ef557180SMike Galbraith return cpu;
2219a8ec5880SAmmar Faizi } else {
2220a8ec5880SAmmar Faizi pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n");
2221f303fccbSTejun Heo }
2222f303fccbSTejun Heo
2223ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last);
2224ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask);
2225ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) {
2226ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask);
2227ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids))
2228ef557180SMike Galbraith return cpu;
2229ef557180SMike Galbraith }
2230ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu);
2231ef557180SMike Galbraith
2232ef557180SMike Galbraith return new_cpu;
2233ef557180SMike Galbraith }
2234ef557180SMike Galbraith
__queue_work(int cpu,struct workqueue_struct * wq,struct work_struct * work)2235d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq,
22361da177e4SLinus Torvalds struct work_struct *work)
22371da177e4SLinus Torvalds {
2238112202d9STejun Heo struct pool_workqueue *pwq;
2239fe089f87STejun Heo struct worker_pool *last_pool, *pool;
22408a2e8e5dSTejun Heo unsigned int work_flags;
2241b75cac93SJoonsoo Kim unsigned int req_cpu = cpu;
22428930cabaSTejun Heo
22438930cabaSTejun Heo /*
22448930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to
22458930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get
22468930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should
22478930cabaSTejun Heo * happen with IRQ disabled.
22488930cabaSTejun Heo */
22498e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled();
22501da177e4SLinus Torvalds
225133e3f0a3SRichard Clark /*
225233e3f0a3SRichard Clark * For a draining wq, only works from the same workqueue are
225333e3f0a3SRichard Clark * allowed. The __WQ_DESTROYING helps to spot the issue that
225433e3f0a3SRichard Clark * queues a new work item to a wq after destroy_workqueue(wq).
225533e3f0a3SRichard Clark */
225633e3f0a3SRichard Clark if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) &&
22578221fd1aSWill Deacon WARN_ONCE(!is_chained_work(wq), "workqueue: cannot queue %ps on wq %s\n",
22588221fd1aSWill Deacon work->func, wq->name))) {
2259e41e704bSTejun Heo return;
22608221fd1aSWill Deacon }
226124acfb71SThomas Gleixner rcu_read_lock();
22629e8cd2f5STejun Heo retry:
2263aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */
2264636b927eSTejun Heo if (req_cpu == WORK_CPU_UNBOUND) {
2265636b927eSTejun Heo if (wq->flags & WQ_UNBOUND)
2266ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id());
2267636b927eSTejun Heo else
2268aa202f1fSHillf Danton cpu = raw_smp_processor_id();
2269aa202f1fSHillf Danton }
2270f3421797STejun Heo
2271636b927eSTejun Heo pwq = rcu_dereference(*per_cpu_ptr(wq->cpu_pwq, cpu));
2272fe089f87STejun Heo pool = pwq->pool;
2273fe089f87STejun Heo
227418aa9effSTejun Heo /*
2275c9178087STejun Heo * If @work was previously on a different pool, it might still be
2276c9178087STejun Heo * running there, in which case the work needs to be queued on that
2277c9178087STejun Heo * pool to guarantee non-reentrancy.
227858629d48SLai Jiangshan *
227958629d48SLai Jiangshan * For ordered workqueue, work items must be queued on the newest pwq
228058629d48SLai Jiangshan * for accurate order management. Guaranteed order also guarantees
228158629d48SLai Jiangshan * non-reentrancy. See the comments above unplug_oldest_pwq().
228218aa9effSTejun Heo */
2283c9e7cf27STejun Heo last_pool = get_work_pool(work);
228458629d48SLai Jiangshan if (last_pool && last_pool != pool && !(wq->flags & __WQ_ORDERED)) {
228518aa9effSTejun Heo struct worker *worker;
228618aa9effSTejun Heo
2287a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&last_pool->lock);
228818aa9effSTejun Heo
2289c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work);
229018aa9effSTejun Heo
2291112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) {
2292c9178087STejun Heo pwq = worker->current_pwq;
2293fe089f87STejun Heo pool = pwq->pool;
2294fe089f87STejun Heo WARN_ON_ONCE(pool != last_pool);
22958594fadeSLai Jiangshan } else {
229618aa9effSTejun Heo /* meh... not running there, queue here */
2297a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&last_pool->lock);
2298fe089f87STejun Heo raw_spin_lock(&pool->lock);
229918aa9effSTejun Heo }
23008930cabaSTejun Heo } else {
2301fe089f87STejun Heo raw_spin_lock(&pool->lock);
23028930cabaSTejun Heo }
2303502ca9d8STejun Heo
23049e8cd2f5STejun Heo /*
2305636b927eSTejun Heo * pwq is determined and locked. For unbound pools, we could have raced
2306636b927eSTejun Heo * with pwq release and it could already be dead. If its refcnt is zero,
2307636b927eSTejun Heo * repeat pwq selection. Note that unbound pwqs never die without
2308636b927eSTejun Heo * another pwq replacing it in cpu_pwq or while work items are executing
2309636b927eSTejun Heo * on it, so the retrying is guaranteed to make forward-progress.
23109e8cd2f5STejun Heo */
23119e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) {
23129e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) {
2313fe089f87STejun Heo raw_spin_unlock(&pool->lock);
23149e8cd2f5STejun Heo cpu_relax();
23159e8cd2f5STejun Heo goto retry;
23169e8cd2f5STejun Heo }
23179e8cd2f5STejun Heo /* oops */
23189e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
23199e8cd2f5STejun Heo wq->name, cpu);
23209e8cd2f5STejun Heo }
23219e8cd2f5STejun Heo
2322112202d9STejun Heo /* pwq determined, queue */
2323112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work);
2324502ca9d8STejun Heo
232524acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry)))
232624acfb71SThomas Gleixner goto out;
23271e19ffc6STejun Heo
2328112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++;
2329112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color);
23301e19ffc6STejun Heo
2331a045a272STejun Heo /*
2332a045a272STejun Heo * Limit the number of concurrently active work items to max_active.
2333a045a272STejun Heo * @work must also queue behind existing inactive work items to maintain
2334a045a272STejun Heo * ordering when max_active changes. See wq_adjust_max_active().
2335a045a272STejun Heo */
23365797b1c1STejun Heo if (list_empty(&pwq->inactive_works) && pwq_tryinc_nr_active(pwq, false)) {
2337fe089f87STejun Heo if (list_empty(&pool->worklist))
2338fe089f87STejun Heo pool->watchdog_ts = jiffies;
2339fe089f87STejun Heo
2340cdadf009STejun Heo trace_workqueue_activate_work(work);
2341fe089f87STejun Heo insert_work(pwq, work, &pool->worklist, work_flags);
23420219a352STejun Heo kick_pool(pool);
23438a2e8e5dSTejun Heo } else {
2344f97a4a1aSLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE;
2345fe089f87STejun Heo insert_work(pwq, work, &pwq->inactive_works, work_flags);
23468a2e8e5dSTejun Heo }
23471e19ffc6STejun Heo
234824acfb71SThomas Gleixner out:
2349fe089f87STejun Heo raw_spin_unlock(&pool->lock);
235024acfb71SThomas Gleixner rcu_read_unlock();
23511da177e4SLinus Torvalds }
23521da177e4SLinus Torvalds
clear_pending_if_disabled(struct work_struct * work)235386898fa6STejun Heo static bool clear_pending_if_disabled(struct work_struct *work)
235486898fa6STejun Heo {
235586898fa6STejun Heo unsigned long data = *work_data_bits(work);
235686898fa6STejun Heo struct work_offq_data offqd;
235786898fa6STejun Heo
235886898fa6STejun Heo if (likely((data & WORK_STRUCT_PWQ) ||
235986898fa6STejun Heo !(data & WORK_OFFQ_DISABLE_MASK)))
236086898fa6STejun Heo return false;
236186898fa6STejun Heo
236286898fa6STejun Heo work_offqd_unpack(&offqd, data);
236386898fa6STejun Heo set_work_pool_and_clear_pending(work, offqd.pool_id,
236486898fa6STejun Heo work_offqd_pack_flags(&offqd));
236586898fa6STejun Heo return true;
236686898fa6STejun Heo }
236786898fa6STejun Heo
23680fcb78c2SRolf Eike Beer /**
2369c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu
2370c1a220e7SZhang Rui * @cpu: CPU number to execute work on
2371c1a220e7SZhang Rui * @wq: workqueue to use
2372c1a220e7SZhang Rui * @work: work to queue
2373c1a220e7SZhang Rui *
2374c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it
2375443378f0SPaul E. McKenney * can't go away. Callers that fail to ensure that the specified
2376443378f0SPaul E. McKenney * CPU cannot go away will execute on a randomly chosen CPU.
2377854f5cc5SPaul E. McKenney * But note well that callers specifying a CPU that never has been
2378854f5cc5SPaul E. McKenney * online will get a splat.
2379d185af30SYacine Belkadi *
2380d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise.
2381c1a220e7SZhang Rui */
queue_work_on(int cpu,struct workqueue_struct * wq,struct work_struct * work)2382d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq,
2383d4283e93STejun Heo struct work_struct *work)
2384c1a220e7SZhang Rui {
2385d4283e93STejun Heo bool ret = false;
2386c26e2f2eSTejun Heo unsigned long irq_flags;
23878930cabaSTejun Heo
2388c26e2f2eSTejun Heo local_irq_save(irq_flags);
2389c1a220e7SZhang Rui
239086898fa6STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) &&
239186898fa6STejun Heo !clear_pending_if_disabled(work)) {
23924690c4abSTejun Heo __queue_work(cpu, wq, work);
2393d4283e93STejun Heo ret = true;
2394c1a220e7SZhang Rui }
23958930cabaSTejun Heo
2396c26e2f2eSTejun Heo local_irq_restore(irq_flags);
2397c1a220e7SZhang Rui return ret;
2398c1a220e7SZhang Rui }
2399ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on);
2400c1a220e7SZhang Rui
24018204e0c1SAlexander Duyck /**
2402fef59c9cSTejun Heo * select_numa_node_cpu - Select a CPU based on NUMA node
24038204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from
24048204e0c1SAlexander Duyck *
24058204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given
24068204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return
24078204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any
24088204e0c1SAlexander Duyck * available CPU if we need to schedule this work.
24098204e0c1SAlexander Duyck */
select_numa_node_cpu(int node)2410fef59c9cSTejun Heo static int select_numa_node_cpu(int node)
24118204e0c1SAlexander Duyck {
24128204e0c1SAlexander Duyck int cpu;
24138204e0c1SAlexander Duyck
24148204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */
24158204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node))
24168204e0c1SAlexander Duyck return WORK_CPU_UNBOUND;
24178204e0c1SAlexander Duyck
24188204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */
24198204e0c1SAlexander Duyck cpu = raw_smp_processor_id();
24208204e0c1SAlexander Duyck if (node == cpu_to_node(cpu))
24218204e0c1SAlexander Duyck return cpu;
24228204e0c1SAlexander Duyck
24238204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */
24248204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask);
24258204e0c1SAlexander Duyck
24268204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */
24278204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND;
24288204e0c1SAlexander Duyck }
24298204e0c1SAlexander Duyck
24308204e0c1SAlexander Duyck /**
24318204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node
24328204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for
24338204e0c1SAlexander Duyck * @wq: workqueue to use
24348204e0c1SAlexander Duyck * @work: work to queue
24358204e0c1SAlexander Duyck *
24368204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic
24378204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given
24388204e0c1SAlexander Duyck * NUMA node.
24398204e0c1SAlexander Duyck *
24408204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto
24418204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is
24428204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior.
24438204e0c1SAlexander Duyck *
24448204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the
24458204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we
24468204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU.
24478204e0c1SAlexander Duyck *
24488204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise.
24498204e0c1SAlexander Duyck */
queue_work_node(int node,struct workqueue_struct * wq,struct work_struct * work)24508204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq,
24518204e0c1SAlexander Duyck struct work_struct *work)
24528204e0c1SAlexander Duyck {
2453c26e2f2eSTejun Heo unsigned long irq_flags;
24548204e0c1SAlexander Duyck bool ret = false;
24558204e0c1SAlexander Duyck
24568204e0c1SAlexander Duyck /*
24578204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues.
24588204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given
24598204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node.
24608204e0c1SAlexander Duyck *
24618204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in
24628204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for
24638204e0c1SAlexander Duyck * some round robin type logic.
24648204e0c1SAlexander Duyck */
24658204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND));
24668204e0c1SAlexander Duyck
2467c26e2f2eSTejun Heo local_irq_save(irq_flags);
24688204e0c1SAlexander Duyck
246986898fa6STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) &&
247086898fa6STejun Heo !clear_pending_if_disabled(work)) {
2471fef59c9cSTejun Heo int cpu = select_numa_node_cpu(node);
24728204e0c1SAlexander Duyck
24738204e0c1SAlexander Duyck __queue_work(cpu, wq, work);
24748204e0c1SAlexander Duyck ret = true;
24758204e0c1SAlexander Duyck }
24768204e0c1SAlexander Duyck
2477c26e2f2eSTejun Heo local_irq_restore(irq_flags);
24788204e0c1SAlexander Duyck return ret;
24798204e0c1SAlexander Duyck }
24808204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node);
24818204e0c1SAlexander Duyck
delayed_work_timer_fn(struct timer_list * t)24828c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t)
24831da177e4SLinus Torvalds {
24848c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer);
24851da177e4SLinus Torvalds
2486e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */
248760c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work);
24881da177e4SLinus Torvalds }
24891438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn);
24901da177e4SLinus Torvalds
__queue_delayed_work(int cpu,struct workqueue_struct * wq,struct delayed_work * dwork,unsigned long delay)24917beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
249252bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay)
24931da177e4SLinus Torvalds {
24947beb2edfSTejun Heo struct timer_list *timer = &dwork->timer;
24957beb2edfSTejun Heo struct work_struct *work = &dwork->work;
24961da177e4SLinus Torvalds
2497637fdbaeSTejun Heo WARN_ON_ONCE(!wq);
24984b243563SSami Tolvanen WARN_ON_ONCE(timer->function != delayed_work_timer_fn);
2499fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer));
2500fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry));
25017beb2edfSTejun Heo
25028852aac2STejun Heo /*
25038852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for
25048852aac2STejun Heo * both optimization and correctness. The earliest @timer can
25058852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend
25068852aac2STejun Heo * on that there's no such delay when @delay is 0.
25078852aac2STejun Heo */
25088852aac2STejun Heo if (!delay) {
25098852aac2STejun Heo __queue_work(cpu, wq, &dwork->work);
25108852aac2STejun Heo return;
25118852aac2STejun Heo }
25128852aac2STejun Heo
2513da30ba22SImran Khan WARN_ON_ONCE(cpu != WORK_CPU_UNBOUND && !cpu_online(cpu));
251460c057bcSLai Jiangshan dwork->wq = wq;
25151265057fSTejun Heo dwork->cpu = cpu;
25167beb2edfSTejun Heo timer->expires = jiffies + delay;
25177beb2edfSTejun Heo
2518aae17ebbSLeonardo Bras if (housekeeping_enabled(HK_TYPE_TIMER)) {
2519aae17ebbSLeonardo Bras /* If the current cpu is a housekeeping cpu, use it. */
2520aae17ebbSLeonardo Bras cpu = smp_processor_id();
2521aae17ebbSLeonardo Bras if (!housekeeping_test_cpu(cpu, HK_TYPE_TIMER))
2522aae17ebbSLeonardo Bras cpu = housekeeping_any_cpu(HK_TYPE_TIMER);
25237beb2edfSTejun Heo add_timer_on(timer, cpu);
2524aae17ebbSLeonardo Bras } else {
2525aae17ebbSLeonardo Bras if (likely(cpu == WORK_CPU_UNBOUND))
2526c0e8c5b5SAnna-Maria Behnsen add_timer_global(timer);
2527aae17ebbSLeonardo Bras else
2528aae17ebbSLeonardo Bras add_timer_on(timer, cpu);
2529aae17ebbSLeonardo Bras }
25307beb2edfSTejun Heo }
25311da177e4SLinus Torvalds
25320fcb78c2SRolf Eike Beer /**
25330fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay
25340fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on
25350fcb78c2SRolf Eike Beer * @wq: workqueue to use
2536af9997e4SRandy Dunlap * @dwork: work to queue
25370fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing
25380fcb78c2SRolf Eike Beer *
2539da30ba22SImran Khan * We queue the delayed_work to a specific CPU, for non-zero delays the
2540da30ba22SImran Khan * caller must ensure it is online and can't go away. Callers that fail
2541da30ba22SImran Khan * to ensure this, may get @dwork->timer queued to an offlined CPU and
2542da30ba22SImran Khan * this will prevent queueing of @dwork->work unless the offlined CPU
2543da30ba22SImran Khan * becomes online again.
2544da30ba22SImran Khan *
2545d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If
2546715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate
2547715f1300STejun Heo * execution.
25480fcb78c2SRolf Eike Beer */
queue_delayed_work_on(int cpu,struct workqueue_struct * wq,struct delayed_work * dwork,unsigned long delay)2549d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
255052bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay)
25517a6bc1cdSVenkatesh Pallipadi {
255252bad64dSDavid Howells struct work_struct *work = &dwork->work;
2553d4283e93STejun Heo bool ret = false;
2554c26e2f2eSTejun Heo unsigned long irq_flags;
25558930cabaSTejun Heo
25568930cabaSTejun Heo /* read the comment in __queue_work() */
2557c26e2f2eSTejun Heo local_irq_save(irq_flags);
25587a6bc1cdSVenkatesh Pallipadi
255986898fa6STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) &&
256086898fa6STejun Heo !clear_pending_if_disabled(work)) {
25617beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay);
2562d4283e93STejun Heo ret = true;
25637a6bc1cdSVenkatesh Pallipadi }
25648930cabaSTejun Heo
2565c26e2f2eSTejun Heo local_irq_restore(irq_flags);
25667a6bc1cdSVenkatesh Pallipadi return ret;
25677a6bc1cdSVenkatesh Pallipadi }
2568ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on);
25691da177e4SLinus Torvalds
2570c8e55f36STejun Heo /**
25718376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
25728376fe22STejun Heo * @cpu: CPU number to execute work on
25738376fe22STejun Heo * @wq: workqueue to use
25748376fe22STejun Heo * @dwork: work to queue
25758376fe22STejun Heo * @delay: number of jiffies to wait before queueing
25768376fe22STejun Heo *
25778376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
25788376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is
25798376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its
25808376fe22STejun Heo * current state.
25818376fe22STejun Heo *
2582d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was
25838376fe22STejun Heo * pending and its timer was modified.
25848376fe22STejun Heo *
2585e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler.
25868376fe22STejun Heo * See try_to_grab_pending() for details.
25878376fe22STejun Heo */
mod_delayed_work_on(int cpu,struct workqueue_struct * wq,struct delayed_work * dwork,unsigned long delay)25888376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
25898376fe22STejun Heo struct delayed_work *dwork, unsigned long delay)
25908376fe22STejun Heo {
2591c26e2f2eSTejun Heo unsigned long irq_flags;
2592f09b10b6STejun Heo bool ret;
25938376fe22STejun Heo
2594f09b10b6STejun Heo ret = work_grab_pending(&dwork->work, WORK_CANCEL_DELAYED, &irq_flags);
25958376fe22STejun Heo
2596f09b10b6STejun Heo if (!clear_pending_if_disabled(&dwork->work))
25978376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay);
25988376fe22STejun Heo
2599f09b10b6STejun Heo local_irq_restore(irq_flags);
26008376fe22STejun Heo return ret;
26018376fe22STejun Heo }
26028376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on);
26038376fe22STejun Heo
rcu_work_rcufn(struct rcu_head * rcu)260405f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu)
260505f0fe6bSTejun Heo {
260605f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu);
260705f0fe6bSTejun Heo
260805f0fe6bSTejun Heo /* read the comment in __queue_work() */
260905f0fe6bSTejun Heo local_irq_disable();
261005f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work);
261105f0fe6bSTejun Heo local_irq_enable();
261205f0fe6bSTejun Heo }
261305f0fe6bSTejun Heo
261405f0fe6bSTejun Heo /**
261505f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period
261605f0fe6bSTejun Heo * @wq: workqueue to use
261705f0fe6bSTejun Heo * @rwork: work to queue
261805f0fe6bSTejun Heo *
261905f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note
262005f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return.
2621bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the
262205f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed.
262305f0fe6bSTejun Heo */
queue_rcu_work(struct workqueue_struct * wq,struct rcu_work * rwork)262405f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork)
262505f0fe6bSTejun Heo {
262605f0fe6bSTejun Heo struct work_struct *work = &rwork->work;
262705f0fe6bSTejun Heo
262886898fa6STejun Heo /*
262986898fa6STejun Heo * rcu_work can't be canceled or disabled. Warn if the user reached
263086898fa6STejun Heo * inside @rwork and disabled the inner work.
263186898fa6STejun Heo */
263286898fa6STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) &&
263386898fa6STejun Heo !WARN_ON_ONCE(clear_pending_if_disabled(work))) {
263405f0fe6bSTejun Heo rwork->wq = wq;
2635a7e30c0eSUladzislau Rezki call_rcu_hurry(&rwork->rcu, rcu_work_rcufn);
263605f0fe6bSTejun Heo return true;
263705f0fe6bSTejun Heo }
263805f0fe6bSTejun Heo
263905f0fe6bSTejun Heo return false;
264005f0fe6bSTejun Heo }
264105f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work);
264205f0fe6bSTejun Heo
alloc_worker(int node)2643f7537df5SLai Jiangshan static struct worker *alloc_worker(int node)
2644c34056a3STejun Heo {
2645c34056a3STejun Heo struct worker *worker;
2646c34056a3STejun Heo
2647f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node);
2648c8e55f36STejun Heo if (worker) {
2649c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry);
2650affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled);
2651da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node);
2652e22bee78STejun Heo /* on creation a worker is in !idle && prep state */
2653e22bee78STejun Heo worker->flags = WORKER_PREP;
2654c8e55f36STejun Heo }
2655c34056a3STejun Heo return worker;
2656c34056a3STejun Heo }
2657c34056a3STejun Heo
pool_allowed_cpus(struct worker_pool * pool)26589546b29eSTejun Heo static cpumask_t *pool_allowed_cpus(struct worker_pool *pool)
26599546b29eSTejun Heo {
26608639ecebSTejun Heo if (pool->cpu < 0 && pool->attrs->affn_strict)
26619546b29eSTejun Heo return pool->attrs->__pod_cpumask;
26628639ecebSTejun Heo else
26638639ecebSTejun Heo return pool->attrs->cpumask;
26649546b29eSTejun Heo }
26659546b29eSTejun Heo
2666c34056a3STejun Heo /**
26674736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool
26684736cbf7SLai Jiangshan * @worker: worker to be attached
26694736cbf7SLai Jiangshan * @pool: the target pool
26704736cbf7SLai Jiangshan *
26714736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and
26724736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across
26734736cbf7SLai Jiangshan * cpu-[un]hotplugs.
26744736cbf7SLai Jiangshan */
worker_attach_to_pool(struct worker * worker,struct worker_pool * pool)26754736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker,
26764736cbf7SLai Jiangshan struct worker_pool *pool)
26774736cbf7SLai Jiangshan {
26781258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex);
26794736cbf7SLai Jiangshan
26804736cbf7SLai Jiangshan /*
26814cb1ef64STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains stable
26824cb1ef64STejun Heo * across this function. See the comments above the flag definition for
26834cb1ef64STejun Heo * details. BH workers are, while per-CPU, always DISASSOCIATED.
26844736cbf7SLai Jiangshan */
26854cb1ef64STejun Heo if (pool->flags & POOL_DISASSOCIATED) {
26864736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND;
26874cb1ef64STejun Heo } else {
26884cb1ef64STejun Heo WARN_ON_ONCE(pool->flags & POOL_BH);
26895c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu);
26904cb1ef64STejun Heo }
26914736cbf7SLai Jiangshan
2692640f17c8SPeter Zijlstra if (worker->rescue_wq)
26939546b29eSTejun Heo set_cpus_allowed_ptr(worker->task, pool_allowed_cpus(pool));
2694640f17c8SPeter Zijlstra
26954736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers);
2696a2d812a2STejun Heo worker->pool = pool;
26974736cbf7SLai Jiangshan
26981258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex);
26994736cbf7SLai Jiangshan }
27004736cbf7SLai Jiangshan
unbind_worker(struct worker * worker)2701f45b1c3cSLai Jiangshan static void unbind_worker(struct worker *worker)
2702f45b1c3cSLai Jiangshan {
2703f45b1c3cSLai Jiangshan lockdep_assert_held(&wq_pool_attach_mutex);
2704f45b1c3cSLai Jiangshan
2705f45b1c3cSLai Jiangshan kthread_set_per_cpu(worker->task, -1);
2706f45b1c3cSLai Jiangshan if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask))
2707f45b1c3cSLai Jiangshan WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0);
2708f45b1c3cSLai Jiangshan else
2709f45b1c3cSLai Jiangshan WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0);
2710f45b1c3cSLai Jiangshan }
2711f45b1c3cSLai Jiangshan
2712f4b7b53cSLai Jiangshan
detach_worker(struct worker * worker)2713f4b7b53cSLai Jiangshan static void detach_worker(struct worker *worker)
2714f4b7b53cSLai Jiangshan {
2715f4b7b53cSLai Jiangshan lockdep_assert_held(&wq_pool_attach_mutex);
2716f4b7b53cSLai Jiangshan
2717f4b7b53cSLai Jiangshan unbind_worker(worker);
2718f4b7b53cSLai Jiangshan list_del(&worker->node);
2719f4b7b53cSLai Jiangshan }
2720f4b7b53cSLai Jiangshan
27214736cbf7SLai Jiangshan /**
272260f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool
272360f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool
272460f5a4bcSLai Jiangshan *
27254736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The
27264736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has
27274736cbf7SLai Jiangshan * other reference to the pool.
272860f5a4bcSLai Jiangshan */
worker_detach_from_pool(struct worker * worker)2729a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker)
273060f5a4bcSLai Jiangshan {
2731a2d812a2STejun Heo struct worker_pool *pool = worker->pool;
273260f5a4bcSLai Jiangshan
27334cb1ef64STejun Heo /* there is one permanent BH worker per CPU which should never detach */
27344cb1ef64STejun Heo WARN_ON_ONCE(pool->flags & POOL_BH);
27354cb1ef64STejun Heo
27361258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex);
2737f4b7b53cSLai Jiangshan detach_worker(worker);
273873613840SLai Jiangshan worker->pool = NULL;
27391258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex);
274060f5a4bcSLai Jiangshan
2741b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */
2742b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND);
274360f5a4bcSLai Jiangshan }
274460f5a4bcSLai Jiangshan
format_worker_id(char * buf,size_t size,struct worker * worker,struct worker_pool * pool)27452a1b02bcSTejun Heo static int format_worker_id(char *buf, size_t size, struct worker *worker,
27462a1b02bcSTejun Heo struct worker_pool *pool)
27472a1b02bcSTejun Heo {
27482a1b02bcSTejun Heo if (worker->rescue_wq)
27492a1b02bcSTejun Heo return scnprintf(buf, size, "kworker/R-%s",
27502a1b02bcSTejun Heo worker->rescue_wq->name);
27512a1b02bcSTejun Heo
27522a1b02bcSTejun Heo if (pool) {
27532a1b02bcSTejun Heo if (pool->cpu >= 0)
27542a1b02bcSTejun Heo return scnprintf(buf, size, "kworker/%d:%d%s",
27552a1b02bcSTejun Heo pool->cpu, worker->id,
27562a1b02bcSTejun Heo pool->attrs->nice < 0 ? "H" : "");
27572a1b02bcSTejun Heo else
27582a1b02bcSTejun Heo return scnprintf(buf, size, "kworker/u%d:%d",
27592a1b02bcSTejun Heo pool->id, worker->id);
27602a1b02bcSTejun Heo } else {
27612a1b02bcSTejun Heo return scnprintf(buf, size, "kworker/dying");
27622a1b02bcSTejun Heo }
27632a1b02bcSTejun Heo }
27642a1b02bcSTejun Heo
276560f5a4bcSLai Jiangshan /**
2766c34056a3STejun Heo * create_worker - create a new workqueue worker
276763d95a91STejun Heo * @pool: pool the new worker will belong to
2768c34056a3STejun Heo *
2769051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool.
2770c34056a3STejun Heo *
2771c34056a3STejun Heo * CONTEXT:
2772c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations.
2773c34056a3STejun Heo *
2774d185af30SYacine Belkadi * Return:
2775c34056a3STejun Heo * Pointer to the newly created worker.
2776c34056a3STejun Heo */
create_worker(struct worker_pool * pool)2777bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool)
2778c34056a3STejun Heo {
2779e441b56fSZhen Lei struct worker *worker;
2780e441b56fSZhen Lei int id;
2781c34056a3STejun Heo
27827cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */
2783e441b56fSZhen Lei id = ida_alloc(&pool->worker_ida, GFP_KERNEL);
27843f0ea0b8SPetr Mladek if (id < 0) {
27853f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n",
27863f0ea0b8SPetr Mladek ERR_PTR(id));
2787e441b56fSZhen Lei return NULL;
27883f0ea0b8SPetr Mladek }
2789c34056a3STejun Heo
2790f7537df5SLai Jiangshan worker = alloc_worker(pool->node);
27913f0ea0b8SPetr Mladek if (!worker) {
27923f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker\n");
2793c34056a3STejun Heo goto fail;
27943f0ea0b8SPetr Mladek }
2795c34056a3STejun Heo
2796c34056a3STejun Heo worker->id = id;
2797c34056a3STejun Heo
27984cb1ef64STejun Heo if (!(pool->flags & POOL_BH)) {
27992a1b02bcSTejun Heo char id_buf[WORKER_ID_LEN];
2800e3c916a4STejun Heo
28012a1b02bcSTejun Heo format_worker_id(id_buf, sizeof(id_buf), worker, pool);
28024cb1ef64STejun Heo worker->task = kthread_create_on_node(worker_thread, worker,
28032a1b02bcSTejun Heo pool->node, "%s", id_buf);
28043f0ea0b8SPetr Mladek if (IS_ERR(worker->task)) {
280560f54038SPetr Mladek if (PTR_ERR(worker->task) == -EINTR) {
28062a1b02bcSTejun Heo pr_err("workqueue: Interrupted when creating a worker thread \"%s\"\n",
280760f54038SPetr Mladek id_buf);
280860f54038SPetr Mladek } else {
28093f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to create a worker thread: %pe",
28103f0ea0b8SPetr Mladek worker->task);
281160f54038SPetr Mladek }
2812c34056a3STejun Heo goto fail;
28133f0ea0b8SPetr Mladek }
2814c34056a3STejun Heo
281591151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice);
28169546b29eSTejun Heo kthread_bind_mask(worker->task, pool_allowed_cpus(pool));
28174cb1ef64STejun Heo }
281891151228SOleg Nesterov
2819da028469SLai Jiangshan /* successful, attach the worker to the pool */
28204736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool);
2821822d8405STejun Heo
2822051e1850SLai Jiangshan /* start the newly created worker */
2823a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock);
28240219a352STejun Heo
2825051e1850SLai Jiangshan worker->pool->nr_workers++;
2826051e1850SLai Jiangshan worker_enter_idle(worker);
28270219a352STejun Heo
28280219a352STejun Heo /*
28290219a352STejun Heo * @worker is waiting on a completion in kthread() and will trigger hung
28306a229b0eSTejun Heo * check if not woken up soon. As kick_pool() is noop if @pool is empty,
28316a229b0eSTejun Heo * wake it up explicitly.
28320219a352STejun Heo */
28334cb1ef64STejun Heo if (worker->task)
2834051e1850SLai Jiangshan wake_up_process(worker->task);
28350219a352STejun Heo
2836a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock);
2837051e1850SLai Jiangshan
2838c34056a3STejun Heo return worker;
2839822d8405STejun Heo
2840c34056a3STejun Heo fail:
2841e441b56fSZhen Lei ida_free(&pool->worker_ida, id);
2842c34056a3STejun Heo kfree(worker);
2843c34056a3STejun Heo return NULL;
2844c34056a3STejun Heo }
2845c34056a3STejun Heo
detach_dying_workers(struct list_head * cull_list)2846f4b7b53cSLai Jiangshan static void detach_dying_workers(struct list_head *cull_list)
2847793777bcSValentin Schneider {
284868f83057SLai Jiangshan struct worker *worker;
2849793777bcSValentin Schneider
2850f4b7b53cSLai Jiangshan list_for_each_entry(worker, cull_list, entry)
2851f4b7b53cSLai Jiangshan detach_worker(worker);
2852793777bcSValentin Schneider }
2853793777bcSValentin Schneider
reap_dying_workers(struct list_head * cull_list)285468f83057SLai Jiangshan static void reap_dying_workers(struct list_head *cull_list)
2855e02b9312SValentin Schneider {
2856e02b9312SValentin Schneider struct worker *worker, *tmp;
2857e02b9312SValentin Schneider
2858e02b9312SValentin Schneider list_for_each_entry_safe(worker, tmp, cull_list, entry) {
2859e02b9312SValentin Schneider list_del_init(&worker->entry);
286068f83057SLai Jiangshan kthread_stop_put(worker->task);
286168f83057SLai Jiangshan kfree(worker);
2862e02b9312SValentin Schneider }
2863e02b9312SValentin Schneider }
2864e02b9312SValentin Schneider
2865e02b9312SValentin Schneider /**
2866e02b9312SValentin Schneider * set_worker_dying - Tag a worker for destruction
2867e02b9312SValentin Schneider * @worker: worker to be destroyed
2868e02b9312SValentin Schneider * @list: transfer worker away from its pool->idle_list and into list
2869e02b9312SValentin Schneider *
2870e02b9312SValentin Schneider * Tag @worker for destruction and adjust @pool stats accordingly. The worker
2871e02b9312SValentin Schneider * should be idle.
2872c8e55f36STejun Heo *
2873c8e55f36STejun Heo * CONTEXT:
2874a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock).
2875c34056a3STejun Heo */
set_worker_dying(struct worker * worker,struct list_head * list)2876e02b9312SValentin Schneider static void set_worker_dying(struct worker *worker, struct list_head *list)
2877c34056a3STejun Heo {
2878bd7bdd43STejun Heo struct worker_pool *pool = worker->pool;
2879c34056a3STejun Heo
2880cd549687STejun Heo lockdep_assert_held(&pool->lock);
2881e02b9312SValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex);
2882cd549687STejun Heo
2883c34056a3STejun Heo /* sanity check frenzy */
28846183c009STejun Heo if (WARN_ON(worker->current_work) ||
288573eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) ||
288673eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE)))
28876183c009STejun Heo return;
2888c34056a3STejun Heo
2889bd7bdd43STejun Heo pool->nr_workers--;
2890bd7bdd43STejun Heo pool->nr_idle--;
2891c8e55f36STejun Heo
2892cb444766STejun Heo worker->flags |= WORKER_DIE;
2893e02b9312SValentin Schneider
2894e02b9312SValentin Schneider list_move(&worker->entry, list);
289568f83057SLai Jiangshan
289668f83057SLai Jiangshan /* get an extra task struct reference for later kthread_stop_put() */
289768f83057SLai Jiangshan get_task_struct(worker->task);
2898c34056a3STejun Heo }
2899c34056a3STejun Heo
29003f959aa3SValentin Schneider /**
29013f959aa3SValentin Schneider * idle_worker_timeout - check if some idle workers can now be deleted.
29023f959aa3SValentin Schneider * @t: The pool's idle_timer that just expired
29033f959aa3SValentin Schneider *
29043f959aa3SValentin Schneider * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in
29053f959aa3SValentin Schneider * worker_leave_idle(), as a worker flicking between idle and active while its
29063f959aa3SValentin Schneider * pool is at the too_many_workers() tipping point would cause too much timer
29073f959aa3SValentin Schneider * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let
29083f959aa3SValentin Schneider * it expire and re-evaluate things from there.
29093f959aa3SValentin Schneider */
idle_worker_timeout(struct timer_list * t)291032a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t)
2911e22bee78STejun Heo {
291232a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer);
29133f959aa3SValentin Schneider bool do_cull = false;
29143f959aa3SValentin Schneider
29153f959aa3SValentin Schneider if (work_pending(&pool->idle_cull_work))
29163f959aa3SValentin Schneider return;
29173f959aa3SValentin Schneider
29183f959aa3SValentin Schneider raw_spin_lock_irq(&pool->lock);
29193f959aa3SValentin Schneider
29203f959aa3SValentin Schneider if (too_many_workers(pool)) {
29213f959aa3SValentin Schneider struct worker *worker;
29223f959aa3SValentin Schneider unsigned long expires;
29233f959aa3SValentin Schneider
29243f959aa3SValentin Schneider /* idle_list is kept in LIFO order, check the last one */
2925d70f5d57SLai Jiangshan worker = list_last_entry(&pool->idle_list, struct worker, entry);
29263f959aa3SValentin Schneider expires = worker->last_active + IDLE_WORKER_TIMEOUT;
29273f959aa3SValentin Schneider do_cull = !time_before(jiffies, expires);
29283f959aa3SValentin Schneider
29293f959aa3SValentin Schneider if (!do_cull)
29303f959aa3SValentin Schneider mod_timer(&pool->idle_timer, expires);
29313f959aa3SValentin Schneider }
29323f959aa3SValentin Schneider raw_spin_unlock_irq(&pool->lock);
29333f959aa3SValentin Schneider
29343f959aa3SValentin Schneider if (do_cull)
29353f959aa3SValentin Schneider queue_work(system_unbound_wq, &pool->idle_cull_work);
29363f959aa3SValentin Schneider }
29373f959aa3SValentin Schneider
29383f959aa3SValentin Schneider /**
29393f959aa3SValentin Schneider * idle_cull_fn - cull workers that have been idle for too long.
29403f959aa3SValentin Schneider * @work: the pool's work for handling these idle workers
29413f959aa3SValentin Schneider *
29423f959aa3SValentin Schneider * This goes through a pool's idle workers and gets rid of those that have been
29433f959aa3SValentin Schneider * idle for at least IDLE_WORKER_TIMEOUT seconds.
2944e02b9312SValentin Schneider *
2945e02b9312SValentin Schneider * We don't want to disturb isolated CPUs because of a pcpu kworker being
2946e02b9312SValentin Schneider * culled, so this also resets worker affinity. This requires a sleepable
2947e02b9312SValentin Schneider * context, hence the split between timer callback and work item.
29483f959aa3SValentin Schneider */
idle_cull_fn(struct work_struct * work)29493f959aa3SValentin Schneider static void idle_cull_fn(struct work_struct *work)
29503f959aa3SValentin Schneider {
29513f959aa3SValentin Schneider struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work);
29529680540cSYang Yingliang LIST_HEAD(cull_list);
2953e22bee78STejun Heo
2954e02b9312SValentin Schneider /*
2955e02b9312SValentin Schneider * Grabbing wq_pool_attach_mutex here ensures an already-running worker
2956f4b7b53cSLai Jiangshan * cannot proceed beyong set_pf_worker() in its self-destruct path.
2957f4b7b53cSLai Jiangshan * This is required as a previously-preempted worker could run after
2958f4b7b53cSLai Jiangshan * set_worker_dying() has happened but before detach_dying_workers() did.
2959e02b9312SValentin Schneider */
2960e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex);
2961a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock);
2962e22bee78STejun Heo
29633347fc9fSLai Jiangshan while (too_many_workers(pool)) {
2964e22bee78STejun Heo struct worker *worker;
2965e22bee78STejun Heo unsigned long expires;
2966e22bee78STejun Heo
2967d70f5d57SLai Jiangshan worker = list_last_entry(&pool->idle_list, struct worker, entry);
2968e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2969e22bee78STejun Heo
29703347fc9fSLai Jiangshan if (time_before(jiffies, expires)) {
297163d95a91STejun Heo mod_timer(&pool->idle_timer, expires);
29723347fc9fSLai Jiangshan break;
2973e22bee78STejun Heo }
29743347fc9fSLai Jiangshan
2975e02b9312SValentin Schneider set_worker_dying(worker, &cull_list);
2976e22bee78STejun Heo }
2977e22bee78STejun Heo
2978a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock);
2979f4b7b53cSLai Jiangshan detach_dying_workers(&cull_list);
2980e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex);
298168f83057SLai Jiangshan
298268f83057SLai Jiangshan reap_dying_workers(&cull_list);
2983e22bee78STejun Heo }
2984e22bee78STejun Heo
send_mayday(struct work_struct * work)2985493a1724STejun Heo static void send_mayday(struct work_struct *work)
2986e22bee78STejun Heo {
2987112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work);
2988112202d9STejun Heo struct workqueue_struct *wq = pwq->wq;
2989493a1724STejun Heo
29902e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock);
2991e22bee78STejun Heo
2992493008a8STejun Heo if (!wq->rescuer)
2993493a1724STejun Heo return;
2994e22bee78STejun Heo
2995e22bee78STejun Heo /* mayday mayday mayday */
2996493a1724STejun Heo if (list_empty(&pwq->mayday_node)) {
299777668c8bSLai Jiangshan /*
299877668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at
299977668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the
300077668c8bSLai Jiangshan * rescuer is done with it.
300177668c8bSLai Jiangshan */
300277668c8bSLai Jiangshan get_pwq(pwq);
3003493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays);
3004e22bee78STejun Heo wake_up_process(wq->rescuer->task);
3005725e8ec5STejun Heo pwq->stats[PWQ_STAT_MAYDAY]++;
3006493a1724STejun Heo }
3007e22bee78STejun Heo }
3008e22bee78STejun Heo
pool_mayday_timeout(struct timer_list * t)300932a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t)
3010e22bee78STejun Heo {
301132a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer);
3012e22bee78STejun Heo struct work_struct *work;
3013e22bee78STejun Heo
3014a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock);
3015a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */
3016e22bee78STejun Heo
301763d95a91STejun Heo if (need_to_create_worker(pool)) {
3018e22bee78STejun Heo /*
3019e22bee78STejun Heo * We've been trying to create a new worker but
3020e22bee78STejun Heo * haven't been successful. We might be hitting an
3021e22bee78STejun Heo * allocation deadlock. Send distress signals to
3022e22bee78STejun Heo * rescuers.
3023e22bee78STejun Heo */
302463d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry)
3025e22bee78STejun Heo send_mayday(work);
3026e22bee78STejun Heo }
3027e22bee78STejun Heo
3028a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock);
3029a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock);
3030e22bee78STejun Heo
303163d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
3032e22bee78STejun Heo }
3033e22bee78STejun Heo
3034e22bee78STejun Heo /**
3035e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary
303663d95a91STejun Heo * @pool: pool to create a new worker for
3037e22bee78STejun Heo *
303863d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to
3039e22bee78STejun Heo * have at least one idle worker on return from this function. If
3040e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
304163d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve
3042e22bee78STejun Heo * possible allocation deadlock.
3043e22bee78STejun Heo *
3044c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and
3045c5aa87bbSTejun Heo * may_start_working() %true.
3046e22bee78STejun Heo *
3047e22bee78STejun Heo * LOCKING:
3048a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed
3049e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from
3050e22bee78STejun Heo * manager.
3051e22bee78STejun Heo */
maybe_create_worker(struct worker_pool * pool)305229187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool)
3053d565ed63STejun Heo __releases(&pool->lock)
3054d565ed63STejun Heo __acquires(&pool->lock)
3055e22bee78STejun Heo {
3056e22bee78STejun Heo restart:
3057a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock);
30589f9c2364STejun Heo
3059e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
306063d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
3061e22bee78STejun Heo
3062e22bee78STejun Heo while (true) {
3063051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool))
3064e22bee78STejun Heo break;
3065e22bee78STejun Heo
3066e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN);
30679f9c2364STejun Heo
306863d95a91STejun Heo if (!need_to_create_worker(pool))
3069e22bee78STejun Heo break;
3070e22bee78STejun Heo }
3071e22bee78STejun Heo
3072*8fa7292fSThomas Gleixner timer_delete_sync(&pool->mayday_timer);
3073a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock);
3074051e1850SLai Jiangshan /*
3075051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully
3076051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have
3077051e1850SLai Jiangshan * already become busy.
3078051e1850SLai Jiangshan */
307963d95a91STejun Heo if (need_to_create_worker(pool))
3080e22bee78STejun Heo goto restart;
3081e22bee78STejun Heo }
3082e22bee78STejun Heo
3083e22bee78STejun Heo /**
3084e22bee78STejun Heo * manage_workers - manage worker pool
3085e22bee78STejun Heo * @worker: self
3086e22bee78STejun Heo *
3087706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs
3088e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per
3089706026c2STejun Heo * pool. The exclusion is handled automatically by this function.
3090e22bee78STejun Heo *
3091e22bee78STejun Heo * The caller can safely start processing works on false return. On
3092e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false
3093e22bee78STejun Heo * and may_start_working() is true.
3094e22bee78STejun Heo *
3095e22bee78STejun Heo * CONTEXT:
3096a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed
3097e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations.
3098e22bee78STejun Heo *
3099d185af30SYacine Belkadi * Return:
310029187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely
310129187a9eSTejun Heo * start processing works, %true if management function was performed and
310229187a9eSTejun Heo * the conditions that the caller verified before calling the function may
310329187a9eSTejun Heo * no longer be true.
3104e22bee78STejun Heo */
manage_workers(struct worker * worker)3105e22bee78STejun Heo static bool manage_workers(struct worker *worker)
3106e22bee78STejun Heo {
310763d95a91STejun Heo struct worker_pool *pool = worker->pool;
3108e22bee78STejun Heo
3109692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE)
311029187a9eSTejun Heo return false;
3111692b4825STejun Heo
3112692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE;
31132607d7a6STejun Heo pool->manager = worker;
3114e22bee78STejun Heo
311529187a9eSTejun Heo maybe_create_worker(pool);
3116e22bee78STejun Heo
31172607d7a6STejun Heo pool->manager = NULL;
3118692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE;
3119d8bb65abSSebastian Andrzej Siewior rcuwait_wake_up(&manager_wait);
312029187a9eSTejun Heo return true;
3121e22bee78STejun Heo }
3122e22bee78STejun Heo
3123a62428c0STejun Heo /**
3124a62428c0STejun Heo * process_one_work - process single work
3125c34056a3STejun Heo * @worker: self
3126a62428c0STejun Heo * @work: work to process
3127a62428c0STejun Heo *
3128a62428c0STejun Heo * Process @work. This function contains all the logics necessary to
3129a62428c0STejun Heo * process a single work including synchronization against and
3130a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and
3131a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can
3132a62428c0STejun Heo * call this function to process a work.
3133a62428c0STejun Heo *
3134a62428c0STejun Heo * CONTEXT:
3135a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which is released and regrabbed.
3136a62428c0STejun Heo */
process_one_work(struct worker * worker,struct work_struct * work)3137c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work)
3138d565ed63STejun Heo __releases(&pool->lock)
3139d565ed63STejun Heo __acquires(&pool->lock)
31401da177e4SLinus Torvalds {
3141112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work);
3142bd7bdd43STejun Heo struct worker_pool *pool = worker->pool;
3143c4560c2cSLai Jiangshan unsigned long work_data;
3144c35aea39STejun Heo int lockdep_start_depth, rcu_start_depth;
31451acd92d9STejun Heo bool bh_draining = pool->flags & POOL_BH_DRAINING;
31464e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
31474e6045f1SJohannes Berg /*
3148a62428c0STejun Heo * It is permissible to free the struct work_struct from
3149a62428c0STejun Heo * inside the function that is called from it, this we need to
3150a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held
3151a62428c0STejun Heo * lock freed" warnings as well as problems when looking into
3152a62428c0STejun Heo * work->lockdep_map, make a copy and use that here.
31534e6045f1SJohannes Berg */
31544d82a1deSPeter Zijlstra struct lockdep_map lockdep_map;
31554d82a1deSPeter Zijlstra
31564d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map);
31574e6045f1SJohannes Berg #endif
3158807407c0SLai Jiangshan /* ensure we're on the correct CPU */
315985327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
3160ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu);
316125511a47STejun Heo
31628930cabaSTejun Heo /* claim and dequeue */
3163dc186ad7SThomas Gleixner debug_work_deactivate(work);
3164c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
3165c34056a3STejun Heo worker->current_work = work;
3166a2c1c57bSTejun Heo worker->current_func = work->func;
3167112202d9STejun Heo worker->current_pwq = pwq;
31684cb1ef64STejun Heo if (worker->task)
3169616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime;
3170c4560c2cSLai Jiangshan work_data = *work_data_bits(work);
3171d812796eSLai Jiangshan worker->current_color = get_work_color(work_data);
31727a22ad75STejun Heo
31738bf89593STejun Heo /*
31748bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get
31758bf89593STejun Heo * overridden through set_worker_desc().
31768bf89593STejun Heo */
31778bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN);
31788bf89593STejun Heo
3179a62428c0STejun Heo list_del_init(&work->entry);
3180a62428c0STejun Heo
3181649027d7STejun Heo /*
3182228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management.
3183228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out
3184228f1d00SLai Jiangshan * of concurrency management and the next code block will chain
3185228f1d00SLai Jiangshan * execution of the pending work items.
3186fb0e7bebSTejun Heo */
3187616db877STejun Heo if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE))
3188228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE);
3189fb0e7bebSTejun Heo
3190974271c4STejun Heo /*
31910219a352STejun Heo * Kick @pool if necessary. It's always noop for per-cpu worker pools
31920219a352STejun Heo * since nr_running would always be >= 1 at this point. This is used to
31930219a352STejun Heo * chain execution of the pending work items for WORKER_NOT_RUNNING
31940219a352STejun Heo * workers such as the UNBOUND and CPU_INTENSIVE ones.
3195974271c4STejun Heo */
31960219a352STejun Heo kick_pool(pool);
3197974271c4STejun Heo
31988930cabaSTejun Heo /*
31997c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last
3200d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that
320123657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is
320223657bb1STejun Heo * disabled.
32038930cabaSTejun Heo */
3204456a78eeSTejun Heo set_work_pool_and_clear_pending(work, pool->id, pool_offq_flags(pool));
32051da177e4SLinus Torvalds
3206fe48ba7dSMirsad Goran Todorovac pwq->stats[PWQ_STAT_STARTED]++;
3207a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock);
3208365970a1SDavid Howells
3209c35aea39STejun Heo rcu_start_depth = rcu_preempt_depth();
3210c35aea39STejun Heo lockdep_start_depth = lockdep_depth(current);
32111acd92d9STejun Heo /* see drain_dead_softirq_workfn() */
32121acd92d9STejun Heo if (!bh_draining)
32134f022f43SMatthew Brost lock_map_acquire(pwq->wq->lockdep_map);
32143295f0efSIngo Molnar lock_map_acquire(&lockdep_map);
3215e6f3faa7SPeter Zijlstra /*
3216f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding
3217f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s.
3218e6f3faa7SPeter Zijlstra *
3219e6f3faa7SPeter Zijlstra * However, that would result in:
3220e6f3faa7SPeter Zijlstra *
3221e6f3faa7SPeter Zijlstra * A(W1)
3222e6f3faa7SPeter Zijlstra * WFC(C)
3223e6f3faa7SPeter Zijlstra * A(W1)
3224e6f3faa7SPeter Zijlstra * C(C)
3225e6f3faa7SPeter Zijlstra *
3226e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no
3227e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a
3228e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then
3229f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard
3230e6f3faa7SPeter Zijlstra * these locks.
3231e6f3faa7SPeter Zijlstra *
3232e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the
3233e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded
3234e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem.
3235e6f3faa7SPeter Zijlstra */
3236f52be570SPeter Zijlstra lockdep_invariant_state(true);
3237e36c886aSArjan van de Ven trace_workqueue_execute_start(work);
3238a2c1c57bSTejun Heo worker->current_func(work);
3239e36c886aSArjan van de Ven /*
3240e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace
3241e36c886aSArjan van de Ven * point will only record its address.
3242e36c886aSArjan van de Ven */
32431c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func);
3244725e8ec5STejun Heo pwq->stats[PWQ_STAT_COMPLETED]++;
32453295f0efSIngo Molnar lock_map_release(&lockdep_map);
32461acd92d9STejun Heo if (!bh_draining)
32474f022f43SMatthew Brost lock_map_release(pwq->wq->lockdep_map);
32481da177e4SLinus Torvalds
3249c35aea39STejun Heo if (unlikely((worker->task && in_atomic()) ||
3250c35aea39STejun Heo lockdep_depth(current) != lockdep_start_depth ||
3251c35aea39STejun Heo rcu_preempt_depth() != rcu_start_depth)) {
3252c35aea39STejun Heo pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n"
3253c35aea39STejun Heo " preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n",
3254c35aea39STejun Heo current->comm, task_pid_nr(current), preempt_count(),
3255c35aea39STejun Heo lockdep_start_depth, lockdep_depth(current),
3256c35aea39STejun Heo rcu_start_depth, rcu_preempt_depth(),
3257c35aea39STejun Heo worker->current_func);
3258d5abe669SPeter Zijlstra debug_show_held_locks(current);
3259d5abe669SPeter Zijlstra dump_stack();
3260d5abe669SPeter Zijlstra }
3261d5abe669SPeter Zijlstra
3262b22ce278STejun Heo /*
3263025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION
3264b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to
3265b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could
3266b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in
3267789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so
3268789cbbecSJoe Lawrence * the same condition doesn't freeze RCU.
3269b22ce278STejun Heo */
32704cb1ef64STejun Heo if (worker->task)
3271a7e6425eSPaul E. McKenney cond_resched();
3272b22ce278STejun Heo
3273a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock);
3274a62428c0STejun Heo
3275616db877STejun Heo /*
3276616db877STejun Heo * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked
3277616db877STejun Heo * CPU intensive by wq_worker_tick() if @work hogged CPU longer than
3278616db877STejun Heo * wq_cpu_intensive_thresh_us. Clear it.
3279616db877STejun Heo */
3280fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
3281fb0e7bebSTejun Heo
32821b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */
32831b69ac6bSJohannes Weiner worker->last_func = worker->current_func;
32841b69ac6bSJohannes Weiner
3285a62428c0STejun Heo /* we're done with it, release */
328642f8570fSSasha Levin hash_del(&worker->hentry);
3287c34056a3STejun Heo worker->current_work = NULL;
3288a2c1c57bSTejun Heo worker->current_func = NULL;
3289112202d9STejun Heo worker->current_pwq = NULL;
3290d812796eSLai Jiangshan worker->current_color = INT_MAX;
3291dd6c3c54STejun Heo
3292dd6c3c54STejun Heo /* must be the last step, see the function comment */
3293c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, work_data);
32941da177e4SLinus Torvalds }
32951da177e4SLinus Torvalds
3296affee4b2STejun Heo /**
3297affee4b2STejun Heo * process_scheduled_works - process scheduled works
3298affee4b2STejun Heo * @worker: self
3299affee4b2STejun Heo *
3300affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list
3301affee4b2STejun Heo * may change while processing a work, so this function repeatedly
3302affee4b2STejun Heo * fetches a work from the top and executes it.
3303affee4b2STejun Heo *
3304affee4b2STejun Heo * CONTEXT:
3305a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed
3306affee4b2STejun Heo * multiple times.
3307affee4b2STejun Heo */
process_scheduled_works(struct worker * worker)3308affee4b2STejun Heo static void process_scheduled_works(struct worker *worker)
33091da177e4SLinus Torvalds {
3310c0ab017dSTejun Heo struct work_struct *work;
3311c0ab017dSTejun Heo bool first = true;
3312c0ab017dSTejun Heo
3313c0ab017dSTejun Heo while ((work = list_first_entry_or_null(&worker->scheduled,
3314c0ab017dSTejun Heo struct work_struct, entry))) {
3315c0ab017dSTejun Heo if (first) {
3316c0ab017dSTejun Heo worker->pool->watchdog_ts = jiffies;
3317c0ab017dSTejun Heo first = false;
3318c0ab017dSTejun Heo }
3319c34056a3STejun Heo process_one_work(worker, work);
3320a62428c0STejun Heo }
33211da177e4SLinus Torvalds }
33221da177e4SLinus Torvalds
set_pf_worker(bool val)3323197f6accSTejun Heo static void set_pf_worker(bool val)
3324197f6accSTejun Heo {
3325197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex);
3326197f6accSTejun Heo if (val)
3327197f6accSTejun Heo current->flags |= PF_WQ_WORKER;
3328197f6accSTejun Heo else
3329197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER;
3330197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex);
3331197f6accSTejun Heo }
3332197f6accSTejun Heo
33334690c4abSTejun Heo /**
33344690c4abSTejun Heo * worker_thread - the worker thread function
3335c34056a3STejun Heo * @__worker: self
33364690c4abSTejun Heo *
3337c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool -
3338c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all
3339c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only
3340c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which
3341c5aa87bbSTejun Heo * will be explained in rescuer_thread().
3342d185af30SYacine Belkadi *
3343d185af30SYacine Belkadi * Return: 0
33444690c4abSTejun Heo */
worker_thread(void * __worker)3345c34056a3STejun Heo static int worker_thread(void *__worker)
33461da177e4SLinus Torvalds {
3347c34056a3STejun Heo struct worker *worker = __worker;
3348bd7bdd43STejun Heo struct worker_pool *pool = worker->pool;
33491da177e4SLinus Torvalds
3350e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */
3351197f6accSTejun Heo set_pf_worker(true);
3352c8e55f36STejun Heo woke_up:
3353a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock);
3354affee4b2STejun Heo
3355a9ab775bSTejun Heo /* am I supposed to die? */
3356a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) {
3357a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock);
3358197f6accSTejun Heo set_pf_worker(false);
335973613840SLai Jiangshan /*
336073613840SLai Jiangshan * The worker is dead and PF_WQ_WORKER is cleared, worker->pool
336173613840SLai Jiangshan * shouldn't be accessed, reset it to NULL in case otherwise.
336273613840SLai Jiangshan */
336373613840SLai Jiangshan worker->pool = NULL;
3364e441b56fSZhen Lei ida_free(&pool->worker_ida, worker->id);
3365c8e55f36STejun Heo return 0;
3366c8e55f36STejun Heo }
3367c8e55f36STejun Heo
3368c8e55f36STejun Heo worker_leave_idle(worker);
3369db7bccf4STejun Heo recheck:
3370e22bee78STejun Heo /* no more worker necessary? */
337163d95a91STejun Heo if (!need_more_worker(pool))
3372e22bee78STejun Heo goto sleep;
3373e22bee78STejun Heo
3374e22bee78STejun Heo /* do we need to manage? */
337563d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker))
3376e22bee78STejun Heo goto recheck;
3377e22bee78STejun Heo
3378c8e55f36STejun Heo /*
3379c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is
3380c8e55f36STejun Heo * preparing to process a work or actually processing it.
3381c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping.
3382c8e55f36STejun Heo */
33836183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled));
3384c8e55f36STejun Heo
3385e22bee78STejun Heo /*
3386a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle
3387a9ab775bSTejun Heo * worker or that someone else has already assumed the manager
3388a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency
3389a9ab775bSTejun Heo * management if applicable and concurrency management is restored
3390a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details.
3391e22bee78STejun Heo */
3392a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
3393e22bee78STejun Heo
3394e22bee78STejun Heo do {
3395affee4b2STejun Heo struct work_struct *work =
3396bd7bdd43STejun Heo list_first_entry(&pool->worklist,
3397affee4b2STejun Heo struct work_struct, entry);
3398affee4b2STejun Heo
3399873eaca6STejun Heo if (assign_work(work, worker, NULL))
3400affee4b2STejun Heo process_scheduled_works(worker);
340163d95a91STejun Heo } while (keep_working(pool));
3402affee4b2STejun Heo
3403228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP);
3404d313dd85STejun Heo sleep:
3405c8e55f36STejun Heo /*
3406d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to
3407d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding
3408d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state
3409d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any
3410d565ed63STejun Heo * event.
3411c8e55f36STejun Heo */
3412c8e55f36STejun Heo worker_enter_idle(worker);
3413c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE);
3414a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock);
34151da177e4SLinus Torvalds schedule();
3416c8e55f36STejun Heo goto woke_up;
34171da177e4SLinus Torvalds }
34181da177e4SLinus Torvalds
3419e22bee78STejun Heo /**
3420e22bee78STejun Heo * rescuer_thread - the rescuer thread function
3421111c225aSTejun Heo * @__rescuer: self
3422e22bee78STejun Heo *
3423e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each
3424493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set.
3425e22bee78STejun Heo *
3426706026c2STejun Heo * Regular work processing on a pool may block trying to create a new
3427e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of
3428e22bee78STejun Heo * developing into deadlock if some works currently on the same queue
3429e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is
3430e22bee78STejun Heo * the problem rescuer solves.
3431e22bee78STejun Heo *
3432706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all
3433706026c2STejun Heo * workqueues which have works queued on the pool and let them process
3434e22bee78STejun Heo * those works so that forward progress can be guaranteed.
3435e22bee78STejun Heo *
3436e22bee78STejun Heo * This should happen rarely.
3437d185af30SYacine Belkadi *
3438d185af30SYacine Belkadi * Return: 0
3439e22bee78STejun Heo */
rescuer_thread(void * __rescuer)3440111c225aSTejun Heo static int rescuer_thread(void *__rescuer)
3441e22bee78STejun Heo {
3442111c225aSTejun Heo struct worker *rescuer = __rescuer;
3443111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq;
34444d595b86SLai Jiangshan bool should_stop;
3445e22bee78STejun Heo
3446e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL);
3447111c225aSTejun Heo
3448111c225aSTejun Heo /*
3449111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
3450111c225aSTejun Heo * doesn't participate in concurrency management.
3451111c225aSTejun Heo */
3452197f6accSTejun Heo set_pf_worker(true);
3453e22bee78STejun Heo repeat:
3454c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE);
34551da177e4SLinus Torvalds
34564d595b86SLai Jiangshan /*
34574d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue
34584d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have
34594d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming
34604d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through
34614d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the
34624d595b86SLai Jiangshan * list is always empty on exit.
34634d595b86SLai Jiangshan */
34644d595b86SLai Jiangshan should_stop = kthread_should_stop();
34651da177e4SLinus Torvalds
3466493a1724STejun Heo /* see whether any pwq is asking for help */
3467a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock);
3468493a1724STejun Heo
3469493a1724STejun Heo while (!list_empty(&wq->maydays)) {
3470493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
3471493a1724STejun Heo struct pool_workqueue, mayday_node);
3472112202d9STejun Heo struct worker_pool *pool = pwq->pool;
3473e22bee78STejun Heo struct work_struct *work, *n;
3474e22bee78STejun Heo
3475e22bee78STejun Heo __set_current_state(TASK_RUNNING);
3476493a1724STejun Heo list_del_init(&pwq->mayday_node);
3477493a1724STejun Heo
3478a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock);
3479e22bee78STejun Heo
348051697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool);
348151697d39SLai Jiangshan
3482a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock);
3483e22bee78STejun Heo
3484e22bee78STejun Heo /*
3485e22bee78STejun Heo * Slurp in all works issued via this workqueue and
3486e22bee78STejun Heo * process'em.
3487e22bee78STejun Heo */
3488873eaca6STejun Heo WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
348982607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) {
3490873eaca6STejun Heo if (get_work_pwq(work) == pwq &&
3491873eaca6STejun Heo assign_work(work, rescuer, &n))
3492725e8ec5STejun Heo pwq->stats[PWQ_STAT_RESCUED]++;
349382607adcSTejun Heo }
3494e22bee78STejun Heo
3495873eaca6STejun Heo if (!list_empty(&rescuer->scheduled)) {
3496e22bee78STejun Heo process_scheduled_works(rescuer);
34977576958aSTejun Heo
34987576958aSTejun Heo /*
3499008847f6SNeilBrown * The above execution of rescued work items could
3500008847f6SNeilBrown * have created more to rescue through
3501f97a4a1aSLai Jiangshan * pwq_activate_first_inactive() or chained
3502008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so
3503008847f6SNeilBrown * that such back-to-back work items, which may be
3504008847f6SNeilBrown * being used to relieve memory pressure, don't
3505008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween.
3506008847f6SNeilBrown */
35074f3f4cf3SLai Jiangshan if (pwq->nr_active && need_to_create_worker(pool)) {
3508a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock);
3509e66b39afSTejun Heo /*
3510e66b39afSTejun Heo * Queue iff we aren't racing destruction
3511e66b39afSTejun Heo * and somebody else hasn't queued it already.
3512e66b39afSTejun Heo */
3513e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) {
3514008847f6SNeilBrown get_pwq(pwq);
3515e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays);
3516e66b39afSTejun Heo }
3517a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock);
3518008847f6SNeilBrown }
3519008847f6SNeilBrown }
3520008847f6SNeilBrown
3521008847f6SNeilBrown /*
35220219a352STejun Heo * Leave this pool. Notify regular workers; otherwise, we end up
35230219a352STejun Heo * with 0 concurrency and stalling the execution.
35247576958aSTejun Heo */
35250219a352STejun Heo kick_pool(pool);
35267576958aSTejun Heo
3527a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock);
352813b1d625SLai Jiangshan
3529a2d812a2STejun Heo worker_detach_from_pool(rescuer);
353013b1d625SLai Jiangshan
3531e7694611SLai Jiangshan /*
3532e7694611SLai Jiangshan * Put the reference grabbed by send_mayday(). @pool might
3533e7694611SLai Jiangshan * go away any time after it.
3534e7694611SLai Jiangshan */
3535e7694611SLai Jiangshan put_pwq_unlocked(pwq);
3536e7694611SLai Jiangshan
3537a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock);
35381da177e4SLinus Torvalds }
35391da177e4SLinus Torvalds
3540a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock);
3541493a1724STejun Heo
35424d595b86SLai Jiangshan if (should_stop) {
35434d595b86SLai Jiangshan __set_current_state(TASK_RUNNING);
3544197f6accSTejun Heo set_pf_worker(false);
35454d595b86SLai Jiangshan return 0;
35464d595b86SLai Jiangshan }
35474d595b86SLai Jiangshan
3548111c225aSTejun Heo /* rescuers should never participate in concurrency management */
3549111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
3550e22bee78STejun Heo schedule();
3551e22bee78STejun Heo goto repeat;
35521da177e4SLinus Torvalds }
35531da177e4SLinus Torvalds
bh_worker(struct worker * worker)35544cb1ef64STejun Heo static void bh_worker(struct worker *worker)
35554cb1ef64STejun Heo {
35564cb1ef64STejun Heo struct worker_pool *pool = worker->pool;
35574cb1ef64STejun Heo int nr_restarts = BH_WORKER_RESTARTS;
35584cb1ef64STejun Heo unsigned long end = jiffies + BH_WORKER_JIFFIES;
35594cb1ef64STejun Heo
35604cb1ef64STejun Heo raw_spin_lock_irq(&pool->lock);
35614cb1ef64STejun Heo worker_leave_idle(worker);
35624cb1ef64STejun Heo
35634cb1ef64STejun Heo /*
35644cb1ef64STejun Heo * This function follows the structure of worker_thread(). See there for
35654cb1ef64STejun Heo * explanations on each step.
35664cb1ef64STejun Heo */
35674cb1ef64STejun Heo if (!need_more_worker(pool))
35684cb1ef64STejun Heo goto done;
35694cb1ef64STejun Heo
35704cb1ef64STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled));
35714cb1ef64STejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
35724cb1ef64STejun Heo
35734cb1ef64STejun Heo do {
35744cb1ef64STejun Heo struct work_struct *work =
35754cb1ef64STejun Heo list_first_entry(&pool->worklist,
35764cb1ef64STejun Heo struct work_struct, entry);
35774cb1ef64STejun Heo
35784cb1ef64STejun Heo if (assign_work(work, worker, NULL))
35794cb1ef64STejun Heo process_scheduled_works(worker);
35804cb1ef64STejun Heo } while (keep_working(pool) &&
35814cb1ef64STejun Heo --nr_restarts && time_before(jiffies, end));
35824cb1ef64STejun Heo
35834cb1ef64STejun Heo worker_set_flags(worker, WORKER_PREP);
35844cb1ef64STejun Heo done:
35854cb1ef64STejun Heo worker_enter_idle(worker);
35864cb1ef64STejun Heo kick_pool(pool);
35874cb1ef64STejun Heo raw_spin_unlock_irq(&pool->lock);
35884cb1ef64STejun Heo }
35894cb1ef64STejun Heo
35904cb1ef64STejun Heo /*
35914cb1ef64STejun Heo * TODO: Convert all tasklet users to workqueue and use softirq directly.
35924cb1ef64STejun Heo *
35934cb1ef64STejun Heo * This is currently called from tasklet[_hi]action() and thus is also called
35944cb1ef64STejun Heo * whenever there are tasklets to run. Let's do an early exit if there's nothing
35954cb1ef64STejun Heo * queued. Once conversion from tasklet is complete, the need_more_worker() test
35964cb1ef64STejun Heo * can be dropped.
35974cb1ef64STejun Heo *
35984cb1ef64STejun Heo * After full conversion, we'll add worker->softirq_action, directly use the
35994cb1ef64STejun Heo * softirq action and obtain the worker pointer from the softirq_action pointer.
36004cb1ef64STejun Heo */
workqueue_softirq_action(bool highpri)36014cb1ef64STejun Heo void workqueue_softirq_action(bool highpri)
36024cb1ef64STejun Heo {
36034cb1ef64STejun Heo struct worker_pool *pool =
36044cb1ef64STejun Heo &per_cpu(bh_worker_pools, smp_processor_id())[highpri];
36054cb1ef64STejun Heo if (need_more_worker(pool))
36064cb1ef64STejun Heo bh_worker(list_first_entry(&pool->workers, struct worker, node));
36074cb1ef64STejun Heo }
36084cb1ef64STejun Heo
36091acd92d9STejun Heo struct wq_drain_dead_softirq_work {
36101acd92d9STejun Heo struct work_struct work;
36111acd92d9STejun Heo struct worker_pool *pool;
36121acd92d9STejun Heo struct completion done;
36131acd92d9STejun Heo };
36141acd92d9STejun Heo
drain_dead_softirq_workfn(struct work_struct * work)36151acd92d9STejun Heo static void drain_dead_softirq_workfn(struct work_struct *work)
36161acd92d9STejun Heo {
36171acd92d9STejun Heo struct wq_drain_dead_softirq_work *dead_work =
36181acd92d9STejun Heo container_of(work, struct wq_drain_dead_softirq_work, work);
36191acd92d9STejun Heo struct worker_pool *pool = dead_work->pool;
36201acd92d9STejun Heo bool repeat;
36211acd92d9STejun Heo
36221acd92d9STejun Heo /*
36231acd92d9STejun Heo * @pool's CPU is dead and we want to execute its still pending work
36241acd92d9STejun Heo * items from this BH work item which is running on a different CPU. As
36251acd92d9STejun Heo * its CPU is dead, @pool can't be kicked and, as work execution path
36261acd92d9STejun Heo * will be nested, a lockdep annotation needs to be suppressed. Mark
36271acd92d9STejun Heo * @pool with %POOL_BH_DRAINING for the special treatments.
36281acd92d9STejun Heo */
36291acd92d9STejun Heo raw_spin_lock_irq(&pool->lock);
36301acd92d9STejun Heo pool->flags |= POOL_BH_DRAINING;
36311acd92d9STejun Heo raw_spin_unlock_irq(&pool->lock);
36321acd92d9STejun Heo
36331acd92d9STejun Heo bh_worker(list_first_entry(&pool->workers, struct worker, node));
36341acd92d9STejun Heo
36351acd92d9STejun Heo raw_spin_lock_irq(&pool->lock);
36361acd92d9STejun Heo pool->flags &= ~POOL_BH_DRAINING;
36371acd92d9STejun Heo repeat = need_more_worker(pool);
36381acd92d9STejun Heo raw_spin_unlock_irq(&pool->lock);
36391acd92d9STejun Heo
36401acd92d9STejun Heo /*
36411acd92d9STejun Heo * bh_worker() might hit consecutive execution limit and bail. If there
36421acd92d9STejun Heo * still are pending work items, reschedule self and return so that we
36431acd92d9STejun Heo * don't hog this CPU's BH.
36441acd92d9STejun Heo */
36451acd92d9STejun Heo if (repeat) {
36461acd92d9STejun Heo if (pool->attrs->nice == HIGHPRI_NICE_LEVEL)
36471acd92d9STejun Heo queue_work(system_bh_highpri_wq, work);
36481acd92d9STejun Heo else
36491acd92d9STejun Heo queue_work(system_bh_wq, work);
36501acd92d9STejun Heo } else {
36511acd92d9STejun Heo complete(&dead_work->done);
36521acd92d9STejun Heo }
36531acd92d9STejun Heo }
36541acd92d9STejun Heo
36551acd92d9STejun Heo /*
36561acd92d9STejun Heo * @cpu is dead. Drain the remaining BH work items on the current CPU. It's
36571acd92d9STejun Heo * possible to allocate dead_work per CPU and avoid flushing. However, then we
36581acd92d9STejun Heo * have to worry about draining overlapping with CPU coming back online or
36591acd92d9STejun Heo * nesting (one CPU's dead_work queued on another CPU which is also dead and so
36601acd92d9STejun Heo * on). Let's keep it simple and drain them synchronously. These are BH work
36611acd92d9STejun Heo * items which shouldn't be requeued on the same pool. Shouldn't take long.
36621acd92d9STejun Heo */
workqueue_softirq_dead(unsigned int cpu)36631acd92d9STejun Heo void workqueue_softirq_dead(unsigned int cpu)
36641acd92d9STejun Heo {
36651acd92d9STejun Heo int i;
36661acd92d9STejun Heo
36671acd92d9STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
36681acd92d9STejun Heo struct worker_pool *pool = &per_cpu(bh_worker_pools, cpu)[i];
36691acd92d9STejun Heo struct wq_drain_dead_softirq_work dead_work;
36701acd92d9STejun Heo
36711acd92d9STejun Heo if (!need_more_worker(pool))
36721acd92d9STejun Heo continue;
36731acd92d9STejun Heo
3674e7cc3be6SLai Jiangshan INIT_WORK_ONSTACK(&dead_work.work, drain_dead_softirq_workfn);
36751acd92d9STejun Heo dead_work.pool = pool;
36761acd92d9STejun Heo init_completion(&dead_work.done);
36771acd92d9STejun Heo
36781acd92d9STejun Heo if (pool->attrs->nice == HIGHPRI_NICE_LEVEL)
36791acd92d9STejun Heo queue_work(system_bh_highpri_wq, &dead_work.work);
36801acd92d9STejun Heo else
36811acd92d9STejun Heo queue_work(system_bh_wq, &dead_work.work);
36821acd92d9STejun Heo
36831acd92d9STejun Heo wait_for_completion(&dead_work.done);
368431103f40SZqiang destroy_work_on_stack(&dead_work.work);
36851acd92d9STejun Heo }
36861acd92d9STejun Heo }
36871acd92d9STejun Heo
3688fca839c0STejun Heo /**
3689fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity
3690fca839c0STejun Heo * @target_wq: workqueue being flushed
3691fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes)
3692de35994eSTvrtko Ursulin * @from_cancel: are we called from the work cancel path
3693fca839c0STejun Heo *
3694fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it.
3695de35994eSTvrtko Ursulin * If this is not the cancel path (which implies work being flushed is either
3696de35994eSTvrtko Ursulin * already running, or will not be at all), check if @target_wq doesn't have
3697de35994eSTvrtko Ursulin * %WQ_MEM_RECLAIM and verify that %current is not reclaiming memory or running
3698de35994eSTvrtko Ursulin * on a workqueue which doesn't have %WQ_MEM_RECLAIM as that can break forward-
3699de35994eSTvrtko Ursulin * progress guarantee leading to a deadlock.
3700fca839c0STejun Heo */
check_flush_dependency(struct workqueue_struct * target_wq,struct work_struct * target_work,bool from_cancel)3701fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq,
3702de35994eSTvrtko Ursulin struct work_struct *target_work,
3703de35994eSTvrtko Ursulin bool from_cancel)
3704fca839c0STejun Heo {
3705de35994eSTvrtko Ursulin work_func_t target_func;
3706fca839c0STejun Heo struct worker *worker;
3707fca839c0STejun Heo
3708de35994eSTvrtko Ursulin if (from_cancel || target_wq->flags & WQ_MEM_RECLAIM)
3709fca839c0STejun Heo return;
3710fca839c0STejun Heo
3711fca839c0STejun Heo worker = current_wq_worker();
3712de35994eSTvrtko Ursulin target_func = target_work ? target_work->func : NULL;
3713fca839c0STejun Heo
3714fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC,
3715d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps",
3716fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func);
371723d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags &
371823d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM),
3719d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps",
3720fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func,
3721fca839c0STejun Heo target_wq->name, target_func);
3722fca839c0STejun Heo }
3723fca839c0STejun Heo
3724fc2e4d70SOleg Nesterov struct wq_barrier {
3725fc2e4d70SOleg Nesterov struct work_struct work;
3726fc2e4d70SOleg Nesterov struct completion done;
37272607d7a6STejun Heo struct task_struct *task; /* purely informational */
3728fc2e4d70SOleg Nesterov };
3729fc2e4d70SOleg Nesterov
wq_barrier_func(struct work_struct * work)3730fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work)
3731fc2e4d70SOleg Nesterov {
3732fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
3733fc2e4d70SOleg Nesterov complete(&barr->done);
3734fc2e4d70SOleg Nesterov }
3735fc2e4d70SOleg Nesterov
37364690c4abSTejun Heo /**
37374690c4abSTejun Heo * insert_wq_barrier - insert a barrier work
3738112202d9STejun Heo * @pwq: pwq to insert barrier into
37394690c4abSTejun Heo * @barr: wq_barrier to insert
3740affee4b2STejun Heo * @target: target work to attach @barr to
3741affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing
37424690c4abSTejun Heo *
3743affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after
3744affee4b2STejun Heo * @target finishes execution. Please note that the ordering
3745affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local
3746affee4b2STejun Heo * cpu.
3747affee4b2STejun Heo *
3748affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because
3749affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be
3750affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED
3751affee4b2STejun Heo * flag of the previous work while there must be a valid next work
3752affee4b2STejun Heo * after a work with LINKED flag set.
3753affee4b2STejun Heo *
3754affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified
3755112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target.
37564690c4abSTejun Heo *
37574690c4abSTejun Heo * CONTEXT:
3758a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock).
37594690c4abSTejun Heo */
insert_wq_barrier(struct pool_workqueue * pwq,struct wq_barrier * barr,struct work_struct * target,struct worker * worker)3760112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq,
3761affee4b2STejun Heo struct wq_barrier *barr,
3762affee4b2STejun Heo struct work_struct *target, struct worker *worker)
3763fc2e4d70SOleg Nesterov {
37644cb1ef64STejun Heo static __maybe_unused struct lock_class_key bh_key, thr_key;
3765d812796eSLai Jiangshan unsigned int work_flags = 0;
3766d812796eSLai Jiangshan unsigned int work_color;
3767affee4b2STejun Heo struct list_head *head;
3768affee4b2STejun Heo
3769dc186ad7SThomas Gleixner /*
3770d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked
3771dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the
3772dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we
3773dc186ad7SThomas Gleixner * might deadlock.
37744cb1ef64STejun Heo *
37754cb1ef64STejun Heo * BH and threaded workqueues need separate lockdep keys to avoid
37764cb1ef64STejun Heo * spuriously triggering "inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W}
37774cb1ef64STejun Heo * usage".
3778dc186ad7SThomas Gleixner */
37794cb1ef64STejun Heo INIT_WORK_ONSTACK_KEY(&barr->work, wq_barrier_func,
37804cb1ef64STejun Heo (pwq->wq->flags & WQ_BH) ? &bh_key : &thr_key);
378122df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
378252fa5bc5SBoqun Feng
3783fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map);
3784fd1a5b04SByungchul Park
37852607d7a6STejun Heo barr->task = current;
378683c22520SOleg Nesterov
37875797b1c1STejun Heo /* The barrier work item does not participate in nr_active. */
3788018f3a13SLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE;
3789018f3a13SLai Jiangshan
3790affee4b2STejun Heo /*
3791affee4b2STejun Heo * If @target is currently being executed, schedule the
3792affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target.
3793affee4b2STejun Heo */
3794d812796eSLai Jiangshan if (worker) {
3795affee4b2STejun Heo head = worker->scheduled.next;
3796d812796eSLai Jiangshan work_color = worker->current_color;
3797d812796eSLai Jiangshan } else {
3798affee4b2STejun Heo unsigned long *bits = work_data_bits(target);
3799affee4b2STejun Heo
3800affee4b2STejun Heo head = target->entry.next;
3801affee4b2STejun Heo /* there can already be other linked works, inherit and set */
3802d21cece0SLai Jiangshan work_flags |= *bits & WORK_STRUCT_LINKED;
3803d812796eSLai Jiangshan work_color = get_work_color(*bits);
3804affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits);
3805affee4b2STejun Heo }
3806affee4b2STejun Heo
3807d812796eSLai Jiangshan pwq->nr_in_flight[work_color]++;
3808d812796eSLai Jiangshan work_flags |= work_color_to_flags(work_color);
3809d812796eSLai Jiangshan
3810d21cece0SLai Jiangshan insert_work(pwq, &barr->work, head, work_flags);
3811fc2e4d70SOleg Nesterov }
3812fc2e4d70SOleg Nesterov
381373f53c4aSTejun Heo /**
3814112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
381573f53c4aSTejun Heo * @wq: workqueue being flushed
381673f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op
381773f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op
381873f53c4aSTejun Heo *
3819112202d9STejun Heo * Prepare pwqs for workqueue flushing.
382073f53c4aSTejun Heo *
3821112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be
3822112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all
3823112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq
3824112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to
3825112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
382673f53c4aSTejun Heo * wakeup logic is armed and %true is returned.
382773f53c4aSTejun Heo *
382873f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to
382973f53c4aSTejun Heo * calling this function with non-negative @flush_color. If
383073f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false
383173f53c4aSTejun Heo * is returned.
383273f53c4aSTejun Heo *
3833112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same
383473f53c4aSTejun Heo * work_color which is previous to @work_color and all will be
383573f53c4aSTejun Heo * advanced to @work_color.
383673f53c4aSTejun Heo *
383773f53c4aSTejun Heo * CONTEXT:
38383c25a55dSLai Jiangshan * mutex_lock(wq->mutex).
383973f53c4aSTejun Heo *
3840d185af30SYacine Belkadi * Return:
384173f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false
384273f53c4aSTejun Heo * otherwise.
384373f53c4aSTejun Heo */
flush_workqueue_prep_pwqs(struct workqueue_struct * wq,int flush_color,int work_color)3844112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
384573f53c4aSTejun Heo int flush_color, int work_color)
38461da177e4SLinus Torvalds {
384773f53c4aSTejun Heo bool wait = false;
384849e3cf44STejun Heo struct pool_workqueue *pwq;
384985f0d8e3SWangyang Guo struct worker_pool *current_pool = NULL;
38501da177e4SLinus Torvalds
385173f53c4aSTejun Heo if (flush_color >= 0) {
38526183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
3853112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1);
3854dc186ad7SThomas Gleixner }
385514441960SOleg Nesterov
385685f0d8e3SWangyang Guo /*
385785f0d8e3SWangyang Guo * For unbound workqueue, pwqs will map to only a few pools.
385885f0d8e3SWangyang Guo * Most of the time, pwqs within the same pool will be linked
385985f0d8e3SWangyang Guo * sequentially to wq->pwqs by cpu index. So in the majority
386085f0d8e3SWangyang Guo * of pwq iters, the pool is the same, only doing lock/unlock
386185f0d8e3SWangyang Guo * if the pool has changed. This can largely reduce expensive
386285f0d8e3SWangyang Guo * lock operations.
386385f0d8e3SWangyang Guo */
386449e3cf44STejun Heo for_each_pwq(pwq, wq) {
386585f0d8e3SWangyang Guo if (current_pool != pwq->pool) {
386685f0d8e3SWangyang Guo if (likely(current_pool))
386785f0d8e3SWangyang Guo raw_spin_unlock_irq(¤t_pool->lock);
386885f0d8e3SWangyang Guo current_pool = pwq->pool;
386985f0d8e3SWangyang Guo raw_spin_lock_irq(¤t_pool->lock);
387085f0d8e3SWangyang Guo }
387173f53c4aSTejun Heo
387273f53c4aSTejun Heo if (flush_color >= 0) {
38736183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1);
387473f53c4aSTejun Heo
3875112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) {
3876112202d9STejun Heo pwq->flush_color = flush_color;
3877112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush);
387873f53c4aSTejun Heo wait = true;
38791da177e4SLinus Torvalds }
388073f53c4aSTejun Heo }
388173f53c4aSTejun Heo
388273f53c4aSTejun Heo if (work_color >= 0) {
38836183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
3884112202d9STejun Heo pwq->work_color = work_color;
388573f53c4aSTejun Heo }
388673f53c4aSTejun Heo
38871da177e4SLinus Torvalds }
38881da177e4SLinus Torvalds
388985f0d8e3SWangyang Guo if (current_pool)
389085f0d8e3SWangyang Guo raw_spin_unlock_irq(¤t_pool->lock);
389185f0d8e3SWangyang Guo
3892112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
389373f53c4aSTejun Heo complete(&wq->first_flusher->done);
389473f53c4aSTejun Heo
389573f53c4aSTejun Heo return wait;
389683c22520SOleg Nesterov }
38971da177e4SLinus Torvalds
touch_wq_lockdep_map(struct workqueue_struct * wq)3898c35aea39STejun Heo static void touch_wq_lockdep_map(struct workqueue_struct *wq)
3899c35aea39STejun Heo {
39004cb1ef64STejun Heo #ifdef CONFIG_LOCKDEP
390184c425beSSergey Senozhatsky if (unlikely(!wq->lockdep_map))
390284c425beSSergey Senozhatsky return;
390384c425beSSergey Senozhatsky
39044cb1ef64STejun Heo if (wq->flags & WQ_BH)
39054cb1ef64STejun Heo local_bh_disable();
39064cb1ef64STejun Heo
39074f022f43SMatthew Brost lock_map_acquire(wq->lockdep_map);
39084f022f43SMatthew Brost lock_map_release(wq->lockdep_map);
39094cb1ef64STejun Heo
39104cb1ef64STejun Heo if (wq->flags & WQ_BH)
39114cb1ef64STejun Heo local_bh_enable();
39124cb1ef64STejun Heo #endif
3913c35aea39STejun Heo }
3914c35aea39STejun Heo
touch_work_lockdep_map(struct work_struct * work,struct workqueue_struct * wq)3915c35aea39STejun Heo static void touch_work_lockdep_map(struct work_struct *work,
3916c35aea39STejun Heo struct workqueue_struct *wq)
3917c35aea39STejun Heo {
39184cb1ef64STejun Heo #ifdef CONFIG_LOCKDEP
39194cb1ef64STejun Heo if (wq->flags & WQ_BH)
39204cb1ef64STejun Heo local_bh_disable();
39214cb1ef64STejun Heo
3922c35aea39STejun Heo lock_map_acquire(&work->lockdep_map);
3923c35aea39STejun Heo lock_map_release(&work->lockdep_map);
39244cb1ef64STejun Heo
39254cb1ef64STejun Heo if (wq->flags & WQ_BH)
39264cb1ef64STejun Heo local_bh_enable();
39274cb1ef64STejun Heo #endif
3928c35aea39STejun Heo }
3929c35aea39STejun Heo
39300fcb78c2SRolf Eike Beer /**
3931c4f135d6STetsuo Handa * __flush_workqueue - ensure that any scheduled work has run to completion.
39320fcb78c2SRolf Eike Beer * @wq: workqueue to flush
39331da177e4SLinus Torvalds *
3934c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry
3935c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones.
39361da177e4SLinus Torvalds */
__flush_workqueue(struct workqueue_struct * wq)3937c4f135d6STetsuo Handa void __flush_workqueue(struct workqueue_struct *wq)
39381da177e4SLinus Torvalds {
393973f53c4aSTejun Heo struct wq_flusher this_flusher = {
394073f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list),
394173f53c4aSTejun Heo .flush_color = -1,
39424f022f43SMatthew Brost .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, (*wq->lockdep_map)),
394373f53c4aSTejun Heo };
394473f53c4aSTejun Heo int next_color;
3945b1f4ec17SOleg Nesterov
39463347fa09STejun Heo if (WARN_ON(!wq_online))
39473347fa09STejun Heo return;
39483347fa09STejun Heo
3949c35aea39STejun Heo touch_wq_lockdep_map(wq);
395087915adcSJohannes Berg
39513c25a55dSLai Jiangshan mutex_lock(&wq->mutex);
395273f53c4aSTejun Heo
395373f53c4aSTejun Heo /*
395473f53c4aSTejun Heo * Start-to-wait phase
395573f53c4aSTejun Heo */
395673f53c4aSTejun Heo next_color = work_next_color(wq->work_color);
395773f53c4aSTejun Heo
395873f53c4aSTejun Heo if (next_color != wq->flush_color) {
395973f53c4aSTejun Heo /*
396073f53c4aSTejun Heo * Color space is not full. The current work_color
396173f53c4aSTejun Heo * becomes our flush_color and work_color is advanced
396273f53c4aSTejun Heo * by one.
396373f53c4aSTejun Heo */
39646183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
396573f53c4aSTejun Heo this_flusher.flush_color = wq->work_color;
396673f53c4aSTejun Heo wq->work_color = next_color;
396773f53c4aSTejun Heo
396873f53c4aSTejun Heo if (!wq->first_flusher) {
396973f53c4aSTejun Heo /* no flush in progress, become the first flusher */
39706183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
397173f53c4aSTejun Heo
397273f53c4aSTejun Heo wq->first_flusher = &this_flusher;
397373f53c4aSTejun Heo
3974112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
397573f53c4aSTejun Heo wq->work_color)) {
397673f53c4aSTejun Heo /* nothing to flush, done */
397773f53c4aSTejun Heo wq->flush_color = next_color;
397873f53c4aSTejun Heo wq->first_flusher = NULL;
397973f53c4aSTejun Heo goto out_unlock;
398073f53c4aSTejun Heo }
398173f53c4aSTejun Heo } else {
398273f53c4aSTejun Heo /* wait in queue */
39836183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
398473f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue);
3985112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
398673f53c4aSTejun Heo }
398773f53c4aSTejun Heo } else {
398873f53c4aSTejun Heo /*
398973f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue.
399073f53c4aSTejun Heo * The next flush completion will assign us
399173f53c4aSTejun Heo * flush_color and transfer to flusher_queue.
399273f53c4aSTejun Heo */
399373f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow);
399473f53c4aSTejun Heo }
399573f53c4aSTejun Heo
3996de35994eSTvrtko Ursulin check_flush_dependency(wq, NULL, false);
3997fca839c0STejun Heo
39983c25a55dSLai Jiangshan mutex_unlock(&wq->mutex);
399973f53c4aSTejun Heo
400073f53c4aSTejun Heo wait_for_completion(&this_flusher.done);
400173f53c4aSTejun Heo
400273f53c4aSTejun Heo /*
400373f53c4aSTejun Heo * Wake-up-and-cascade phase
400473f53c4aSTejun Heo *
400573f53c4aSTejun Heo * First flushers are responsible for cascading flushes and
400673f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return.
400773f53c4aSTejun Heo */
400800d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher)
400973f53c4aSTejun Heo return;
401073f53c4aSTejun Heo
40113c25a55dSLai Jiangshan mutex_lock(&wq->mutex);
401273f53c4aSTejun Heo
40134ce48b37STejun Heo /* we might have raced, check again with mutex held */
40144ce48b37STejun Heo if (wq->first_flusher != &this_flusher)
40154ce48b37STejun Heo goto out_unlock;
40164ce48b37STejun Heo
401700d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL);
401873f53c4aSTejun Heo
40196183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list));
40206183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
402173f53c4aSTejun Heo
402273f53c4aSTejun Heo while (true) {
402373f53c4aSTejun Heo struct wq_flusher *next, *tmp;
402473f53c4aSTejun Heo
402573f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */
402673f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
402773f53c4aSTejun Heo if (next->flush_color != wq->flush_color)
402873f53c4aSTejun Heo break;
402973f53c4aSTejun Heo list_del_init(&next->list);
403073f53c4aSTejun Heo complete(&next->done);
403173f53c4aSTejun Heo }
403273f53c4aSTejun Heo
40336183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
403473f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color));
403573f53c4aSTejun Heo
403673f53c4aSTejun Heo /* this flush_color is finished, advance by one */
403773f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color);
403873f53c4aSTejun Heo
403973f53c4aSTejun Heo /* one color has been freed, handle overflow queue */
404073f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) {
404173f53c4aSTejun Heo /*
404273f53c4aSTejun Heo * Assign the same color to all overflowed
404373f53c4aSTejun Heo * flushers, advance work_color and append to
404473f53c4aSTejun Heo * flusher_queue. This is the start-to-wait
404573f53c4aSTejun Heo * phase for these overflowed flushers.
404673f53c4aSTejun Heo */
404773f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list)
404873f53c4aSTejun Heo tmp->flush_color = wq->work_color;
404973f53c4aSTejun Heo
405073f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color);
405173f53c4aSTejun Heo
405273f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow,
405373f53c4aSTejun Heo &wq->flusher_queue);
4054112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
405573f53c4aSTejun Heo }
405673f53c4aSTejun Heo
405773f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) {
40586183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color);
405973f53c4aSTejun Heo break;
406073f53c4aSTejun Heo }
406173f53c4aSTejun Heo
406273f53c4aSTejun Heo /*
406373f53c4aSTejun Heo * Need to flush more colors. Make the next flusher
4064112202d9STejun Heo * the new first flusher and arm pwqs.
406573f53c4aSTejun Heo */
40666183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color);
40676183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color);
406873f53c4aSTejun Heo
406973f53c4aSTejun Heo list_del_init(&next->list);
407073f53c4aSTejun Heo wq->first_flusher = next;
407173f53c4aSTejun Heo
4072112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
407373f53c4aSTejun Heo break;
407473f53c4aSTejun Heo
407573f53c4aSTejun Heo /*
407673f53c4aSTejun Heo * Meh... this color is already done, clear first
407773f53c4aSTejun Heo * flusher and repeat cascading.
407873f53c4aSTejun Heo */
407973f53c4aSTejun Heo wq->first_flusher = NULL;
408073f53c4aSTejun Heo }
408173f53c4aSTejun Heo
408273f53c4aSTejun Heo out_unlock:
40833c25a55dSLai Jiangshan mutex_unlock(&wq->mutex);
40841da177e4SLinus Torvalds }
4085c4f135d6STetsuo Handa EXPORT_SYMBOL(__flush_workqueue);
40861da177e4SLinus Torvalds
40879c5a2ba7STejun Heo /**
40889c5a2ba7STejun Heo * drain_workqueue - drain a workqueue
40899c5a2ba7STejun Heo * @wq: workqueue to drain
40909c5a2ba7STejun Heo *
40919c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress,
40929c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running
40939c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed
4094b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined
40959c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it
40969c5a2ba7STejun Heo * takes too long.
40979c5a2ba7STejun Heo */
drain_workqueue(struct workqueue_struct * wq)40989c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq)
40999c5a2ba7STejun Heo {
41009c5a2ba7STejun Heo unsigned int flush_cnt = 0;
410149e3cf44STejun Heo struct pool_workqueue *pwq;
41029c5a2ba7STejun Heo
41039c5a2ba7STejun Heo /*
41049c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much
41059c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags.
4106618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
41079c5a2ba7STejun Heo */
410887fc741eSLai Jiangshan mutex_lock(&wq->mutex);
41099c5a2ba7STejun Heo if (!wq->nr_drainers++)
4110618b01ebSTejun Heo wq->flags |= __WQ_DRAINING;
411187fc741eSLai Jiangshan mutex_unlock(&wq->mutex);
41129c5a2ba7STejun Heo reflush:
4113c4f135d6STetsuo Handa __flush_workqueue(wq);
41149c5a2ba7STejun Heo
4115b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex);
411676af4d93STejun Heo
411749e3cf44STejun Heo for_each_pwq(pwq, wq) {
4118fa2563e4SThomas Tuttle bool drained;
41199c5a2ba7STejun Heo
4120a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock);
4121afa87ce8STejun Heo drained = pwq_is_empty(pwq);
4122a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock);
4123fa2563e4SThomas Tuttle
4124fa2563e4SThomas Tuttle if (drained)
41259c5a2ba7STejun Heo continue;
41269c5a2ba7STejun Heo
41279c5a2ba7STejun Heo if (++flush_cnt == 10 ||
41289c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000))
4129e9ad2eb3SStephen Zhang pr_warn("workqueue %s: %s() isn't complete after %u tries\n",
4130e9ad2eb3SStephen Zhang wq->name, __func__, flush_cnt);
413176af4d93STejun Heo
4132b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex);
41339c5a2ba7STejun Heo goto reflush;
41349c5a2ba7STejun Heo }
41359c5a2ba7STejun Heo
41369c5a2ba7STejun Heo if (!--wq->nr_drainers)
4137618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING;
413887fc741eSLai Jiangshan mutex_unlock(&wq->mutex);
41399c5a2ba7STejun Heo }
41409c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue);
41419c5a2ba7STejun Heo
start_flush_work(struct work_struct * work,struct wq_barrier * barr,bool from_cancel)4142d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
4143d6e89786SJohannes Berg bool from_cancel)
4144baf59022STejun Heo {
4145baf59022STejun Heo struct worker *worker = NULL;
4146c9e7cf27STejun Heo struct worker_pool *pool;
4147112202d9STejun Heo struct pool_workqueue *pwq;
4148c35aea39STejun Heo struct workqueue_struct *wq;
4149baf59022STejun Heo
415024acfb71SThomas Gleixner rcu_read_lock();
4151fa1b54e6STejun Heo pool = get_work_pool(work);
4152fa1b54e6STejun Heo if (!pool) {
415324acfb71SThomas Gleixner rcu_read_unlock();
4154fa1b54e6STejun Heo return false;
4155fa1b54e6STejun Heo }
4156fa1b54e6STejun Heo
4157a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock);
41580b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */
4159112202d9STejun Heo pwq = get_work_pwq(work);
4160112202d9STejun Heo if (pwq) {
4161112202d9STejun Heo if (unlikely(pwq->pool != pool))
4162baf59022STejun Heo goto already_gone;
4163606a5020STejun Heo } else {
4164c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work);
4165baf59022STejun Heo if (!worker)
4166baf59022STejun Heo goto already_gone;
4167112202d9STejun Heo pwq = worker->current_pwq;
4168606a5020STejun Heo }
4169baf59022STejun Heo
4170c35aea39STejun Heo wq = pwq->wq;
4171de35994eSTvrtko Ursulin check_flush_dependency(wq, work, from_cancel);
4172fca839c0STejun Heo
4173112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker);
4174a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock);
4175baf59022STejun Heo
4176c35aea39STejun Heo touch_work_lockdep_map(work, wq);
4177c35aea39STejun Heo
4178e159489bSTejun Heo /*
4179a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a
4180a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue.
4181a1d14934SPeter Zijlstra *
4182a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work
4183a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped
4184a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking
4185a1d14934SPeter Zijlstra * forward progress.
4186e159489bSTejun Heo */
4187c35aea39STejun Heo if (!from_cancel && (wq->saved_max_active == 1 || wq->rescuer))
4188c35aea39STejun Heo touch_wq_lockdep_map(wq);
4189c35aea39STejun Heo
419024acfb71SThomas Gleixner rcu_read_unlock();
4191baf59022STejun Heo return true;
4192baf59022STejun Heo already_gone:
4193a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock);
419424acfb71SThomas Gleixner rcu_read_unlock();
4195baf59022STejun Heo return false;
4196baf59022STejun Heo }
4197baf59022STejun Heo
__flush_work(struct work_struct * work,bool from_cancel)4198d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel)
4199d6e89786SJohannes Berg {
4200d6e89786SJohannes Berg struct wq_barrier barr;
4201d6e89786SJohannes Berg
4202d6e89786SJohannes Berg if (WARN_ON(!wq_online))
4203d6e89786SJohannes Berg return false;
4204d6e89786SJohannes Berg
42054d43d395STetsuo Handa if (WARN_ON(!work->func))
42064d43d395STetsuo Handa return false;
42074d43d395STetsuo Handa
4208134874e2STejun Heo if (!start_flush_work(work, &barr, from_cancel))
4209134874e2STejun Heo return false;
4210134874e2STejun Heo
4211134874e2STejun Heo /*
4212134874e2STejun Heo * start_flush_work() returned %true. If @from_cancel is set, we know
4213134874e2STejun Heo * that @work must have been executing during start_flush_work() and
4214134874e2STejun Heo * can't currently be queued. Its data must contain OFFQ bits. If @work
4215134874e2STejun Heo * was queued on a BH workqueue, we also know that it was running in the
4216134874e2STejun Heo * BH context and thus can be busy-waited.
4217134874e2STejun Heo */
42188bc35475STejun Heo if (from_cancel) {
42198bc35475STejun Heo unsigned long data = *work_data_bits(work);
42208bc35475STejun Heo
42218bc35475STejun Heo if (!WARN_ON_ONCE(data & WORK_STRUCT_PWQ) &&
42228bc35475STejun Heo (data & WORK_OFFQ_BH)) {
4223134874e2STejun Heo /*
42248bc35475STejun Heo * On RT, prevent a live lock when %current preempted
42258bc35475STejun Heo * soft interrupt processing or prevents ksoftirqd from
42268bc35475STejun Heo * running by keeping flipping BH. If the BH work item
42278bc35475STejun Heo * runs on a different CPU then this has no effect other
42288bc35475STejun Heo * than doing the BH disable/enable dance for nothing.
42298bc35475STejun Heo * This is copied from
4230134874e2STejun Heo * kernel/softirq.c::tasklet_unlock_spin_wait().
4231134874e2STejun Heo */
4232134874e2STejun Heo while (!try_wait_for_completion(&barr.done)) {
4233134874e2STejun Heo if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
4234134874e2STejun Heo local_bh_disable();
4235134874e2STejun Heo local_bh_enable();
4236134874e2STejun Heo } else {
4237134874e2STejun Heo cpu_relax();
4238134874e2STejun Heo }
4239134874e2STejun Heo }
42408bc35475STejun Heo goto out_destroy;
42418bc35475STejun Heo }
4242134874e2STejun Heo }
4243134874e2STejun Heo
42448bc35475STejun Heo wait_for_completion(&barr.done);
42458bc35475STejun Heo
42468bc35475STejun Heo out_destroy:
4247d6e89786SJohannes Berg destroy_work_on_stack(&barr.work);
4248d6e89786SJohannes Berg return true;
4249d6e89786SJohannes Berg }
4250d6e89786SJohannes Berg
4251db700897SOleg Nesterov /**
4252401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance
4253401a8d04STejun Heo * @work: the work to flush
4254db700897SOleg Nesterov *
4255606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle
4256606a5020STejun Heo * on return if it hasn't been requeued since flush started.
4257401a8d04STejun Heo *
4258d185af30SYacine Belkadi * Return:
4259401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution,
4260401a8d04STejun Heo * %false if it was already idle.
4261db700897SOleg Nesterov */
flush_work(struct work_struct * work)4262401a8d04STejun Heo bool flush_work(struct work_struct *work)
4263db700897SOleg Nesterov {
4264134874e2STejun Heo might_sleep();
4265d6e89786SJohannes Berg return __flush_work(work, false);
4266606a5020STejun Heo }
4267db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work);
4268db700897SOleg Nesterov
4269cdc6e4b3STejun Heo /**
4270cdc6e4b3STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing
4271cdc6e4b3STejun Heo * @dwork: the delayed work to flush
4272cdc6e4b3STejun Heo *
4273cdc6e4b3STejun Heo * Delayed timer is cancelled and the pending work is queued for
4274cdc6e4b3STejun Heo * immediate execution. Like flush_work(), this function only
4275cdc6e4b3STejun Heo * considers the last queueing instance of @dwork.
4276cdc6e4b3STejun Heo *
4277cdc6e4b3STejun Heo * Return:
4278cdc6e4b3STejun Heo * %true if flush_work() waited for the work to finish execution,
4279cdc6e4b3STejun Heo * %false if it was already idle.
4280cdc6e4b3STejun Heo */
flush_delayed_work(struct delayed_work * dwork)4281cdc6e4b3STejun Heo bool flush_delayed_work(struct delayed_work *dwork)
4282cdc6e4b3STejun Heo {
4283cdc6e4b3STejun Heo local_irq_disable();
4284*8fa7292fSThomas Gleixner if (timer_delete_sync(&dwork->timer))
4285cdc6e4b3STejun Heo __queue_work(dwork->cpu, dwork->wq, &dwork->work);
4286cdc6e4b3STejun Heo local_irq_enable();
4287cdc6e4b3STejun Heo return flush_work(&dwork->work);
4288cdc6e4b3STejun Heo }
4289cdc6e4b3STejun Heo EXPORT_SYMBOL(flush_delayed_work);
4290cdc6e4b3STejun Heo
4291cdc6e4b3STejun Heo /**
4292cdc6e4b3STejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing
4293cdc6e4b3STejun Heo * @rwork: the rcu work to flush
4294cdc6e4b3STejun Heo *
4295cdc6e4b3STejun Heo * Return:
4296cdc6e4b3STejun Heo * %true if flush_rcu_work() waited for the work to finish execution,
4297cdc6e4b3STejun Heo * %false if it was already idle.
4298cdc6e4b3STejun Heo */
flush_rcu_work(struct rcu_work * rwork)4299cdc6e4b3STejun Heo bool flush_rcu_work(struct rcu_work *rwork)
4300cdc6e4b3STejun Heo {
4301cdc6e4b3STejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) {
4302cdc6e4b3STejun Heo rcu_barrier();
4303cdc6e4b3STejun Heo flush_work(&rwork->work);
4304cdc6e4b3STejun Heo return true;
4305cdc6e4b3STejun Heo } else {
4306cdc6e4b3STejun Heo return flush_work(&rwork->work);
4307cdc6e4b3STejun Heo }
4308cdc6e4b3STejun Heo }
4309cdc6e4b3STejun Heo EXPORT_SYMBOL(flush_rcu_work);
4310cdc6e4b3STejun Heo
work_offqd_disable(struct work_offq_data * offqd)431186898fa6STejun Heo static void work_offqd_disable(struct work_offq_data *offqd)
431286898fa6STejun Heo {
431386898fa6STejun Heo const unsigned long max = (1lu << WORK_OFFQ_DISABLE_BITS) - 1;
431486898fa6STejun Heo
431586898fa6STejun Heo if (likely(offqd->disable < max))
431686898fa6STejun Heo offqd->disable++;
431786898fa6STejun Heo else
431886898fa6STejun Heo WARN_ONCE(true, "workqueue: work disable count overflowed\n");
431986898fa6STejun Heo }
432086898fa6STejun Heo
work_offqd_enable(struct work_offq_data * offqd)432186898fa6STejun Heo static void work_offqd_enable(struct work_offq_data *offqd)
432286898fa6STejun Heo {
432386898fa6STejun Heo if (likely(offqd->disable > 0))
432486898fa6STejun Heo offqd->disable--;
432586898fa6STejun Heo else
432686898fa6STejun Heo WARN_ONCE(true, "workqueue: work disable count underflowed\n");
432786898fa6STejun Heo }
432886898fa6STejun Heo
__cancel_work(struct work_struct * work,u32 cflags)4329c5f5b942STejun Heo static bool __cancel_work(struct work_struct *work, u32 cflags)
4330cdc6e4b3STejun Heo {
43311211f3b2STejun Heo struct work_offq_data offqd;
4332c26e2f2eSTejun Heo unsigned long irq_flags;
4333cdc6e4b3STejun Heo int ret;
4334cdc6e4b3STejun Heo
433586898fa6STejun Heo ret = work_grab_pending(work, cflags, &irq_flags);
4336cdc6e4b3STejun Heo
43371211f3b2STejun Heo work_offqd_unpack(&offqd, *work_data_bits(work));
4338cdc6e4b3STejun Heo
433986898fa6STejun Heo if (cflags & WORK_CANCEL_DISABLE)
434086898fa6STejun Heo work_offqd_disable(&offqd);
434186898fa6STejun Heo
43421211f3b2STejun Heo set_work_pool_and_clear_pending(work, offqd.pool_id,
43431211f3b2STejun Heo work_offqd_pack_flags(&offqd));
4344c26e2f2eSTejun Heo local_irq_restore(irq_flags);
4345cdc6e4b3STejun Heo return ret;
4346cdc6e4b3STejun Heo }
4347cdc6e4b3STejun Heo
__cancel_work_sync(struct work_struct * work,u32 cflags)4348c5f5b942STejun Heo static bool __cancel_work_sync(struct work_struct *work, u32 cflags)
4349401a8d04STejun Heo {
4350978b8409STejun Heo bool ret;
43511f1f642eSOleg Nesterov
4352f09b10b6STejun Heo ret = __cancel_work(work, cflags | WORK_CANCEL_DISABLE);
4353bbb68dfaSTejun Heo
4354134874e2STejun Heo if (*work_data_bits(work) & WORK_OFFQ_BH)
4355134874e2STejun Heo WARN_ON_ONCE(in_hardirq());
4356134874e2STejun Heo else
4357134874e2STejun Heo might_sleep();
4358bbb68dfaSTejun Heo
43593347fa09STejun Heo /*
4360c7a40c49STejun Heo * Skip __flush_work() during early boot when we know that @work isn't
4361c7a40c49STejun Heo * executing. This allows canceling during early boot.
43623347fa09STejun Heo */
43633347fa09STejun Heo if (wq_online)
4364d6e89786SJohannes Berg __flush_work(work, true);
43653347fa09STejun Heo
4366f09b10b6STejun Heo if (!(cflags & WORK_CANCEL_DISABLE))
4367f09b10b6STejun Heo enable_work(work);
43688603e1b3STejun Heo
43691f1f642eSOleg Nesterov return ret;
43701f1f642eSOleg Nesterov }
43711f1f642eSOleg Nesterov
4372cdc6e4b3STejun Heo /*
4373cdc6e4b3STejun Heo * See cancel_delayed_work()
4374cdc6e4b3STejun Heo */
cancel_work(struct work_struct * work)4375cdc6e4b3STejun Heo bool cancel_work(struct work_struct *work)
4376cdc6e4b3STejun Heo {
4377c5f5b942STejun Heo return __cancel_work(work, 0);
4378cdc6e4b3STejun Heo }
4379cdc6e4b3STejun Heo EXPORT_SYMBOL(cancel_work);
4380cdc6e4b3STejun Heo
43816e84d644SOleg Nesterov /**
4382401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish
4383401a8d04STejun Heo * @work: the work to cancel
43846e84d644SOleg Nesterov *
4385134874e2STejun Heo * Cancel @work and wait for its execution to finish. This function can be used
4386134874e2STejun Heo * even if the work re-queues itself or migrates to another workqueue. On return
4387134874e2STejun Heo * from this function, @work is guaranteed to be not pending or executing on any
4388134874e2STejun Heo * CPU as long as there aren't racing enqueues.
43891f1f642eSOleg Nesterov *
4390134874e2STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for delayed_work's.
4391134874e2STejun Heo * Use cancel_delayed_work_sync() instead.
43926e84d644SOleg Nesterov *
4393134874e2STejun Heo * Must be called from a sleepable context if @work was last queued on a non-BH
4394134874e2STejun Heo * workqueue. Can also be called from non-hardirq atomic contexts including BH
4395134874e2STejun Heo * if @work was last queued on a BH workqueue.
4396401a8d04STejun Heo *
4397134874e2STejun Heo * Returns %true if @work was pending, %false otherwise.
43986e84d644SOleg Nesterov */
cancel_work_sync(struct work_struct * work)4399401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work)
44006e84d644SOleg Nesterov {
4401c5f5b942STejun Heo return __cancel_work_sync(work, 0);
4402b89deed3SOleg Nesterov }
440328e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync);
4404b89deed3SOleg Nesterov
44056e84d644SOleg Nesterov /**
440657b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work
440757b30ae7STejun Heo * @dwork: delayed_work to cancel
440809383498STejun Heo *
4409d185af30SYacine Belkadi * Kill off a pending delayed_work.
4410d185af30SYacine Belkadi *
4411d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't
4412d185af30SYacine Belkadi * pending.
4413d185af30SYacine Belkadi *
4414d185af30SYacine Belkadi * Note:
4415d185af30SYacine Belkadi * The work callback function may still be running on return, unless
4416d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or
4417d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it.
441809383498STejun Heo *
441957b30ae7STejun Heo * This function is safe to call from any context including IRQ handler.
442009383498STejun Heo */
cancel_delayed_work(struct delayed_work * dwork)442157b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork)
442209383498STejun Heo {
4423c5f5b942STejun Heo return __cancel_work(&dwork->work, WORK_CANCEL_DELAYED);
442409383498STejun Heo }
442557b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work);
442609383498STejun Heo
442709383498STejun Heo /**
4428401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
4429401a8d04STejun Heo * @dwork: the delayed work cancel
4430401a8d04STejun Heo *
4431401a8d04STejun Heo * This is cancel_work_sync() for delayed works.
4432401a8d04STejun Heo *
4433d185af30SYacine Belkadi * Return:
4434401a8d04STejun Heo * %true if @dwork was pending, %false otherwise.
4435401a8d04STejun Heo */
cancel_delayed_work_sync(struct delayed_work * dwork)4436401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork)
44376e84d644SOleg Nesterov {
4438c5f5b942STejun Heo return __cancel_work_sync(&dwork->work, WORK_CANCEL_DELAYED);
44396e84d644SOleg Nesterov }
4440f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync);
44411da177e4SLinus Torvalds
44420fcb78c2SRolf Eike Beer /**
444386898fa6STejun Heo * disable_work - Disable and cancel a work item
444486898fa6STejun Heo * @work: work item to disable
444586898fa6STejun Heo *
444686898fa6STejun Heo * Disable @work by incrementing its disable count and cancel it if currently
444786898fa6STejun Heo * pending. As long as the disable count is non-zero, any attempt to queue @work
444886898fa6STejun Heo * will fail and return %false. The maximum supported disable depth is 2 to the
444986898fa6STejun Heo * power of %WORK_OFFQ_DISABLE_BITS, currently 65536.
445086898fa6STejun Heo *
4451f09b10b6STejun Heo * Can be called from any context. Returns %true if @work was pending, %false
4452f09b10b6STejun Heo * otherwise.
445386898fa6STejun Heo */
disable_work(struct work_struct * work)445486898fa6STejun Heo bool disable_work(struct work_struct *work)
445586898fa6STejun Heo {
445686898fa6STejun Heo return __cancel_work(work, WORK_CANCEL_DISABLE);
445786898fa6STejun Heo }
445886898fa6STejun Heo EXPORT_SYMBOL_GPL(disable_work);
445986898fa6STejun Heo
446086898fa6STejun Heo /**
446186898fa6STejun Heo * disable_work_sync - Disable, cancel and drain a work item
446286898fa6STejun Heo * @work: work item to disable
446386898fa6STejun Heo *
446486898fa6STejun Heo * Similar to disable_work() but also wait for @work to finish if currently
446586898fa6STejun Heo * executing.
446686898fa6STejun Heo *
4467134874e2STejun Heo * Must be called from a sleepable context if @work was last queued on a non-BH
4468134874e2STejun Heo * workqueue. Can also be called from non-hardirq atomic contexts including BH
4469134874e2STejun Heo * if @work was last queued on a BH workqueue.
4470134874e2STejun Heo *
4471134874e2STejun Heo * Returns %true if @work was pending, %false otherwise.
447286898fa6STejun Heo */
disable_work_sync(struct work_struct * work)447386898fa6STejun Heo bool disable_work_sync(struct work_struct *work)
447486898fa6STejun Heo {
447586898fa6STejun Heo return __cancel_work_sync(work, WORK_CANCEL_DISABLE);
447686898fa6STejun Heo }
447786898fa6STejun Heo EXPORT_SYMBOL_GPL(disable_work_sync);
447886898fa6STejun Heo
447986898fa6STejun Heo /**
448086898fa6STejun Heo * enable_work - Enable a work item
448186898fa6STejun Heo * @work: work item to enable
448286898fa6STejun Heo *
448386898fa6STejun Heo * Undo disable_work[_sync]() by decrementing @work's disable count. @work can
448486898fa6STejun Heo * only be queued if its disable count is 0.
448586898fa6STejun Heo *
4486f09b10b6STejun Heo * Can be called from any context. Returns %true if the disable count reached 0.
4487f09b10b6STejun Heo * Otherwise, %false.
448886898fa6STejun Heo */
enable_work(struct work_struct * work)448986898fa6STejun Heo bool enable_work(struct work_struct *work)
449086898fa6STejun Heo {
449186898fa6STejun Heo struct work_offq_data offqd;
449286898fa6STejun Heo unsigned long irq_flags;
449386898fa6STejun Heo
449486898fa6STejun Heo work_grab_pending(work, 0, &irq_flags);
449586898fa6STejun Heo
449686898fa6STejun Heo work_offqd_unpack(&offqd, *work_data_bits(work));
449786898fa6STejun Heo work_offqd_enable(&offqd);
449886898fa6STejun Heo set_work_pool_and_clear_pending(work, offqd.pool_id,
449986898fa6STejun Heo work_offqd_pack_flags(&offqd));
450086898fa6STejun Heo local_irq_restore(irq_flags);
450186898fa6STejun Heo
450286898fa6STejun Heo return !offqd.disable;
450386898fa6STejun Heo }
450486898fa6STejun Heo EXPORT_SYMBOL_GPL(enable_work);
450586898fa6STejun Heo
450686898fa6STejun Heo /**
450786898fa6STejun Heo * disable_delayed_work - Disable and cancel a delayed work item
450886898fa6STejun Heo * @dwork: delayed work item to disable
450986898fa6STejun Heo *
451086898fa6STejun Heo * disable_work() for delayed work items.
451186898fa6STejun Heo */
disable_delayed_work(struct delayed_work * dwork)451286898fa6STejun Heo bool disable_delayed_work(struct delayed_work *dwork)
451386898fa6STejun Heo {
451486898fa6STejun Heo return __cancel_work(&dwork->work,
451586898fa6STejun Heo WORK_CANCEL_DELAYED | WORK_CANCEL_DISABLE);
451686898fa6STejun Heo }
451786898fa6STejun Heo EXPORT_SYMBOL_GPL(disable_delayed_work);
451886898fa6STejun Heo
451986898fa6STejun Heo /**
452086898fa6STejun Heo * disable_delayed_work_sync - Disable, cancel and drain a delayed work item
452186898fa6STejun Heo * @dwork: delayed work item to disable
452286898fa6STejun Heo *
452386898fa6STejun Heo * disable_work_sync() for delayed work items.
452486898fa6STejun Heo */
disable_delayed_work_sync(struct delayed_work * dwork)452586898fa6STejun Heo bool disable_delayed_work_sync(struct delayed_work *dwork)
452686898fa6STejun Heo {
452786898fa6STejun Heo return __cancel_work_sync(&dwork->work,
452886898fa6STejun Heo WORK_CANCEL_DELAYED | WORK_CANCEL_DISABLE);
452986898fa6STejun Heo }
453086898fa6STejun Heo EXPORT_SYMBOL_GPL(disable_delayed_work_sync);
453186898fa6STejun Heo
453286898fa6STejun Heo /**
453386898fa6STejun Heo * enable_delayed_work - Enable a delayed work item
453486898fa6STejun Heo * @dwork: delayed work item to enable
453586898fa6STejun Heo *
453686898fa6STejun Heo * enable_work() for delayed work items.
453786898fa6STejun Heo */
enable_delayed_work(struct delayed_work * dwork)453886898fa6STejun Heo bool enable_delayed_work(struct delayed_work *dwork)
453986898fa6STejun Heo {
454086898fa6STejun Heo return enable_work(&dwork->work);
454186898fa6STejun Heo }
454286898fa6STejun Heo EXPORT_SYMBOL_GPL(enable_delayed_work);
454386898fa6STejun Heo
454486898fa6STejun Heo /**
454531ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU
4546b6136773SAndrew Morton * @func: the function to call
4547b6136773SAndrew Morton *
454831ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the
454931ddd871STejun Heo * system workqueue and blocks until all CPUs have completed.
4550b6136773SAndrew Morton * schedule_on_each_cpu() is very slow.
455131ddd871STejun Heo *
4552d185af30SYacine Belkadi * Return:
455331ddd871STejun Heo * 0 on success, -errno on failure.
4554b6136773SAndrew Morton */
schedule_on_each_cpu(work_func_t func)455565f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func)
455615316ba8SChristoph Lameter {
455715316ba8SChristoph Lameter int cpu;
455838f51568SNamhyung Kim struct work_struct __percpu *works;
455915316ba8SChristoph Lameter
4560b6136773SAndrew Morton works = alloc_percpu(struct work_struct);
4561b6136773SAndrew Morton if (!works)
456215316ba8SChristoph Lameter return -ENOMEM;
4563b6136773SAndrew Morton
4564ffd8bea8SSebastian Andrzej Siewior cpus_read_lock();
456593981800STejun Heo
456615316ba8SChristoph Lameter for_each_online_cpu(cpu) {
45679bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu);
45689bfb1839SIngo Molnar
45699bfb1839SIngo Molnar INIT_WORK(work, func);
45708de6d308SOleg Nesterov schedule_work_on(cpu, work);
457115316ba8SChristoph Lameter }
457293981800STejun Heo
457393981800STejun Heo for_each_online_cpu(cpu)
45748616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu));
457593981800STejun Heo
4576ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock();
4577b6136773SAndrew Morton free_percpu(works);
457815316ba8SChristoph Lameter return 0;
457915316ba8SChristoph Lameter }
458015316ba8SChristoph Lameter
4581eef6a7d5SAlan Stern /**
45821fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context
45831fa44ecaSJames Bottomley * @fn: the function to execute
45841fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must
45851fa44ecaSJames Bottomley * be available when the work executes)
45861fa44ecaSJames Bottomley *
45871fa44ecaSJames Bottomley * Executes the function immediately if process context is available,
45881fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution.
45891fa44ecaSJames Bottomley *
4590d185af30SYacine Belkadi * Return: 0 - function was executed
45911fa44ecaSJames Bottomley * 1 - function was scheduled for execution
45921fa44ecaSJames Bottomley */
execute_in_process_context(work_func_t fn,struct execute_work * ew)459365f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew)
45941fa44ecaSJames Bottomley {
45951fa44ecaSJames Bottomley if (!in_interrupt()) {
459665f27f38SDavid Howells fn(&ew->work);
45971fa44ecaSJames Bottomley return 0;
45981fa44ecaSJames Bottomley }
45991fa44ecaSJames Bottomley
460065f27f38SDavid Howells INIT_WORK(&ew->work, fn);
46011fa44ecaSJames Bottomley schedule_work(&ew->work);
46021fa44ecaSJames Bottomley
46031fa44ecaSJames Bottomley return 1;
46041fa44ecaSJames Bottomley }
46051fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context);
46061fa44ecaSJames Bottomley
46077a4e344cSTejun Heo /**
46087a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs
46097a4e344cSTejun Heo * @attrs: workqueue_attrs to free
46107a4e344cSTejun Heo *
46117a4e344cSTejun Heo * Undo alloc_workqueue_attrs().
46127a4e344cSTejun Heo */
free_workqueue_attrs(struct workqueue_attrs * attrs)4613513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs)
46147a4e344cSTejun Heo {
46157a4e344cSTejun Heo if (attrs) {
46167a4e344cSTejun Heo free_cpumask_var(attrs->cpumask);
46179546b29eSTejun Heo free_cpumask_var(attrs->__pod_cpumask);
46187a4e344cSTejun Heo kfree(attrs);
46197a4e344cSTejun Heo }
46207a4e344cSTejun Heo }
46217a4e344cSTejun Heo
46227a4e344cSTejun Heo /**
46237a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs
46247a4e344cSTejun Heo *
46257a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and
4626d185af30SYacine Belkadi * return it.
4627d185af30SYacine Belkadi *
4628d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure.
46297a4e344cSTejun Heo */
alloc_workqueue_attrs(void)4630513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void)
46317a4e344cSTejun Heo {
46327a4e344cSTejun Heo struct workqueue_attrs *attrs;
46337a4e344cSTejun Heo
4634be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
46357a4e344cSTejun Heo if (!attrs)
46367a4e344cSTejun Heo goto fail;
4637be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL))
46387a4e344cSTejun Heo goto fail;
46399546b29eSTejun Heo if (!alloc_cpumask_var(&attrs->__pod_cpumask, GFP_KERNEL))
46409546b29eSTejun Heo goto fail;
46417a4e344cSTejun Heo
464213e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask);
4643523a301eSTejun Heo attrs->affn_scope = WQ_AFFN_DFL;
46447a4e344cSTejun Heo return attrs;
46457a4e344cSTejun Heo fail:
46467a4e344cSTejun Heo free_workqueue_attrs(attrs);
46477a4e344cSTejun Heo return NULL;
46487a4e344cSTejun Heo }
46497a4e344cSTejun Heo
copy_workqueue_attrs(struct workqueue_attrs * to,const struct workqueue_attrs * from)465029c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to,
465129c91e99STejun Heo const struct workqueue_attrs *from)
465229c91e99STejun Heo {
465329c91e99STejun Heo to->nice = from->nice;
465429c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask);
46559546b29eSTejun Heo cpumask_copy(to->__pod_cpumask, from->__pod_cpumask);
46568639ecebSTejun Heo to->affn_strict = from->affn_strict;
465784193c07STejun Heo
46582865a8fbSShaohua Li /*
465984193c07STejun Heo * Unlike hash and equality test, copying shouldn't ignore wq-only
466084193c07STejun Heo * fields as copying is used for both pool and wq attrs. Instead,
466184193c07STejun Heo * get_unbound_pool() explicitly clears the fields.
46622865a8fbSShaohua Li */
466384193c07STejun Heo to->affn_scope = from->affn_scope;
4664af73f5c9STejun Heo to->ordered = from->ordered;
466529c91e99STejun Heo }
466629c91e99STejun Heo
46675de7a03cSTejun Heo /*
46685de7a03cSTejun Heo * Some attrs fields are workqueue-only. Clear them for worker_pool's. See the
46695de7a03cSTejun Heo * comments in 'struct workqueue_attrs' definition.
46705de7a03cSTejun Heo */
wqattrs_clear_for_pool(struct workqueue_attrs * attrs)46715de7a03cSTejun Heo static void wqattrs_clear_for_pool(struct workqueue_attrs *attrs)
46725de7a03cSTejun Heo {
467384193c07STejun Heo attrs->affn_scope = WQ_AFFN_NR_TYPES;
46745de7a03cSTejun Heo attrs->ordered = false;
4675ae1296a7SLai Jiangshan if (attrs->affn_strict)
4676ae1296a7SLai Jiangshan cpumask_copy(attrs->cpumask, cpu_possible_mask);
46775de7a03cSTejun Heo }
46785de7a03cSTejun Heo
467929c91e99STejun Heo /* hash value of the content of @attr */
wqattrs_hash(const struct workqueue_attrs * attrs)468029c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
468129c91e99STejun Heo {
468229c91e99STejun Heo u32 hash = 0;
468329c91e99STejun Heo
468429c91e99STejun Heo hash = jhash_1word(attrs->nice, hash);
4685ae1296a7SLai Jiangshan hash = jhash_1word(attrs->affn_strict, hash);
46869546b29eSTejun Heo hash = jhash(cpumask_bits(attrs->__pod_cpumask),
46879546b29eSTejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash);
4688ae1296a7SLai Jiangshan if (!attrs->affn_strict)
4689ae1296a7SLai Jiangshan hash = jhash(cpumask_bits(attrs->cpumask),
4690ae1296a7SLai Jiangshan BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash);
469129c91e99STejun Heo return hash;
469229c91e99STejun Heo }
469329c91e99STejun Heo
469429c91e99STejun Heo /* content equality test */
wqattrs_equal(const struct workqueue_attrs * a,const struct workqueue_attrs * b)469529c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a,
469629c91e99STejun Heo const struct workqueue_attrs *b)
469729c91e99STejun Heo {
469829c91e99STejun Heo if (a->nice != b->nice)
469929c91e99STejun Heo return false;
4700ae1296a7SLai Jiangshan if (a->affn_strict != b->affn_strict)
470129c91e99STejun Heo return false;
47029546b29eSTejun Heo if (!cpumask_equal(a->__pod_cpumask, b->__pod_cpumask))
47039546b29eSTejun Heo return false;
4704ae1296a7SLai Jiangshan if (!a->affn_strict && !cpumask_equal(a->cpumask, b->cpumask))
47058639ecebSTejun Heo return false;
470629c91e99STejun Heo return true;
470729c91e99STejun Heo }
470829c91e99STejun Heo
47090f36ee24STejun Heo /* Update @attrs with actually available CPUs */
wqattrs_actualize_cpumask(struct workqueue_attrs * attrs,const cpumask_t * unbound_cpumask)47100f36ee24STejun Heo static void wqattrs_actualize_cpumask(struct workqueue_attrs *attrs,
47110f36ee24STejun Heo const cpumask_t *unbound_cpumask)
47120f36ee24STejun Heo {
47130f36ee24STejun Heo /*
47140f36ee24STejun Heo * Calculate the effective CPU mask of @attrs given @unbound_cpumask. If
47150f36ee24STejun Heo * @attrs->cpumask doesn't overlap with @unbound_cpumask, we fallback to
47160f36ee24STejun Heo * @unbound_cpumask.
47170f36ee24STejun Heo */
47180f36ee24STejun Heo cpumask_and(attrs->cpumask, attrs->cpumask, unbound_cpumask);
47190f36ee24STejun Heo if (unlikely(cpumask_empty(attrs->cpumask)))
47200f36ee24STejun Heo cpumask_copy(attrs->cpumask, unbound_cpumask);
47210f36ee24STejun Heo }
47220f36ee24STejun Heo
472384193c07STejun Heo /* find wq_pod_type to use for @attrs */
472484193c07STejun Heo static const struct wq_pod_type *
wqattrs_pod_type(const struct workqueue_attrs * attrs)472584193c07STejun Heo wqattrs_pod_type(const struct workqueue_attrs *attrs)
472684193c07STejun Heo {
4727523a301eSTejun Heo enum wq_affn_scope scope;
4728523a301eSTejun Heo struct wq_pod_type *pt;
4729523a301eSTejun Heo
4730523a301eSTejun Heo /* to synchronize access to wq_affn_dfl */
4731523a301eSTejun Heo lockdep_assert_held(&wq_pool_mutex);
4732523a301eSTejun Heo
4733523a301eSTejun Heo if (attrs->affn_scope == WQ_AFFN_DFL)
4734523a301eSTejun Heo scope = wq_affn_dfl;
4735523a301eSTejun Heo else
4736523a301eSTejun Heo scope = attrs->affn_scope;
4737523a301eSTejun Heo
4738523a301eSTejun Heo pt = &wq_pod_types[scope];
473984193c07STejun Heo
474084193c07STejun Heo if (!WARN_ON_ONCE(attrs->affn_scope == WQ_AFFN_NR_TYPES) &&
474184193c07STejun Heo likely(pt->nr_pods))
474284193c07STejun Heo return pt;
474384193c07STejun Heo
474484193c07STejun Heo /*
474584193c07STejun Heo * Before workqueue_init_topology(), only SYSTEM is available which is
474684193c07STejun Heo * initialized in workqueue_init_early().
474784193c07STejun Heo */
474884193c07STejun Heo pt = &wq_pod_types[WQ_AFFN_SYSTEM];
474984193c07STejun Heo BUG_ON(!pt->nr_pods);
475084193c07STejun Heo return pt;
475184193c07STejun Heo }
475284193c07STejun Heo
47537a4e344cSTejun Heo /**
47547a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool
47557a4e344cSTejun Heo * @pool: worker_pool to initialize
47567a4e344cSTejun Heo *
4757402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs.
4758d185af30SYacine Belkadi *
4759d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields
476029c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called
476129c91e99STejun Heo * on @pool safely to release it.
47627a4e344cSTejun Heo */
init_worker_pool(struct worker_pool * pool)47637a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool)
47644e1a1f9aSTejun Heo {
4765a9b8a985SSebastian Andrzej Siewior raw_spin_lock_init(&pool->lock);
476629c91e99STejun Heo pool->id = -1;
476729c91e99STejun Heo pool->cpu = -1;
4768f3f90ad4STejun Heo pool->node = NUMA_NO_NODE;
47694e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED;
477082607adcSTejun Heo pool->watchdog_ts = jiffies;
47714e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist);
47724e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list);
47734e1a1f9aSTejun Heo hash_init(pool->busy_hash);
47744e1a1f9aSTejun Heo
477532a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE);
47763f959aa3SValentin Schneider INIT_WORK(&pool->idle_cull_work, idle_cull_fn);
47774e1a1f9aSTejun Heo
477832a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0);
47794e1a1f9aSTejun Heo
4780da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers);
47817a4e344cSTejun Heo
47827cda9aaeSLai Jiangshan ida_init(&pool->worker_ida);
478329c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node);
478429c91e99STejun Heo pool->refcnt = 1;
478529c91e99STejun Heo
478629c91e99STejun Heo /* shouldn't fail above this point */
4787be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs();
47887a4e344cSTejun Heo if (!pool->attrs)
47897a4e344cSTejun Heo return -ENOMEM;
47905de7a03cSTejun Heo
47915de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs);
47925de7a03cSTejun Heo
47937a4e344cSTejun Heo return 0;
47944e1a1f9aSTejun Heo }
47954e1a1f9aSTejun Heo
4796669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP
wq_init_lockdep(struct workqueue_struct * wq)4797669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq)
4798669de8bdSBart Van Assche {
4799669de8bdSBart Van Assche char *lock_name;
4800669de8bdSBart Van Assche
4801669de8bdSBart Van Assche lockdep_register_key(&wq->key);
4802669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name);
4803669de8bdSBart Van Assche if (!lock_name)
4804669de8bdSBart Van Assche lock_name = wq->name;
480569a106c0SQian Cai
480669a106c0SQian Cai wq->lock_name = lock_name;
48074f022f43SMatthew Brost wq->lockdep_map = &wq->__lockdep_map;
48084f022f43SMatthew Brost lockdep_init_map(wq->lockdep_map, lock_name, &wq->key, 0);
4809669de8bdSBart Van Assche }
4810669de8bdSBart Van Assche
wq_unregister_lockdep(struct workqueue_struct * wq)4811669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq)
4812669de8bdSBart Van Assche {
4813ec0a7d44SMatthew Brost if (wq->lockdep_map != &wq->__lockdep_map)
4814ec0a7d44SMatthew Brost return;
4815ec0a7d44SMatthew Brost
4816669de8bdSBart Van Assche lockdep_unregister_key(&wq->key);
4817669de8bdSBart Van Assche }
4818669de8bdSBart Van Assche
wq_free_lockdep(struct workqueue_struct * wq)4819669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq)
4820669de8bdSBart Van Assche {
4821ec0a7d44SMatthew Brost if (wq->lockdep_map != &wq->__lockdep_map)
4822ec0a7d44SMatthew Brost return;
4823ec0a7d44SMatthew Brost
4824669de8bdSBart Van Assche if (wq->lock_name != wq->name)
4825669de8bdSBart Van Assche kfree(wq->lock_name);
4826669de8bdSBart Van Assche }
4827669de8bdSBart Van Assche #else
wq_init_lockdep(struct workqueue_struct * wq)4828669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq)
4829669de8bdSBart Van Assche {
4830669de8bdSBart Van Assche }
4831669de8bdSBart Van Assche
wq_unregister_lockdep(struct workqueue_struct * wq)4832669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq)
4833669de8bdSBart Van Assche {
4834669de8bdSBart Van Assche }
4835669de8bdSBart Van Assche
wq_free_lockdep(struct workqueue_struct * wq)4836669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq)
4837669de8bdSBart Van Assche {
4838669de8bdSBart Van Assche }
4839669de8bdSBart Van Assche #endif
4840669de8bdSBart Van Assche
free_node_nr_active(struct wq_node_nr_active ** nna_ar)484191ccc6e7STejun Heo static void free_node_nr_active(struct wq_node_nr_active **nna_ar)
484291ccc6e7STejun Heo {
484391ccc6e7STejun Heo int node;
484491ccc6e7STejun Heo
484591ccc6e7STejun Heo for_each_node(node) {
484691ccc6e7STejun Heo kfree(nna_ar[node]);
484791ccc6e7STejun Heo nna_ar[node] = NULL;
484891ccc6e7STejun Heo }
484991ccc6e7STejun Heo
485091ccc6e7STejun Heo kfree(nna_ar[nr_node_ids]);
485191ccc6e7STejun Heo nna_ar[nr_node_ids] = NULL;
485291ccc6e7STejun Heo }
485391ccc6e7STejun Heo
init_node_nr_active(struct wq_node_nr_active * nna)485491ccc6e7STejun Heo static void init_node_nr_active(struct wq_node_nr_active *nna)
485591ccc6e7STejun Heo {
4856c5f8cd6cSTejun Heo nna->max = WQ_DFL_MIN_ACTIVE;
485791ccc6e7STejun Heo atomic_set(&nna->nr, 0);
48585797b1c1STejun Heo raw_spin_lock_init(&nna->lock);
48595797b1c1STejun Heo INIT_LIST_HEAD(&nna->pending_pwqs);
486091ccc6e7STejun Heo }
486191ccc6e7STejun Heo
486291ccc6e7STejun Heo /*
486391ccc6e7STejun Heo * Each node's nr_active counter will be accessed mostly from its own node and
486491ccc6e7STejun Heo * should be allocated in the node.
486591ccc6e7STejun Heo */
alloc_node_nr_active(struct wq_node_nr_active ** nna_ar)486691ccc6e7STejun Heo static int alloc_node_nr_active(struct wq_node_nr_active **nna_ar)
486791ccc6e7STejun Heo {
486891ccc6e7STejun Heo struct wq_node_nr_active *nna;
486991ccc6e7STejun Heo int node;
487091ccc6e7STejun Heo
487191ccc6e7STejun Heo for_each_node(node) {
487291ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, node);
487391ccc6e7STejun Heo if (!nna)
487491ccc6e7STejun Heo goto err_free;
487591ccc6e7STejun Heo init_node_nr_active(nna);
487691ccc6e7STejun Heo nna_ar[node] = nna;
487791ccc6e7STejun Heo }
487891ccc6e7STejun Heo
487991ccc6e7STejun Heo /* [nr_node_ids] is used as the fallback */
488091ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, NUMA_NO_NODE);
488191ccc6e7STejun Heo if (!nna)
488291ccc6e7STejun Heo goto err_free;
488391ccc6e7STejun Heo init_node_nr_active(nna);
488491ccc6e7STejun Heo nna_ar[nr_node_ids] = nna;
488591ccc6e7STejun Heo
488691ccc6e7STejun Heo return 0;
488791ccc6e7STejun Heo
488891ccc6e7STejun Heo err_free:
488991ccc6e7STejun Heo free_node_nr_active(nna_ar);
489091ccc6e7STejun Heo return -ENOMEM;
489191ccc6e7STejun Heo }
489291ccc6e7STejun Heo
rcu_free_wq(struct rcu_head * rcu)4893e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu)
4894e2dca7adSTejun Heo {
4895e2dca7adSTejun Heo struct workqueue_struct *wq =
4896e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu);
4897e2dca7adSTejun Heo
489891ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND)
489991ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active);
490091ccc6e7STejun Heo
4901669de8bdSBart Van Assche wq_free_lockdep(wq);
4902ee1ceef7STejun Heo free_percpu(wq->cpu_pwq);
4903e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs);
4904e2dca7adSTejun Heo kfree(wq);
4905e2dca7adSTejun Heo }
4906e2dca7adSTejun Heo
rcu_free_pool(struct rcu_head * rcu)490729c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu)
490829c91e99STejun Heo {
490929c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
491029c91e99STejun Heo
49117cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida);
491229c91e99STejun Heo free_workqueue_attrs(pool->attrs);
491329c91e99STejun Heo kfree(pool);
491429c91e99STejun Heo }
491529c91e99STejun Heo
491629c91e99STejun Heo /**
491729c91e99STejun Heo * put_unbound_pool - put a worker_pool
491829c91e99STejun Heo * @pool: worker_pool to put
491929c91e99STejun Heo *
492024acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU
4921c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path
4922c5aa87bbSTejun Heo * and this function should be able to release pools which went through,
4923c5aa87bbSTejun Heo * successfully or not, init_worker_pool().
4924a892caccSTejun Heo *
4925a892caccSTejun Heo * Should be called with wq_pool_mutex held.
492629c91e99STejun Heo */
put_unbound_pool(struct worker_pool * pool)492729c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool)
492829c91e99STejun Heo {
492929c91e99STejun Heo struct worker *worker;
49309680540cSYang Yingliang LIST_HEAD(cull_list);
4931e02b9312SValentin Schneider
4932a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex);
4933a892caccSTejun Heo
4934a892caccSTejun Heo if (--pool->refcnt)
493529c91e99STejun Heo return;
493629c91e99STejun Heo
493729c91e99STejun Heo /* sanity checks */
493861d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) ||
4939a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist)))
494029c91e99STejun Heo return;
494129c91e99STejun Heo
494229c91e99STejun Heo /* release id and unhash */
494329c91e99STejun Heo if (pool->id >= 0)
494429c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id);
494529c91e99STejun Heo hash_del(&pool->hash_node);
494629c91e99STejun Heo
4947c5aa87bbSTejun Heo /*
4948692b4825STejun Heo * Become the manager and destroy all workers. This prevents
4949692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last
4950692b4825STejun Heo * manager and @pool gets freed with the flag set.
49519ab03be4SValentin Schneider *
49529ab03be4SValentin Schneider * Having a concurrent manager is quite unlikely to happen as we can
49539ab03be4SValentin Schneider * only get here with
49549ab03be4SValentin Schneider * pwq->refcnt == pool->refcnt == 0
49559ab03be4SValentin Schneider * which implies no work queued to the pool, which implies no worker can
49569ab03be4SValentin Schneider * become the manager. However a worker could have taken the role of
49579ab03be4SValentin Schneider * manager before the refcnts dropped to 0, since maybe_create_worker()
49589ab03be4SValentin Schneider * drops pool->lock
4959c5aa87bbSTejun Heo */
49609ab03be4SValentin Schneider while (true) {
49619ab03be4SValentin Schneider rcuwait_wait_event(&manager_wait,
49629ab03be4SValentin Schneider !(pool->flags & POOL_MANAGER_ACTIVE),
4963d8bb65abSSebastian Andrzej Siewior TASK_UNINTERRUPTIBLE);
4964e02b9312SValentin Schneider
4965e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex);
49669ab03be4SValentin Schneider raw_spin_lock_irq(&pool->lock);
49679ab03be4SValentin Schneider if (!(pool->flags & POOL_MANAGER_ACTIVE)) {
4968692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE;
49699ab03be4SValentin Schneider break;
49709ab03be4SValentin Schneider }
49719ab03be4SValentin Schneider raw_spin_unlock_irq(&pool->lock);
4972e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex);
49739ab03be4SValentin Schneider }
4974692b4825STejun Heo
49751037de36SLai Jiangshan while ((worker = first_idle_worker(pool)))
4976e02b9312SValentin Schneider set_worker_dying(worker, &cull_list);
497729c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle);
4978a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock);
497960f5a4bcSLai Jiangshan
4980f4b7b53cSLai Jiangshan detach_dying_workers(&cull_list);
4981e02b9312SValentin Schneider
49821258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex);
498360f5a4bcSLai Jiangshan
498468f83057SLai Jiangshan reap_dying_workers(&cull_list);
498560f5a4bcSLai Jiangshan
498629c91e99STejun Heo /* shut down the timers */
4987*8fa7292fSThomas Gleixner timer_delete_sync(&pool->idle_timer);
49883f959aa3SValentin Schneider cancel_work_sync(&pool->idle_cull_work);
4989*8fa7292fSThomas Gleixner timer_delete_sync(&pool->mayday_timer);
499029c91e99STejun Heo
499124acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */
499225b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool);
499329c91e99STejun Heo }
499429c91e99STejun Heo
499529c91e99STejun Heo /**
499629c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes
499729c91e99STejun Heo * @attrs: the attributes of the worker_pool to get
499829c91e99STejun Heo *
499929c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the
500029c91e99STejun Heo * reference count and return it. If there already is a matching
500129c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to
5002d185af30SYacine Belkadi * create a new one.
5003a892caccSTejun Heo *
5004a892caccSTejun Heo * Should be called with wq_pool_mutex held.
5005d185af30SYacine Belkadi *
5006d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs.
5007d185af30SYacine Belkadi * On failure, %NULL.
500829c91e99STejun Heo */
get_unbound_pool(const struct workqueue_attrs * attrs)500929c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
501029c91e99STejun Heo {
501184193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_NUMA];
501229c91e99STejun Heo u32 hash = wqattrs_hash(attrs);
501329c91e99STejun Heo struct worker_pool *pool;
501484193c07STejun Heo int pod, node = NUMA_NO_NODE;
501529c91e99STejun Heo
5016a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex);
501729c91e99STejun Heo
501829c91e99STejun Heo /* do we already have a matching pool? */
501929c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
502029c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) {
502129c91e99STejun Heo pool->refcnt++;
50223fb1823cSLai Jiangshan return pool;
502329c91e99STejun Heo }
502429c91e99STejun Heo }
502529c91e99STejun Heo
50269546b29eSTejun Heo /* If __pod_cpumask is contained inside a NUMA pod, that's our node */
502784193c07STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) {
50289546b29eSTejun Heo if (cpumask_subset(attrs->__pod_cpumask, pt->pod_cpus[pod])) {
502984193c07STejun Heo node = pt->pod_node[pod];
5030e2273584SXunlei Pang break;
5031e2273584SXunlei Pang }
5032e2273584SXunlei Pang }
5033e2273584SXunlei Pang
503429c91e99STejun Heo /* nope, create a new one */
503584193c07STejun Heo pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, node);
503629c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0)
503729c91e99STejun Heo goto fail;
503829c91e99STejun Heo
503984193c07STejun Heo pool->node = node;
50405de7a03cSTejun Heo copy_workqueue_attrs(pool->attrs, attrs);
50415de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs);
50422865a8fbSShaohua Li
504329c91e99STejun Heo if (worker_pool_assign_id(pool) < 0)
504429c91e99STejun Heo goto fail;
504529c91e99STejun Heo
504629c91e99STejun Heo /* create and start the initial worker */
50473347fa09STejun Heo if (wq_online && !create_worker(pool))
504829c91e99STejun Heo goto fail;
504929c91e99STejun Heo
505029c91e99STejun Heo /* install */
505129c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash);
50523fb1823cSLai Jiangshan
505329c91e99STejun Heo return pool;
505429c91e99STejun Heo fail:
505529c91e99STejun Heo if (pool)
505629c91e99STejun Heo put_unbound_pool(pool);
505729c91e99STejun Heo return NULL;
505829c91e99STejun Heo }
505929c91e99STejun Heo
50608864b4e5STejun Heo /*
5061967b494eSTejun Heo * Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero
5062967b494eSTejun Heo * refcnt and needs to be destroyed.
50638864b4e5STejun Heo */
pwq_release_workfn(struct kthread_work * work)5064687a9aa5STejun Heo static void pwq_release_workfn(struct kthread_work *work)
50658864b4e5STejun Heo {
50668864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
5067687a9aa5STejun Heo release_work);
50688864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq;
50698864b4e5STejun Heo struct worker_pool *pool = pwq->pool;
5070b42b0bddSYang Yingliang bool is_last = false;
50718864b4e5STejun Heo
5072b42b0bddSYang Yingliang /*
5073687a9aa5STejun Heo * When @pwq is not linked, it doesn't hold any reference to the
5074b42b0bddSYang Yingliang * @wq, and @wq is invalid to access.
5075b42b0bddSYang Yingliang */
5076b42b0bddSYang Yingliang if (!list_empty(&pwq->pwqs_node)) {
50773c25a55dSLai Jiangshan mutex_lock(&wq->mutex);
50788864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node);
5079bc0caf09STejun Heo is_last = list_empty(&wq->pwqs);
50804c065dbcSWaiman Long
50814c065dbcSWaiman Long /*
50824c065dbcSWaiman Long * For ordered workqueue with a plugged dfl_pwq, restart it now.
50834c065dbcSWaiman Long */
50844c065dbcSWaiman Long if (!is_last && (wq->flags & __WQ_ORDERED))
50854c065dbcSWaiman Long unplug_oldest_pwq(wq);
50864c065dbcSWaiman Long
50873c25a55dSLai Jiangshan mutex_unlock(&wq->mutex);
5088b42b0bddSYang Yingliang }
50898864b4e5STejun Heo
5090687a9aa5STejun Heo if (wq->flags & WQ_UNBOUND) {
5091a892caccSTejun Heo mutex_lock(&wq_pool_mutex);
50928864b4e5STejun Heo put_unbound_pool(pool);
5093a892caccSTejun Heo mutex_unlock(&wq_pool_mutex);
5094687a9aa5STejun Heo }
5095a892caccSTejun Heo
50965797b1c1STejun Heo if (!list_empty(&pwq->pending_node)) {
50975797b1c1STejun Heo struct wq_node_nr_active *nna =
50985797b1c1STejun Heo wq_node_nr_active(pwq->wq, pwq->pool->node);
50995797b1c1STejun Heo
51005797b1c1STejun Heo raw_spin_lock_irq(&nna->lock);
51015797b1c1STejun Heo list_del_init(&pwq->pending_node);
51025797b1c1STejun Heo raw_spin_unlock_irq(&nna->lock);
51035797b1c1STejun Heo }
51045797b1c1STejun Heo
510537c2277fSJulia Lawall kfree_rcu(pwq, rcu);
51068864b4e5STejun Heo
51078864b4e5STejun Heo /*
51088864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one
5109e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free.
51108864b4e5STejun Heo */
5111669de8bdSBart Van Assche if (is_last) {
5112669de8bdSBart Van Assche wq_unregister_lockdep(wq);
511325b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq);
51146029a918STejun Heo }
5115669de8bdSBart Van Assche }
51168864b4e5STejun Heo
511767dc8325SCai Huoqing /* initialize newly allocated @pwq which is associated with @wq and @pool */
init_pwq(struct pool_workqueue * pwq,struct workqueue_struct * wq,struct worker_pool * pool)5118f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq,
5119f147f29eSTejun Heo struct worker_pool *pool)
5120d2c1d404STejun Heo {
5121e9a8e01fSTejun Heo BUG_ON((unsigned long)pwq & ~WORK_STRUCT_PWQ_MASK);
5122d2c1d404STejun Heo
5123e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq));
5124e50aba9aSTejun Heo
5125d2c1d404STejun Heo pwq->pool = pool;
5126d2c1d404STejun Heo pwq->wq = wq;
5127d2c1d404STejun Heo pwq->flush_color = -1;
51288864b4e5STejun Heo pwq->refcnt = 1;
5129f97a4a1aSLai Jiangshan INIT_LIST_HEAD(&pwq->inactive_works);
51305797b1c1STejun Heo INIT_LIST_HEAD(&pwq->pending_node);
51311befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node);
5132d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node);
5133687a9aa5STejun Heo kthread_init_work(&pwq->release_work, pwq_release_workfn);
5134f147f29eSTejun Heo }
5135d2c1d404STejun Heo
5136f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */
link_pwq(struct pool_workqueue * pwq)51371befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq)
5138f147f29eSTejun Heo {
5139f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq;
5140f147f29eSTejun Heo
5141f147f29eSTejun Heo lockdep_assert_held(&wq->mutex);
514275ccf595STejun Heo
51431befcf30STejun Heo /* may be called multiple times, ignore if already linked */
51441befcf30STejun Heo if (!list_empty(&pwq->pwqs_node))
51451befcf30STejun Heo return;
51461befcf30STejun Heo
514729b1cb41SLai Jiangshan /* set the matching work_color */
514875ccf595STejun Heo pwq->work_color = wq->work_color;
5149983ca25eSTejun Heo
5150983ca25eSTejun Heo /* link in @pwq */
515126fb7e3dSWaiman Long list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs);
5152df2d5ae4STejun Heo }
51536029a918STejun Heo
5154f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */
alloc_unbound_pwq(struct workqueue_struct * wq,const struct workqueue_attrs * attrs)5155f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,
5156f147f29eSTejun Heo const struct workqueue_attrs *attrs)
5157f147f29eSTejun Heo {
5158f147f29eSTejun Heo struct worker_pool *pool;
5159f147f29eSTejun Heo struct pool_workqueue *pwq;
5160f147f29eSTejun Heo
5161f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex);
5162f147f29eSTejun Heo
5163f147f29eSTejun Heo pool = get_unbound_pool(attrs);
5164f147f29eSTejun Heo if (!pool)
5165f147f29eSTejun Heo return NULL;
5166f147f29eSTejun Heo
5167e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node);
5168f147f29eSTejun Heo if (!pwq) {
5169f147f29eSTejun Heo put_unbound_pool(pool);
5170f147f29eSTejun Heo return NULL;
5171f147f29eSTejun Heo }
5172f147f29eSTejun Heo
5173f147f29eSTejun Heo init_pwq(pwq, wq, pool);
5174f147f29eSTejun Heo return pwq;
5175d2c1d404STejun Heo }
5176d2c1d404STejun Heo
apply_wqattrs_lock(void)51771726a171SLai Jiangshan static void apply_wqattrs_lock(void)
51781726a171SLai Jiangshan {
51791726a171SLai Jiangshan mutex_lock(&wq_pool_mutex);
51801726a171SLai Jiangshan }
51811726a171SLai Jiangshan
apply_wqattrs_unlock(void)51821726a171SLai Jiangshan static void apply_wqattrs_unlock(void)
51831726a171SLai Jiangshan {
51841726a171SLai Jiangshan mutex_unlock(&wq_pool_mutex);
51851726a171SLai Jiangshan }
51861726a171SLai Jiangshan
51874c16bd32STejun Heo /**
5188fef59c9cSTejun Heo * wq_calc_pod_cpumask - calculate a wq_attrs' cpumask for a pod
5189042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue
519084193c07STejun Heo * @cpu: the target CPU
51914c16bd32STejun Heo *
519288a41b18SLai Jiangshan * Calculate the cpumask a workqueue with @attrs should use on @pod.
51939546b29eSTejun Heo * The result is stored in @attrs->__pod_cpumask.
51944c16bd32STejun Heo *
5195fef59c9cSTejun Heo * If pod affinity is not enabled, @attrs->cpumask is always used. If enabled
5196fef59c9cSTejun Heo * and @pod has online CPUs requested by @attrs, the returned cpumask is the
5197fef59c9cSTejun Heo * intersection of the possible CPUs of @pod and @attrs->cpumask.
51984c16bd32STejun Heo *
5199fef59c9cSTejun Heo * The caller is responsible for ensuring that the cpumask of @pod stays stable.
52004c16bd32STejun Heo */
wq_calc_pod_cpumask(struct workqueue_attrs * attrs,int cpu)520188a41b18SLai Jiangshan static void wq_calc_pod_cpumask(struct workqueue_attrs *attrs, int cpu)
52024c16bd32STejun Heo {
520384193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs);
520484193c07STejun Heo int pod = pt->cpu_pod[cpu];
52054c16bd32STejun Heo
5206fbb3d4c1SLai Jiangshan /* calculate possible CPUs in @pod that @attrs wants */
52079546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, pt->pod_cpus[pod], attrs->cpumask);
5208fbb3d4c1SLai Jiangshan /* does @pod have any online CPUs @attrs wants? */
5209fbb3d4c1SLai Jiangshan if (!cpumask_intersects(attrs->__pod_cpumask, wq_online_cpumask)) {
52109546b29eSTejun Heo cpumask_copy(attrs->__pod_cpumask, attrs->cpumask);
521184193c07STejun Heo return;
521284193c07STejun Heo }
52134c16bd32STejun Heo }
52144c16bd32STejun Heo
52159f66cff2STejun Heo /* install @pwq into @wq and return the old pwq, @cpu < 0 for dfl_pwq */
install_unbound_pwq(struct workqueue_struct * wq,int cpu,struct pool_workqueue * pwq)5216636b927eSTejun Heo static struct pool_workqueue *install_unbound_pwq(struct workqueue_struct *wq,
5217636b927eSTejun Heo int cpu, struct pool_workqueue *pwq)
52181befcf30STejun Heo {
52199f66cff2STejun Heo struct pool_workqueue __rcu **slot = unbound_pwq_slot(wq, cpu);
52201befcf30STejun Heo struct pool_workqueue *old_pwq;
52211befcf30STejun Heo
52225b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex);
52231befcf30STejun Heo lockdep_assert_held(&wq->mutex);
52241befcf30STejun Heo
52251befcf30STejun Heo /* link_pwq() can handle duplicate calls */
52261befcf30STejun Heo link_pwq(pwq);
52271befcf30STejun Heo
52289f66cff2STejun Heo old_pwq = rcu_access_pointer(*slot);
52299f66cff2STejun Heo rcu_assign_pointer(*slot, pwq);
52301befcf30STejun Heo return old_pwq;
52311befcf30STejun Heo }
52321befcf30STejun Heo
52332d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */
52342d5f0764SLai Jiangshan struct apply_wqattrs_ctx {
52352d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */
52362d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */
5237042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */
52382d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq;
52392d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[];
52402d5f0764SLai Jiangshan };
52412d5f0764SLai Jiangshan
52422d5f0764SLai Jiangshan /* free the resources after success or abort */
apply_wqattrs_cleanup(struct apply_wqattrs_ctx * ctx)52432d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx)
52442d5f0764SLai Jiangshan {
52452d5f0764SLai Jiangshan if (ctx) {
5246636b927eSTejun Heo int cpu;
52472d5f0764SLai Jiangshan
5248636b927eSTejun Heo for_each_possible_cpu(cpu)
5249636b927eSTejun Heo put_pwq_unlocked(ctx->pwq_tbl[cpu]);
52502d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq);
52512d5f0764SLai Jiangshan
52522d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs);
52532d5f0764SLai Jiangshan
52542d5f0764SLai Jiangshan kfree(ctx);
52552d5f0764SLai Jiangshan }
52562d5f0764SLai Jiangshan }
52572d5f0764SLai Jiangshan
52582d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */
52592d5f0764SLai Jiangshan static struct apply_wqattrs_ctx *
apply_wqattrs_prepare(struct workqueue_struct * wq,const struct workqueue_attrs * attrs,const cpumask_var_t unbound_cpumask)52602d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq,
526199c621efSLai Jiangshan const struct workqueue_attrs *attrs,
526299c621efSLai Jiangshan const cpumask_var_t unbound_cpumask)
52632d5f0764SLai Jiangshan {
52642d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx;
52659546b29eSTejun Heo struct workqueue_attrs *new_attrs;
5266636b927eSTejun Heo int cpu;
52672d5f0764SLai Jiangshan
52682d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex);
52692d5f0764SLai Jiangshan
527084193c07STejun Heo if (WARN_ON(attrs->affn_scope < 0 ||
527184193c07STejun Heo attrs->affn_scope >= WQ_AFFN_NR_TYPES))
527284193c07STejun Heo return ERR_PTR(-EINVAL);
527384193c07STejun Heo
5274636b927eSTejun Heo ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_cpu_ids), GFP_KERNEL);
52752d5f0764SLai Jiangshan
5276be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs();
52779546b29eSTejun Heo if (!ctx || !new_attrs)
52782d5f0764SLai Jiangshan goto out_free;
52792d5f0764SLai Jiangshan
5280042f7df1SLai Jiangshan /*
52812d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to
52822d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create
52832d5f0764SLai Jiangshan * it even if we don't use it immediately.
52842d5f0764SLai Jiangshan */
52850f36ee24STejun Heo copy_workqueue_attrs(new_attrs, attrs);
52860f36ee24STejun Heo wqattrs_actualize_cpumask(new_attrs, unbound_cpumask);
52879546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask);
52882d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs);
52892d5f0764SLai Jiangshan if (!ctx->dfl_pwq)
52902d5f0764SLai Jiangshan goto out_free;
52912d5f0764SLai Jiangshan
5292636b927eSTejun Heo for_each_possible_cpu(cpu) {
5293af73f5c9STejun Heo if (new_attrs->ordered) {
52942d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++;
5295636b927eSTejun Heo ctx->pwq_tbl[cpu] = ctx->dfl_pwq;
5296636b927eSTejun Heo } else {
529788a41b18SLai Jiangshan wq_calc_pod_cpumask(new_attrs, cpu);
52989546b29eSTejun Heo ctx->pwq_tbl[cpu] = alloc_unbound_pwq(wq, new_attrs);
5299636b927eSTejun Heo if (!ctx->pwq_tbl[cpu])
5300636b927eSTejun Heo goto out_free;
53012d5f0764SLai Jiangshan }
53022d5f0764SLai Jiangshan }
53032d5f0764SLai Jiangshan
5304042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */
5305042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs);
5306042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);
53079546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask);
53082d5f0764SLai Jiangshan ctx->attrs = new_attrs;
5309042f7df1SLai Jiangshan
53104c065dbcSWaiman Long /*
53114c065dbcSWaiman Long * For initialized ordered workqueues, there should only be one pwq
53124c065dbcSWaiman Long * (dfl_pwq). Set the plugged flag of ctx->dfl_pwq to suspend execution
53134c065dbcSWaiman Long * of newly queued work items until execution of older work items in
53144c065dbcSWaiman Long * the old pwq's have completed.
53154c065dbcSWaiman Long */
53164c065dbcSWaiman Long if ((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs))
53174c065dbcSWaiman Long ctx->dfl_pwq->plugged = true;
53184c065dbcSWaiman Long
53192d5f0764SLai Jiangshan ctx->wq = wq;
53202d5f0764SLai Jiangshan return ctx;
53212d5f0764SLai Jiangshan
53222d5f0764SLai Jiangshan out_free:
53232d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs);
53242d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx);
532584193c07STejun Heo return ERR_PTR(-ENOMEM);
53262d5f0764SLai Jiangshan }
53272d5f0764SLai Jiangshan
53282d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */
apply_wqattrs_commit(struct apply_wqattrs_ctx * ctx)53292d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx)
53302d5f0764SLai Jiangshan {
5331636b927eSTejun Heo int cpu;
53322d5f0764SLai Jiangshan
53332d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */
53342d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex);
53352d5f0764SLai Jiangshan
53362d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs);
53372d5f0764SLai Jiangshan
53389f66cff2STejun Heo /* save the previous pwqs and install the new ones */
5339636b927eSTejun Heo for_each_possible_cpu(cpu)
5340636b927eSTejun Heo ctx->pwq_tbl[cpu] = install_unbound_pwq(ctx->wq, cpu,
5341636b927eSTejun Heo ctx->pwq_tbl[cpu]);
53429f66cff2STejun Heo ctx->dfl_pwq = install_unbound_pwq(ctx->wq, -1, ctx->dfl_pwq);
53432d5f0764SLai Jiangshan
53445797b1c1STejun Heo /* update node_nr_active->max */
53455797b1c1STejun Heo wq_update_node_max_active(ctx->wq, -1);
53465797b1c1STejun Heo
5347d64f2fa0SJuri Lelli /* rescuer needs to respect wq cpumask changes */
5348d64f2fa0SJuri Lelli if (ctx->wq->rescuer)
5349d64f2fa0SJuri Lelli set_cpus_allowed_ptr(ctx->wq->rescuer->task,
5350d64f2fa0SJuri Lelli unbound_effective_cpumask(ctx->wq));
5351d64f2fa0SJuri Lelli
53522d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex);
53532d5f0764SLai Jiangshan }
53542d5f0764SLai Jiangshan
apply_workqueue_attrs_locked(struct workqueue_struct * wq,const struct workqueue_attrs * attrs)5355a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq,
5356a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs)
5357a0111cf6SLai Jiangshan {
5358a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx;
5359a0111cf6SLai Jiangshan
5360a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */
5361a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
5362a0111cf6SLai Jiangshan return -EINVAL;
5363a0111cf6SLai Jiangshan
536499c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask);
536584193c07STejun Heo if (IS_ERR(ctx))
536684193c07STejun Heo return PTR_ERR(ctx);
5367a0111cf6SLai Jiangshan
5368a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */
5369a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx);
5370a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx);
5371a0111cf6SLai Jiangshan
53726201171eSwanghaibin return 0;
5373a0111cf6SLai Jiangshan }
5374a0111cf6SLai Jiangshan
53759e8cd2f5STejun Heo /**
53769e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
53779e8cd2f5STejun Heo * @wq: the target workqueue
53789e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
53799e8cd2f5STejun Heo *
5380fef59c9cSTejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, this function maps
5381fef59c9cSTejun Heo * a separate pwq to each CPU pod with possibles CPUs in @attrs->cpumask so that
5382fef59c9cSTejun Heo * work items are affine to the pod it was issued on. Older pwqs are released as
5383fef59c9cSTejun Heo * in-flight work items finish. Note that a work item which repeatedly requeues
5384fef59c9cSTejun Heo * itself back-to-back will stay on its current pwq.
53859e8cd2f5STejun Heo *
5386d185af30SYacine Belkadi * Performs GFP_KERNEL allocations.
5387d185af30SYacine Belkadi *
5388d185af30SYacine Belkadi * Return: 0 on success and -errno on failure.
53899e8cd2f5STejun Heo */
apply_workqueue_attrs(struct workqueue_struct * wq,const struct workqueue_attrs * attrs)5390513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq,
53919e8cd2f5STejun Heo const struct workqueue_attrs *attrs)
53929e8cd2f5STejun Heo {
5393a0111cf6SLai Jiangshan int ret;
53949e8cd2f5STejun Heo
5395509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex);
5396a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs);
5397509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex);
53982d5f0764SLai Jiangshan
53992d5f0764SLai Jiangshan return ret;
54009e8cd2f5STejun Heo }
54019e8cd2f5STejun Heo
54024c16bd32STejun Heo /**
5403b2b1f933SLai Jiangshan * unbound_wq_update_pwq - update a pwq slot for CPU hot[un]plug
54044c16bd32STejun Heo * @wq: the target workqueue
5405b2b1f933SLai Jiangshan * @cpu: the CPU to update the pwq slot for
54064c16bd32STejun Heo *
54074c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and
5408b2b1f933SLai Jiangshan * %CPU_DOWN_FAILED. @cpu is in the same pod of the CPU being hot[un]plugged.
54094c16bd32STejun Heo *
54104c16bd32STejun Heo *
5411fef59c9cSTejun Heo * If pod affinity can't be adjusted due to memory allocation failure, it falls
5412fef59c9cSTejun Heo * back to @wq->dfl_pwq which may not be optimal but is always correct.
5413fef59c9cSTejun Heo *
5414fef59c9cSTejun Heo * Note that when the last allowed CPU of a pod goes offline for a workqueue
5415fef59c9cSTejun Heo * with a cpumask spanning multiple pods, the workers which were already
5416fef59c9cSTejun Heo * executing the work items for the workqueue will lose their CPU affinity and
5417fef59c9cSTejun Heo * may execute on any CPU. This is similar to how per-cpu workqueues behave on
5418fef59c9cSTejun Heo * CPU_DOWN. If a workqueue user wants strict affinity, it's the user's
5419fef59c9cSTejun Heo * responsibility to flush the work item from CPU_DOWN_PREPARE.
54204c16bd32STejun Heo */
unbound_wq_update_pwq(struct workqueue_struct * wq,int cpu)5421b2b1f933SLai Jiangshan static void unbound_wq_update_pwq(struct workqueue_struct *wq, int cpu)
54224c16bd32STejun Heo {
54234c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq;
54244c16bd32STejun Heo struct workqueue_attrs *target_attrs;
54254c16bd32STejun Heo
54264c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex);
54274c16bd32STejun Heo
542884193c07STejun Heo if (!(wq->flags & WQ_UNBOUND) || wq->unbound_attrs->ordered)
54294c16bd32STejun Heo return;
54304c16bd32STejun Heo
54314c16bd32STejun Heo /*
54324c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU.
54334c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by
54344c16bd32STejun Heo * CPU hotplug exclusion.
54354c16bd32STejun Heo */
5436b2b1f933SLai Jiangshan target_attrs = unbound_wq_update_pwq_attrs_buf;
54374c16bd32STejun Heo
54384c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs);
54390f36ee24STejun Heo wqattrs_actualize_cpumask(target_attrs, wq_unbound_cpumask);
54404c16bd32STejun Heo
5441636b927eSTejun Heo /* nothing to do if the target cpumask matches the current pwq */
544288a41b18SLai Jiangshan wq_calc_pod_cpumask(target_attrs, cpu);
54439f66cff2STejun Heo if (wqattrs_equal(target_attrs, unbound_pwq(wq, cpu)->pool->attrs))
5444f7142ed4SLai Jiangshan return;
54454c16bd32STejun Heo
54464c16bd32STejun Heo /* create a new pwq */
54474c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs);
54484c16bd32STejun Heo if (!pwq) {
5449fef59c9cSTejun Heo pr_warn("workqueue: allocation failed while updating CPU pod affinity of \"%s\"\n",
54504c16bd32STejun Heo wq->name);
545177f300b1SDaeseok Youn goto use_dfl_pwq;
54524c16bd32STejun Heo }
54534c16bd32STejun Heo
5454f7142ed4SLai Jiangshan /* Install the new pwq. */
54554c16bd32STejun Heo mutex_lock(&wq->mutex);
5456636b927eSTejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq);
54574c16bd32STejun Heo goto out_unlock;
54584c16bd32STejun Heo
54594c16bd32STejun Heo use_dfl_pwq:
5460f7142ed4SLai Jiangshan mutex_lock(&wq->mutex);
54619f66cff2STejun Heo pwq = unbound_pwq(wq, -1);
54629f66cff2STejun Heo raw_spin_lock_irq(&pwq->pool->lock);
54639f66cff2STejun Heo get_pwq(pwq);
54649f66cff2STejun Heo raw_spin_unlock_irq(&pwq->pool->lock);
54659f66cff2STejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq);
54664c16bd32STejun Heo out_unlock:
54674c16bd32STejun Heo mutex_unlock(&wq->mutex);
54684c16bd32STejun Heo put_pwq_unlocked(old_pwq);
54694c16bd32STejun Heo }
54704c16bd32STejun Heo
alloc_and_link_pwqs(struct workqueue_struct * wq)547130cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq)
54721da177e4SLinus Torvalds {
547349e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI;
54748a2b7538STejun Heo int cpu, ret;
5475e1d8aa9fSFrederic Weisbecker
54761726a171SLai Jiangshan lockdep_assert_held(&wq_pool_mutex);
54771726a171SLai Jiangshan
5478687a9aa5STejun Heo wq->cpu_pwq = alloc_percpu(struct pool_workqueue *);
5479ee1ceef7STejun Heo if (!wq->cpu_pwq)
5480687a9aa5STejun Heo goto enomem;
548130cdf249STejun Heo
5482636b927eSTejun Heo if (!(wq->flags & WQ_UNBOUND)) {
54834cb1ef64STejun Heo struct worker_pool __percpu *pools;
54844cb1ef64STejun Heo
54854cb1ef64STejun Heo if (wq->flags & WQ_BH)
54864cb1ef64STejun Heo pools = bh_worker_pools;
54874cb1ef64STejun Heo else
54884cb1ef64STejun Heo pools = cpu_worker_pools;
54894cb1ef64STejun Heo
54907ccc2151SWenchao Hao for_each_possible_cpu(cpu) {
54917ccc2151SWenchao Hao struct pool_workqueue **pwq_p;
54927ccc2151SWenchao Hao struct worker_pool *pool;
54937ccc2151SWenchao Hao
54944cb1ef64STejun Heo pool = &(per_cpu_ptr(pools, cpu)[highpri]);
54954cb1ef64STejun Heo pwq_p = per_cpu_ptr(wq->cpu_pwq, cpu);
549630cdf249STejun Heo
5497687a9aa5STejun Heo *pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL,
5498687a9aa5STejun Heo pool->node);
5499687a9aa5STejun Heo if (!*pwq_p)
5500687a9aa5STejun Heo goto enomem;
5501687a9aa5STejun Heo
5502687a9aa5STejun Heo init_pwq(*pwq_p, wq, pool);
5503f147f29eSTejun Heo
5504f147f29eSTejun Heo mutex_lock(&wq->mutex);
5505687a9aa5STejun Heo link_pwq(*pwq_p);
5506f147f29eSTejun Heo mutex_unlock(&wq->mutex);
550730cdf249STejun Heo }
550830cdf249STejun Heo return 0;
5509509b3204SDaniel Jordan }
5510509b3204SDaniel Jordan
5511509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) {
55129f66cff2STejun Heo struct pool_workqueue *dfl_pwq;
55139f66cff2STejun Heo
55141726a171SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, ordered_wq_attrs[highpri]);
55158a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */
55169f66cff2STejun Heo dfl_pwq = rcu_access_pointer(wq->dfl_pwq);
55179f66cff2STejun Heo WARN(!ret && (wq->pwqs.next != &dfl_pwq->pwqs_node ||
55189f66cff2STejun Heo wq->pwqs.prev != &dfl_pwq->pwqs_node),
55198a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name);
55209e8cd2f5STejun Heo } else {
55211726a171SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, unbound_std_wq_attrs[highpri]);
55229e8cd2f5STejun Heo }
552364344553SZqiang
5524509b3204SDaniel Jordan return ret;
5525687a9aa5STejun Heo
5526687a9aa5STejun Heo enomem:
5527687a9aa5STejun Heo if (wq->cpu_pwq) {
55287b42f401SZqiang for_each_possible_cpu(cpu) {
55297b42f401SZqiang struct pool_workqueue *pwq = *per_cpu_ptr(wq->cpu_pwq, cpu);
55307b42f401SZqiang
55317b42f401SZqiang if (pwq)
55327b42f401SZqiang kmem_cache_free(pwq_cache, pwq);
55337b42f401SZqiang }
5534687a9aa5STejun Heo free_percpu(wq->cpu_pwq);
5535687a9aa5STejun Heo wq->cpu_pwq = NULL;
5536687a9aa5STejun Heo }
5537687a9aa5STejun Heo return -ENOMEM;
55380f900049STejun Heo }
55390f900049STejun Heo
wq_clamp_max_active(int max_active,unsigned int flags,const char * name)5540f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags,
5541f3421797STejun Heo const char *name)
5542b71ab8c2STejun Heo {
5543636b927eSTejun Heo if (max_active < 1 || max_active > WQ_MAX_ACTIVE)
5544044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
5545636b927eSTejun Heo max_active, name, 1, WQ_MAX_ACTIVE);
5546b71ab8c2STejun Heo
5547636b927eSTejun Heo return clamp_val(max_active, 1, WQ_MAX_ACTIVE);
5548b71ab8c2STejun Heo }
5549b71ab8c2STejun Heo
5550983c7515STejun Heo /*
5551983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer
5552983c7515STejun Heo * to guarantee forward progress.
5553983c7515STejun Heo */
init_rescuer(struct workqueue_struct * wq)5554983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq)
5555983c7515STejun Heo {
5556983c7515STejun Heo struct worker *rescuer;
55572a1b02bcSTejun Heo char id_buf[WORKER_ID_LEN];
5558b92b36eaSDan Carpenter int ret;
5559983c7515STejun Heo
5560449b31adSLai Jiangshan lockdep_assert_held(&wq_pool_mutex);
5561449b31adSLai Jiangshan
5562983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM))
5563983c7515STejun Heo return 0;
5564983c7515STejun Heo
5565983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE);
55664c0736a7SPetr Mladek if (!rescuer) {
55674c0736a7SPetr Mladek pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n",
55684c0736a7SPetr Mladek wq->name);
5569983c7515STejun Heo return -ENOMEM;
55704c0736a7SPetr Mladek }
5571983c7515STejun Heo
5572983c7515STejun Heo rescuer->rescue_wq = wq;
55732a1b02bcSTejun Heo format_worker_id(id_buf, sizeof(id_buf), rescuer, NULL);
55742a1b02bcSTejun Heo
55752a1b02bcSTejun Heo rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", id_buf);
5576f187b697SSean Fu if (IS_ERR(rescuer->task)) {
5577b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task);
55784c0736a7SPetr Mladek pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe",
55794c0736a7SPetr Mladek wq->name, ERR_PTR(ret));
5580983c7515STejun Heo kfree(rescuer);
5581b92b36eaSDan Carpenter return ret;
5582983c7515STejun Heo }
5583983c7515STejun Heo
5584983c7515STejun Heo wq->rescuer = rescuer;
558585f0ab43SJuri Lelli if (wq->flags & WQ_UNBOUND)
5586449b31adSLai Jiangshan kthread_bind_mask(rescuer->task, unbound_effective_cpumask(wq));
558785f0ab43SJuri Lelli else
5588983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask);
5589983c7515STejun Heo wake_up_process(rescuer->task);
5590983c7515STejun Heo
5591983c7515STejun Heo return 0;
5592983c7515STejun Heo }
5593983c7515STejun Heo
5594a045a272STejun Heo /**
5595a045a272STejun Heo * wq_adjust_max_active - update a wq's max_active to the current setting
5596a045a272STejun Heo * @wq: target workqueue
5597a045a272STejun Heo *
5598a045a272STejun Heo * If @wq isn't freezing, set @wq->max_active to the saved_max_active and
5599a045a272STejun Heo * activate inactive work items accordingly. If @wq is freezing, clear
5600a045a272STejun Heo * @wq->max_active to zero.
5601a045a272STejun Heo */
wq_adjust_max_active(struct workqueue_struct * wq)5602a045a272STejun Heo static void wq_adjust_max_active(struct workqueue_struct *wq)
5603a045a272STejun Heo {
5604c5404d4eSTejun Heo bool activated;
56055797b1c1STejun Heo int new_max, new_min;
5606a045a272STejun Heo
5607a045a272STejun Heo lockdep_assert_held(&wq->mutex);
5608a045a272STejun Heo
5609a045a272STejun Heo if ((wq->flags & WQ_FREEZABLE) && workqueue_freezing) {
56105797b1c1STejun Heo new_max = 0;
56115797b1c1STejun Heo new_min = 0;
56125797b1c1STejun Heo } else {
56135797b1c1STejun Heo new_max = wq->saved_max_active;
56145797b1c1STejun Heo new_min = wq->saved_min_active;
5615a045a272STejun Heo }
5616a045a272STejun Heo
56175797b1c1STejun Heo if (wq->max_active == new_max && wq->min_active == new_min)
5618a045a272STejun Heo return;
5619a045a272STejun Heo
5620a045a272STejun Heo /*
56215797b1c1STejun Heo * Update @wq->max/min_active and then kick inactive work items if more
5622a045a272STejun Heo * active work items are allowed. This doesn't break work item ordering
5623a045a272STejun Heo * because new work items are always queued behind existing inactive
5624a045a272STejun Heo * work items if there are any.
5625a045a272STejun Heo */
56265797b1c1STejun Heo WRITE_ONCE(wq->max_active, new_max);
56275797b1c1STejun Heo WRITE_ONCE(wq->min_active, new_min);
56285797b1c1STejun Heo
56295797b1c1STejun Heo if (wq->flags & WQ_UNBOUND)
56305797b1c1STejun Heo wq_update_node_max_active(wq, -1);
56315797b1c1STejun Heo
56325797b1c1STejun Heo if (new_max == 0)
56335797b1c1STejun Heo return;
5634a045a272STejun Heo
5635c5404d4eSTejun Heo /*
5636c5404d4eSTejun Heo * Round-robin through pwq's activating the first inactive work item
5637c5404d4eSTejun Heo * until max_active is filled.
5638c5404d4eSTejun Heo */
5639c5404d4eSTejun Heo do {
5640c5404d4eSTejun Heo struct pool_workqueue *pwq;
5641c5404d4eSTejun Heo
5642c5404d4eSTejun Heo activated = false;
5643a045a272STejun Heo for_each_pwq(pwq, wq) {
5644c26e2f2eSTejun Heo unsigned long irq_flags;
5645a045a272STejun Heo
5646c5404d4eSTejun Heo /* can be called during early boot w/ irq disabled */
5647c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, irq_flags);
56485797b1c1STejun Heo if (pwq_activate_first_inactive(pwq, true)) {
5649c5404d4eSTejun Heo activated = true;
5650a045a272STejun Heo kick_pool(pwq->pool);
5651c5404d4eSTejun Heo }
5652c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, irq_flags);
5653a045a272STejun Heo }
5654c5404d4eSTejun Heo } while (activated);
5655a045a272STejun Heo }
5656a045a272STejun Heo
5657d57212f2SSu Hui __printf(1, 0)
__alloc_workqueue(const char * fmt,unsigned int flags,int max_active,va_list args)5658b188c57aSMatthew Brost static struct workqueue_struct *__alloc_workqueue(const char *fmt,
565997e37d7bSTejun Heo unsigned int flags,
56609b59a85aSMatthew Brost int max_active, va_list args)
56613af24433SOleg Nesterov {
56623af24433SOleg Nesterov struct workqueue_struct *wq;
566391ccc6e7STejun Heo size_t wq_size;
566491ccc6e7STejun Heo int name_len;
5665b196be89STejun Heo
56664cb1ef64STejun Heo if (flags & WQ_BH) {
56674cb1ef64STejun Heo if (WARN_ON_ONCE(flags & ~__WQ_BH_ALLOWS))
56684cb1ef64STejun Heo return NULL;
56694cb1ef64STejun Heo if (WARN_ON_ONCE(max_active))
56704cb1ef64STejun Heo return NULL;
56714cb1ef64STejun Heo }
56724cb1ef64STejun Heo
5673cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */
5674cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient)
5675cee22a15SViresh Kumar flags |= WQ_UNBOUND;
5676cee22a15SViresh Kumar
5677ecf6881fSTejun Heo /* allocate wq and format name */
567891ccc6e7STejun Heo if (flags & WQ_UNBOUND)
567991ccc6e7STejun Heo wq_size = struct_size(wq, node_nr_active, nr_node_ids + 1);
568091ccc6e7STejun Heo else
568191ccc6e7STejun Heo wq_size = sizeof(*wq);
568291ccc6e7STejun Heo
568391ccc6e7STejun Heo wq = kzalloc(wq_size, GFP_KERNEL);
5684b196be89STejun Heo if (!wq)
5685d2c1d404STejun Heo return NULL;
5686b196be89STejun Heo
56876029a918STejun Heo if (flags & WQ_UNBOUND) {
5688be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs();
56896029a918STejun Heo if (!wq->unbound_attrs)
56906029a918STejun Heo goto err_free_wq;
56916029a918STejun Heo }
56926029a918STejun Heo
569391ccc6e7STejun Heo name_len = vsnprintf(wq->name, sizeof(wq->name), fmt, args);
56943af24433SOleg Nesterov
569591ccc6e7STejun Heo if (name_len >= WQ_NAME_LEN)
569691ccc6e7STejun Heo pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n",
569791ccc6e7STejun Heo wq->name);
569831c89007SAudra Mitchell
56994cb1ef64STejun Heo if (flags & WQ_BH) {
57004cb1ef64STejun Heo /*
57014cb1ef64STejun Heo * BH workqueues always share a single execution context per CPU
57024cb1ef64STejun Heo * and don't impose any max_active limit.
57034cb1ef64STejun Heo */
57044cb1ef64STejun Heo max_active = INT_MAX;
57054cb1ef64STejun Heo } else {
5706d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE;
5707b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name);
57084cb1ef64STejun Heo }
57093af24433SOleg Nesterov
5710b196be89STejun Heo /* init wq */
571197e37d7bSTejun Heo wq->flags = flags;
5712a045a272STejun Heo wq->max_active = max_active;
57135797b1c1STejun Heo wq->min_active = min(max_active, WQ_DFL_MIN_ACTIVE);
57145797b1c1STejun Heo wq->saved_max_active = wq->max_active;
57155797b1c1STejun Heo wq->saved_min_active = wq->min_active;
57163c25a55dSLai Jiangshan mutex_init(&wq->mutex);
5717112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0);
571830cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs);
571973f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue);
572073f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow);
5721493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays);
57223af24433SOleg Nesterov
5723cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list);
57243af24433SOleg Nesterov
572591ccc6e7STejun Heo if (flags & WQ_UNBOUND) {
572691ccc6e7STejun Heo if (alloc_node_nr_active(wq->node_nr_active) < 0)
5727b188c57aSMatthew Brost goto err_free_wq;
572891ccc6e7STejun Heo }
572991ccc6e7STejun Heo
57306af8bf3dSOleg Nesterov /*
57311726a171SLai Jiangshan * wq_pool_mutex protects the workqueues list, allocations of PWQs,
5732aa868475SLai Jiangshan * and the global freeze state.
57336af8bf3dSOleg Nesterov */
57341726a171SLai Jiangshan apply_wqattrs_lock();
57351726a171SLai Jiangshan
57361726a171SLai Jiangshan if (alloc_and_link_pwqs(wq) < 0)
57371726a171SLai Jiangshan goto err_unlock_free_node_nr_active;
5738a0a1a5fdSTejun Heo
5739a357fc03SLai Jiangshan mutex_lock(&wq->mutex);
5740a045a272STejun Heo wq_adjust_max_active(wq);
5741a357fc03SLai Jiangshan mutex_unlock(&wq->mutex);
5742a0a1a5fdSTejun Heo
5743e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues);
5744a0a1a5fdSTejun Heo
5745c5178e6cSLai Jiangshan if (wq_online && init_rescuer(wq) < 0)
5746449b31adSLai Jiangshan goto err_unlock_destroy;
5747449b31adSLai Jiangshan
5748449b31adSLai Jiangshan apply_wqattrs_unlock();
5749c5178e6cSLai Jiangshan
5750c3138f38SLai Jiangshan if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
5751c3138f38SLai Jiangshan goto err_destroy;
57523af24433SOleg Nesterov
57533af24433SOleg Nesterov return wq;
5754d2c1d404STejun Heo
57551726a171SLai Jiangshan err_unlock_free_node_nr_active:
57561726a171SLai Jiangshan apply_wqattrs_unlock();
57574e9a3738SLai Jiangshan /*
57584e9a3738SLai Jiangshan * Failed alloc_and_link_pwqs() may leave pending pwq->release_work,
57594e9a3738SLai Jiangshan * flushing the pwq_release_worker ensures that the pwq_release_workfn()
57604e9a3738SLai Jiangshan * completes before calling kfree(wq).
57614e9a3738SLai Jiangshan */
57624e9a3738SLai Jiangshan if (wq->flags & WQ_UNBOUND) {
57634e9a3738SLai Jiangshan kthread_flush_worker(pwq_release_worker);
576491ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active);
57654e9a3738SLai Jiangshan }
576682efcab3SBart Van Assche err_free_wq:
57676029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs);
57684690c4abSTejun Heo kfree(wq);
5769d2c1d404STejun Heo return NULL;
5770449b31adSLai Jiangshan err_unlock_destroy:
5771449b31adSLai Jiangshan apply_wqattrs_unlock();
5772d2c1d404STejun Heo err_destroy:
5773d2c1d404STejun Heo destroy_workqueue(wq);
57744690c4abSTejun Heo return NULL;
57751da177e4SLinus Torvalds }
5776b188c57aSMatthew Brost
5777b188c57aSMatthew Brost __printf(1, 4)
alloc_workqueue(const char * fmt,unsigned int flags,int max_active,...)5778b188c57aSMatthew Brost struct workqueue_struct *alloc_workqueue(const char *fmt,
5779b188c57aSMatthew Brost unsigned int flags,
5780b188c57aSMatthew Brost int max_active, ...)
5781b188c57aSMatthew Brost {
5782b188c57aSMatthew Brost struct workqueue_struct *wq;
5783b188c57aSMatthew Brost va_list args;
5784b188c57aSMatthew Brost
5785b188c57aSMatthew Brost va_start(args, max_active);
5786b188c57aSMatthew Brost wq = __alloc_workqueue(fmt, flags, max_active, args);
5787b188c57aSMatthew Brost va_end(args);
5788b188c57aSMatthew Brost if (!wq)
5789b188c57aSMatthew Brost return NULL;
5790b188c57aSMatthew Brost
5791b188c57aSMatthew Brost wq_init_lockdep(wq);
5792b188c57aSMatthew Brost
5793b188c57aSMatthew Brost return wq;
5794b188c57aSMatthew Brost }
5795669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue);
57961da177e4SLinus Torvalds
5797ec0a7d44SMatthew Brost #ifdef CONFIG_LOCKDEP
5798ec0a7d44SMatthew Brost __printf(1, 5)
5799ec0a7d44SMatthew Brost struct workqueue_struct *
alloc_workqueue_lockdep_map(const char * fmt,unsigned int flags,int max_active,struct lockdep_map * lockdep_map,...)5800ec0a7d44SMatthew Brost alloc_workqueue_lockdep_map(const char *fmt, unsigned int flags,
5801ec0a7d44SMatthew Brost int max_active, struct lockdep_map *lockdep_map, ...)
5802ec0a7d44SMatthew Brost {
5803ec0a7d44SMatthew Brost struct workqueue_struct *wq;
5804ec0a7d44SMatthew Brost va_list args;
5805ec0a7d44SMatthew Brost
5806ec0a7d44SMatthew Brost va_start(args, lockdep_map);
5807ec0a7d44SMatthew Brost wq = __alloc_workqueue(fmt, flags, max_active, args);
5808ec0a7d44SMatthew Brost va_end(args);
5809ec0a7d44SMatthew Brost if (!wq)
5810ec0a7d44SMatthew Brost return NULL;
5811ec0a7d44SMatthew Brost
5812ec0a7d44SMatthew Brost wq->lockdep_map = lockdep_map;
5813ec0a7d44SMatthew Brost
5814ec0a7d44SMatthew Brost return wq;
5815ec0a7d44SMatthew Brost }
5816ec0a7d44SMatthew Brost EXPORT_SYMBOL_GPL(alloc_workqueue_lockdep_map);
5817ec0a7d44SMatthew Brost #endif
5818ec0a7d44SMatthew Brost
pwq_busy(struct pool_workqueue * pwq)5819c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq)
5820c29eb853STejun Heo {
5821c29eb853STejun Heo int i;
5822c29eb853STejun Heo
5823c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++)
5824c29eb853STejun Heo if (pwq->nr_in_flight[i])
5825c29eb853STejun Heo return true;
5826c29eb853STejun Heo
58279f66cff2STejun Heo if ((pwq != rcu_access_pointer(pwq->wq->dfl_pwq)) && (pwq->refcnt > 1))
5828c29eb853STejun Heo return true;
5829afa87ce8STejun Heo if (!pwq_is_empty(pwq))
5830c29eb853STejun Heo return true;
5831c29eb853STejun Heo
5832c29eb853STejun Heo return false;
5833c29eb853STejun Heo }
5834c29eb853STejun Heo
58353af24433SOleg Nesterov /**
58363af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue
58373af24433SOleg Nesterov * @wq: target workqueue
58383af24433SOleg Nesterov *
58393af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first.
58403af24433SOleg Nesterov */
destroy_workqueue(struct workqueue_struct * wq)58413af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq)
58423af24433SOleg Nesterov {
584349e3cf44STejun Heo struct pool_workqueue *pwq;
5844636b927eSTejun Heo int cpu;
58453af24433SOleg Nesterov
5846def98c84STejun Heo /*
5847def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't
5848def98c84STejun Heo * lead to sysfs name conflicts.
5849def98c84STejun Heo */
5850def98c84STejun Heo workqueue_sysfs_unregister(wq);
5851def98c84STejun Heo
585233e3f0a3SRichard Clark /* mark the workqueue destruction is in progress */
585333e3f0a3SRichard Clark mutex_lock(&wq->mutex);
585433e3f0a3SRichard Clark wq->flags |= __WQ_DESTROYING;
585533e3f0a3SRichard Clark mutex_unlock(&wq->mutex);
585633e3f0a3SRichard Clark
58579c5a2ba7STejun Heo /* drain it before proceeding with destruction */
58589c5a2ba7STejun Heo drain_workqueue(wq);
5859c8efcc25STejun Heo
5860def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */
5861def98c84STejun Heo if (wq->rescuer) {
5862def98c84STejun Heo struct worker *rescuer = wq->rescuer;
5863def98c84STejun Heo
5864def98c84STejun Heo /* this prevents new queueing */
5865a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock);
5866def98c84STejun Heo wq->rescuer = NULL;
5867a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock);
5868def98c84STejun Heo
5869def98c84STejun Heo /* rescuer will empty maydays list before exiting */
5870def98c84STejun Heo kthread_stop(rescuer->task);
58718efe1223STejun Heo kfree(rescuer);
5872def98c84STejun Heo }
5873def98c84STejun Heo
5874c29eb853STejun Heo /*
5875c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all
5876c29eb853STejun Heo * in-flight operations which may do put_pwq().
5877c29eb853STejun Heo */
5878c29eb853STejun Heo mutex_lock(&wq_pool_mutex);
5879b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex);
588049e3cf44STejun Heo for_each_pwq(pwq, wq) {
5881a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock);
5882c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) {
58831d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n",
5884e66b39afSTejun Heo __func__, wq->name);
5885c29eb853STejun Heo show_pwq(pwq);
5886a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock);
5887b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex);
5888c29eb853STejun Heo mutex_unlock(&wq_pool_mutex);
588955df0933SImran Khan show_one_workqueue(wq);
58906183c009STejun Heo return;
589176af4d93STejun Heo }
5892a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock);
589376af4d93STejun Heo }
5894b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex);
58956183c009STejun Heo
5896a0a1a5fdSTejun Heo /*
5897a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after
5898a0a1a5fdSTejun Heo * flushing is complete in case freeze races us.
5899a0a1a5fdSTejun Heo */
5900e2dca7adSTejun Heo list_del_rcu(&wq->list);
590168e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex);
59023af24433SOleg Nesterov
59038864b4e5STejun Heo /*
5904636b927eSTejun Heo * We're the sole accessor of @wq. Directly access cpu_pwq and dfl_pwq
5905636b927eSTejun Heo * to put the base refs. @wq will be auto-destroyed from the last
5906636b927eSTejun Heo * pwq_put. RCU read lock prevents @wq from going away from under us.
59078864b4e5STejun Heo */
5908636b927eSTejun Heo rcu_read_lock();
5909636b927eSTejun Heo
5910636b927eSTejun Heo for_each_possible_cpu(cpu) {
59119f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, cpu));
59129f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, cpu), NULL);
59134c16bd32STejun Heo }
59144c16bd32STejun Heo
59159f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, -1));
59169f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, -1), NULL);
5917636b927eSTejun Heo
5918636b927eSTejun Heo rcu_read_unlock();
59193af24433SOleg Nesterov }
59203af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue);
59213af24433SOleg Nesterov
5922dcd989cbSTejun Heo /**
5923dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue
5924dcd989cbSTejun Heo * @wq: target workqueue
5925dcd989cbSTejun Heo * @max_active: new max_active value.
5926dcd989cbSTejun Heo *
59275797b1c1STejun Heo * Set max_active of @wq to @max_active. See the alloc_workqueue() function
59285797b1c1STejun Heo * comment.
5929dcd989cbSTejun Heo *
5930dcd989cbSTejun Heo * CONTEXT:
5931dcd989cbSTejun Heo * Don't call from IRQ context.
5932dcd989cbSTejun Heo */
workqueue_set_max_active(struct workqueue_struct * wq,int max_active)5933dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
5934dcd989cbSTejun Heo {
59354cb1ef64STejun Heo /* max_active doesn't mean anything for BH workqueues */
59364cb1ef64STejun Heo if (WARN_ON(wq->flags & WQ_BH))
59374cb1ef64STejun Heo return;
59388719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */
59393bc1e711STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED))
59408719dceaSTejun Heo return;
59418719dceaSTejun Heo
5942f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
5943dcd989cbSTejun Heo
5944a357fc03SLai Jiangshan mutex_lock(&wq->mutex);
5945dcd989cbSTejun Heo
5946dcd989cbSTejun Heo wq->saved_max_active = max_active;
59475797b1c1STejun Heo if (wq->flags & WQ_UNBOUND)
59485797b1c1STejun Heo wq->saved_min_active = min(wq->saved_min_active, max_active);
59495797b1c1STejun Heo
5950a045a272STejun Heo wq_adjust_max_active(wq);
5951dcd989cbSTejun Heo
5952a357fc03SLai Jiangshan mutex_unlock(&wq->mutex);
5953dcd989cbSTejun Heo }
5954dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active);
5955dcd989cbSTejun Heo
5956dcd989cbSTejun Heo /**
59578f172181STejun Heo * workqueue_set_min_active - adjust min_active of an unbound workqueue
59588f172181STejun Heo * @wq: target unbound workqueue
59598f172181STejun Heo * @min_active: new min_active value
59608f172181STejun Heo *
59618f172181STejun Heo * Set min_active of an unbound workqueue. Unlike other types of workqueues, an
59628f172181STejun Heo * unbound workqueue is not guaranteed to be able to process max_active
59638f172181STejun Heo * interdependent work items. Instead, an unbound workqueue is guaranteed to be
59648f172181STejun Heo * able to process min_active number of interdependent work items which is
59658f172181STejun Heo * %WQ_DFL_MIN_ACTIVE by default.
59668f172181STejun Heo *
59678f172181STejun Heo * Use this function to adjust the min_active value between 0 and the current
59688f172181STejun Heo * max_active.
59698f172181STejun Heo */
workqueue_set_min_active(struct workqueue_struct * wq,int min_active)59708f172181STejun Heo void workqueue_set_min_active(struct workqueue_struct *wq, int min_active)
59718f172181STejun Heo {
59728f172181STejun Heo /* min_active is only meaningful for non-ordered unbound workqueues */
59738f172181STejun Heo if (WARN_ON((wq->flags & (WQ_BH | WQ_UNBOUND | __WQ_ORDERED)) !=
59748f172181STejun Heo WQ_UNBOUND))
59758f172181STejun Heo return;
59768f172181STejun Heo
59778f172181STejun Heo mutex_lock(&wq->mutex);
59788f172181STejun Heo wq->saved_min_active = clamp(min_active, 0, wq->saved_max_active);
59798f172181STejun Heo wq_adjust_max_active(wq);
59808f172181STejun Heo mutex_unlock(&wq->mutex);
59818f172181STejun Heo }
59828f172181STejun Heo
59838f172181STejun Heo /**
598427d4ee03SLukas Wunner * current_work - retrieve %current task's work struct
598527d4ee03SLukas Wunner *
598627d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on.
598727d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in.
598827d4ee03SLukas Wunner *
598927d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise.
599027d4ee03SLukas Wunner */
current_work(void)599127d4ee03SLukas Wunner struct work_struct *current_work(void)
599227d4ee03SLukas Wunner {
599327d4ee03SLukas Wunner struct worker *worker = current_wq_worker();
599427d4ee03SLukas Wunner
599527d4ee03SLukas Wunner return worker ? worker->current_work : NULL;
599627d4ee03SLukas Wunner }
599727d4ee03SLukas Wunner EXPORT_SYMBOL(current_work);
599827d4ee03SLukas Wunner
599927d4ee03SLukas Wunner /**
6000e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer?
6001e6267616STejun Heo *
6002e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from
6003e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task.
6004d185af30SYacine Belkadi *
6005d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise.
6006e6267616STejun Heo */
current_is_workqueue_rescuer(void)6007e6267616STejun Heo bool current_is_workqueue_rescuer(void)
6008e6267616STejun Heo {
6009e6267616STejun Heo struct worker *worker = current_wq_worker();
6010e6267616STejun Heo
60116a092dfdSLai Jiangshan return worker && worker->rescue_wq;
6012e6267616STejun Heo }
6013e6267616STejun Heo
6014e6267616STejun Heo /**
6015dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested
6016dcd989cbSTejun Heo * @cpu: CPU in question
6017dcd989cbSTejun Heo * @wq: target workqueue
6018dcd989cbSTejun Heo *
6019dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is
6020dcd989cbSTejun Heo * no synchronization around this function and the test result is
6021dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging.
6022dcd989cbSTejun Heo *
6023d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU.
6024636b927eSTejun Heo *
6025636b927eSTejun Heo * With the exception of ordered workqueues, all workqueues have per-cpu
6026636b927eSTejun Heo * pool_workqueues, each with its own congested state. A workqueue being
6027636b927eSTejun Heo * congested on one CPU doesn't mean that the workqueue is contested on any
6028636b927eSTejun Heo * other CPUs.
6029d3251859STejun Heo *
6030d185af30SYacine Belkadi * Return:
6031dcd989cbSTejun Heo * %true if congested, %false otherwise.
6032dcd989cbSTejun Heo */
workqueue_congested(int cpu,struct workqueue_struct * wq)6033d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq)
6034dcd989cbSTejun Heo {
60357fb98ea7STejun Heo struct pool_workqueue *pwq;
603676af4d93STejun Heo bool ret;
603776af4d93STejun Heo
603824acfb71SThomas Gleixner rcu_read_lock();
603924acfb71SThomas Gleixner preempt_disable();
60407fb98ea7STejun Heo
6041d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND)
6042d3251859STejun Heo cpu = smp_processor_id();
6043d3251859STejun Heo
6044687a9aa5STejun Heo pwq = *per_cpu_ptr(wq->cpu_pwq, cpu);
6045f97a4a1aSLai Jiangshan ret = !list_empty(&pwq->inactive_works);
6046636b927eSTejun Heo
604724acfb71SThomas Gleixner preempt_enable();
604824acfb71SThomas Gleixner rcu_read_unlock();
604976af4d93STejun Heo
605076af4d93STejun Heo return ret;
6051dcd989cbSTejun Heo }
6052dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested);
6053dcd989cbSTejun Heo
6054dcd989cbSTejun Heo /**
6055dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running
6056dcd989cbSTejun Heo * @work: the work to be tested
6057dcd989cbSTejun Heo *
6058dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no
6059dcd989cbSTejun Heo * synchronization around this function and the test result is
6060dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging.
6061dcd989cbSTejun Heo *
6062d185af30SYacine Belkadi * Return:
6063dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits.
6064dcd989cbSTejun Heo */
work_busy(struct work_struct * work)6065dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work)
6066dcd989cbSTejun Heo {
6067fa1b54e6STejun Heo struct worker_pool *pool;
6068c26e2f2eSTejun Heo unsigned long irq_flags;
6069dcd989cbSTejun Heo unsigned int ret = 0;
6070dcd989cbSTejun Heo
6071dcd989cbSTejun Heo if (work_pending(work))
6072dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING;
6073038366c5SLai Jiangshan
607424acfb71SThomas Gleixner rcu_read_lock();
6075fa1b54e6STejun Heo pool = get_work_pool(work);
6076038366c5SLai Jiangshan if (pool) {
6077c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags);
6078c9e7cf27STejun Heo if (find_worker_executing_work(pool, work))
6079dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING;
6080c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags);
6081038366c5SLai Jiangshan }
608224acfb71SThomas Gleixner rcu_read_unlock();
6083dcd989cbSTejun Heo
6084dcd989cbSTejun Heo return ret;
6085dcd989cbSTejun Heo }
6086dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy);
6087dcd989cbSTejun Heo
60883d1cb205STejun Heo /**
60893d1cb205STejun Heo * set_worker_desc - set description for the current work item
60903d1cb205STejun Heo * @fmt: printf-style format string
60913d1cb205STejun Heo * @...: arguments for the format string
60923d1cb205STejun Heo *
60933d1cb205STejun Heo * This function can be called by a running work function to describe what
60943d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this
60953d1cb205STejun Heo * information will be printed out together to help debugging. The
60963d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'.
60973d1cb205STejun Heo */
set_worker_desc(const char * fmt,...)60983d1cb205STejun Heo void set_worker_desc(const char *fmt, ...)
60993d1cb205STejun Heo {
61003d1cb205STejun Heo struct worker *worker = current_wq_worker();
61013d1cb205STejun Heo va_list args;
61023d1cb205STejun Heo
61033d1cb205STejun Heo if (worker) {
61043d1cb205STejun Heo va_start(args, fmt);
61053d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args);
61063d1cb205STejun Heo va_end(args);
61073d1cb205STejun Heo }
61083d1cb205STejun Heo }
61095c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc);
61103d1cb205STejun Heo
61113d1cb205STejun Heo /**
61123d1cb205STejun Heo * print_worker_info - print out worker information and description
61133d1cb205STejun Heo * @log_lvl: the log level to use when printing
61143d1cb205STejun Heo * @task: target task
61153d1cb205STejun Heo *
61163d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the
61173d1cb205STejun Heo * name of the workqueue being serviced and worker description set with
61183d1cb205STejun Heo * set_worker_desc() by the currently executing work item.
61193d1cb205STejun Heo *
61203d1cb205STejun Heo * This function can be safely called on any task as long as the
61213d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't
61223d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length.
61233d1cb205STejun Heo */
print_worker_info(const char * log_lvl,struct task_struct * task)61243d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task)
61253d1cb205STejun Heo {
61263d1cb205STejun Heo work_func_t *fn = NULL;
61273d1cb205STejun Heo char name[WQ_NAME_LEN] = { };
61283d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { };
61293d1cb205STejun Heo struct pool_workqueue *pwq = NULL;
61303d1cb205STejun Heo struct workqueue_struct *wq = NULL;
61313d1cb205STejun Heo struct worker *worker;
61323d1cb205STejun Heo
61333d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER))
61343d1cb205STejun Heo return;
61353d1cb205STejun Heo
61363d1cb205STejun Heo /*
61373d1cb205STejun Heo * This function is called without any synchronization and @task
61383d1cb205STejun Heo * could be in any state. Be careful with dereferences.
61393d1cb205STejun Heo */
6140e700591aSPetr Mladek worker = kthread_probe_data(task);
61413d1cb205STejun Heo
61423d1cb205STejun Heo /*
61438bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc.
61448bf89593STejun Heo * Keep the original last '\0' in case the original is garbage.
61453d1cb205STejun Heo */
6146fe557319SChristoph Hellwig copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn));
6147fe557319SChristoph Hellwig copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq));
6148fe557319SChristoph Hellwig copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq));
6149fe557319SChristoph Hellwig copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1);
6150fe557319SChristoph Hellwig copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1);
61513d1cb205STejun Heo
61523d1cb205STejun Heo if (fn || name[0] || desc[0]) {
6153d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn);
61548bf89593STejun Heo if (strcmp(name, desc))
61553d1cb205STejun Heo pr_cont(" (%s)", desc);
61563d1cb205STejun Heo pr_cont("\n");
61573d1cb205STejun Heo }
61583d1cb205STejun Heo }
61593d1cb205STejun Heo
pr_cont_pool_info(struct worker_pool * pool)61603494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool)
61613494fc30STejun Heo {
61623494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask);
61633494fc30STejun Heo if (pool->node != NUMA_NO_NODE)
61643494fc30STejun Heo pr_cont(" node=%d", pool->node);
61654cb1ef64STejun Heo pr_cont(" flags=0x%x", pool->flags);
61664cb1ef64STejun Heo if (pool->flags & POOL_BH)
61674cb1ef64STejun Heo pr_cont(" bh%s",
61684cb1ef64STejun Heo pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : "");
61694cb1ef64STejun Heo else
61704cb1ef64STejun Heo pr_cont(" nice=%d", pool->attrs->nice);
61714cb1ef64STejun Heo }
61724cb1ef64STejun Heo
pr_cont_worker_id(struct worker * worker)61734cb1ef64STejun Heo static void pr_cont_worker_id(struct worker *worker)
61744cb1ef64STejun Heo {
61754cb1ef64STejun Heo struct worker_pool *pool = worker->pool;
61764cb1ef64STejun Heo
61774cb1ef64STejun Heo if (pool->flags & WQ_BH)
61784cb1ef64STejun Heo pr_cont("bh%s",
61794cb1ef64STejun Heo pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : "");
61804cb1ef64STejun Heo else
61814cb1ef64STejun Heo pr_cont("%d%s", task_pid_nr(worker->task),
61824cb1ef64STejun Heo worker->rescue_wq ? "(RESCUER)" : "");
61833494fc30STejun Heo }
61843494fc30STejun Heo
6185c76feb0dSPaul E. McKenney struct pr_cont_work_struct {
6186c76feb0dSPaul E. McKenney bool comma;
6187c76feb0dSPaul E. McKenney work_func_t func;
6188c76feb0dSPaul E. McKenney long ctr;
6189c76feb0dSPaul E. McKenney };
6190c76feb0dSPaul E. McKenney
pr_cont_work_flush(bool comma,work_func_t func,struct pr_cont_work_struct * pcwsp)6191c76feb0dSPaul E. McKenney static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp)
6192c76feb0dSPaul E. McKenney {
6193c76feb0dSPaul E. McKenney if (!pcwsp->ctr)
6194c76feb0dSPaul E. McKenney goto out_record;
6195c76feb0dSPaul E. McKenney if (func == pcwsp->func) {
6196c76feb0dSPaul E. McKenney pcwsp->ctr++;
6197c76feb0dSPaul E. McKenney return;
6198c76feb0dSPaul E. McKenney }
6199c76feb0dSPaul E. McKenney if (pcwsp->ctr == 1)
6200c76feb0dSPaul E. McKenney pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func);
6201c76feb0dSPaul E. McKenney else
6202c76feb0dSPaul E. McKenney pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func);
6203c76feb0dSPaul E. McKenney pcwsp->ctr = 0;
6204c76feb0dSPaul E. McKenney out_record:
6205c76feb0dSPaul E. McKenney if ((long)func == -1L)
6206c76feb0dSPaul E. McKenney return;
6207c76feb0dSPaul E. McKenney pcwsp->comma = comma;
6208c76feb0dSPaul E. McKenney pcwsp->func = func;
6209c76feb0dSPaul E. McKenney pcwsp->ctr = 1;
6210c76feb0dSPaul E. McKenney }
6211c76feb0dSPaul E. McKenney
pr_cont_work(bool comma,struct work_struct * work,struct pr_cont_work_struct * pcwsp)6212c76feb0dSPaul E. McKenney static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp)
62133494fc30STejun Heo {
62143494fc30STejun Heo if (work->func == wq_barrier_func) {
62153494fc30STejun Heo struct wq_barrier *barr;
62163494fc30STejun Heo
62173494fc30STejun Heo barr = container_of(work, struct wq_barrier, work);
62183494fc30STejun Heo
6219c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp);
62203494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "",
62213494fc30STejun Heo task_pid_nr(barr->task));
62223494fc30STejun Heo } else {
6223c76feb0dSPaul E. McKenney if (!comma)
6224c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp);
6225c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, work->func, pcwsp);
62263494fc30STejun Heo }
62273494fc30STejun Heo }
62283494fc30STejun Heo
show_pwq(struct pool_workqueue * pwq)62293494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq)
62303494fc30STejun Heo {
6231c76feb0dSPaul E. McKenney struct pr_cont_work_struct pcws = { .ctr = 0, };
62323494fc30STejun Heo struct worker_pool *pool = pwq->pool;
62333494fc30STejun Heo struct work_struct *work;
62343494fc30STejun Heo struct worker *worker;
62353494fc30STejun Heo bool has_in_flight = false, has_pending = false;
62363494fc30STejun Heo int bkt;
62373494fc30STejun Heo
62383494fc30STejun Heo pr_info(" pwq %d:", pool->id);
62393494fc30STejun Heo pr_cont_pool_info(pool);
62403494fc30STejun Heo
6241a045a272STejun Heo pr_cont(" active=%d refcnt=%d%s\n",
6242a045a272STejun Heo pwq->nr_active, pwq->refcnt,
62433494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : "");
62443494fc30STejun Heo
62453494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) {
62463494fc30STejun Heo if (worker->current_pwq == pwq) {
62473494fc30STejun Heo has_in_flight = true;
62483494fc30STejun Heo break;
62493494fc30STejun Heo }
62503494fc30STejun Heo }
62513494fc30STejun Heo if (has_in_flight) {
62523494fc30STejun Heo bool comma = false;
62533494fc30STejun Heo
62543494fc30STejun Heo pr_info(" in-flight:");
62553494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) {
62563494fc30STejun Heo if (worker->current_pwq != pwq)
62573494fc30STejun Heo continue;
62583494fc30STejun Heo
62594cb1ef64STejun Heo pr_cont(" %s", comma ? "," : "");
62604cb1ef64STejun Heo pr_cont_worker_id(worker);
62614cb1ef64STejun Heo pr_cont(":%ps", worker->current_func);
62623494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry)
6263c76feb0dSPaul E. McKenney pr_cont_work(false, work, &pcws);
6264c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws);
62653494fc30STejun Heo comma = true;
62663494fc30STejun Heo }
62673494fc30STejun Heo pr_cont("\n");
62683494fc30STejun Heo }
62693494fc30STejun Heo
62703494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) {
62713494fc30STejun Heo if (get_work_pwq(work) == pwq) {
62723494fc30STejun Heo has_pending = true;
62733494fc30STejun Heo break;
62743494fc30STejun Heo }
62753494fc30STejun Heo }
62763494fc30STejun Heo if (has_pending) {
62773494fc30STejun Heo bool comma = false;
62783494fc30STejun Heo
62793494fc30STejun Heo pr_info(" pending:");
62803494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) {
62813494fc30STejun Heo if (get_work_pwq(work) != pwq)
62823494fc30STejun Heo continue;
62833494fc30STejun Heo
6284c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws);
62853494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED);
62863494fc30STejun Heo }
6287c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws);
62883494fc30STejun Heo pr_cont("\n");
62893494fc30STejun Heo }
62903494fc30STejun Heo
6291f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) {
62923494fc30STejun Heo bool comma = false;
62933494fc30STejun Heo
6294f97a4a1aSLai Jiangshan pr_info(" inactive:");
6295f97a4a1aSLai Jiangshan list_for_each_entry(work, &pwq->inactive_works, entry) {
6296c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws);
62973494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED);
62983494fc30STejun Heo }
6299c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws);
63003494fc30STejun Heo pr_cont("\n");
63013494fc30STejun Heo }
63023494fc30STejun Heo }
63033494fc30STejun Heo
63043494fc30STejun Heo /**
630555df0933SImran Khan * show_one_workqueue - dump state of specified workqueue
630655df0933SImran Khan * @wq: workqueue whose state will be printed
63073494fc30STejun Heo */
show_one_workqueue(struct workqueue_struct * wq)630855df0933SImran Khan void show_one_workqueue(struct workqueue_struct *wq)
63093494fc30STejun Heo {
63103494fc30STejun Heo struct pool_workqueue *pwq;
63113494fc30STejun Heo bool idle = true;
6312c26e2f2eSTejun Heo unsigned long irq_flags;
63133494fc30STejun Heo
63143494fc30STejun Heo for_each_pwq(pwq, wq) {
6315afa87ce8STejun Heo if (!pwq_is_empty(pwq)) {
63163494fc30STejun Heo idle = false;
63173494fc30STejun Heo break;
63183494fc30STejun Heo }
63193494fc30STejun Heo }
632055df0933SImran Khan if (idle) /* Nothing to print for idle workqueue */
632155df0933SImran Khan return;
63223494fc30STejun Heo
63233494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags);
63243494fc30STejun Heo
63253494fc30STejun Heo for_each_pwq(pwq, wq) {
6326c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, irq_flags);
6327afa87ce8STejun Heo if (!pwq_is_empty(pwq)) {
632857116ce1SJohan Hovold /*
632957116ce1SJohan Hovold * Defer printing to avoid deadlocks in console
633057116ce1SJohan Hovold * drivers that queue work while holding locks
633157116ce1SJohan Hovold * also taken in their write paths.
633257116ce1SJohan Hovold */
633357116ce1SJohan Hovold printk_deferred_enter();
63343494fc30STejun Heo show_pwq(pwq);
633557116ce1SJohan Hovold printk_deferred_exit();
633657116ce1SJohan Hovold }
6337c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, irq_flags);
633862635ea8SSergey Senozhatsky /*
633962635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g.
634055df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering
634162635ea8SSergey Senozhatsky * hard lockup.
634262635ea8SSergey Senozhatsky */
634362635ea8SSergey Senozhatsky touch_nmi_watchdog();
63443494fc30STejun Heo }
634555df0933SImran Khan
63463494fc30STejun Heo }
63473494fc30STejun Heo
634855df0933SImran Khan /**
634955df0933SImran Khan * show_one_worker_pool - dump state of specified worker pool
635055df0933SImran Khan * @pool: worker pool whose state will be printed
635155df0933SImran Khan */
show_one_worker_pool(struct worker_pool * pool)635255df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool)
635355df0933SImran Khan {
63543494fc30STejun Heo struct worker *worker;
63553494fc30STejun Heo bool first = true;
6356c26e2f2eSTejun Heo unsigned long irq_flags;
6357335a42ebSPetr Mladek unsigned long hung = 0;
63583494fc30STejun Heo
6359c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags);
63603494fc30STejun Heo if (pool->nr_workers == pool->nr_idle)
63613494fc30STejun Heo goto next_pool;
6362335a42ebSPetr Mladek
6363335a42ebSPetr Mladek /* How long the first pending work is waiting for a worker. */
6364335a42ebSPetr Mladek if (!list_empty(&pool->worklist))
6365335a42ebSPetr Mladek hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000;
6366335a42ebSPetr Mladek
636757116ce1SJohan Hovold /*
636857116ce1SJohan Hovold * Defer printing to avoid deadlocks in console drivers that
636957116ce1SJohan Hovold * queue work while holding locks also taken in their write
637057116ce1SJohan Hovold * paths.
637157116ce1SJohan Hovold */
637257116ce1SJohan Hovold printk_deferred_enter();
63733494fc30STejun Heo pr_info("pool %d:", pool->id);
63743494fc30STejun Heo pr_cont_pool_info(pool);
6375335a42ebSPetr Mladek pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers);
63763494fc30STejun Heo if (pool->manager)
63773494fc30STejun Heo pr_cont(" manager: %d",
63783494fc30STejun Heo task_pid_nr(pool->manager->task));
63793494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) {
63804cb1ef64STejun Heo pr_cont(" %s", first ? "idle: " : "");
63814cb1ef64STejun Heo pr_cont_worker_id(worker);
63823494fc30STejun Heo first = false;
63833494fc30STejun Heo }
63843494fc30STejun Heo pr_cont("\n");
638557116ce1SJohan Hovold printk_deferred_exit();
63863494fc30STejun Heo next_pool:
6387c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags);
638862635ea8SSergey Senozhatsky /*
638962635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g.
639055df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering
639162635ea8SSergey Senozhatsky * hard lockup.
639262635ea8SSergey Senozhatsky */
639362635ea8SSergey Senozhatsky touch_nmi_watchdog();
639455df0933SImran Khan
63953494fc30STejun Heo }
63963494fc30STejun Heo
639755df0933SImran Khan /**
639855df0933SImran Khan * show_all_workqueues - dump workqueue state
639955df0933SImran Khan *
6400704bc669SJungseung Lee * Called from a sysrq handler and prints out all busy workqueues and pools.
640155df0933SImran Khan */
show_all_workqueues(void)640255df0933SImran Khan void show_all_workqueues(void)
640355df0933SImran Khan {
640455df0933SImran Khan struct workqueue_struct *wq;
640555df0933SImran Khan struct worker_pool *pool;
640655df0933SImran Khan int pi;
640755df0933SImran Khan
640855df0933SImran Khan rcu_read_lock();
640955df0933SImran Khan
641055df0933SImran Khan pr_info("Showing busy workqueues and worker pools:\n");
641155df0933SImran Khan
641255df0933SImran Khan list_for_each_entry_rcu(wq, &workqueues, list)
641355df0933SImran Khan show_one_workqueue(wq);
641455df0933SImran Khan
641555df0933SImran Khan for_each_pool(pool, pi)
641655df0933SImran Khan show_one_worker_pool(pool);
641755df0933SImran Khan
641824acfb71SThomas Gleixner rcu_read_unlock();
64193494fc30STejun Heo }
64203494fc30STejun Heo
6421704bc669SJungseung Lee /**
6422704bc669SJungseung Lee * show_freezable_workqueues - dump freezable workqueue state
6423704bc669SJungseung Lee *
6424704bc669SJungseung Lee * Called from try_to_freeze_tasks() and prints out all freezable workqueues
6425704bc669SJungseung Lee * still busy.
6426704bc669SJungseung Lee */
show_freezable_workqueues(void)6427704bc669SJungseung Lee void show_freezable_workqueues(void)
6428704bc669SJungseung Lee {
6429704bc669SJungseung Lee struct workqueue_struct *wq;
6430704bc669SJungseung Lee
6431704bc669SJungseung Lee rcu_read_lock();
6432704bc669SJungseung Lee
6433704bc669SJungseung Lee pr_info("Showing freezable workqueues that are still busy:\n");
6434704bc669SJungseung Lee
6435704bc669SJungseung Lee list_for_each_entry_rcu(wq, &workqueues, list) {
6436704bc669SJungseung Lee if (!(wq->flags & WQ_FREEZABLE))
6437704bc669SJungseung Lee continue;
6438704bc669SJungseung Lee show_one_workqueue(wq);
6439704bc669SJungseung Lee }
6440704bc669SJungseung Lee
6441704bc669SJungseung Lee rcu_read_unlock();
6442704bc669SJungseung Lee }
6443704bc669SJungseung Lee
64446b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */
wq_worker_comm(char * buf,size_t size,struct task_struct * task)64456b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task)
64466b59808bSTejun Heo {
6447197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */
64486b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex);
64496b59808bSTejun Heo
6450197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) {
6451197f6accSTejun Heo struct worker *worker = kthread_data(task);
6452197f6accSTejun Heo struct worker_pool *pool = worker->pool;
64532a1b02bcSTejun Heo int off;
64542a1b02bcSTejun Heo
64552a1b02bcSTejun Heo off = format_worker_id(buf, size, worker, pool);
64566b59808bSTejun Heo
64576b59808bSTejun Heo if (pool) {
6458a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock);
64596b59808bSTejun Heo /*
6460197f6accSTejun Heo * ->desc tracks information (wq name or
6461197f6accSTejun Heo * set_worker_desc()) for the latest execution. If
6462197f6accSTejun Heo * current, prepend '+', otherwise '-'.
64636b59808bSTejun Heo */
64646b59808bSTejun Heo if (worker->desc[0] != '\0') {
64656b59808bSTejun Heo if (worker->current_work)
64666b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s",
64676b59808bSTejun Heo worker->desc);
64686b59808bSTejun Heo else
64696b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s",
64706b59808bSTejun Heo worker->desc);
64716b59808bSTejun Heo }
6472a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock);
64736b59808bSTejun Heo }
64742a1b02bcSTejun Heo } else {
64752a1b02bcSTejun Heo strscpy(buf, task->comm, size);
6476197f6accSTejun Heo }
64776b59808bSTejun Heo
64786b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex);
64796b59808bSTejun Heo }
64806b59808bSTejun Heo
648166448bc2SMathieu Malaterre #ifdef CONFIG_SMP
648266448bc2SMathieu Malaterre
6483db7bccf4STejun Heo /*
6484db7bccf4STejun Heo * CPU hotplug.
6485db7bccf4STejun Heo *
6486e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there
6487112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and
6488706026c2STejun Heo * pool which make migrating pending and scheduled works very
6489e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly,
649094cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making
6491e22bee78STejun Heo * blocked draining impractical.
6492e22bee78STejun Heo *
649324647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU
6494628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the
6495628c78e7STejun Heo * cpu comes back online.
6496db7bccf4STejun Heo */
6497db7bccf4STejun Heo
unbind_workers(int cpu)6498e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu)
6499db7bccf4STejun Heo {
65004ce62e9eSTejun Heo struct worker_pool *pool;
6501db7bccf4STejun Heo struct worker *worker;
6502db7bccf4STejun Heo
6503f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) {
65041258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex);
6505a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock);
6506e22bee78STejun Heo
6507f2d5a0eeSTejun Heo /*
650892f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers
650994cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers
651011b45b0bSLai Jiangshan * must be on the cpu. After this, they may become diasporas.
6511b4ac9384SLai Jiangshan * And the preemption disabled section in their sched callbacks
6512b4ac9384SLai Jiangshan * are guaranteed to see WORKER_UNBOUND since the code here
6513b4ac9384SLai Jiangshan * is on the same cpu.
6514f2d5a0eeSTejun Heo */
6515da028469SLai Jiangshan for_each_pool_worker(worker, pool)
6516403c821dSTejun Heo worker->flags |= WORKER_UNBOUND;
6517db7bccf4STejun Heo
651824647570STejun Heo pool->flags |= POOL_DISASSOCIATED;
6519f2d5a0eeSTejun Heo
6520e22bee78STejun Heo /*
6521989442d7SLai Jiangshan * The handling of nr_running in sched callbacks are disabled
6522989442d7SLai Jiangshan * now. Zap nr_running. After this, nr_running stays zero and
6523989442d7SLai Jiangshan * need_more_worker() and keep_working() are always true as
6524989442d7SLai Jiangshan * long as the worklist is not empty. This pool now behaves as
6525989442d7SLai Jiangshan * an unbound (in terms of concurrency management) pool which
6526eb283428SLai Jiangshan * are served by workers tied to the pool.
6527e22bee78STejun Heo */
6528bc35f7efSLai Jiangshan pool->nr_running = 0;
6529eb283428SLai Jiangshan
6530eb283428SLai Jiangshan /*
6531eb283428SLai Jiangshan * With concurrency management just turned off, a busy
6532eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off
6533eb283428SLai Jiangshan * unbound chain execution of currently pending work items.
6534eb283428SLai Jiangshan */
65350219a352STejun Heo kick_pool(pool);
6536989442d7SLai Jiangshan
6537a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock);
6538989442d7SLai Jiangshan
6539793777bcSValentin Schneider for_each_pool_worker(worker, pool)
6540793777bcSValentin Schneider unbind_worker(worker);
6541989442d7SLai Jiangshan
6542989442d7SLai Jiangshan mutex_unlock(&wq_pool_attach_mutex);
6543eb283428SLai Jiangshan }
6544db7bccf4STejun Heo }
6545db7bccf4STejun Heo
6546bd7c089eSTejun Heo /**
6547bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU
6548bd7c089eSTejun Heo * @pool: pool of interest
6549bd7c089eSTejun Heo *
6550a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU.
6551bd7c089eSTejun Heo */
rebind_workers(struct worker_pool * pool)6552bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool)
6553bd7c089eSTejun Heo {
6554a9ab775bSTejun Heo struct worker *worker;
6555bd7c089eSTejun Heo
65561258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex);
6557bd7c089eSTejun Heo
6558bd7c089eSTejun Heo /*
6559a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should
6560a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local
6561402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity
6562a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called
6563a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail.
6564bd7c089eSTejun Heo */
6565c63a2e52SValentin Schneider for_each_pool_worker(worker, pool) {
6566c63a2e52SValentin Schneider kthread_set_per_cpu(worker->task, pool->cpu);
6567c63a2e52SValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
65689546b29eSTejun Heo pool_allowed_cpus(pool)) < 0);
6569c63a2e52SValentin Schneider }
6570a9ab775bSTejun Heo
6571a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock);
6572f7c17d26SWanpeng Li
65733de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED;
6574a9ab775bSTejun Heo
6575da028469SLai Jiangshan for_each_pool_worker(worker, pool) {
6576a9ab775bSTejun Heo unsigned int worker_flags = worker->flags;
6577a9ab775bSTejun Heo
6578a9ab775bSTejun Heo /*
6579a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call
6580a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically
6581a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND.
6582a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when
6583a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring
6584a9ab775bSTejun Heo * concurrency management. Note that when or whether
6585a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness.
6586a9ab775bSTejun Heo *
6587c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be
6588a9ab775bSTejun Heo * tested without holding any lock in
65896d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may
6590a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency
6591a9ab775bSTejun Heo * management operations.
6592bd7c089eSTejun Heo */
6593a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
6594a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND;
6595a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND;
6596c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags);
6597bd7c089eSTejun Heo }
6598a9ab775bSTejun Heo
6599a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock);
6600bd7c089eSTejun Heo }
6601bd7c089eSTejun Heo
66027dbc725eSTejun Heo /**
66037dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers
66047dbc725eSTejun Heo * @pool: unbound pool of interest
66057dbc725eSTejun Heo * @cpu: the CPU which is coming up
66067dbc725eSTejun Heo *
66077dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online
66087dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets
66097dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
66107dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored.
66117dbc725eSTejun Heo */
restore_unbound_workers_cpumask(struct worker_pool * pool,int cpu)66127dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
66137dbc725eSTejun Heo {
66147dbc725eSTejun Heo static cpumask_t cpumask;
66157dbc725eSTejun Heo struct worker *worker;
66167dbc725eSTejun Heo
66171258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex);
66187dbc725eSTejun Heo
66197dbc725eSTejun Heo /* is @cpu allowed for @pool? */
66207dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
66217dbc725eSTejun Heo return;
66227dbc725eSTejun Heo
66237dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
66247dbc725eSTejun Heo
66257dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */
6626da028469SLai Jiangshan for_each_pool_worker(worker, pool)
6627d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0);
66287dbc725eSTejun Heo }
66297dbc725eSTejun Heo
workqueue_prepare_cpu(unsigned int cpu)66307ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu)
66311da177e4SLinus Torvalds {
66324ce62e9eSTejun Heo struct worker_pool *pool;
66331da177e4SLinus Torvalds
6634f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) {
66353ce63377STejun Heo if (pool->nr_workers)
66363ce63377STejun Heo continue;
6637051e1850SLai Jiangshan if (!create_worker(pool))
66387ee681b2SThomas Gleixner return -ENOMEM;
66393af24433SOleg Nesterov }
66407ee681b2SThomas Gleixner return 0;
66417ee681b2SThomas Gleixner }
66421da177e4SLinus Torvalds
workqueue_online_cpu(unsigned int cpu)66437ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu)
66447ee681b2SThomas Gleixner {
66457ee681b2SThomas Gleixner struct worker_pool *pool;
66467ee681b2SThomas Gleixner struct workqueue_struct *wq;
66477ee681b2SThomas Gleixner int pi;
66487ee681b2SThomas Gleixner
664968e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex);
66507dbc725eSTejun Heo
66518d84baf7SLai Jiangshan cpumask_set_cpu(cpu, wq_online_cpumask);
66528d84baf7SLai Jiangshan
66537dbc725eSTejun Heo for_each_pool(pool, pi) {
66544cb1ef64STejun Heo /* BH pools aren't affected by hotplug */
66554cb1ef64STejun Heo if (pool->flags & POOL_BH)
66564cb1ef64STejun Heo continue;
665794cf58bbSTejun Heo
66584cb1ef64STejun Heo mutex_lock(&wq_pool_attach_mutex);
6659f05b558dSLai Jiangshan if (pool->cpu == cpu)
666094cf58bbSTejun Heo rebind_workers(pool);
6661f05b558dSLai Jiangshan else if (pool->cpu < 0)
66627dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu);
66631258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex);
666494cf58bbSTejun Heo }
66657dbc725eSTejun Heo
6666fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */
66674cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) {
666884193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs;
666984193c07STejun Heo
667084193c07STejun Heo if (attrs) {
667184193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs);
66724cbfd3deSTejun Heo int tcpu;
66734cbfd3deSTejun Heo
667484193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]])
6675b2b1f933SLai Jiangshan unbound_wq_update_pwq(wq, tcpu);
66765797b1c1STejun Heo
66775797b1c1STejun Heo mutex_lock(&wq->mutex);
66785797b1c1STejun Heo wq_update_node_max_active(wq, -1);
66795797b1c1STejun Heo mutex_unlock(&wq->mutex);
66804cbfd3deSTejun Heo }
66814cbfd3deSTejun Heo }
66824c16bd32STejun Heo
668368e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex);
66847ee681b2SThomas Gleixner return 0;
668565758202STejun Heo }
668665758202STejun Heo
workqueue_offline_cpu(unsigned int cpu)66877ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu)
668865758202STejun Heo {
66894c16bd32STejun Heo struct workqueue_struct *wq;
66908db25e78STejun Heo
66914c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */
6692e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id()))
6693e8b3f8dbSLai Jiangshan return -1;
6694e8b3f8dbSLai Jiangshan
6695e8b3f8dbSLai Jiangshan unbind_workers(cpu);
66964c16bd32STejun Heo
6697fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */
66984c16bd32STejun Heo mutex_lock(&wq_pool_mutex);
66998d84baf7SLai Jiangshan
67008d84baf7SLai Jiangshan cpumask_clear_cpu(cpu, wq_online_cpumask);
67018d84baf7SLai Jiangshan
67024cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) {
670384193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs;
670484193c07STejun Heo
670584193c07STejun Heo if (attrs) {
670684193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs);
67074cbfd3deSTejun Heo int tcpu;
67084cbfd3deSTejun Heo
670984193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]])
6710b2b1f933SLai Jiangshan unbound_wq_update_pwq(wq, tcpu);
67115797b1c1STejun Heo
67125797b1c1STejun Heo mutex_lock(&wq->mutex);
67135797b1c1STejun Heo wq_update_node_max_active(wq, cpu);
67145797b1c1STejun Heo mutex_unlock(&wq->mutex);
67154cbfd3deSTejun Heo }
67164cbfd3deSTejun Heo }
67174c16bd32STejun Heo mutex_unlock(&wq_pool_mutex);
67184c16bd32STejun Heo
67197ee681b2SThomas Gleixner return 0;
672065758202STejun Heo }
672165758202STejun Heo
67222d3854a3SRusty Russell struct work_for_cpu {
6723ed48ece2STejun Heo struct work_struct work;
67242d3854a3SRusty Russell long (*fn)(void *);
67252d3854a3SRusty Russell void *arg;
67262d3854a3SRusty Russell long ret;
67272d3854a3SRusty Russell };
67282d3854a3SRusty Russell
work_for_cpu_fn(struct work_struct * work)6729ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work)
67302d3854a3SRusty Russell {
6731ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
6732ed48ece2STejun Heo
67332d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg);
67342d3854a3SRusty Russell }
67352d3854a3SRusty Russell
67362d3854a3SRusty Russell /**
6737265f3ed0SFrederic Weisbecker * work_on_cpu_key - run a function in thread context on a particular cpu
67382d3854a3SRusty Russell * @cpu: the cpu to run on
67392d3854a3SRusty Russell * @fn: the function to run
67402d3854a3SRusty Russell * @arg: the function arg
6741265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes
67422d3854a3SRusty Russell *
674331ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline.
67446b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing.
6745d185af30SYacine Belkadi *
6746d185af30SYacine Belkadi * Return: The value @fn returns.
67472d3854a3SRusty Russell */
work_on_cpu_key(int cpu,long (* fn)(void *),void * arg,struct lock_class_key * key)6748265f3ed0SFrederic Weisbecker long work_on_cpu_key(int cpu, long (*fn)(void *),
6749265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key)
67502d3854a3SRusty Russell {
6751ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg };
67522d3854a3SRusty Russell
6753265f3ed0SFrederic Weisbecker INIT_WORK_ONSTACK_KEY(&wfc.work, work_for_cpu_fn, key);
6754ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work);
675512997d1aSBjorn Helgaas flush_work(&wfc.work);
6756440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work);
67572d3854a3SRusty Russell return wfc.ret;
67582d3854a3SRusty Russell }
6759265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_key);
67600e8d6a93SThomas Gleixner
67610e8d6a93SThomas Gleixner /**
6762265f3ed0SFrederic Weisbecker * work_on_cpu_safe_key - run a function in thread context on a particular cpu
67630e8d6a93SThomas Gleixner * @cpu: the cpu to run on
67640e8d6a93SThomas Gleixner * @fn: the function to run
67650e8d6a93SThomas Gleixner * @arg: the function argument
6766265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes
67670e8d6a93SThomas Gleixner *
67680e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold
67690e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing.
67700e8d6a93SThomas Gleixner *
67710e8d6a93SThomas Gleixner * Return: The value @fn returns.
67720e8d6a93SThomas Gleixner */
work_on_cpu_safe_key(int cpu,long (* fn)(void *),void * arg,struct lock_class_key * key)6773265f3ed0SFrederic Weisbecker long work_on_cpu_safe_key(int cpu, long (*fn)(void *),
6774265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key)
67750e8d6a93SThomas Gleixner {
67760e8d6a93SThomas Gleixner long ret = -ENODEV;
67770e8d6a93SThomas Gleixner
6778ffd8bea8SSebastian Andrzej Siewior cpus_read_lock();
67790e8d6a93SThomas Gleixner if (cpu_online(cpu))
6780265f3ed0SFrederic Weisbecker ret = work_on_cpu_key(cpu, fn, arg, key);
6781ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock();
67820e8d6a93SThomas Gleixner return ret;
67830e8d6a93SThomas Gleixner }
6784265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_safe_key);
67852d3854a3SRusty Russell #endif /* CONFIG_SMP */
67862d3854a3SRusty Russell
6787a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER
6788e7577c50SRusty Russell
6789a0a1a5fdSTejun Heo /**
6790a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues
6791a0a1a5fdSTejun Heo *
679258a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable
6793f97a4a1aSLai Jiangshan * workqueues will queue new works to their inactive_works list instead of
6794706026c2STejun Heo * pool->worklist.
6795a0a1a5fdSTejun Heo *
6796a0a1a5fdSTejun Heo * CONTEXT:
6797a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
6798a0a1a5fdSTejun Heo */
freeze_workqueues_begin(void)6799a0a1a5fdSTejun Heo void freeze_workqueues_begin(void)
6800a0a1a5fdSTejun Heo {
680124b8a847STejun Heo struct workqueue_struct *wq;
6802a0a1a5fdSTejun Heo
680368e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex);
6804a0a1a5fdSTejun Heo
68056183c009STejun Heo WARN_ON_ONCE(workqueue_freezing);
6806a0a1a5fdSTejun Heo workqueue_freezing = true;
6807a0a1a5fdSTejun Heo
680824b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) {
6809a357fc03SLai Jiangshan mutex_lock(&wq->mutex);
6810a045a272STejun Heo wq_adjust_max_active(wq);
6811a357fc03SLai Jiangshan mutex_unlock(&wq->mutex);
6812a1056305STejun Heo }
68135bcab335STejun Heo
681468e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex);
6815a0a1a5fdSTejun Heo }
6816a0a1a5fdSTejun Heo
6817a0a1a5fdSTejun Heo /**
681858a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy?
6819a0a1a5fdSTejun Heo *
6820a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called
6821a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues().
6822a0a1a5fdSTejun Heo *
6823a0a1a5fdSTejun Heo * CONTEXT:
682468e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex.
6825a0a1a5fdSTejun Heo *
6826d185af30SYacine Belkadi * Return:
682758a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing
682858a69cb4STejun Heo * is complete.
6829a0a1a5fdSTejun Heo */
freeze_workqueues_busy(void)6830a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void)
6831a0a1a5fdSTejun Heo {
6832a0a1a5fdSTejun Heo bool busy = false;
683324b8a847STejun Heo struct workqueue_struct *wq;
683424b8a847STejun Heo struct pool_workqueue *pwq;
6835a0a1a5fdSTejun Heo
683668e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex);
6837a0a1a5fdSTejun Heo
68386183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing);
6839a0a1a5fdSTejun Heo
684024b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) {
684124b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE))
684224b8a847STejun Heo continue;
6843a0a1a5fdSTejun Heo /*
6844a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe
6845a0a1a5fdSTejun Heo * to peek without lock.
6846a0a1a5fdSTejun Heo */
684724acfb71SThomas Gleixner rcu_read_lock();
684824b8a847STejun Heo for_each_pwq(pwq, wq) {
68496183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0);
6850112202d9STejun Heo if (pwq->nr_active) {
6851a0a1a5fdSTejun Heo busy = true;
685224acfb71SThomas Gleixner rcu_read_unlock();
6853a0a1a5fdSTejun Heo goto out_unlock;
6854a0a1a5fdSTejun Heo }
6855a0a1a5fdSTejun Heo }
685624acfb71SThomas Gleixner rcu_read_unlock();
6857a0a1a5fdSTejun Heo }
6858a0a1a5fdSTejun Heo out_unlock:
685968e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex);
6860a0a1a5fdSTejun Heo return busy;
6861a0a1a5fdSTejun Heo }
6862a0a1a5fdSTejun Heo
6863a0a1a5fdSTejun Heo /**
6864a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues
6865a0a1a5fdSTejun Heo *
6866a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected
6867706026c2STejun Heo * frozen works are transferred to their respective pool worklists.
6868a0a1a5fdSTejun Heo *
6869a0a1a5fdSTejun Heo * CONTEXT:
6870a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
6871a0a1a5fdSTejun Heo */
thaw_workqueues(void)6872a0a1a5fdSTejun Heo void thaw_workqueues(void)
6873a0a1a5fdSTejun Heo {
687424b8a847STejun Heo struct workqueue_struct *wq;
6875a0a1a5fdSTejun Heo
687668e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex);
6877a0a1a5fdSTejun Heo
6878a0a1a5fdSTejun Heo if (!workqueue_freezing)
6879a0a1a5fdSTejun Heo goto out_unlock;
6880a0a1a5fdSTejun Heo
688174b414eaSLai Jiangshan workqueue_freezing = false;
688224b8a847STejun Heo
688324b8a847STejun Heo /* restore max_active and repopulate worklist */
688424b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) {
6885a357fc03SLai Jiangshan mutex_lock(&wq->mutex);
6886a045a272STejun Heo wq_adjust_max_active(wq);
6887a357fc03SLai Jiangshan mutex_unlock(&wq->mutex);
688824b8a847STejun Heo }
688924b8a847STejun Heo
6890a0a1a5fdSTejun Heo out_unlock:
689168e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex);
6892a0a1a5fdSTejun Heo }
6893a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */
6894a0a1a5fdSTejun Heo
workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask)689599c621efSLai Jiangshan static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask)
6896042f7df1SLai Jiangshan {
6897042f7df1SLai Jiangshan LIST_HEAD(ctxs);
6898042f7df1SLai Jiangshan int ret = 0;
6899042f7df1SLai Jiangshan struct workqueue_struct *wq;
6900042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n;
6901042f7df1SLai Jiangshan
6902042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex);
6903042f7df1SLai Jiangshan
6904042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) {
69058eb17dc1SWaiman Long if (!(wq->flags & WQ_UNBOUND) || (wq->flags & __WQ_DESTROYING))
6906042f7df1SLai Jiangshan continue;
6907042f7df1SLai Jiangshan
690899c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask);
690984193c07STejun Heo if (IS_ERR(ctx)) {
691084193c07STejun Heo ret = PTR_ERR(ctx);
6911042f7df1SLai Jiangshan break;
6912042f7df1SLai Jiangshan }
6913042f7df1SLai Jiangshan
6914042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs);
6915042f7df1SLai Jiangshan }
6916042f7df1SLai Jiangshan
6917042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) {
6918042f7df1SLai Jiangshan if (!ret)
6919042f7df1SLai Jiangshan apply_wqattrs_commit(ctx);
6920042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx);
6921042f7df1SLai Jiangshan }
6922042f7df1SLai Jiangshan
692399c621efSLai Jiangshan if (!ret) {
692499c621efSLai Jiangshan mutex_lock(&wq_pool_attach_mutex);
692599c621efSLai Jiangshan cpumask_copy(wq_unbound_cpumask, unbound_cpumask);
692699c621efSLai Jiangshan mutex_unlock(&wq_pool_attach_mutex);
692799c621efSLai Jiangshan }
6928042f7df1SLai Jiangshan return ret;
6929042f7df1SLai Jiangshan }
6930042f7df1SLai Jiangshan
6931042f7df1SLai Jiangshan /**
6932fe28f631SWaiman Long * workqueue_unbound_exclude_cpumask - Exclude given CPUs from unbound cpumask
6933fe28f631SWaiman Long * @exclude_cpumask: the cpumask to be excluded from wq_unbound_cpumask
6934fe28f631SWaiman Long *
6935fe28f631SWaiman Long * This function can be called from cpuset code to provide a set of isolated
6936aa868475SLai Jiangshan * CPUs that should be excluded from wq_unbound_cpumask.
6937fe28f631SWaiman Long */
workqueue_unbound_exclude_cpumask(cpumask_var_t exclude_cpumask)6938fe28f631SWaiman Long int workqueue_unbound_exclude_cpumask(cpumask_var_t exclude_cpumask)
6939fe28f631SWaiman Long {
6940fe28f631SWaiman Long cpumask_var_t cpumask;
6941fe28f631SWaiman Long int ret = 0;
6942fe28f631SWaiman Long
6943fe28f631SWaiman Long if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL))
6944fe28f631SWaiman Long return -ENOMEM;
6945fe28f631SWaiman Long
6946fe28f631SWaiman Long mutex_lock(&wq_pool_mutex);
6947fe28f631SWaiman Long
6948fe28f631SWaiman Long /*
6949fe28f631SWaiman Long * If the operation fails, it will fall back to
6950fe28f631SWaiman Long * wq_requested_unbound_cpumask which is initially set to
6951fe28f631SWaiman Long * (HK_TYPE_WQ ∩ HK_TYPE_DOMAIN) house keeping mask and rewritten
6952fe28f631SWaiman Long * by any subsequent write to workqueue/cpumask sysfs file.
6953fe28f631SWaiman Long */
6954fe28f631SWaiman Long if (!cpumask_andnot(cpumask, wq_requested_unbound_cpumask, exclude_cpumask))
6955fe28f631SWaiman Long cpumask_copy(cpumask, wq_requested_unbound_cpumask);
6956fe28f631SWaiman Long if (!cpumask_equal(cpumask, wq_unbound_cpumask))
6957fe28f631SWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask);
6958fe28f631SWaiman Long
695984165883SLai Jiangshan /* Save the current isolated cpumask & export it via sysfs */
696084165883SLai Jiangshan if (!ret)
696184165883SLai Jiangshan cpumask_copy(wq_isolated_cpumask, exclude_cpumask);
696284165883SLai Jiangshan
6963fe28f631SWaiman Long mutex_unlock(&wq_pool_mutex);
6964fe28f631SWaiman Long free_cpumask_var(cpumask);
6965fe28f631SWaiman Long return ret;
6966fe28f631SWaiman Long }
6967fe28f631SWaiman Long
parse_affn_scope(const char * val)696863c5484eSTejun Heo static int parse_affn_scope(const char *val)
696963c5484eSTejun Heo {
697063c5484eSTejun Heo int i;
697163c5484eSTejun Heo
697263c5484eSTejun Heo for (i = 0; i < ARRAY_SIZE(wq_affn_names); i++) {
697363c5484eSTejun Heo if (!strncasecmp(val, wq_affn_names[i], strlen(wq_affn_names[i])))
697463c5484eSTejun Heo return i;
697563c5484eSTejun Heo }
697663c5484eSTejun Heo return -EINVAL;
697763c5484eSTejun Heo }
697863c5484eSTejun Heo
wq_affn_dfl_set(const char * val,const struct kernel_param * kp)697963c5484eSTejun Heo static int wq_affn_dfl_set(const char *val, const struct kernel_param *kp)
698063c5484eSTejun Heo {
6981523a301eSTejun Heo struct workqueue_struct *wq;
6982523a301eSTejun Heo int affn, cpu;
698363c5484eSTejun Heo
698463c5484eSTejun Heo affn = parse_affn_scope(val);
698563c5484eSTejun Heo if (affn < 0)
698663c5484eSTejun Heo return affn;
6987523a301eSTejun Heo if (affn == WQ_AFFN_DFL)
6988523a301eSTejun Heo return -EINVAL;
6989523a301eSTejun Heo
6990523a301eSTejun Heo cpus_read_lock();
6991523a301eSTejun Heo mutex_lock(&wq_pool_mutex);
699263c5484eSTejun Heo
699363c5484eSTejun Heo wq_affn_dfl = affn;
6994523a301eSTejun Heo
6995523a301eSTejun Heo list_for_each_entry(wq, &workqueues, list) {
6996b2b1f933SLai Jiangshan for_each_online_cpu(cpu)
6997b2b1f933SLai Jiangshan unbound_wq_update_pwq(wq, cpu);
6998523a301eSTejun Heo }
6999523a301eSTejun Heo
7000523a301eSTejun Heo mutex_unlock(&wq_pool_mutex);
7001523a301eSTejun Heo cpus_read_unlock();
7002523a301eSTejun Heo
700363c5484eSTejun Heo return 0;
700463c5484eSTejun Heo }
700563c5484eSTejun Heo
wq_affn_dfl_get(char * buffer,const struct kernel_param * kp)700663c5484eSTejun Heo static int wq_affn_dfl_get(char *buffer, const struct kernel_param *kp)
700763c5484eSTejun Heo {
700863c5484eSTejun Heo return scnprintf(buffer, PAGE_SIZE, "%s\n", wq_affn_names[wq_affn_dfl]);
700963c5484eSTejun Heo }
701063c5484eSTejun Heo
701163c5484eSTejun Heo static const struct kernel_param_ops wq_affn_dfl_ops = {
701263c5484eSTejun Heo .set = wq_affn_dfl_set,
701363c5484eSTejun Heo .get = wq_affn_dfl_get,
701463c5484eSTejun Heo };
701563c5484eSTejun Heo
701663c5484eSTejun Heo module_param_cb(default_affinity_scope, &wq_affn_dfl_ops, NULL, 0644);
701763c5484eSTejun Heo
70186ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS
70196ba94429SFrederic Weisbecker /*
70206ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via
70216ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
70226ba94429SFrederic Weisbecker * following attributes.
70236ba94429SFrederic Weisbecker *
70246ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound
70256ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items
70266ba94429SFrederic Weisbecker *
70276ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes.
70286ba94429SFrederic Weisbecker *
70296ba94429SFrederic Weisbecker * nice RW int : nice value of the workers
70306ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers
703163c5484eSTejun Heo * affinity_scope RW str : worker CPU affinity scope (cache, numa, none)
70328639ecebSTejun Heo * affinity_strict RW bool : worker CPU affinity is strict
70336ba94429SFrederic Weisbecker */
70346ba94429SFrederic Weisbecker struct wq_device {
70356ba94429SFrederic Weisbecker struct workqueue_struct *wq;
70366ba94429SFrederic Weisbecker struct device dev;
70376ba94429SFrederic Weisbecker };
70386ba94429SFrederic Weisbecker
dev_to_wq(struct device * dev)70396ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev)
70406ba94429SFrederic Weisbecker {
70416ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
70426ba94429SFrederic Weisbecker
70436ba94429SFrederic Weisbecker return wq_dev->wq;
70446ba94429SFrederic Weisbecker }
70456ba94429SFrederic Weisbecker
per_cpu_show(struct device * dev,struct device_attribute * attr,char * buf)70466ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr,
70476ba94429SFrederic Weisbecker char *buf)
70486ba94429SFrederic Weisbecker {
70496ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev);
70506ba94429SFrederic Weisbecker
70516ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
70526ba94429SFrederic Weisbecker }
70536ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu);
70546ba94429SFrederic Weisbecker
max_active_show(struct device * dev,struct device_attribute * attr,char * buf)70556ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev,
70566ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf)
70576ba94429SFrederic Weisbecker {
70586ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev);
70596ba94429SFrederic Weisbecker
70606ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
70616ba94429SFrederic Weisbecker }
70626ba94429SFrederic Weisbecker
max_active_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)70636ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev,
70646ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf,
70656ba94429SFrederic Weisbecker size_t count)
70666ba94429SFrederic Weisbecker {
70676ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev);
70686ba94429SFrederic Weisbecker int val;
70696ba94429SFrederic Weisbecker
70706ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0)
70716ba94429SFrederic Weisbecker return -EINVAL;
70726ba94429SFrederic Weisbecker
70736ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val);
70746ba94429SFrederic Weisbecker return count;
70756ba94429SFrederic Weisbecker }
70766ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active);
70776ba94429SFrederic Weisbecker
70786ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = {
70796ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr,
70806ba94429SFrederic Weisbecker &dev_attr_max_active.attr,
70816ba94429SFrederic Weisbecker NULL,
70826ba94429SFrederic Weisbecker };
70836ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs);
70846ba94429SFrederic Weisbecker
wq_nice_show(struct device * dev,struct device_attribute * attr,char * buf)70856ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
70866ba94429SFrederic Weisbecker char *buf)
70876ba94429SFrederic Weisbecker {
70886ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev);
70896ba94429SFrederic Weisbecker int written;
70906ba94429SFrederic Weisbecker
70916ba94429SFrederic Weisbecker mutex_lock(&wq->mutex);
70926ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice);
70936ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex);
70946ba94429SFrederic Weisbecker
70956ba94429SFrederic Weisbecker return written;
70966ba94429SFrederic Weisbecker }
70976ba94429SFrederic Weisbecker
70986ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */
wq_sysfs_prep_attrs(struct workqueue_struct * wq)70996ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
71006ba94429SFrederic Weisbecker {
71016ba94429SFrederic Weisbecker struct workqueue_attrs *attrs;
71026ba94429SFrederic Weisbecker
7103899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex);
7104899a94feSLai Jiangshan
7105be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs();
71066ba94429SFrederic Weisbecker if (!attrs)
71076ba94429SFrederic Weisbecker return NULL;
71086ba94429SFrederic Weisbecker
71096ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs);
71106ba94429SFrederic Weisbecker return attrs;
71116ba94429SFrederic Weisbecker }
71126ba94429SFrederic Weisbecker
wq_nice_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)71136ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
71146ba94429SFrederic Weisbecker const char *buf, size_t count)
71156ba94429SFrederic Weisbecker {
71166ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev);
71176ba94429SFrederic Weisbecker struct workqueue_attrs *attrs;
7118d4d3e257SLai Jiangshan int ret = -ENOMEM;
7119d4d3e257SLai Jiangshan
7120d4d3e257SLai Jiangshan apply_wqattrs_lock();
71216ba94429SFrederic Weisbecker
71226ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq);
71236ba94429SFrederic Weisbecker if (!attrs)
7124d4d3e257SLai Jiangshan goto out_unlock;
71256ba94429SFrederic Weisbecker
71266ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 &&
71276ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE)
7128d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs);
71296ba94429SFrederic Weisbecker else
71306ba94429SFrederic Weisbecker ret = -EINVAL;
71316ba94429SFrederic Weisbecker
7132d4d3e257SLai Jiangshan out_unlock:
7133d4d3e257SLai Jiangshan apply_wqattrs_unlock();
71346ba94429SFrederic Weisbecker free_workqueue_attrs(attrs);
71356ba94429SFrederic Weisbecker return ret ?: count;
71366ba94429SFrederic Weisbecker }
71376ba94429SFrederic Weisbecker
wq_cpumask_show(struct device * dev,struct device_attribute * attr,char * buf)71386ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev,
71396ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf)
71406ba94429SFrederic Weisbecker {
71416ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev);
71426ba94429SFrederic Weisbecker int written;
71436ba94429SFrederic Weisbecker
71446ba94429SFrederic Weisbecker mutex_lock(&wq->mutex);
71456ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n",
71466ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask));
71476ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex);
71486ba94429SFrederic Weisbecker return written;
71496ba94429SFrederic Weisbecker }
71506ba94429SFrederic Weisbecker
wq_cpumask_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)71516ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev,
71526ba94429SFrederic Weisbecker struct device_attribute *attr,
71536ba94429SFrederic Weisbecker const char *buf, size_t count)
71546ba94429SFrederic Weisbecker {
71556ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev);
71566ba94429SFrederic Weisbecker struct workqueue_attrs *attrs;
7157d4d3e257SLai Jiangshan int ret = -ENOMEM;
7158d4d3e257SLai Jiangshan
7159d4d3e257SLai Jiangshan apply_wqattrs_lock();
71606ba94429SFrederic Weisbecker
71616ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq);
71626ba94429SFrederic Weisbecker if (!attrs)
7163d4d3e257SLai Jiangshan goto out_unlock;
71646ba94429SFrederic Weisbecker
71656ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask);
71666ba94429SFrederic Weisbecker if (!ret)
7167d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs);
71686ba94429SFrederic Weisbecker
7169d4d3e257SLai Jiangshan out_unlock:
7170d4d3e257SLai Jiangshan apply_wqattrs_unlock();
71716ba94429SFrederic Weisbecker free_workqueue_attrs(attrs);
71726ba94429SFrederic Weisbecker return ret ?: count;
71736ba94429SFrederic Weisbecker }
71746ba94429SFrederic Weisbecker
wq_affn_scope_show(struct device * dev,struct device_attribute * attr,char * buf)717563c5484eSTejun Heo static ssize_t wq_affn_scope_show(struct device *dev,
717663c5484eSTejun Heo struct device_attribute *attr, char *buf)
717763c5484eSTejun Heo {
717863c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev);
717963c5484eSTejun Heo int written;
718063c5484eSTejun Heo
718163c5484eSTejun Heo mutex_lock(&wq->mutex);
7182523a301eSTejun Heo if (wq->unbound_attrs->affn_scope == WQ_AFFN_DFL)
7183523a301eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s (%s)\n",
7184523a301eSTejun Heo wq_affn_names[WQ_AFFN_DFL],
7185523a301eSTejun Heo wq_affn_names[wq_affn_dfl]);
7186523a301eSTejun Heo else
718763c5484eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s\n",
718863c5484eSTejun Heo wq_affn_names[wq->unbound_attrs->affn_scope]);
718963c5484eSTejun Heo mutex_unlock(&wq->mutex);
719063c5484eSTejun Heo
719163c5484eSTejun Heo return written;
719263c5484eSTejun Heo }
719363c5484eSTejun Heo
wq_affn_scope_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)719463c5484eSTejun Heo static ssize_t wq_affn_scope_store(struct device *dev,
719563c5484eSTejun Heo struct device_attribute *attr,
719663c5484eSTejun Heo const char *buf, size_t count)
719763c5484eSTejun Heo {
719863c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev);
719963c5484eSTejun Heo struct workqueue_attrs *attrs;
720063c5484eSTejun Heo int affn, ret = -ENOMEM;
720163c5484eSTejun Heo
720263c5484eSTejun Heo affn = parse_affn_scope(buf);
720363c5484eSTejun Heo if (affn < 0)
720463c5484eSTejun Heo return affn;
720563c5484eSTejun Heo
720663c5484eSTejun Heo apply_wqattrs_lock();
720763c5484eSTejun Heo attrs = wq_sysfs_prep_attrs(wq);
720863c5484eSTejun Heo if (attrs) {
720963c5484eSTejun Heo attrs->affn_scope = affn;
721063c5484eSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs);
721163c5484eSTejun Heo }
721263c5484eSTejun Heo apply_wqattrs_unlock();
721363c5484eSTejun Heo free_workqueue_attrs(attrs);
721463c5484eSTejun Heo return ret ?: count;
721563c5484eSTejun Heo }
721663c5484eSTejun Heo
wq_affinity_strict_show(struct device * dev,struct device_attribute * attr,char * buf)72178639ecebSTejun Heo static ssize_t wq_affinity_strict_show(struct device *dev,
72188639ecebSTejun Heo struct device_attribute *attr, char *buf)
72198639ecebSTejun Heo {
72208639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev);
72218639ecebSTejun Heo
72228639ecebSTejun Heo return scnprintf(buf, PAGE_SIZE, "%d\n",
72238639ecebSTejun Heo wq->unbound_attrs->affn_strict);
72248639ecebSTejun Heo }
72258639ecebSTejun Heo
wq_affinity_strict_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)72268639ecebSTejun Heo static ssize_t wq_affinity_strict_store(struct device *dev,
72278639ecebSTejun Heo struct device_attribute *attr,
72288639ecebSTejun Heo const char *buf, size_t count)
72298639ecebSTejun Heo {
72308639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev);
72318639ecebSTejun Heo struct workqueue_attrs *attrs;
72328639ecebSTejun Heo int v, ret = -ENOMEM;
72338639ecebSTejun Heo
72348639ecebSTejun Heo if (sscanf(buf, "%d", &v) != 1)
72358639ecebSTejun Heo return -EINVAL;
72368639ecebSTejun Heo
72378639ecebSTejun Heo apply_wqattrs_lock();
72388639ecebSTejun Heo attrs = wq_sysfs_prep_attrs(wq);
72398639ecebSTejun Heo if (attrs) {
72408639ecebSTejun Heo attrs->affn_strict = (bool)v;
72418639ecebSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs);
72428639ecebSTejun Heo }
72438639ecebSTejun Heo apply_wqattrs_unlock();
72448639ecebSTejun Heo free_workqueue_attrs(attrs);
72458639ecebSTejun Heo return ret ?: count;
72468639ecebSTejun Heo }
72478639ecebSTejun Heo
72486ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = {
72496ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
72506ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
725163c5484eSTejun Heo __ATTR(affinity_scope, 0644, wq_affn_scope_show, wq_affn_scope_store),
72528639ecebSTejun Heo __ATTR(affinity_strict, 0644, wq_affinity_strict_show, wq_affinity_strict_store),
72536ba94429SFrederic Weisbecker __ATTR_NULL,
72546ba94429SFrederic Weisbecker };
72556ba94429SFrederic Weisbecker
72565df9197eSRicardo B. Marliere static const struct bus_type wq_subsys = {
72576ba94429SFrederic Weisbecker .name = "workqueue",
72586ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups,
72596ba94429SFrederic Weisbecker };
72606ba94429SFrederic Weisbecker
726149277a5bSWaiman Long /**
726249277a5bSWaiman Long * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask
726349277a5bSWaiman Long * @cpumask: the cpumask to set
726449277a5bSWaiman Long *
726549277a5bSWaiman Long * The low-level workqueues cpumask is a global cpumask that limits
726649277a5bSWaiman Long * the affinity of all unbound workqueues. This function check the @cpumask
726749277a5bSWaiman Long * and apply it to all unbound workqueues and updates all pwqs of them.
726849277a5bSWaiman Long *
726949277a5bSWaiman Long * Return: 0 - Success
727049277a5bSWaiman Long * -EINVAL - Invalid @cpumask
727149277a5bSWaiman Long * -ENOMEM - Failed to allocate memory for attrs or pwqs.
727249277a5bSWaiman Long */
workqueue_set_unbound_cpumask(cpumask_var_t cpumask)727349277a5bSWaiman Long static int workqueue_set_unbound_cpumask(cpumask_var_t cpumask)
727449277a5bSWaiman Long {
727549277a5bSWaiman Long int ret = -EINVAL;
727649277a5bSWaiman Long
727749277a5bSWaiman Long /*
727849277a5bSWaiman Long * Not excluding isolated cpus on purpose.
727949277a5bSWaiman Long * If the user wishes to include them, we allow that.
728049277a5bSWaiman Long */
728149277a5bSWaiman Long cpumask_and(cpumask, cpumask, cpu_possible_mask);
728249277a5bSWaiman Long if (!cpumask_empty(cpumask)) {
728349277a5bSWaiman Long ret = 0;
7284b3d20916SLai Jiangshan apply_wqattrs_lock();
7285b3d20916SLai Jiangshan if (!cpumask_equal(cpumask, wq_unbound_cpumask))
728649277a5bSWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask);
728784165883SLai Jiangshan if (!ret)
728884165883SLai Jiangshan cpumask_copy(wq_requested_unbound_cpumask, cpumask);
728949277a5bSWaiman Long apply_wqattrs_unlock();
729049277a5bSWaiman Long }
729149277a5bSWaiman Long
729249277a5bSWaiman Long return ret;
729349277a5bSWaiman Long }
729449277a5bSWaiman Long
__wq_cpumask_show(struct device * dev,struct device_attribute * attr,char * buf,cpumask_var_t mask)7295fe28f631SWaiman Long static ssize_t __wq_cpumask_show(struct device *dev,
7296fe28f631SWaiman Long struct device_attribute *attr, char *buf, cpumask_var_t mask)
7297b05a7928SFrederic Weisbecker {
7298b05a7928SFrederic Weisbecker int written;
7299b05a7928SFrederic Weisbecker
7300042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex);
7301fe28f631SWaiman Long written = scnprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
7302042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex);
7303b05a7928SFrederic Weisbecker
7304b05a7928SFrederic Weisbecker return written;
7305b05a7928SFrederic Weisbecker }
7306b05a7928SFrederic Weisbecker
cpumask_requested_show(struct device * dev,struct device_attribute * attr,char * buf)730779202591SDan Williams static ssize_t cpumask_requested_show(struct device *dev,
730879202591SDan Williams struct device_attribute *attr, char *buf)
730979202591SDan Williams {
731079202591SDan Williams return __wq_cpumask_show(dev, attr, buf, wq_requested_unbound_cpumask);
731179202591SDan Williams }
731279202591SDan Williams static DEVICE_ATTR_RO(cpumask_requested);
731379202591SDan Williams
cpumask_isolated_show(struct device * dev,struct device_attribute * attr,char * buf)731479202591SDan Williams static ssize_t cpumask_isolated_show(struct device *dev,
731579202591SDan Williams struct device_attribute *attr, char *buf)
731679202591SDan Williams {
731779202591SDan Williams return __wq_cpumask_show(dev, attr, buf, wq_isolated_cpumask);
731879202591SDan Williams }
731979202591SDan Williams static DEVICE_ATTR_RO(cpumask_isolated);
732079202591SDan Williams
cpumask_show(struct device * dev,struct device_attribute * attr,char * buf)732179202591SDan Williams static ssize_t cpumask_show(struct device *dev,
7322fe28f631SWaiman Long struct device_attribute *attr, char *buf)
7323fe28f631SWaiman Long {
7324fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_unbound_cpumask);
7325fe28f631SWaiman Long }
7326fe28f631SWaiman Long
cpumask_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)732779202591SDan Williams static ssize_t cpumask_store(struct device *dev,
7328042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count)
7329042f7df1SLai Jiangshan {
7330042f7df1SLai Jiangshan cpumask_var_t cpumask;
7331042f7df1SLai Jiangshan int ret;
7332042f7df1SLai Jiangshan
7333042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL))
7334042f7df1SLai Jiangshan return -ENOMEM;
7335042f7df1SLai Jiangshan
7336042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask);
7337042f7df1SLai Jiangshan if (!ret)
7338042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask);
7339042f7df1SLai Jiangshan
7340042f7df1SLai Jiangshan free_cpumask_var(cpumask);
7341042f7df1SLai Jiangshan return ret ? ret : count;
7342042f7df1SLai Jiangshan }
734379202591SDan Williams static DEVICE_ATTR_RW(cpumask);
7344042f7df1SLai Jiangshan
734579202591SDan Williams static struct attribute *wq_sysfs_cpumask_attrs[] = {
734679202591SDan Williams &dev_attr_cpumask.attr,
734779202591SDan Williams &dev_attr_cpumask_requested.attr,
734879202591SDan Williams &dev_attr_cpumask_isolated.attr,
734979202591SDan Williams NULL,
7350fe28f631SWaiman Long };
735179202591SDan Williams ATTRIBUTE_GROUPS(wq_sysfs_cpumask);
7352b05a7928SFrederic Weisbecker
wq_sysfs_init(void)73536ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void)
73546ba94429SFrederic Weisbecker {
735579202591SDan Williams return subsys_virtual_register(&wq_subsys, wq_sysfs_cpumask_groups);
73566ba94429SFrederic Weisbecker }
73576ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init);
73586ba94429SFrederic Weisbecker
wq_device_release(struct device * dev)73596ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev)
73606ba94429SFrederic Weisbecker {
73616ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
73626ba94429SFrederic Weisbecker
73636ba94429SFrederic Weisbecker kfree(wq_dev);
73646ba94429SFrederic Weisbecker }
73656ba94429SFrederic Weisbecker
73666ba94429SFrederic Weisbecker /**
73676ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs
73686ba94429SFrederic Weisbecker * @wq: the workqueue to register
73696ba94429SFrederic Weisbecker *
73706ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices.
73716ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
73726ba94429SFrederic Weisbecker * which is the preferred method.
73736ba94429SFrederic Weisbecker *
73746ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply
73756ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
73766ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the
73776ba94429SFrederic Weisbecker * attributes.
73786ba94429SFrederic Weisbecker *
73796ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure.
73806ba94429SFrederic Weisbecker */
workqueue_sysfs_register(struct workqueue_struct * wq)73816ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq)
73826ba94429SFrederic Weisbecker {
73836ba94429SFrederic Weisbecker struct wq_device *wq_dev;
73846ba94429SFrederic Weisbecker int ret;
73856ba94429SFrederic Weisbecker
73866ba94429SFrederic Weisbecker /*
73874c065dbcSWaiman Long * Adjusting max_active breaks ordering guarantee. Disallow exposing
73884c065dbcSWaiman Long * ordered workqueues.
73896ba94429SFrederic Weisbecker */
73903bc1e711STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED))
73916ba94429SFrederic Weisbecker return -EINVAL;
73926ba94429SFrederic Weisbecker
73936ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
73946ba94429SFrederic Weisbecker if (!wq_dev)
73956ba94429SFrederic Weisbecker return -ENOMEM;
73966ba94429SFrederic Weisbecker
73976ba94429SFrederic Weisbecker wq_dev->wq = wq;
73986ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys;
73996ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release;
740023217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name);
74016ba94429SFrederic Weisbecker
74026ba94429SFrederic Weisbecker /*
74036ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until
74046ba94429SFrederic Weisbecker * everything is ready.
74056ba94429SFrederic Weisbecker */
74066ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true);
74076ba94429SFrederic Weisbecker
74086ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev);
74096ba94429SFrederic Weisbecker if (ret) {
7410537f4146SArvind Yadav put_device(&wq_dev->dev);
74116ba94429SFrederic Weisbecker wq->wq_dev = NULL;
74126ba94429SFrederic Weisbecker return ret;
74136ba94429SFrederic Weisbecker }
74146ba94429SFrederic Weisbecker
74156ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) {
74166ba94429SFrederic Weisbecker struct device_attribute *attr;
74176ba94429SFrederic Weisbecker
74186ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
74196ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr);
74206ba94429SFrederic Weisbecker if (ret) {
74216ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev);
74226ba94429SFrederic Weisbecker wq->wq_dev = NULL;
74236ba94429SFrederic Weisbecker return ret;
74246ba94429SFrederic Weisbecker }
74256ba94429SFrederic Weisbecker }
74266ba94429SFrederic Weisbecker }
74276ba94429SFrederic Weisbecker
74286ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false);
74296ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
74306ba94429SFrederic Weisbecker return 0;
74316ba94429SFrederic Weisbecker }
74326ba94429SFrederic Weisbecker
74336ba94429SFrederic Weisbecker /**
74346ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
74356ba94429SFrederic Weisbecker * @wq: the workqueue to unregister
74366ba94429SFrederic Weisbecker *
74376ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
74386ba94429SFrederic Weisbecker */
workqueue_sysfs_unregister(struct workqueue_struct * wq)74396ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
74406ba94429SFrederic Weisbecker {
74416ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev;
74426ba94429SFrederic Weisbecker
74436ba94429SFrederic Weisbecker if (!wq->wq_dev)
74446ba94429SFrederic Weisbecker return;
74456ba94429SFrederic Weisbecker
74466ba94429SFrederic Weisbecker wq->wq_dev = NULL;
74476ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev);
74486ba94429SFrederic Weisbecker }
74496ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */
workqueue_sysfs_unregister(struct workqueue_struct * wq)74506ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
74516ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */
74526ba94429SFrederic Weisbecker
745382607adcSTejun Heo /*
745482607adcSTejun Heo * Workqueue watchdog.
745582607adcSTejun Heo *
745682607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal
745782607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING
745882607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the
745982607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is
746082607adcSTejun Heo * largely opaque.
746182607adcSTejun Heo *
746282607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps
746382607adcSTejun Heo * state if some pools failed to make forward progress for a while where
746482607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing.
746582607adcSTejun Heo *
746682607adcSTejun Heo * This mechanism is controlled through the kernel parameter
746782607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the
746882607adcSTejun Heo * corresponding sysfs parameter file.
746982607adcSTejun Heo */
747082607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG
747182607adcSTejun Heo
747282607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30;
74735cd79d6aSKees Cook static struct timer_list wq_watchdog_timer;
747482607adcSTejun Heo
747582607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES;
747682607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES;
747782607adcSTejun Heo
7478073107b3SSangmoon Kim static unsigned int wq_panic_on_stall;
7479073107b3SSangmoon Kim module_param_named(panic_on_stall, wq_panic_on_stall, uint, 0644);
7480073107b3SSangmoon Kim
7481cd2440d6SPetr Mladek /*
7482cd2440d6SPetr Mladek * Show workers that might prevent the processing of pending work items.
7483cd2440d6SPetr Mladek * The only candidates are CPU-bound workers in the running state.
7484cd2440d6SPetr Mladek * Pending work items should be handled by another idle worker
7485cd2440d6SPetr Mladek * in all other situations.
7486cd2440d6SPetr Mladek */
show_cpu_pool_hog(struct worker_pool * pool)7487cd2440d6SPetr Mladek static void show_cpu_pool_hog(struct worker_pool *pool)
7488cd2440d6SPetr Mladek {
7489cd2440d6SPetr Mladek struct worker *worker;
7490c26e2f2eSTejun Heo unsigned long irq_flags;
7491cd2440d6SPetr Mladek int bkt;
7492cd2440d6SPetr Mladek
7493c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags);
7494cd2440d6SPetr Mladek
7495cd2440d6SPetr Mladek hash_for_each(pool->busy_hash, bkt, worker, hentry) {
7496cd2440d6SPetr Mladek if (task_is_running(worker->task)) {
7497cd2440d6SPetr Mladek /*
7498cd2440d6SPetr Mladek * Defer printing to avoid deadlocks in console
7499cd2440d6SPetr Mladek * drivers that queue work while holding locks
7500cd2440d6SPetr Mladek * also taken in their write paths.
7501cd2440d6SPetr Mladek */
7502cd2440d6SPetr Mladek printk_deferred_enter();
7503cd2440d6SPetr Mladek
7504cd2440d6SPetr Mladek pr_info("pool %d:\n", pool->id);
7505cd2440d6SPetr Mladek sched_show_task(worker->task);
7506cd2440d6SPetr Mladek
7507cd2440d6SPetr Mladek printk_deferred_exit();
7508cd2440d6SPetr Mladek }
7509cd2440d6SPetr Mladek }
7510cd2440d6SPetr Mladek
7511c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags);
7512cd2440d6SPetr Mladek }
7513cd2440d6SPetr Mladek
show_cpu_pools_hogs(void)7514cd2440d6SPetr Mladek static void show_cpu_pools_hogs(void)
7515cd2440d6SPetr Mladek {
7516cd2440d6SPetr Mladek struct worker_pool *pool;
7517cd2440d6SPetr Mladek int pi;
7518cd2440d6SPetr Mladek
7519cd2440d6SPetr Mladek pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n");
7520cd2440d6SPetr Mladek
7521cd2440d6SPetr Mladek rcu_read_lock();
7522cd2440d6SPetr Mladek
7523cd2440d6SPetr Mladek for_each_pool(pool, pi) {
7524cd2440d6SPetr Mladek if (pool->cpu_stall)
7525cd2440d6SPetr Mladek show_cpu_pool_hog(pool);
7526cd2440d6SPetr Mladek
7527cd2440d6SPetr Mladek }
7528cd2440d6SPetr Mladek
7529cd2440d6SPetr Mladek rcu_read_unlock();
7530cd2440d6SPetr Mladek }
7531cd2440d6SPetr Mladek
panic_on_wq_watchdog(void)7532073107b3SSangmoon Kim static void panic_on_wq_watchdog(void)
7533073107b3SSangmoon Kim {
7534073107b3SSangmoon Kim static unsigned int wq_stall;
7535073107b3SSangmoon Kim
7536073107b3SSangmoon Kim if (wq_panic_on_stall) {
7537073107b3SSangmoon Kim wq_stall++;
7538073107b3SSangmoon Kim BUG_ON(wq_stall >= wq_panic_on_stall);
7539073107b3SSangmoon Kim }
7540073107b3SSangmoon Kim }
7541073107b3SSangmoon Kim
wq_watchdog_reset_touched(void)754282607adcSTejun Heo static void wq_watchdog_reset_touched(void)
754382607adcSTejun Heo {
754482607adcSTejun Heo int cpu;
754582607adcSTejun Heo
754682607adcSTejun Heo wq_watchdog_touched = jiffies;
754782607adcSTejun Heo for_each_possible_cpu(cpu)
754882607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies;
754982607adcSTejun Heo }
755082607adcSTejun Heo
wq_watchdog_timer_fn(struct timer_list * unused)75515cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused)
755282607adcSTejun Heo {
755382607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ;
755482607adcSTejun Heo bool lockup_detected = false;
7555cd2440d6SPetr Mladek bool cpu_pool_stall = false;
7556940d71c6SSergey Senozhatsky unsigned long now = jiffies;
755782607adcSTejun Heo struct worker_pool *pool;
755882607adcSTejun Heo int pi;
755982607adcSTejun Heo
756082607adcSTejun Heo if (!thresh)
756182607adcSTejun Heo return;
756282607adcSTejun Heo
756382607adcSTejun Heo rcu_read_lock();
756482607adcSTejun Heo
756582607adcSTejun Heo for_each_pool(pool, pi) {
756682607adcSTejun Heo unsigned long pool_ts, touched, ts;
756782607adcSTejun Heo
7568cd2440d6SPetr Mladek pool->cpu_stall = false;
756982607adcSTejun Heo if (list_empty(&pool->worklist))
757082607adcSTejun Heo continue;
757182607adcSTejun Heo
7572940d71c6SSergey Senozhatsky /*
7573940d71c6SSergey Senozhatsky * If a virtual machine is stopped by the host it can look to
7574940d71c6SSergey Senozhatsky * the watchdog like a stall.
7575940d71c6SSergey Senozhatsky */
7576940d71c6SSergey Senozhatsky kvm_check_and_clear_guest_paused();
7577940d71c6SSergey Senozhatsky
757882607adcSTejun Heo /* get the latest of pool and touched timestamps */
757989e28ce6SWang Qing if (pool->cpu >= 0)
758089e28ce6SWang Qing touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu));
758189e28ce6SWang Qing else
758282607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched);
758389e28ce6SWang Qing pool_ts = READ_ONCE(pool->watchdog_ts);
758482607adcSTejun Heo
758582607adcSTejun Heo if (time_after(pool_ts, touched))
758682607adcSTejun Heo ts = pool_ts;
758782607adcSTejun Heo else
758882607adcSTejun Heo ts = touched;
758982607adcSTejun Heo
759082607adcSTejun Heo /* did we stall? */
7591940d71c6SSergey Senozhatsky if (time_after(now, ts + thresh)) {
759282607adcSTejun Heo lockup_detected = true;
75934cb1ef64STejun Heo if (pool->cpu >= 0 && !(pool->flags & POOL_BH)) {
7594cd2440d6SPetr Mladek pool->cpu_stall = true;
7595cd2440d6SPetr Mladek cpu_pool_stall = true;
7596cd2440d6SPetr Mladek }
759782607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool");
759882607adcSTejun Heo pr_cont_pool_info(pool);
759982607adcSTejun Heo pr_cont(" stuck for %us!\n",
7600940d71c6SSergey Senozhatsky jiffies_to_msecs(now - pool_ts) / 1000);
760182607adcSTejun Heo }
7602cd2440d6SPetr Mladek
7603cd2440d6SPetr Mladek
760482607adcSTejun Heo }
760582607adcSTejun Heo
760682607adcSTejun Heo rcu_read_unlock();
760782607adcSTejun Heo
760882607adcSTejun Heo if (lockup_detected)
760955df0933SImran Khan show_all_workqueues();
761082607adcSTejun Heo
7611cd2440d6SPetr Mladek if (cpu_pool_stall)
7612cd2440d6SPetr Mladek show_cpu_pools_hogs();
7613cd2440d6SPetr Mladek
7614073107b3SSangmoon Kim if (lockup_detected)
7615073107b3SSangmoon Kim panic_on_wq_watchdog();
7616073107b3SSangmoon Kim
761782607adcSTejun Heo wq_watchdog_reset_touched();
761882607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh);
761982607adcSTejun Heo }
762082607adcSTejun Heo
wq_watchdog_touch(int cpu)7621cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu)
762282607adcSTejun Heo {
762398f887f8SNicholas Piggin unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ;
762498f887f8SNicholas Piggin unsigned long touch_ts = READ_ONCE(wq_watchdog_touched);
762598f887f8SNicholas Piggin unsigned long now = jiffies;
762689e28ce6SWang Qing
762782607adcSTejun Heo if (cpu >= 0)
762898f887f8SNicholas Piggin per_cpu(wq_watchdog_touched_cpu, cpu) = now;
762918e24debSNicholas Piggin else
763018e24debSNicholas Piggin WARN_ONCE(1, "%s should be called with valid CPU", __func__);
763182607adcSTejun Heo
763298f887f8SNicholas Piggin /* Don't unnecessarily store to global cacheline */
763398f887f8SNicholas Piggin if (time_after(now, touch_ts + thresh / 4))
763498f887f8SNicholas Piggin WRITE_ONCE(wq_watchdog_touched, jiffies);
763582607adcSTejun Heo }
763682607adcSTejun Heo
wq_watchdog_set_thresh(unsigned long thresh)763782607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh)
763882607adcSTejun Heo {
763982607adcSTejun Heo wq_watchdog_thresh = 0;
7640*8fa7292fSThomas Gleixner timer_delete_sync(&wq_watchdog_timer);
764182607adcSTejun Heo
764282607adcSTejun Heo if (thresh) {
764382607adcSTejun Heo wq_watchdog_thresh = thresh;
764482607adcSTejun Heo wq_watchdog_reset_touched();
764582607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ);
764682607adcSTejun Heo }
764782607adcSTejun Heo }
764882607adcSTejun Heo
wq_watchdog_param_set_thresh(const char * val,const struct kernel_param * kp)764982607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val,
765082607adcSTejun Heo const struct kernel_param *kp)
765182607adcSTejun Heo {
765282607adcSTejun Heo unsigned long thresh;
765382607adcSTejun Heo int ret;
765482607adcSTejun Heo
765582607adcSTejun Heo ret = kstrtoul(val, 0, &thresh);
765682607adcSTejun Heo if (ret)
765782607adcSTejun Heo return ret;
765882607adcSTejun Heo
765982607adcSTejun Heo if (system_wq)
766082607adcSTejun Heo wq_watchdog_set_thresh(thresh);
766182607adcSTejun Heo else
766282607adcSTejun Heo wq_watchdog_thresh = thresh;
766382607adcSTejun Heo
766482607adcSTejun Heo return 0;
766582607adcSTejun Heo }
766682607adcSTejun Heo
766782607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = {
766882607adcSTejun Heo .set = wq_watchdog_param_set_thresh,
766982607adcSTejun Heo .get = param_get_ulong,
767082607adcSTejun Heo };
767182607adcSTejun Heo
767282607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh,
767382607adcSTejun Heo 0644);
767482607adcSTejun Heo
wq_watchdog_init(void)767582607adcSTejun Heo static void wq_watchdog_init(void)
767682607adcSTejun Heo {
76775cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE);
767882607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh);
767982607adcSTejun Heo }
768082607adcSTejun Heo
768182607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */
768282607adcSTejun Heo
wq_watchdog_init(void)768382607adcSTejun Heo static inline void wq_watchdog_init(void) { }
768482607adcSTejun Heo
768582607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */
768682607adcSTejun Heo
bh_pool_kick_normal(struct irq_work * irq_work)76872f34d733STejun Heo static void bh_pool_kick_normal(struct irq_work *irq_work)
76882f34d733STejun Heo {
76892f34d733STejun Heo raise_softirq_irqoff(TASKLET_SOFTIRQ);
76902f34d733STejun Heo }
76912f34d733STejun Heo
bh_pool_kick_highpri(struct irq_work * irq_work)76922f34d733STejun Heo static void bh_pool_kick_highpri(struct irq_work *irq_work)
76932f34d733STejun Heo {
76942f34d733STejun Heo raise_softirq_irqoff(HI_SOFTIRQ);
76952f34d733STejun Heo }
76962f34d733STejun Heo
restrict_unbound_cpumask(const char * name,const struct cpumask * mask)76974a6c5607STejun Heo static void __init restrict_unbound_cpumask(const char *name, const struct cpumask *mask)
76984a6c5607STejun Heo {
76994a6c5607STejun Heo if (!cpumask_intersects(wq_unbound_cpumask, mask)) {
77004a6c5607STejun Heo pr_warn("workqueue: Restricting unbound_cpumask (%*pb) with %s (%*pb) leaves no CPU, ignoring\n",
77014a6c5607STejun Heo cpumask_pr_args(wq_unbound_cpumask), name, cpumask_pr_args(mask));
77024a6c5607STejun Heo return;
77034a6c5607STejun Heo }
77044a6c5607STejun Heo
77054a6c5607STejun Heo cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, mask);
77064a6c5607STejun Heo }
77074a6c5607STejun Heo
init_cpu_worker_pool(struct worker_pool * pool,int cpu,int nice)77082fcdb1b4STejun Heo static void __init init_cpu_worker_pool(struct worker_pool *pool, int cpu, int nice)
77092fcdb1b4STejun Heo {
77102fcdb1b4STejun Heo BUG_ON(init_worker_pool(pool));
77112fcdb1b4STejun Heo pool->cpu = cpu;
77122fcdb1b4STejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
77132fcdb1b4STejun Heo cpumask_copy(pool->attrs->__pod_cpumask, cpumask_of(cpu));
77142fcdb1b4STejun Heo pool->attrs->nice = nice;
77152fcdb1b4STejun Heo pool->attrs->affn_strict = true;
77162fcdb1b4STejun Heo pool->node = cpu_to_node(cpu);
77172fcdb1b4STejun Heo
77182fcdb1b4STejun Heo /* alloc pool ID */
77192fcdb1b4STejun Heo mutex_lock(&wq_pool_mutex);
77202fcdb1b4STejun Heo BUG_ON(worker_pool_assign_id(pool));
77212fcdb1b4STejun Heo mutex_unlock(&wq_pool_mutex);
77222fcdb1b4STejun Heo }
77232fcdb1b4STejun Heo
77243347fa09STejun Heo /**
77253347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem
77263347fa09STejun Heo *
77272930155bSTejun Heo * This is the first step of three-staged workqueue subsystem initialization and
77282930155bSTejun Heo * invoked as soon as the bare basics - memory allocation, cpumasks and idr are
77292930155bSTejun Heo * up. It sets up all the data structures and system workqueues and allows early
77302930155bSTejun Heo * boot code to create workqueues and queue/cancel work items. Actual work item
77312930155bSTejun Heo * execution starts only after kthreads can be created and scheduled right
77322930155bSTejun Heo * before early initcalls.
77333347fa09STejun Heo */
workqueue_init_early(void)77342333e829SYu Chen void __init workqueue_init_early(void)
77351da177e4SLinus Torvalds {
773684193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_SYSTEM];
77377a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
77382f34d733STejun Heo void (*irq_work_fns[2])(struct irq_work *) = { bh_pool_kick_normal,
77392f34d733STejun Heo bh_pool_kick_highpri };
77407a4e344cSTejun Heo int i, cpu;
7741c34056a3STejun Heo
774210cdb157SLai Jiangshan BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
7743e904e6c2STejun Heo
77448d84baf7SLai Jiangshan BUG_ON(!alloc_cpumask_var(&wq_online_cpumask, GFP_KERNEL));
7745b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL));
7746fe28f631SWaiman Long BUG_ON(!alloc_cpumask_var(&wq_requested_unbound_cpumask, GFP_KERNEL));
7747fe28f631SWaiman Long BUG_ON(!zalloc_cpumask_var(&wq_isolated_cpumask, GFP_KERNEL));
7748b05a7928SFrederic Weisbecker
77498d84baf7SLai Jiangshan cpumask_copy(wq_online_cpumask, cpu_online_mask);
77504a6c5607STejun Heo cpumask_copy(wq_unbound_cpumask, cpu_possible_mask);
77514a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_WQ", housekeeping_cpumask(HK_TYPE_WQ));
77524a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_DOMAIN", housekeeping_cpumask(HK_TYPE_DOMAIN));
7753ace3c549Stiozhang if (!cpumask_empty(&wq_cmdline_cpumask))
77544a6c5607STejun Heo restrict_unbound_cpumask("workqueue.unbound_cpus", &wq_cmdline_cpumask);
7755ace3c549Stiozhang
7756fe28f631SWaiman Long cpumask_copy(wq_requested_unbound_cpumask, wq_unbound_cpumask);
77578b03ae3cSTejun Heo
77588b03ae3cSTejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
7759e22bee78STejun Heo
7760b2b1f933SLai Jiangshan unbound_wq_update_pwq_attrs_buf = alloc_workqueue_attrs();
7761b2b1f933SLai Jiangshan BUG_ON(!unbound_wq_update_pwq_attrs_buf);
77622930155bSTejun Heo
77637bd20b6bSMarcelo Tosatti /*
77647bd20b6bSMarcelo Tosatti * If nohz_full is enabled, set power efficient workqueue as unbound.
77657bd20b6bSMarcelo Tosatti * This allows workqueue items to be moved to HK CPUs.
77667bd20b6bSMarcelo Tosatti */
77677bd20b6bSMarcelo Tosatti if (housekeeping_enabled(HK_TYPE_TICK))
77687bd20b6bSMarcelo Tosatti wq_power_efficient = true;
77697bd20b6bSMarcelo Tosatti
777084193c07STejun Heo /* initialize WQ_AFFN_SYSTEM pods */
777184193c07STejun Heo pt->pod_cpus = kcalloc(1, sizeof(pt->pod_cpus[0]), GFP_KERNEL);
777284193c07STejun Heo pt->pod_node = kcalloc(1, sizeof(pt->pod_node[0]), GFP_KERNEL);
777384193c07STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL);
777484193c07STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node || !pt->cpu_pod);
777584193c07STejun Heo
777684193c07STejun Heo BUG_ON(!zalloc_cpumask_var_node(&pt->pod_cpus[0], GFP_KERNEL, NUMA_NO_NODE));
777784193c07STejun Heo
777884193c07STejun Heo pt->nr_pods = 1;
777984193c07STejun Heo cpumask_copy(pt->pod_cpus[0], cpu_possible_mask);
778084193c07STejun Heo pt->pod_node[0] = NUMA_NO_NODE;
778184193c07STejun Heo pt->cpu_pod[0] = 0;
778284193c07STejun Heo
77834cb1ef64STejun Heo /* initialize BH and CPU pools */
77841da177e4SLinus Torvalds for_each_possible_cpu(cpu) {
77851da177e4SLinus Torvalds struct worker_pool *pool;
77861da177e4SLinus Torvalds
77871da177e4SLinus Torvalds i = 0;
77884cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) {
77892f34d733STejun Heo init_cpu_worker_pool(pool, cpu, std_nice[i]);
77904cb1ef64STejun Heo pool->flags |= POOL_BH;
77912f34d733STejun Heo init_irq_work(bh_pool_irq_work(pool), irq_work_fns[i]);
77922f34d733STejun Heo i++;
77934cb1ef64STejun Heo }
77944cb1ef64STejun Heo
77954cb1ef64STejun Heo i = 0;
77962fcdb1b4STejun Heo for_each_cpu_worker_pool(pool, cpu)
77972fcdb1b4STejun Heo init_cpu_worker_pool(pool, cpu, std_nice[i++]);
77981da177e4SLinus Torvalds }
77991da177e4SLinus Torvalds
78008a2b7538STejun Heo /* create default unbound and ordered wq attrs */
780129c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
780229c91e99STejun Heo struct workqueue_attrs *attrs;
780329c91e99STejun Heo
7804be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs()));
780529c91e99STejun Heo attrs->nice = std_nice[i];
780629c91e99STejun Heo unbound_std_wq_attrs[i] = attrs;
78078a2b7538STejun Heo
78088a2b7538STejun Heo /*
78098a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is
78108a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs.
78118a2b7538STejun Heo */
7812be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs()));
78138a2b7538STejun Heo attrs->nice = std_nice[i];
7814af73f5c9STejun Heo attrs->ordered = true;
78158a2b7538STejun Heo ordered_wq_attrs[i] = attrs;
781629c91e99STejun Heo }
781729c91e99STejun Heo
7818d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0);
78191aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
7820d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0);
7821f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
7822636b927eSTejun Heo WQ_MAX_ACTIVE);
782324d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable",
782424d51addSTejun Heo WQ_FREEZABLE, 0);
78250668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient",
78260668106cSViresh Kumar WQ_POWER_EFFICIENT, 0);
78278318d6a6SAudra Mitchell system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_pwr_efficient",
78280668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT,
78290668106cSViresh Kumar 0);
78304cb1ef64STejun Heo system_bh_wq = alloc_workqueue("events_bh", WQ_BH, 0);
78314cb1ef64STejun Heo system_bh_highpri_wq = alloc_workqueue("events_bh_highpri",
78324cb1ef64STejun Heo WQ_BH | WQ_HIGHPRI, 0);
78331aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
78340668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq ||
78350668106cSViresh Kumar !system_power_efficient_wq ||
78364cb1ef64STejun Heo !system_freezable_power_efficient_wq ||
78374cb1ef64STejun Heo !system_bh_wq || !system_bh_highpri_wq);
78383347fa09STejun Heo }
78393347fa09STejun Heo
wq_cpu_intensive_thresh_init(void)7840aa6fde93STejun Heo static void __init wq_cpu_intensive_thresh_init(void)
7841aa6fde93STejun Heo {
7842aa6fde93STejun Heo unsigned long thresh;
7843aa6fde93STejun Heo unsigned long bogo;
7844aa6fde93STejun Heo
7845b04e317bSFrederic Weisbecker pwq_release_worker = kthread_run_worker(0, "pool_workqueue_release");
7846dd64c873SZqiang BUG_ON(IS_ERR(pwq_release_worker));
7847dd64c873SZqiang
7848aa6fde93STejun Heo /* if the user set it to a specific value, keep it */
7849aa6fde93STejun Heo if (wq_cpu_intensive_thresh_us != ULONG_MAX)
7850aa6fde93STejun Heo return;
7851aa6fde93STejun Heo
7852aa6fde93STejun Heo /*
7853aa6fde93STejun Heo * The default of 10ms is derived from the fact that most modern (as of
7854aa6fde93STejun Heo * 2023) processors can do a lot in 10ms and that it's just below what
7855aa6fde93STejun Heo * most consider human-perceivable. However, the kernel also runs on a
7856aa6fde93STejun Heo * lot slower CPUs including microcontrollers where the threshold is way
7857aa6fde93STejun Heo * too low.
7858aa6fde93STejun Heo *
7859aa6fde93STejun Heo * Let's scale up the threshold upto 1 second if BogoMips is below 4000.
7860aa6fde93STejun Heo * This is by no means accurate but it doesn't have to be. The mechanism
7861aa6fde93STejun Heo * is still useful even when the threshold is fully scaled up. Also, as
7862aa6fde93STejun Heo * the reports would usually be applicable to everyone, some machines
7863aa6fde93STejun Heo * operating on longer thresholds won't significantly diminish their
7864aa6fde93STejun Heo * usefulness.
7865aa6fde93STejun Heo */
7866aa6fde93STejun Heo thresh = 10 * USEC_PER_MSEC;
7867aa6fde93STejun Heo
7868aa6fde93STejun Heo /* see init/calibrate.c for lpj -> BogoMIPS calculation */
7869aa6fde93STejun Heo bogo = max_t(unsigned long, loops_per_jiffy / 500000 * HZ, 1);
7870aa6fde93STejun Heo if (bogo < 4000)
7871aa6fde93STejun Heo thresh = min_t(unsigned long, thresh * 4000 / bogo, USEC_PER_SEC);
7872aa6fde93STejun Heo
7873aa6fde93STejun Heo pr_debug("wq_cpu_intensive_thresh: lpj=%lu BogoMIPS=%lu thresh_us=%lu\n",
7874aa6fde93STejun Heo loops_per_jiffy, bogo, thresh);
7875aa6fde93STejun Heo
7876aa6fde93STejun Heo wq_cpu_intensive_thresh_us = thresh;
7877aa6fde93STejun Heo }
7878aa6fde93STejun Heo
78793347fa09STejun Heo /**
78803347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online
78813347fa09STejun Heo *
78822930155bSTejun Heo * This is the second step of three-staged workqueue subsystem initialization
78832930155bSTejun Heo * and invoked as soon as kthreads can be created and scheduled. Workqueues have
78842930155bSTejun Heo * been created and work items queued on them, but there are no kworkers
78852930155bSTejun Heo * executing the work items yet. Populate the worker pools with the initial
78862930155bSTejun Heo * workers and enable future kworker creations.
78873347fa09STejun Heo */
workqueue_init(void)78882333e829SYu Chen void __init workqueue_init(void)
78893347fa09STejun Heo {
78902186d9f9STejun Heo struct workqueue_struct *wq;
78913347fa09STejun Heo struct worker_pool *pool;
78923347fa09STejun Heo int cpu, bkt;
78933347fa09STejun Heo
7894aa6fde93STejun Heo wq_cpu_intensive_thresh_init();
7895aa6fde93STejun Heo
78962186d9f9STejun Heo mutex_lock(&wq_pool_mutex);
78972186d9f9STejun Heo
78982930155bSTejun Heo /*
78992930155bSTejun Heo * Per-cpu pools created earlier could be missing node hint. Fix them
79002930155bSTejun Heo * up. Also, create a rescuer for workqueues that requested it.
79012930155bSTejun Heo */
79022186d9f9STejun Heo for_each_possible_cpu(cpu) {
79034cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu)
79042186d9f9STejun Heo pool->node = cpu_to_node(cpu);
79054cb1ef64STejun Heo for_each_cpu_worker_pool(pool, cpu)
79064cb1ef64STejun Heo pool->node = cpu_to_node(cpu);
79072186d9f9STejun Heo }
79082186d9f9STejun Heo
790940c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) {
791040c17f75STejun Heo WARN(init_rescuer(wq),
791140c17f75STejun Heo "workqueue: failed to create early rescuer for %s",
791240c17f75STejun Heo wq->name);
791340c17f75STejun Heo }
79142186d9f9STejun Heo
79152186d9f9STejun Heo mutex_unlock(&wq_pool_mutex);
79162186d9f9STejun Heo
79174cb1ef64STejun Heo /*
79184cb1ef64STejun Heo * Create the initial workers. A BH pool has one pseudo worker that
79194cb1ef64STejun Heo * represents the shared BH execution context and thus doesn't get
79204cb1ef64STejun Heo * affected by hotplug events. Create the BH pseudo workers for all
79214cb1ef64STejun Heo * possible CPUs here.
79224cb1ef64STejun Heo */
79234cb1ef64STejun Heo for_each_possible_cpu(cpu)
79244cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu)
79254cb1ef64STejun Heo BUG_ON(!create_worker(pool));
79264cb1ef64STejun Heo
79273347fa09STejun Heo for_each_online_cpu(cpu) {
79283347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) {
79293347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED;
79303347fa09STejun Heo BUG_ON(!create_worker(pool));
79313347fa09STejun Heo }
79323347fa09STejun Heo }
79333347fa09STejun Heo
79343347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node)
79353347fa09STejun Heo BUG_ON(!create_worker(pool));
79363347fa09STejun Heo
79373347fa09STejun Heo wq_online = true;
793882607adcSTejun Heo wq_watchdog_init();
79391da177e4SLinus Torvalds }
7940c4f135d6STetsuo Handa
7941025e1684STejun Heo /*
7942025e1684STejun Heo * Initialize @pt by first initializing @pt->cpu_pod[] with pod IDs according to
7943025e1684STejun Heo * @cpu_shares_pod(). Each subset of CPUs that share a pod is assigned a unique
7944025e1684STejun Heo * and consecutive pod ID. The rest of @pt is initialized accordingly.
7945025e1684STejun Heo */
init_pod_type(struct wq_pod_type * pt,bool (* cpus_share_pod)(int,int))7946025e1684STejun Heo static void __init init_pod_type(struct wq_pod_type *pt,
7947025e1684STejun Heo bool (*cpus_share_pod)(int, int))
7948025e1684STejun Heo {
7949025e1684STejun Heo int cur, pre, cpu, pod;
7950025e1684STejun Heo
7951025e1684STejun Heo pt->nr_pods = 0;
7952025e1684STejun Heo
7953025e1684STejun Heo /* init @pt->cpu_pod[] according to @cpus_share_pod() */
7954025e1684STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL);
7955025e1684STejun Heo BUG_ON(!pt->cpu_pod);
7956025e1684STejun Heo
7957025e1684STejun Heo for_each_possible_cpu(cur) {
7958025e1684STejun Heo for_each_possible_cpu(pre) {
7959025e1684STejun Heo if (pre >= cur) {
7960025e1684STejun Heo pt->cpu_pod[cur] = pt->nr_pods++;
7961025e1684STejun Heo break;
7962025e1684STejun Heo }
7963025e1684STejun Heo if (cpus_share_pod(cur, pre)) {
7964025e1684STejun Heo pt->cpu_pod[cur] = pt->cpu_pod[pre];
7965025e1684STejun Heo break;
7966025e1684STejun Heo }
7967025e1684STejun Heo }
7968025e1684STejun Heo }
7969025e1684STejun Heo
7970025e1684STejun Heo /* init the rest to match @pt->cpu_pod[] */
7971025e1684STejun Heo pt->pod_cpus = kcalloc(pt->nr_pods, sizeof(pt->pod_cpus[0]), GFP_KERNEL);
7972025e1684STejun Heo pt->pod_node = kcalloc(pt->nr_pods, sizeof(pt->pod_node[0]), GFP_KERNEL);
7973025e1684STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node);
7974025e1684STejun Heo
7975025e1684STejun Heo for (pod = 0; pod < pt->nr_pods; pod++)
7976025e1684STejun Heo BUG_ON(!zalloc_cpumask_var(&pt->pod_cpus[pod], GFP_KERNEL));
7977025e1684STejun Heo
7978025e1684STejun Heo for_each_possible_cpu(cpu) {
7979025e1684STejun Heo cpumask_set_cpu(cpu, pt->pod_cpus[pt->cpu_pod[cpu]]);
7980025e1684STejun Heo pt->pod_node[pt->cpu_pod[cpu]] = cpu_to_node(cpu);
7981025e1684STejun Heo }
7982025e1684STejun Heo }
7983025e1684STejun Heo
cpus_dont_share(int cpu0,int cpu1)798463c5484eSTejun Heo static bool __init cpus_dont_share(int cpu0, int cpu1)
798563c5484eSTejun Heo {
798663c5484eSTejun Heo return false;
798763c5484eSTejun Heo }
798863c5484eSTejun Heo
cpus_share_smt(int cpu0,int cpu1)798963c5484eSTejun Heo static bool __init cpus_share_smt(int cpu0, int cpu1)
799063c5484eSTejun Heo {
799163c5484eSTejun Heo #ifdef CONFIG_SCHED_SMT
799263c5484eSTejun Heo return cpumask_test_cpu(cpu0, cpu_smt_mask(cpu1));
799363c5484eSTejun Heo #else
799463c5484eSTejun Heo return false;
799563c5484eSTejun Heo #endif
799663c5484eSTejun Heo }
799763c5484eSTejun Heo
cpus_share_numa(int cpu0,int cpu1)7998025e1684STejun Heo static bool __init cpus_share_numa(int cpu0, int cpu1)
7999025e1684STejun Heo {
8000025e1684STejun Heo return cpu_to_node(cpu0) == cpu_to_node(cpu1);
8001025e1684STejun Heo }
8002025e1684STejun Heo
80032930155bSTejun Heo /**
80042930155bSTejun Heo * workqueue_init_topology - initialize CPU pods for unbound workqueues
80052930155bSTejun Heo *
800696068b60SWang Jinchao * This is the third step of three-staged workqueue subsystem initialization and
80072930155bSTejun Heo * invoked after SMP and topology information are fully initialized. It
80082930155bSTejun Heo * initializes the unbound CPU pods accordingly.
80092930155bSTejun Heo */
workqueue_init_topology(void)80102930155bSTejun Heo void __init workqueue_init_topology(void)
8011a86feae6STejun Heo {
80122930155bSTejun Heo struct workqueue_struct *wq;
8013025e1684STejun Heo int cpu;
8014a86feae6STejun Heo
801563c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CPU], cpus_dont_share);
801663c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_SMT], cpus_share_smt);
801763c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CACHE], cpus_share_cache);
8018025e1684STejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_NUMA], cpus_share_numa);
8019a86feae6STejun Heo
8020c5f8cd6cSTejun Heo wq_topo_initialized = true;
8021c5f8cd6cSTejun Heo
80222930155bSTejun Heo mutex_lock(&wq_pool_mutex);
8023a86feae6STejun Heo
8024a86feae6STejun Heo /*
80252930155bSTejun Heo * Workqueues allocated earlier would have all CPUs sharing the default
8026b2b1f933SLai Jiangshan * worker pool. Explicitly call unbound_wq_update_pwq() on all workqueue
8027b2b1f933SLai Jiangshan * and CPU combinations to apply per-pod sharing.
80282930155bSTejun Heo */
80292930155bSTejun Heo list_for_each_entry(wq, &workqueues, list) {
80305797b1c1STejun Heo for_each_online_cpu(cpu)
8031b2b1f933SLai Jiangshan unbound_wq_update_pwq(wq, cpu);
80325797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) {
80335797b1c1STejun Heo mutex_lock(&wq->mutex);
80345797b1c1STejun Heo wq_update_node_max_active(wq, -1);
80355797b1c1STejun Heo mutex_unlock(&wq->mutex);
80362930155bSTejun Heo }
80372930155bSTejun Heo }
80382930155bSTejun Heo
80392930155bSTejun Heo mutex_unlock(&wq_pool_mutex);
8040a86feae6STejun Heo }
8041a86feae6STejun Heo
__warn_flushing_systemwide_wq(void)804220bdedafSTetsuo Handa void __warn_flushing_systemwide_wq(void)
804320bdedafSTetsuo Handa {
804420bdedafSTetsuo Handa pr_warn("WARNING: Flushing system-wide workqueues will be prohibited in near future.\n");
804520bdedafSTetsuo Handa dump_stack();
804620bdedafSTetsuo Handa }
8047c4f135d6STetsuo Handa EXPORT_SYMBOL(__warn_flushing_systemwide_wq);
8048ace3c549Stiozhang
workqueue_unbound_cpus_setup(char * str)8049ace3c549Stiozhang static int __init workqueue_unbound_cpus_setup(char *str)
8050ace3c549Stiozhang {
8051ace3c549Stiozhang if (cpulist_parse(str, &wq_cmdline_cpumask) < 0) {
8052ace3c549Stiozhang cpumask_clear(&wq_cmdline_cpumask);
8053ace3c549Stiozhang pr_warn("workqueue.unbound_cpus: incorrect CPU range, using default\n");
8054ace3c549Stiozhang }
8055ace3c549Stiozhang
8056ace3c549Stiozhang return 1;
8057ace3c549Stiozhang }
8058ace3c549Stiozhang __setup("workqueue.unbound_cpus=", workqueue_unbound_cpus_setup);
8059