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