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