xref: /linux-6.15/include/linux/workqueue.h (revision 63c5484e)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * workqueue.h --- work queue handling for Linux.
4  */
5 
6 #ifndef _LINUX_WORKQUEUE_H
7 #define _LINUX_WORKQUEUE_H
8 
9 #include <linux/timer.h>
10 #include <linux/linkage.h>
11 #include <linux/bitops.h>
12 #include <linux/lockdep.h>
13 #include <linux/threads.h>
14 #include <linux/atomic.h>
15 #include <linux/cpumask.h>
16 #include <linux/rcupdate.h>
17 
18 struct workqueue_struct;
19 
20 struct work_struct;
21 typedef void (*work_func_t)(struct work_struct *work);
22 void delayed_work_timer_fn(struct timer_list *t);
23 
24 /*
25  * The first word is the work queue pointer and the flags rolled into
26  * one
27  */
28 #define work_data_bits(work) ((unsigned long *)(&(work)->data))
29 
30 enum {
31 	WORK_STRUCT_PENDING_BIT	= 0,	/* work item is pending execution */
32 	WORK_STRUCT_INACTIVE_BIT= 1,	/* work item is inactive */
33 	WORK_STRUCT_PWQ_BIT	= 2,	/* data points to pwq */
34 	WORK_STRUCT_LINKED_BIT	= 3,	/* next work is linked to this one */
35 #ifdef CONFIG_DEBUG_OBJECTS_WORK
36 	WORK_STRUCT_STATIC_BIT	= 4,	/* static initializer (debugobjects) */
37 	WORK_STRUCT_COLOR_SHIFT	= 5,	/* color for workqueue flushing */
38 #else
39 	WORK_STRUCT_COLOR_SHIFT	= 4,	/* color for workqueue flushing */
40 #endif
41 
42 	WORK_STRUCT_COLOR_BITS	= 4,
43 
44 	WORK_STRUCT_PENDING	= 1 << WORK_STRUCT_PENDING_BIT,
45 	WORK_STRUCT_INACTIVE	= 1 << WORK_STRUCT_INACTIVE_BIT,
46 	WORK_STRUCT_PWQ		= 1 << WORK_STRUCT_PWQ_BIT,
47 	WORK_STRUCT_LINKED	= 1 << WORK_STRUCT_LINKED_BIT,
48 #ifdef CONFIG_DEBUG_OBJECTS_WORK
49 	WORK_STRUCT_STATIC	= 1 << WORK_STRUCT_STATIC_BIT,
50 #else
51 	WORK_STRUCT_STATIC	= 0,
52 #endif
53 
54 	WORK_NR_COLORS		= (1 << WORK_STRUCT_COLOR_BITS),
55 
56 	/* not bound to any CPU, prefer the local CPU */
57 	WORK_CPU_UNBOUND	= NR_CPUS,
58 
59 	/*
60 	 * Reserve 8 bits off of pwq pointer w/ debugobjects turned off.
61 	 * This makes pwqs aligned to 256 bytes and allows 16 workqueue
62 	 * flush colors.
63 	 */
64 	WORK_STRUCT_FLAG_BITS	= WORK_STRUCT_COLOR_SHIFT +
65 				  WORK_STRUCT_COLOR_BITS,
66 
67 	/* data contains off-queue information when !WORK_STRUCT_PWQ */
68 	WORK_OFFQ_FLAG_BASE	= WORK_STRUCT_COLOR_SHIFT,
69 
70 	__WORK_OFFQ_CANCELING	= WORK_OFFQ_FLAG_BASE,
71 
72 	/*
73 	 * When a work item is off queue, its high bits point to the last
74 	 * pool it was on.  Cap at 31 bits and use the highest number to
75 	 * indicate that no pool is associated.
76 	 */
77 	WORK_OFFQ_FLAG_BITS	= 1,
78 	WORK_OFFQ_POOL_SHIFT	= WORK_OFFQ_FLAG_BASE + WORK_OFFQ_FLAG_BITS,
79 	WORK_OFFQ_LEFT		= BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT,
80 	WORK_OFFQ_POOL_BITS	= WORK_OFFQ_LEFT <= 31 ? WORK_OFFQ_LEFT : 31,
81 
82 	/* bit mask for work_busy() return values */
83 	WORK_BUSY_PENDING	= 1 << 0,
84 	WORK_BUSY_RUNNING	= 1 << 1,
85 
86 	/* maximum string length for set_worker_desc() */
87 	WORKER_DESC_LEN		= 24,
88 };
89 
90 /* Convenience constants - of type 'unsigned long', not 'enum'! */
91 #define WORK_OFFQ_CANCELING	(1ul << __WORK_OFFQ_CANCELING)
92 #define WORK_OFFQ_POOL_NONE	((1ul << WORK_OFFQ_POOL_BITS) - 1)
93 #define WORK_STRUCT_NO_POOL	(WORK_OFFQ_POOL_NONE << WORK_OFFQ_POOL_SHIFT)
94 
95 #define WORK_STRUCT_FLAG_MASK    ((1ul << WORK_STRUCT_FLAG_BITS) - 1)
96 #define WORK_STRUCT_WQ_DATA_MASK (~WORK_STRUCT_FLAG_MASK)
97 
98 struct work_struct {
99 	atomic_long_t data;
100 	struct list_head entry;
101 	work_func_t func;
102 #ifdef CONFIG_LOCKDEP
103 	struct lockdep_map lockdep_map;
104 #endif
105 };
106 
107 #define WORK_DATA_INIT()	ATOMIC_LONG_INIT((unsigned long)WORK_STRUCT_NO_POOL)
108 #define WORK_DATA_STATIC_INIT()	\
109 	ATOMIC_LONG_INIT((unsigned long)(WORK_STRUCT_NO_POOL | WORK_STRUCT_STATIC))
110 
111 struct delayed_work {
112 	struct work_struct work;
113 	struct timer_list timer;
114 
115 	/* target workqueue and CPU ->timer uses to queue ->work */
116 	struct workqueue_struct *wq;
117 	int cpu;
118 };
119 
120 struct rcu_work {
121 	struct work_struct work;
122 	struct rcu_head rcu;
123 
124 	/* target workqueue ->rcu uses to queue ->work */
125 	struct workqueue_struct *wq;
126 };
127 
128 enum wq_affn_scope {
129 	WQ_AFFN_CPU,			/* one pod per CPU */
130 	WQ_AFFN_SMT,			/* one pod poer SMT */
131 	WQ_AFFN_CACHE,			/* one pod per LLC */
132 	WQ_AFFN_NUMA,			/* one pod per NUMA node */
133 	WQ_AFFN_SYSTEM,			/* one pod across the whole system */
134 
135 	WQ_AFFN_NR_TYPES,
136 
137 	WQ_AFFN_DFL = WQ_AFFN_CACHE,
138 };
139 
140 /**
141  * struct workqueue_attrs - A struct for workqueue attributes.
142  *
143  * This can be used to change attributes of an unbound workqueue.
144  */
145 struct workqueue_attrs {
146 	/**
147 	 * @nice: nice level
148 	 */
149 	int nice;
150 
151 	/**
152 	 * @cpumask: allowed CPUs
153 	 */
154 	cpumask_var_t cpumask;
155 
156 	/*
157 	 * Below fields aren't properties of a worker_pool. They only modify how
158 	 * :c:func:`apply_workqueue_attrs` select pools and thus don't
159 	 * participate in pool hash calculations or equality comparisons.
160 	 */
161 
162 	/**
163 	 * @affn_scope: unbound CPU affinity scope
164 	 *
165 	 * CPU pods are used to improve execution locality of unbound work
166 	 * items. There are multiple pod types, one for each wq_affn_scope, and
167 	 * every CPU in the system belongs to one pod in every pod type. CPUs
168 	 * that belong to the same pod share the worker pool. For example,
169 	 * selecting %WQ_AFFN_NUMA makes the workqueue use a separate worker
170 	 * pool for each NUMA node.
171 	 */
172 	enum wq_affn_scope affn_scope;
173 
174 	/**
175 	 * @ordered: work items must be executed one by one in queueing order
176 	 */
177 	bool ordered;
178 };
179 
180 static inline struct delayed_work *to_delayed_work(struct work_struct *work)
181 {
182 	return container_of(work, struct delayed_work, work);
183 }
184 
185 static inline struct rcu_work *to_rcu_work(struct work_struct *work)
186 {
187 	return container_of(work, struct rcu_work, work);
188 }
189 
190 struct execute_work {
191 	struct work_struct work;
192 };
193 
194 #ifdef CONFIG_LOCKDEP
195 /*
196  * NB: because we have to copy the lockdep_map, setting _key
197  * here is required, otherwise it could get initialised to the
198  * copy of the lockdep_map!
199  */
200 #define __WORK_INIT_LOCKDEP_MAP(n, k) \
201 	.lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k),
202 #else
203 #define __WORK_INIT_LOCKDEP_MAP(n, k)
204 #endif
205 
206 #define __WORK_INITIALIZER(n, f) {					\
207 	.data = WORK_DATA_STATIC_INIT(),				\
208 	.entry	= { &(n).entry, &(n).entry },				\
209 	.func = (f),							\
210 	__WORK_INIT_LOCKDEP_MAP(#n, &(n))				\
211 	}
212 
213 #define __DELAYED_WORK_INITIALIZER(n, f, tflags) {			\
214 	.work = __WORK_INITIALIZER((n).work, (f)),			\
215 	.timer = __TIMER_INITIALIZER(delayed_work_timer_fn,\
216 				     (tflags) | TIMER_IRQSAFE),		\
217 	}
218 
219 #define DECLARE_WORK(n, f)						\
220 	struct work_struct n = __WORK_INITIALIZER(n, f)
221 
222 #define DECLARE_DELAYED_WORK(n, f)					\
223 	struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, 0)
224 
225 #define DECLARE_DEFERRABLE_WORK(n, f)					\
226 	struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, TIMER_DEFERRABLE)
227 
228 #ifdef CONFIG_DEBUG_OBJECTS_WORK
229 extern void __init_work(struct work_struct *work, int onstack);
230 extern void destroy_work_on_stack(struct work_struct *work);
231 extern void destroy_delayed_work_on_stack(struct delayed_work *work);
232 static inline unsigned int work_static(struct work_struct *work)
233 {
234 	return *work_data_bits(work) & WORK_STRUCT_STATIC;
235 }
236 #else
237 static inline void __init_work(struct work_struct *work, int onstack) { }
238 static inline void destroy_work_on_stack(struct work_struct *work) { }
239 static inline void destroy_delayed_work_on_stack(struct delayed_work *work) { }
240 static inline unsigned int work_static(struct work_struct *work) { return 0; }
241 #endif
242 
243 /*
244  * initialize all of a work item in one go
245  *
246  * NOTE! No point in using "atomic_long_set()": using a direct
247  * assignment of the work data initializer allows the compiler
248  * to generate better code.
249  */
250 #ifdef CONFIG_LOCKDEP
251 #define __INIT_WORK(_work, _func, _onstack)				\
252 	do {								\
253 		static struct lock_class_key __key;			\
254 									\
255 		__init_work((_work), _onstack);				\
256 		(_work)->data = (atomic_long_t) WORK_DATA_INIT();	\
257 		lockdep_init_map(&(_work)->lockdep_map, "(work_completion)"#_work, &__key, 0); \
258 		INIT_LIST_HEAD(&(_work)->entry);			\
259 		(_work)->func = (_func);				\
260 	} while (0)
261 #else
262 #define __INIT_WORK(_work, _func, _onstack)				\
263 	do {								\
264 		__init_work((_work), _onstack);				\
265 		(_work)->data = (atomic_long_t) WORK_DATA_INIT();	\
266 		INIT_LIST_HEAD(&(_work)->entry);			\
267 		(_work)->func = (_func);				\
268 	} while (0)
269 #endif
270 
271 #define INIT_WORK(_work, _func)						\
272 	__INIT_WORK((_work), (_func), 0)
273 
274 #define INIT_WORK_ONSTACK(_work, _func)					\
275 	__INIT_WORK((_work), (_func), 1)
276 
277 #define __INIT_DELAYED_WORK(_work, _func, _tflags)			\
278 	do {								\
279 		INIT_WORK(&(_work)->work, (_func));			\
280 		__init_timer(&(_work)->timer,				\
281 			     delayed_work_timer_fn,			\
282 			     (_tflags) | TIMER_IRQSAFE);		\
283 	} while (0)
284 
285 #define __INIT_DELAYED_WORK_ONSTACK(_work, _func, _tflags)		\
286 	do {								\
287 		INIT_WORK_ONSTACK(&(_work)->work, (_func));		\
288 		__init_timer_on_stack(&(_work)->timer,			\
289 				      delayed_work_timer_fn,		\
290 				      (_tflags) | TIMER_IRQSAFE);	\
291 	} while (0)
292 
293 #define INIT_DELAYED_WORK(_work, _func)					\
294 	__INIT_DELAYED_WORK(_work, _func, 0)
295 
296 #define INIT_DELAYED_WORK_ONSTACK(_work, _func)				\
297 	__INIT_DELAYED_WORK_ONSTACK(_work, _func, 0)
298 
299 #define INIT_DEFERRABLE_WORK(_work, _func)				\
300 	__INIT_DELAYED_WORK(_work, _func, TIMER_DEFERRABLE)
301 
302 #define INIT_DEFERRABLE_WORK_ONSTACK(_work, _func)			\
303 	__INIT_DELAYED_WORK_ONSTACK(_work, _func, TIMER_DEFERRABLE)
304 
305 #define INIT_RCU_WORK(_work, _func)					\
306 	INIT_WORK(&(_work)->work, (_func))
307 
308 #define INIT_RCU_WORK_ONSTACK(_work, _func)				\
309 	INIT_WORK_ONSTACK(&(_work)->work, (_func))
310 
311 /**
312  * work_pending - Find out whether a work item is currently pending
313  * @work: The work item in question
314  */
315 #define work_pending(work) \
316 	test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
317 
318 /**
319  * delayed_work_pending - Find out whether a delayable work item is currently
320  * pending
321  * @w: The work item in question
322  */
323 #define delayed_work_pending(w) \
324 	work_pending(&(w)->work)
325 
326 /*
327  * Workqueue flags and constants.  For details, please refer to
328  * Documentation/core-api/workqueue.rst.
329  */
330 enum {
331 	WQ_UNBOUND		= 1 << 1, /* not bound to any cpu */
332 	WQ_FREEZABLE		= 1 << 2, /* freeze during suspend */
333 	WQ_MEM_RECLAIM		= 1 << 3, /* may be used for memory reclaim */
334 	WQ_HIGHPRI		= 1 << 4, /* high priority */
335 	WQ_CPU_INTENSIVE	= 1 << 5, /* cpu intensive workqueue */
336 	WQ_SYSFS		= 1 << 6, /* visible in sysfs, see workqueue_sysfs_register() */
337 
338 	/*
339 	 * Per-cpu workqueues are generally preferred because they tend to
340 	 * show better performance thanks to cache locality.  Per-cpu
341 	 * workqueues exclude the scheduler from choosing the CPU to
342 	 * execute the worker threads, which has an unfortunate side effect
343 	 * of increasing power consumption.
344 	 *
345 	 * The scheduler considers a CPU idle if it doesn't have any task
346 	 * to execute and tries to keep idle cores idle to conserve power;
347 	 * however, for example, a per-cpu work item scheduled from an
348 	 * interrupt handler on an idle CPU will force the scheduler to
349 	 * execute the work item on that CPU breaking the idleness, which in
350 	 * turn may lead to more scheduling choices which are sub-optimal
351 	 * in terms of power consumption.
352 	 *
353 	 * Workqueues marked with WQ_POWER_EFFICIENT are per-cpu by default
354 	 * but become unbound if workqueue.power_efficient kernel param is
355 	 * specified.  Per-cpu workqueues which are identified to
356 	 * contribute significantly to power-consumption are identified and
357 	 * marked with this flag and enabling the power_efficient mode
358 	 * leads to noticeable power saving at the cost of small
359 	 * performance disadvantage.
360 	 *
361 	 * http://thread.gmane.org/gmane.linux.kernel/1480396
362 	 */
363 	WQ_POWER_EFFICIENT	= 1 << 7,
364 
365 	__WQ_DESTROYING		= 1 << 15, /* internal: workqueue is destroying */
366 	__WQ_DRAINING		= 1 << 16, /* internal: workqueue is draining */
367 	__WQ_ORDERED		= 1 << 17, /* internal: workqueue is ordered */
368 	__WQ_LEGACY		= 1 << 18, /* internal: create*_workqueue() */
369 	__WQ_ORDERED_EXPLICIT	= 1 << 19, /* internal: alloc_ordered_workqueue() */
370 
371 	WQ_MAX_ACTIVE		= 512,	  /* I like 512, better ideas? */
372 	WQ_UNBOUND_MAX_ACTIVE	= WQ_MAX_ACTIVE,
373 	WQ_DFL_ACTIVE		= WQ_MAX_ACTIVE / 2,
374 };
375 
376 /*
377  * System-wide workqueues which are always present.
378  *
379  * system_wq is the one used by schedule[_delayed]_work[_on]().
380  * Multi-CPU multi-threaded.  There are users which expect relatively
381  * short queue flush time.  Don't queue works which can run for too
382  * long.
383  *
384  * system_highpri_wq is similar to system_wq but for work items which
385  * require WQ_HIGHPRI.
386  *
387  * system_long_wq is similar to system_wq but may host long running
388  * works.  Queue flushing might take relatively long.
389  *
390  * system_unbound_wq is unbound workqueue.  Workers are not bound to
391  * any specific CPU, not concurrency managed, and all queued works are
392  * executed immediately as long as max_active limit is not reached and
393  * resources are available.
394  *
395  * system_freezable_wq is equivalent to system_wq except that it's
396  * freezable.
397  *
398  * *_power_efficient_wq are inclined towards saving power and converted
399  * into WQ_UNBOUND variants if 'wq_power_efficient' is enabled; otherwise,
400  * they are same as their non-power-efficient counterparts - e.g.
401  * system_power_efficient_wq is identical to system_wq if
402  * 'wq_power_efficient' is disabled.  See WQ_POWER_EFFICIENT for more info.
403  */
404 extern struct workqueue_struct *system_wq;
405 extern struct workqueue_struct *system_highpri_wq;
406 extern struct workqueue_struct *system_long_wq;
407 extern struct workqueue_struct *system_unbound_wq;
408 extern struct workqueue_struct *system_freezable_wq;
409 extern struct workqueue_struct *system_power_efficient_wq;
410 extern struct workqueue_struct *system_freezable_power_efficient_wq;
411 
412 /**
413  * alloc_workqueue - allocate a workqueue
414  * @fmt: printf format for the name of the workqueue
415  * @flags: WQ_* flags
416  * @max_active: max in-flight work items per CPU, 0 for default
417  * remaining args: args for @fmt
418  *
419  * Allocate a workqueue with the specified parameters.  For detailed
420  * information on WQ_* flags, please refer to
421  * Documentation/core-api/workqueue.rst.
422  *
423  * RETURNS:
424  * Pointer to the allocated workqueue on success, %NULL on failure.
425  */
426 __printf(1, 4) struct workqueue_struct *
427 alloc_workqueue(const char *fmt, unsigned int flags, int max_active, ...);
428 
429 /**
430  * alloc_ordered_workqueue - allocate an ordered workqueue
431  * @fmt: printf format for the name of the workqueue
432  * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful)
433  * @args: args for @fmt
434  *
435  * Allocate an ordered workqueue.  An ordered workqueue executes at
436  * most one work item at any given time in the queued order.  They are
437  * implemented as unbound workqueues with @max_active of one.
438  *
439  * RETURNS:
440  * Pointer to the allocated workqueue on success, %NULL on failure.
441  */
442 #define alloc_ordered_workqueue(fmt, flags, args...)			\
443 	alloc_workqueue(fmt, WQ_UNBOUND | __WQ_ORDERED |		\
444 			__WQ_ORDERED_EXPLICIT | (flags), 1, ##args)
445 
446 #define create_workqueue(name)						\
447 	alloc_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, 1, (name))
448 #define create_freezable_workqueue(name)				\
449 	alloc_workqueue("%s", __WQ_LEGACY | WQ_FREEZABLE | WQ_UNBOUND |	\
450 			WQ_MEM_RECLAIM, 1, (name))
451 #define create_singlethread_workqueue(name)				\
452 	alloc_ordered_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, name)
453 
454 extern void destroy_workqueue(struct workqueue_struct *wq);
455 
456 struct workqueue_attrs *alloc_workqueue_attrs(void);
457 void free_workqueue_attrs(struct workqueue_attrs *attrs);
458 int apply_workqueue_attrs(struct workqueue_struct *wq,
459 			  const struct workqueue_attrs *attrs);
460 int workqueue_set_unbound_cpumask(cpumask_var_t cpumask);
461 
462 extern bool queue_work_on(int cpu, struct workqueue_struct *wq,
463 			struct work_struct *work);
464 extern bool queue_work_node(int node, struct workqueue_struct *wq,
465 			    struct work_struct *work);
466 extern bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
467 			struct delayed_work *work, unsigned long delay);
468 extern bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
469 			struct delayed_work *dwork, unsigned long delay);
470 extern bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork);
471 
472 extern void __flush_workqueue(struct workqueue_struct *wq);
473 extern void drain_workqueue(struct workqueue_struct *wq);
474 
475 extern int schedule_on_each_cpu(work_func_t func);
476 
477 int execute_in_process_context(work_func_t fn, struct execute_work *);
478 
479 extern bool flush_work(struct work_struct *work);
480 extern bool cancel_work(struct work_struct *work);
481 extern bool cancel_work_sync(struct work_struct *work);
482 
483 extern bool flush_delayed_work(struct delayed_work *dwork);
484 extern bool cancel_delayed_work(struct delayed_work *dwork);
485 extern bool cancel_delayed_work_sync(struct delayed_work *dwork);
486 
487 extern bool flush_rcu_work(struct rcu_work *rwork);
488 
489 extern void workqueue_set_max_active(struct workqueue_struct *wq,
490 				     int max_active);
491 extern struct work_struct *current_work(void);
492 extern bool current_is_workqueue_rescuer(void);
493 extern bool workqueue_congested(int cpu, struct workqueue_struct *wq);
494 extern unsigned int work_busy(struct work_struct *work);
495 extern __printf(1, 2) void set_worker_desc(const char *fmt, ...);
496 extern void print_worker_info(const char *log_lvl, struct task_struct *task);
497 extern void show_all_workqueues(void);
498 extern void show_freezable_workqueues(void);
499 extern void show_one_workqueue(struct workqueue_struct *wq);
500 extern void wq_worker_comm(char *buf, size_t size, struct task_struct *task);
501 
502 /**
503  * queue_work - queue work on a workqueue
504  * @wq: workqueue to use
505  * @work: work to queue
506  *
507  * Returns %false if @work was already on a queue, %true otherwise.
508  *
509  * We queue the work to the CPU on which it was submitted, but if the CPU dies
510  * it can be processed by another CPU.
511  *
512  * Memory-ordering properties:  If it returns %true, guarantees that all stores
513  * preceding the call to queue_work() in the program order will be visible from
514  * the CPU which will execute @work by the time such work executes, e.g.,
515  *
516  * { x is initially 0 }
517  *
518  *   CPU0				CPU1
519  *
520  *   WRITE_ONCE(x, 1);			[ @work is being executed ]
521  *   r0 = queue_work(wq, work);		  r1 = READ_ONCE(x);
522  *
523  * Forbids: r0 == true && r1 == 0
524  */
525 static inline bool queue_work(struct workqueue_struct *wq,
526 			      struct work_struct *work)
527 {
528 	return queue_work_on(WORK_CPU_UNBOUND, wq, work);
529 }
530 
531 /**
532  * queue_delayed_work - queue work on a workqueue after delay
533  * @wq: workqueue to use
534  * @dwork: delayable work to queue
535  * @delay: number of jiffies to wait before queueing
536  *
537  * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
538  */
539 static inline bool queue_delayed_work(struct workqueue_struct *wq,
540 				      struct delayed_work *dwork,
541 				      unsigned long delay)
542 {
543 	return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
544 }
545 
546 /**
547  * mod_delayed_work - modify delay of or queue a delayed work
548  * @wq: workqueue to use
549  * @dwork: work to queue
550  * @delay: number of jiffies to wait before queueing
551  *
552  * mod_delayed_work_on() on local CPU.
553  */
554 static inline bool mod_delayed_work(struct workqueue_struct *wq,
555 				    struct delayed_work *dwork,
556 				    unsigned long delay)
557 {
558 	return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
559 }
560 
561 /**
562  * schedule_work_on - put work task on a specific cpu
563  * @cpu: cpu to put the work task on
564  * @work: job to be done
565  *
566  * This puts a job on a specific cpu
567  */
568 static inline bool schedule_work_on(int cpu, struct work_struct *work)
569 {
570 	return queue_work_on(cpu, system_wq, work);
571 }
572 
573 /**
574  * schedule_work - put work task in global workqueue
575  * @work: job to be done
576  *
577  * Returns %false if @work was already on the kernel-global workqueue and
578  * %true otherwise.
579  *
580  * This puts a job in the kernel-global workqueue if it was not already
581  * queued and leaves it in the same position on the kernel-global
582  * workqueue otherwise.
583  *
584  * Shares the same memory-ordering properties of queue_work(), cf. the
585  * DocBook header of queue_work().
586  */
587 static inline bool schedule_work(struct work_struct *work)
588 {
589 	return queue_work(system_wq, work);
590 }
591 
592 /*
593  * Detect attempt to flush system-wide workqueues at compile time when possible.
594  * Warn attempt to flush system-wide workqueues at runtime.
595  *
596  * See https://lkml.kernel.org/r/[email protected]
597  * for reasons and steps for converting system-wide workqueues into local workqueues.
598  */
599 extern void __warn_flushing_systemwide_wq(void)
600 	__compiletime_warning("Please avoid flushing system-wide workqueues.");
601 
602 /* Please stop using this function, for this function will be removed in near future. */
603 #define flush_scheduled_work()						\
604 ({									\
605 	__warn_flushing_systemwide_wq();				\
606 	__flush_workqueue(system_wq);					\
607 })
608 
609 #define flush_workqueue(wq)						\
610 ({									\
611 	struct workqueue_struct *_wq = (wq);				\
612 									\
613 	if ((__builtin_constant_p(_wq == system_wq) &&			\
614 	     _wq == system_wq) ||					\
615 	    (__builtin_constant_p(_wq == system_highpri_wq) &&		\
616 	     _wq == system_highpri_wq) ||				\
617 	    (__builtin_constant_p(_wq == system_long_wq) &&		\
618 	     _wq == system_long_wq) ||					\
619 	    (__builtin_constant_p(_wq == system_unbound_wq) &&		\
620 	     _wq == system_unbound_wq) ||				\
621 	    (__builtin_constant_p(_wq == system_freezable_wq) &&	\
622 	     _wq == system_freezable_wq) ||				\
623 	    (__builtin_constant_p(_wq == system_power_efficient_wq) &&	\
624 	     _wq == system_power_efficient_wq) ||			\
625 	    (__builtin_constant_p(_wq == system_freezable_power_efficient_wq) && \
626 	     _wq == system_freezable_power_efficient_wq))		\
627 		__warn_flushing_systemwide_wq();			\
628 	__flush_workqueue(_wq);						\
629 })
630 
631 /**
632  * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
633  * @cpu: cpu to use
634  * @dwork: job to be done
635  * @delay: number of jiffies to wait
636  *
637  * After waiting for a given time this puts a job in the kernel-global
638  * workqueue on the specified CPU.
639  */
640 static inline bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
641 					    unsigned long delay)
642 {
643 	return queue_delayed_work_on(cpu, system_wq, dwork, delay);
644 }
645 
646 /**
647  * schedule_delayed_work - put work task in global workqueue after delay
648  * @dwork: job to be done
649  * @delay: number of jiffies to wait or 0 for immediate execution
650  *
651  * After waiting for a given time this puts a job in the kernel-global
652  * workqueue.
653  */
654 static inline bool schedule_delayed_work(struct delayed_work *dwork,
655 					 unsigned long delay)
656 {
657 	return queue_delayed_work(system_wq, dwork, delay);
658 }
659 
660 #ifndef CONFIG_SMP
661 static inline long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
662 {
663 	return fn(arg);
664 }
665 static inline long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg)
666 {
667 	return fn(arg);
668 }
669 #else
670 long work_on_cpu(int cpu, long (*fn)(void *), void *arg);
671 long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg);
672 #endif /* CONFIG_SMP */
673 
674 #ifdef CONFIG_FREEZER
675 extern void freeze_workqueues_begin(void);
676 extern bool freeze_workqueues_busy(void);
677 extern void thaw_workqueues(void);
678 #endif /* CONFIG_FREEZER */
679 
680 #ifdef CONFIG_SYSFS
681 int workqueue_sysfs_register(struct workqueue_struct *wq);
682 #else	/* CONFIG_SYSFS */
683 static inline int workqueue_sysfs_register(struct workqueue_struct *wq)
684 { return 0; }
685 #endif	/* CONFIG_SYSFS */
686 
687 #ifdef CONFIG_WQ_WATCHDOG
688 void wq_watchdog_touch(int cpu);
689 #else	/* CONFIG_WQ_WATCHDOG */
690 static inline void wq_watchdog_touch(int cpu) { }
691 #endif	/* CONFIG_WQ_WATCHDOG */
692 
693 #ifdef CONFIG_SMP
694 int workqueue_prepare_cpu(unsigned int cpu);
695 int workqueue_online_cpu(unsigned int cpu);
696 int workqueue_offline_cpu(unsigned int cpu);
697 #endif
698 
699 void __init workqueue_init_early(void);
700 void __init workqueue_init(void);
701 void __init workqueue_init_topology(void);
702 
703 #endif
704