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