Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * workqueue.h --- work queue handling for Linux.
0004  */
0005 
0006 #ifndef _LINUX_WORKQUEUE_H
0007 #define _LINUX_WORKQUEUE_H
0008 
0009 #include <linux/timer.h>
0010 #include <linux/linkage.h>
0011 #include <linux/bitops.h>
0012 #include <linux/lockdep.h>
0013 #include <linux/threads.h>
0014 #include <linux/atomic.h>
0015 #include <linux/cpumask.h>
0016 #include <linux/rcupdate.h>
0017 
0018 struct workqueue_struct;
0019 
0020 struct work_struct;
0021 typedef void (*work_func_t)(struct work_struct *work);
0022 void delayed_work_timer_fn(struct timer_list *t);
0023 
0024 /*
0025  * The first word is the work queue pointer and the flags rolled into
0026  * one
0027  */
0028 #define work_data_bits(work) ((unsigned long *)(&(work)->data))
0029 
0030 enum {
0031     WORK_STRUCT_PENDING_BIT = 0,    /* work item is pending execution */
0032     WORK_STRUCT_INACTIVE_BIT= 1,    /* work item is inactive */
0033     WORK_STRUCT_PWQ_BIT = 2,    /* data points to pwq */
0034     WORK_STRUCT_LINKED_BIT  = 3,    /* next work is linked to this one */
0035 #ifdef CONFIG_DEBUG_OBJECTS_WORK
0036     WORK_STRUCT_STATIC_BIT  = 4,    /* static initializer (debugobjects) */
0037     WORK_STRUCT_COLOR_SHIFT = 5,    /* color for workqueue flushing */
0038 #else
0039     WORK_STRUCT_COLOR_SHIFT = 4,    /* color for workqueue flushing */
0040 #endif
0041 
0042     WORK_STRUCT_COLOR_BITS  = 4,
0043 
0044     WORK_STRUCT_PENDING = 1 << WORK_STRUCT_PENDING_BIT,
0045     WORK_STRUCT_INACTIVE    = 1 << WORK_STRUCT_INACTIVE_BIT,
0046     WORK_STRUCT_PWQ     = 1 << WORK_STRUCT_PWQ_BIT,
0047     WORK_STRUCT_LINKED  = 1 << WORK_STRUCT_LINKED_BIT,
0048 #ifdef CONFIG_DEBUG_OBJECTS_WORK
0049     WORK_STRUCT_STATIC  = 1 << WORK_STRUCT_STATIC_BIT,
0050 #else
0051     WORK_STRUCT_STATIC  = 0,
0052 #endif
0053 
0054     WORK_NR_COLORS      = (1 << WORK_STRUCT_COLOR_BITS),
0055 
0056     /* not bound to any CPU, prefer the local CPU */
0057     WORK_CPU_UNBOUND    = NR_CPUS,
0058 
0059     /*
0060      * Reserve 8 bits off of pwq pointer w/ debugobjects turned off.
0061      * This makes pwqs aligned to 256 bytes and allows 16 workqueue
0062      * flush colors.
0063      */
0064     WORK_STRUCT_FLAG_BITS   = WORK_STRUCT_COLOR_SHIFT +
0065                   WORK_STRUCT_COLOR_BITS,
0066 
0067     /* data contains off-queue information when !WORK_STRUCT_PWQ */
0068     WORK_OFFQ_FLAG_BASE = WORK_STRUCT_COLOR_SHIFT,
0069 
0070     __WORK_OFFQ_CANCELING   = WORK_OFFQ_FLAG_BASE,
0071     WORK_OFFQ_CANCELING = (1 << __WORK_OFFQ_CANCELING),
0072 
0073     /*
0074      * When a work item is off queue, its high bits point to the last
0075      * pool it was on.  Cap at 31 bits and use the highest number to
0076      * indicate that no pool is associated.
0077      */
0078     WORK_OFFQ_FLAG_BITS = 1,
0079     WORK_OFFQ_POOL_SHIFT    = WORK_OFFQ_FLAG_BASE + WORK_OFFQ_FLAG_BITS,
0080     WORK_OFFQ_LEFT      = BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT,
0081     WORK_OFFQ_POOL_BITS = WORK_OFFQ_LEFT <= 31 ? WORK_OFFQ_LEFT : 31,
0082     WORK_OFFQ_POOL_NONE = (1LU << WORK_OFFQ_POOL_BITS) - 1,
0083 
0084     /* convenience constants */
0085     WORK_STRUCT_FLAG_MASK   = (1UL << WORK_STRUCT_FLAG_BITS) - 1,
0086     WORK_STRUCT_WQ_DATA_MASK = ~WORK_STRUCT_FLAG_MASK,
0087     WORK_STRUCT_NO_POOL = (unsigned long)WORK_OFFQ_POOL_NONE << WORK_OFFQ_POOL_SHIFT,
0088 
0089     /* bit mask for work_busy() return values */
0090     WORK_BUSY_PENDING   = 1 << 0,
0091     WORK_BUSY_RUNNING   = 1 << 1,
0092 
0093     /* maximum string length for set_worker_desc() */
0094     WORKER_DESC_LEN     = 24,
0095 };
0096 
0097 struct work_struct {
0098     atomic_long_t data;
0099     struct list_head entry;
0100     work_func_t func;
0101 #ifdef CONFIG_LOCKDEP
0102     struct lockdep_map lockdep_map;
0103 #endif
0104 };
0105 
0106 #define WORK_DATA_INIT()    ATOMIC_LONG_INIT((unsigned long)WORK_STRUCT_NO_POOL)
0107 #define WORK_DATA_STATIC_INIT() \
0108     ATOMIC_LONG_INIT((unsigned long)(WORK_STRUCT_NO_POOL | WORK_STRUCT_STATIC))
0109 
0110 struct delayed_work {
0111     struct work_struct work;
0112     struct timer_list timer;
0113 
0114     /* target workqueue and CPU ->timer uses to queue ->work */
0115     struct workqueue_struct *wq;
0116     int cpu;
0117 };
0118 
0119 struct rcu_work {
0120     struct work_struct work;
0121     struct rcu_head rcu;
0122 
0123     /* target workqueue ->rcu uses to queue ->work */
0124     struct workqueue_struct *wq;
0125 };
0126 
0127 /**
0128  * struct workqueue_attrs - A struct for workqueue attributes.
0129  *
0130  * This can be used to change attributes of an unbound workqueue.
0131  */
0132 struct workqueue_attrs {
0133     /**
0134      * @nice: nice level
0135      */
0136     int nice;
0137 
0138     /**
0139      * @cpumask: allowed CPUs
0140      */
0141     cpumask_var_t cpumask;
0142 
0143     /**
0144      * @no_numa: disable NUMA affinity
0145      *
0146      * Unlike other fields, ``no_numa`` isn't a property of a worker_pool. It
0147      * only modifies how :c:func:`apply_workqueue_attrs` select pools and thus
0148      * doesn't participate in pool hash calculations or equality comparisons.
0149      */
0150     bool no_numa;
0151 };
0152 
0153 static inline struct delayed_work *to_delayed_work(struct work_struct *work)
0154 {
0155     return container_of(work, struct delayed_work, work);
0156 }
0157 
0158 static inline struct rcu_work *to_rcu_work(struct work_struct *work)
0159 {
0160     return container_of(work, struct rcu_work, work);
0161 }
0162 
0163 struct execute_work {
0164     struct work_struct work;
0165 };
0166 
0167 #ifdef CONFIG_LOCKDEP
0168 /*
0169  * NB: because we have to copy the lockdep_map, setting _key
0170  * here is required, otherwise it could get initialised to the
0171  * copy of the lockdep_map!
0172  */
0173 #define __WORK_INIT_LOCKDEP_MAP(n, k) \
0174     .lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k),
0175 #else
0176 #define __WORK_INIT_LOCKDEP_MAP(n, k)
0177 #endif
0178 
0179 #define __WORK_INITIALIZER(n, f) {                  \
0180     .data = WORK_DATA_STATIC_INIT(),                \
0181     .entry  = { &(n).entry, &(n).entry },               \
0182     .func = (f),                            \
0183     __WORK_INIT_LOCKDEP_MAP(#n, &(n))               \
0184     }
0185 
0186 #define __DELAYED_WORK_INITIALIZER(n, f, tflags) {          \
0187     .work = __WORK_INITIALIZER((n).work, (f)),          \
0188     .timer = __TIMER_INITIALIZER(delayed_work_timer_fn,\
0189                      (tflags) | TIMER_IRQSAFE),     \
0190     }
0191 
0192 #define DECLARE_WORK(n, f)                      \
0193     struct work_struct n = __WORK_INITIALIZER(n, f)
0194 
0195 #define DECLARE_DELAYED_WORK(n, f)                  \
0196     struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, 0)
0197 
0198 #define DECLARE_DEFERRABLE_WORK(n, f)                   \
0199     struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, TIMER_DEFERRABLE)
0200 
0201 #ifdef CONFIG_DEBUG_OBJECTS_WORK
0202 extern void __init_work(struct work_struct *work, int onstack);
0203 extern void destroy_work_on_stack(struct work_struct *work);
0204 extern void destroy_delayed_work_on_stack(struct delayed_work *work);
0205 static inline unsigned int work_static(struct work_struct *work)
0206 {
0207     return *work_data_bits(work) & WORK_STRUCT_STATIC;
0208 }
0209 #else
0210 static inline void __init_work(struct work_struct *work, int onstack) { }
0211 static inline void destroy_work_on_stack(struct work_struct *work) { }
0212 static inline void destroy_delayed_work_on_stack(struct delayed_work *work) { }
0213 static inline unsigned int work_static(struct work_struct *work) { return 0; }
0214 #endif
0215 
0216 /*
0217  * initialize all of a work item in one go
0218  *
0219  * NOTE! No point in using "atomic_long_set()": using a direct
0220  * assignment of the work data initializer allows the compiler
0221  * to generate better code.
0222  */
0223 #ifdef CONFIG_LOCKDEP
0224 #define __INIT_WORK(_work, _func, _onstack)             \
0225     do {                                \
0226         static struct lock_class_key __key;         \
0227                                     \
0228         __init_work((_work), _onstack);             \
0229         (_work)->data = (atomic_long_t) WORK_DATA_INIT();   \
0230         lockdep_init_map(&(_work)->lockdep_map, "(work_completion)"#_work, &__key, 0); \
0231         INIT_LIST_HEAD(&(_work)->entry);            \
0232         (_work)->func = (_func);                \
0233     } while (0)
0234 #else
0235 #define __INIT_WORK(_work, _func, _onstack)             \
0236     do {                                \
0237         __init_work((_work), _onstack);             \
0238         (_work)->data = (atomic_long_t) WORK_DATA_INIT();   \
0239         INIT_LIST_HEAD(&(_work)->entry);            \
0240         (_work)->func = (_func);                \
0241     } while (0)
0242 #endif
0243 
0244 #define INIT_WORK(_work, _func)                     \
0245     __INIT_WORK((_work), (_func), 0)
0246 
0247 #define INIT_WORK_ONSTACK(_work, _func)                 \
0248     __INIT_WORK((_work), (_func), 1)
0249 
0250 #define __INIT_DELAYED_WORK(_work, _func, _tflags)          \
0251     do {                                \
0252         INIT_WORK(&(_work)->work, (_func));         \
0253         __init_timer(&(_work)->timer,               \
0254                  delayed_work_timer_fn,         \
0255                  (_tflags) | TIMER_IRQSAFE);        \
0256     } while (0)
0257 
0258 #define __INIT_DELAYED_WORK_ONSTACK(_work, _func, _tflags)      \
0259     do {                                \
0260         INIT_WORK_ONSTACK(&(_work)->work, (_func));     \
0261         __init_timer_on_stack(&(_work)->timer,          \
0262                       delayed_work_timer_fn,        \
0263                       (_tflags) | TIMER_IRQSAFE);   \
0264     } while (0)
0265 
0266 #define INIT_DELAYED_WORK(_work, _func)                 \
0267     __INIT_DELAYED_WORK(_work, _func, 0)
0268 
0269 #define INIT_DELAYED_WORK_ONSTACK(_work, _func)             \
0270     __INIT_DELAYED_WORK_ONSTACK(_work, _func, 0)
0271 
0272 #define INIT_DEFERRABLE_WORK(_work, _func)              \
0273     __INIT_DELAYED_WORK(_work, _func, TIMER_DEFERRABLE)
0274 
0275 #define INIT_DEFERRABLE_WORK_ONSTACK(_work, _func)          \
0276     __INIT_DELAYED_WORK_ONSTACK(_work, _func, TIMER_DEFERRABLE)
0277 
0278 #define INIT_RCU_WORK(_work, _func)                 \
0279     INIT_WORK(&(_work)->work, (_func))
0280 
0281 #define INIT_RCU_WORK_ONSTACK(_work, _func)             \
0282     INIT_WORK_ONSTACK(&(_work)->work, (_func))
0283 
0284 /**
0285  * work_pending - Find out whether a work item is currently pending
0286  * @work: The work item in question
0287  */
0288 #define work_pending(work) \
0289     test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
0290 
0291 /**
0292  * delayed_work_pending - Find out whether a delayable work item is currently
0293  * pending
0294  * @w: The work item in question
0295  */
0296 #define delayed_work_pending(w) \
0297     work_pending(&(w)->work)
0298 
0299 /*
0300  * Workqueue flags and constants.  For details, please refer to
0301  * Documentation/core-api/workqueue.rst.
0302  */
0303 enum {
0304     WQ_UNBOUND      = 1 << 1, /* not bound to any cpu */
0305     WQ_FREEZABLE        = 1 << 2, /* freeze during suspend */
0306     WQ_MEM_RECLAIM      = 1 << 3, /* may be used for memory reclaim */
0307     WQ_HIGHPRI      = 1 << 4, /* high priority */
0308     WQ_CPU_INTENSIVE    = 1 << 5, /* cpu intensive workqueue */
0309     WQ_SYSFS        = 1 << 6, /* visible in sysfs, see workqueue_sysfs_register() */
0310 
0311     /*
0312      * Per-cpu workqueues are generally preferred because they tend to
0313      * show better performance thanks to cache locality.  Per-cpu
0314      * workqueues exclude the scheduler from choosing the CPU to
0315      * execute the worker threads, which has an unfortunate side effect
0316      * of increasing power consumption.
0317      *
0318      * The scheduler considers a CPU idle if it doesn't have any task
0319      * to execute and tries to keep idle cores idle to conserve power;
0320      * however, for example, a per-cpu work item scheduled from an
0321      * interrupt handler on an idle CPU will force the scheduler to
0322      * execute the work item on that CPU breaking the idleness, which in
0323      * turn may lead to more scheduling choices which are sub-optimal
0324      * in terms of power consumption.
0325      *
0326      * Workqueues marked with WQ_POWER_EFFICIENT are per-cpu by default
0327      * but become unbound if workqueue.power_efficient kernel param is
0328      * specified.  Per-cpu workqueues which are identified to
0329      * contribute significantly to power-consumption are identified and
0330      * marked with this flag and enabling the power_efficient mode
0331      * leads to noticeable power saving at the cost of small
0332      * performance disadvantage.
0333      *
0334      * http://thread.gmane.org/gmane.linux.kernel/1480396
0335      */
0336     WQ_POWER_EFFICIENT  = 1 << 7,
0337 
0338     __WQ_DRAINING       = 1 << 16, /* internal: workqueue is draining */
0339     __WQ_ORDERED        = 1 << 17, /* internal: workqueue is ordered */
0340     __WQ_LEGACY     = 1 << 18, /* internal: create*_workqueue() */
0341     __WQ_ORDERED_EXPLICIT   = 1 << 19, /* internal: alloc_ordered_workqueue() */
0342 
0343     WQ_MAX_ACTIVE       = 512,    /* I like 512, better ideas? */
0344     WQ_MAX_UNBOUND_PER_CPU  = 4,      /* 4 * #cpus for unbound wq */
0345     WQ_DFL_ACTIVE       = WQ_MAX_ACTIVE / 2,
0346 };
0347 
0348 /* unbound wq's aren't per-cpu, scale max_active according to #cpus */
0349 #define WQ_UNBOUND_MAX_ACTIVE   \
0350     max_t(int, WQ_MAX_ACTIVE, num_possible_cpus() * WQ_MAX_UNBOUND_PER_CPU)
0351 
0352 /*
0353  * System-wide workqueues which are always present.
0354  *
0355  * system_wq is the one used by schedule[_delayed]_work[_on]().
0356  * Multi-CPU multi-threaded.  There are users which expect relatively
0357  * short queue flush time.  Don't queue works which can run for too
0358  * long.
0359  *
0360  * system_highpri_wq is similar to system_wq but for work items which
0361  * require WQ_HIGHPRI.
0362  *
0363  * system_long_wq is similar to system_wq but may host long running
0364  * works.  Queue flushing might take relatively long.
0365  *
0366  * system_unbound_wq is unbound workqueue.  Workers are not bound to
0367  * any specific CPU, not concurrency managed, and all queued works are
0368  * executed immediately as long as max_active limit is not reached and
0369  * resources are available.
0370  *
0371  * system_freezable_wq is equivalent to system_wq except that it's
0372  * freezable.
0373  *
0374  * *_power_efficient_wq are inclined towards saving power and converted
0375  * into WQ_UNBOUND variants if 'wq_power_efficient' is enabled; otherwise,
0376  * they are same as their non-power-efficient counterparts - e.g.
0377  * system_power_efficient_wq is identical to system_wq if
0378  * 'wq_power_efficient' is disabled.  See WQ_POWER_EFFICIENT for more info.
0379  */
0380 extern struct workqueue_struct *system_wq;
0381 extern struct workqueue_struct *system_highpri_wq;
0382 extern struct workqueue_struct *system_long_wq;
0383 extern struct workqueue_struct *system_unbound_wq;
0384 extern struct workqueue_struct *system_freezable_wq;
0385 extern struct workqueue_struct *system_power_efficient_wq;
0386 extern struct workqueue_struct *system_freezable_power_efficient_wq;
0387 
0388 /**
0389  * alloc_workqueue - allocate a workqueue
0390  * @fmt: printf format for the name of the workqueue
0391  * @flags: WQ_* flags
0392  * @max_active: max in-flight work items, 0 for default
0393  * remaining args: args for @fmt
0394  *
0395  * Allocate a workqueue with the specified parameters.  For detailed
0396  * information on WQ_* flags, please refer to
0397  * Documentation/core-api/workqueue.rst.
0398  *
0399  * RETURNS:
0400  * Pointer to the allocated workqueue on success, %NULL on failure.
0401  */
0402 __printf(1, 4) struct workqueue_struct *
0403 alloc_workqueue(const char *fmt, unsigned int flags, int max_active, ...);
0404 
0405 /**
0406  * alloc_ordered_workqueue - allocate an ordered workqueue
0407  * @fmt: printf format for the name of the workqueue
0408  * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful)
0409  * @args: args for @fmt
0410  *
0411  * Allocate an ordered workqueue.  An ordered workqueue executes at
0412  * most one work item at any given time in the queued order.  They are
0413  * implemented as unbound workqueues with @max_active of one.
0414  *
0415  * RETURNS:
0416  * Pointer to the allocated workqueue on success, %NULL on failure.
0417  */
0418 #define alloc_ordered_workqueue(fmt, flags, args...)            \
0419     alloc_workqueue(fmt, WQ_UNBOUND | __WQ_ORDERED |        \
0420             __WQ_ORDERED_EXPLICIT | (flags), 1, ##args)
0421 
0422 #define create_workqueue(name)                      \
0423     alloc_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, 1, (name))
0424 #define create_freezable_workqueue(name)                \
0425     alloc_workqueue("%s", __WQ_LEGACY | WQ_FREEZABLE | WQ_UNBOUND | \
0426             WQ_MEM_RECLAIM, 1, (name))
0427 #define create_singlethread_workqueue(name)             \
0428     alloc_ordered_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, name)
0429 
0430 extern void destroy_workqueue(struct workqueue_struct *wq);
0431 
0432 struct workqueue_attrs *alloc_workqueue_attrs(void);
0433 void free_workqueue_attrs(struct workqueue_attrs *attrs);
0434 int apply_workqueue_attrs(struct workqueue_struct *wq,
0435               const struct workqueue_attrs *attrs);
0436 int workqueue_set_unbound_cpumask(cpumask_var_t cpumask);
0437 
0438 extern bool queue_work_on(int cpu, struct workqueue_struct *wq,
0439             struct work_struct *work);
0440 extern bool queue_work_node(int node, struct workqueue_struct *wq,
0441                 struct work_struct *work);
0442 extern bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
0443             struct delayed_work *work, unsigned long delay);
0444 extern bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
0445             struct delayed_work *dwork, unsigned long delay);
0446 extern bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork);
0447 
0448 extern void __flush_workqueue(struct workqueue_struct *wq);
0449 extern void drain_workqueue(struct workqueue_struct *wq);
0450 
0451 extern int schedule_on_each_cpu(work_func_t func);
0452 
0453 int execute_in_process_context(work_func_t fn, struct execute_work *);
0454 
0455 extern bool flush_work(struct work_struct *work);
0456 extern bool cancel_work(struct work_struct *work);
0457 extern bool cancel_work_sync(struct work_struct *work);
0458 
0459 extern bool flush_delayed_work(struct delayed_work *dwork);
0460 extern bool cancel_delayed_work(struct delayed_work *dwork);
0461 extern bool cancel_delayed_work_sync(struct delayed_work *dwork);
0462 
0463 extern bool flush_rcu_work(struct rcu_work *rwork);
0464 
0465 extern void workqueue_set_max_active(struct workqueue_struct *wq,
0466                      int max_active);
0467 extern struct work_struct *current_work(void);
0468 extern bool current_is_workqueue_rescuer(void);
0469 extern bool workqueue_congested(int cpu, struct workqueue_struct *wq);
0470 extern unsigned int work_busy(struct work_struct *work);
0471 extern __printf(1, 2) void set_worker_desc(const char *fmt, ...);
0472 extern void print_worker_info(const char *log_lvl, struct task_struct *task);
0473 extern void show_all_workqueues(void);
0474 extern void show_one_workqueue(struct workqueue_struct *wq);
0475 extern void wq_worker_comm(char *buf, size_t size, struct task_struct *task);
0476 
0477 /**
0478  * queue_work - queue work on a workqueue
0479  * @wq: workqueue to use
0480  * @work: work to queue
0481  *
0482  * Returns %false if @work was already on a queue, %true otherwise.
0483  *
0484  * We queue the work to the CPU on which it was submitted, but if the CPU dies
0485  * it can be processed by another CPU.
0486  *
0487  * Memory-ordering properties:  If it returns %true, guarantees that all stores
0488  * preceding the call to queue_work() in the program order will be visible from
0489  * the CPU which will execute @work by the time such work executes, e.g.,
0490  *
0491  * { x is initially 0 }
0492  *
0493  *   CPU0               CPU1
0494  *
0495  *   WRITE_ONCE(x, 1);          [ @work is being executed ]
0496  *   r0 = queue_work(wq, work);       r1 = READ_ONCE(x);
0497  *
0498  * Forbids: r0 == true && r1 == 0
0499  */
0500 static inline bool queue_work(struct workqueue_struct *wq,
0501                   struct work_struct *work)
0502 {
0503     return queue_work_on(WORK_CPU_UNBOUND, wq, work);
0504 }
0505 
0506 /**
0507  * queue_delayed_work - queue work on a workqueue after delay
0508  * @wq: workqueue to use
0509  * @dwork: delayable work to queue
0510  * @delay: number of jiffies to wait before queueing
0511  *
0512  * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
0513  */
0514 static inline bool queue_delayed_work(struct workqueue_struct *wq,
0515                       struct delayed_work *dwork,
0516                       unsigned long delay)
0517 {
0518     return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
0519 }
0520 
0521 /**
0522  * mod_delayed_work - modify delay of or queue a delayed work
0523  * @wq: workqueue to use
0524  * @dwork: work to queue
0525  * @delay: number of jiffies to wait before queueing
0526  *
0527  * mod_delayed_work_on() on local CPU.
0528  */
0529 static inline bool mod_delayed_work(struct workqueue_struct *wq,
0530                     struct delayed_work *dwork,
0531                     unsigned long delay)
0532 {
0533     return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
0534 }
0535 
0536 /**
0537  * schedule_work_on - put work task on a specific cpu
0538  * @cpu: cpu to put the work task on
0539  * @work: job to be done
0540  *
0541  * This puts a job on a specific cpu
0542  */
0543 static inline bool schedule_work_on(int cpu, struct work_struct *work)
0544 {
0545     return queue_work_on(cpu, system_wq, work);
0546 }
0547 
0548 /**
0549  * schedule_work - put work task in global workqueue
0550  * @work: job to be done
0551  *
0552  * Returns %false if @work was already on the kernel-global workqueue and
0553  * %true otherwise.
0554  *
0555  * This puts a job in the kernel-global workqueue if it was not already
0556  * queued and leaves it in the same position on the kernel-global
0557  * workqueue otherwise.
0558  *
0559  * Shares the same memory-ordering properties of queue_work(), cf. the
0560  * DocBook header of queue_work().
0561  */
0562 static inline bool schedule_work(struct work_struct *work)
0563 {
0564     return queue_work(system_wq, work);
0565 }
0566 
0567 /*
0568  * Detect attempt to flush system-wide workqueues at compile time when possible.
0569  *
0570  * See https://lkml.kernel.org/r/49925af7-78a8-a3dd-bce6-cfc02e1a9236@I-love.SAKURA.ne.jp
0571  * for reasons and steps for converting system-wide workqueues into local workqueues.
0572  */
0573 extern void __warn_flushing_systemwide_wq(void)
0574     __compiletime_warning("Please avoid flushing system-wide workqueues.");
0575 
0576 /**
0577  * flush_scheduled_work - ensure that any scheduled work has run to completion.
0578  *
0579  * Forces execution of the kernel-global workqueue and blocks until its
0580  * completion.
0581  *
0582  * It's very easy to get into trouble if you don't take great care.
0583  * Either of the following situations will lead to deadlock:
0584  *
0585  *  One of the work items currently on the workqueue needs to acquire
0586  *  a lock held by your code or its caller.
0587  *
0588  *  Your code is running in the context of a work routine.
0589  *
0590  * They will be detected by lockdep when they occur, but the first might not
0591  * occur very often.  It depends on what work items are on the workqueue and
0592  * what locks they need, which you have no control over.
0593  *
0594  * In most situations flushing the entire workqueue is overkill; you merely
0595  * need to know that a particular work item isn't queued and isn't running.
0596  * In such cases you should use cancel_delayed_work_sync() or
0597  * cancel_work_sync() instead.
0598  *
0599  * Please stop calling this function! A conversion to stop flushing system-wide
0600  * workqueues is in progress. This function will be removed after all in-tree
0601  * users stopped calling this function.
0602  */
0603 /*
0604  * The background of commit 771c035372a036f8 ("deprecate the
0605  * '__deprecated' attribute warnings entirely and for good") is that,
0606  * since Linus builds all modules between every single pull he does,
0607  * the standard kernel build needs to be _clean_ in order to be able to
0608  * notice when new problems happen. Therefore, don't emit warning while
0609  * there are in-tree users.
0610  */
0611 #define flush_scheduled_work()                      \
0612 ({                                  \
0613     if (0)                              \
0614         __warn_flushing_systemwide_wq();            \
0615     __flush_workqueue(system_wq);                   \
0616 })
0617 
0618 /*
0619  * Although there is no longer in-tree caller, for now just emit warning
0620  * in order to give out-of-tree callers time to update.
0621  */
0622 #define flush_workqueue(wq)                     \
0623 ({                                  \
0624     struct workqueue_struct *_wq = (wq);                \
0625                                     \
0626     if ((__builtin_constant_p(_wq == system_wq) &&          \
0627          _wq == system_wq) ||                   \
0628         (__builtin_constant_p(_wq == system_highpri_wq) &&      \
0629          _wq == system_highpri_wq) ||               \
0630         (__builtin_constant_p(_wq == system_long_wq) &&     \
0631          _wq == system_long_wq) ||                  \
0632         (__builtin_constant_p(_wq == system_unbound_wq) &&      \
0633          _wq == system_unbound_wq) ||               \
0634         (__builtin_constant_p(_wq == system_freezable_wq) &&    \
0635          _wq == system_freezable_wq) ||             \
0636         (__builtin_constant_p(_wq == system_power_efficient_wq) &&  \
0637          _wq == system_power_efficient_wq) ||           \
0638         (__builtin_constant_p(_wq == system_freezable_power_efficient_wq) && \
0639          _wq == system_freezable_power_efficient_wq))       \
0640         __warn_flushing_systemwide_wq();            \
0641     __flush_workqueue(_wq);                     \
0642 })
0643 
0644 /**
0645  * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
0646  * @cpu: cpu to use
0647  * @dwork: job to be done
0648  * @delay: number of jiffies to wait
0649  *
0650  * After waiting for a given time this puts a job in the kernel-global
0651  * workqueue on the specified CPU.
0652  */
0653 static inline bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
0654                         unsigned long delay)
0655 {
0656     return queue_delayed_work_on(cpu, system_wq, dwork, delay);
0657 }
0658 
0659 /**
0660  * schedule_delayed_work - put work task in global workqueue after delay
0661  * @dwork: job to be done
0662  * @delay: number of jiffies to wait or 0 for immediate execution
0663  *
0664  * After waiting for a given time this puts a job in the kernel-global
0665  * workqueue.
0666  */
0667 static inline bool schedule_delayed_work(struct delayed_work *dwork,
0668                      unsigned long delay)
0669 {
0670     return queue_delayed_work(system_wq, dwork, delay);
0671 }
0672 
0673 #ifndef CONFIG_SMP
0674 static inline long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
0675 {
0676     return fn(arg);
0677 }
0678 static inline long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg)
0679 {
0680     return fn(arg);
0681 }
0682 #else
0683 long work_on_cpu(int cpu, long (*fn)(void *), void *arg);
0684 long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg);
0685 #endif /* CONFIG_SMP */
0686 
0687 #ifdef CONFIG_FREEZER
0688 extern void freeze_workqueues_begin(void);
0689 extern bool freeze_workqueues_busy(void);
0690 extern void thaw_workqueues(void);
0691 #endif /* CONFIG_FREEZER */
0692 
0693 #ifdef CONFIG_SYSFS
0694 int workqueue_sysfs_register(struct workqueue_struct *wq);
0695 #else   /* CONFIG_SYSFS */
0696 static inline int workqueue_sysfs_register(struct workqueue_struct *wq)
0697 { return 0; }
0698 #endif  /* CONFIG_SYSFS */
0699 
0700 #ifdef CONFIG_WQ_WATCHDOG
0701 void wq_watchdog_touch(int cpu);
0702 #else   /* CONFIG_WQ_WATCHDOG */
0703 static inline void wq_watchdog_touch(int cpu) { }
0704 #endif  /* CONFIG_WQ_WATCHDOG */
0705 
0706 #ifdef CONFIG_SMP
0707 int workqueue_prepare_cpu(unsigned int cpu);
0708 int workqueue_online_cpu(unsigned int cpu);
0709 int workqueue_offline_cpu(unsigned int cpu);
0710 #endif
0711 
0712 void __init workqueue_init_early(void);
0713 void __init workqueue_init(void);
0714 
0715 #endif