2222 */
2323#define work_data_bits (work ) ((unsigned long *)(&(work)->data))
2424
25- enum {
25+ enum work_bits {
2626 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 */
27+ WORK_STRUCT_INACTIVE_BIT , /* work item is inactive */
28+ WORK_STRUCT_PWQ_BIT , /* data points to pwq */
29+ WORK_STRUCT_LINKED_BIT , /* next work is linked to this one */
3030#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 */
31+ WORK_STRUCT_STATIC_BIT , /* static initializer (debugobjects) */
3532#endif
33+ WORK_STRUCT_FLAG_BITS ,
3634
35+ /* color for workqueue flushing */
36+ WORK_STRUCT_COLOR_SHIFT = WORK_STRUCT_FLAG_BITS ,
3737 WORK_STRUCT_COLOR_BITS = 4 ,
3838
39+ /*
40+ * When WORK_STRUCT_PWQ is set, reserve 8 bits off of pwq pointer w/
41+ * debugobjects turned off. This makes pwqs aligned to 256 bytes (512
42+ * bytes w/ DEBUG_OBJECTS_WORK) and allows 16 workqueue flush colors.
43+ *
44+ * MSB
45+ * [ pwq pointer ] [ flush color ] [ STRUCT flags ]
46+ * 4 bits 4 or 5 bits
47+ */
48+ WORK_STRUCT_PWQ_SHIFT = WORK_STRUCT_COLOR_SHIFT + WORK_STRUCT_COLOR_BITS ,
49+
50+ /*
51+ * data contains off-queue information when !WORK_STRUCT_PWQ.
52+ *
53+ * MSB
54+ * [ pool ID ] [ OFFQ flags ] [ STRUCT flags ]
55+ * 1 bit 4 or 5 bits
56+ */
57+ WORK_OFFQ_FLAG_SHIFT = WORK_STRUCT_FLAG_BITS ,
58+ WORK_OFFQ_CANCELING_BIT = WORK_OFFQ_FLAG_SHIFT ,
59+ WORK_OFFQ_FLAG_END ,
60+ WORK_OFFQ_FLAG_BITS = WORK_OFFQ_FLAG_END - WORK_OFFQ_FLAG_SHIFT ,
61+
62+ /*
63+ * When a work item is off queue, the high bits encode off-queue flags
64+ * and the last pool it was on. Cap pool ID to 31 bits and use the
65+ * highest number to indicate that no pool is associated.
66+ */
67+ WORK_OFFQ_POOL_SHIFT = WORK_OFFQ_FLAG_SHIFT + WORK_OFFQ_FLAG_BITS ,
68+ WORK_OFFQ_LEFT = BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT ,
69+ WORK_OFFQ_POOL_BITS = WORK_OFFQ_LEFT <= 31 ? WORK_OFFQ_LEFT : 31 ,
70+ };
71+
72+ enum work_flags {
3973 WORK_STRUCT_PENDING = 1 << WORK_STRUCT_PENDING_BIT ,
4074 WORK_STRUCT_INACTIVE = 1 << WORK_STRUCT_INACTIVE_BIT ,
4175 WORK_STRUCT_PWQ = 1 << WORK_STRUCT_PWQ_BIT ,
@@ -45,35 +79,14 @@ enum {
4579#else
4680 WORK_STRUCT_STATIC = 0 ,
4781#endif
82+ };
4883
84+ enum wq_misc_consts {
4985 WORK_NR_COLORS = (1 << WORK_STRUCT_COLOR_BITS ),
5086
5187 /* not bound to any CPU, prefer the local CPU */
5288 WORK_CPU_UNBOUND = NR_CPUS ,
5389
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-
7790 /* bit mask for work_busy() return values */
7891 WORK_BUSY_PENDING = 1 << 0 ,
7992 WORK_BUSY_RUNNING = 1 << 1 ,
@@ -83,12 +96,10 @@ enum {
8396};
8497
8598/* Convenience constants - of type 'unsigned long', not 'enum'! */
86- #define WORK_OFFQ_CANCELING (1ul << __WORK_OFFQ_CANCELING )
99+ #define WORK_OFFQ_CANCELING (1ul << WORK_OFFQ_CANCELING_BIT )
87100#define WORK_OFFQ_POOL_NONE ((1ul << WORK_OFFQ_POOL_BITS) - 1)
88101#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)
102+ #define WORK_STRUCT_PWQ_MASK (~((1ul << WORK_STRUCT_PWQ_SHIFT) - 1))
92103
93104#define WORK_DATA_INIT () ATOMIC_LONG_INIT((unsigned long)WORK_STRUCT_NO_POOL)
94105#define WORK_DATA_STATIC_INIT () \
@@ -347,7 +358,8 @@ static inline unsigned int work_static(struct work_struct *work) { return 0; }
347358 * Workqueue flags and constants. For details, please refer to
348359 * Documentation/core-api/workqueue.rst.
349360 */
350- enum {
361+ enum wq_flags {
362+ WQ_BH = 1 << 0 , /* execute in bottom half (softirq) context */
351363 WQ_UNBOUND = 1 << 1 , /* not bound to any cpu */
352364 WQ_FREEZABLE = 1 << 2 , /* freeze during suspend */
353365 WQ_MEM_RECLAIM = 1 << 3 , /* may be used for memory reclaim */
@@ -386,11 +398,22 @@ enum {
386398 __WQ_DRAINING = 1 << 16 , /* internal: workqueue is draining */
387399 __WQ_ORDERED = 1 << 17 , /* internal: workqueue is ordered */
388400 __WQ_LEGACY = 1 << 18 , /* internal: create*_workqueue() */
389- __WQ_ORDERED_EXPLICIT = 1 << 19 , /* internal: alloc_ordered_workqueue() */
390401
402+ /* BH wq only allows the following flags */
403+ __WQ_BH_ALLOWS = WQ_BH | WQ_HIGHPRI ,
404+ };
405+
406+ enum wq_consts {
391407 WQ_MAX_ACTIVE = 512 , /* I like 512, better ideas? */
392408 WQ_UNBOUND_MAX_ACTIVE = WQ_MAX_ACTIVE ,
393409 WQ_DFL_ACTIVE = WQ_MAX_ACTIVE / 2 ,
410+
411+ /*
412+ * Per-node default cap on min_active. Unless explicitly set, min_active
413+ * is set to min(max_active, WQ_DFL_MIN_ACTIVE). For more details, see
414+ * workqueue_struct->min_active definition.
415+ */
416+ WQ_DFL_MIN_ACTIVE = 8 ,
394417};
395418
396419/*
@@ -420,6 +443,9 @@ enum {
420443 * they are same as their non-power-efficient counterparts - e.g.
421444 * system_power_efficient_wq is identical to system_wq if
422445 * 'wq_power_efficient' is disabled. See WQ_POWER_EFFICIENT for more info.
446+ *
447+ * system_bh[_highpri]_wq are convenience interface to softirq. BH work items
448+ * are executed in the queueing CPU's BH context in the queueing order.
423449 */
424450extern struct workqueue_struct * system_wq ;
425451extern struct workqueue_struct * system_highpri_wq ;
@@ -428,16 +454,43 @@ extern struct workqueue_struct *system_unbound_wq;
428454extern struct workqueue_struct * system_freezable_wq ;
429455extern struct workqueue_struct * system_power_efficient_wq ;
430456extern struct workqueue_struct * system_freezable_power_efficient_wq ;
457+ extern struct workqueue_struct * system_bh_wq ;
458+ extern struct workqueue_struct * system_bh_highpri_wq ;
459+
460+ void workqueue_softirq_action (bool highpri );
461+ void workqueue_softirq_dead (unsigned int cpu );
431462
432463/**
433464 * alloc_workqueue - allocate a workqueue
434465 * @fmt: printf format for the name of the workqueue
435466 * @flags: WQ_* flags
436- * @max_active: max in-flight work items per CPU , 0 for default
467+ * @max_active: max in-flight work items, 0 for default
437468 * remaining args: args for @fmt
438469 *
439- * Allocate a workqueue with the specified parameters. For detailed
440- * information on WQ_* flags, please refer to
470+ * For a per-cpu workqueue, @max_active limits the number of in-flight work
471+ * items for each CPU. e.g. @max_active of 1 indicates that each CPU can be
472+ * executing at most one work item for the workqueue.
473+ *
474+ * For unbound workqueues, @max_active limits the number of in-flight work items
475+ * for the whole system. e.g. @max_active of 16 indicates that that there can be
476+ * at most 16 work items executing for the workqueue in the whole system.
477+ *
478+ * As sharing the same active counter for an unbound workqueue across multiple
479+ * NUMA nodes can be expensive, @max_active is distributed to each NUMA node
480+ * according to the proportion of the number of online CPUs and enforced
481+ * independently.
482+ *
483+ * Depending on online CPU distribution, a node may end up with per-node
484+ * max_active which is significantly lower than @max_active, which can lead to
485+ * deadlocks if the per-node concurrency limit is lower than the maximum number
486+ * of interdependent work items for the workqueue.
487+ *
488+ * To guarantee forward progress regardless of online CPU distribution, the
489+ * concurrency limit on every node is guaranteed to be equal to or greater than
490+ * min_active which is set to min(@max_active, %WQ_DFL_MIN_ACTIVE). This means
491+ * that the sum of per-node max_active's may be larger than @max_active.
492+ *
493+ * For detailed information on %WQ_* flags, please refer to
441494 * Documentation/core-api/workqueue.rst.
442495 *
443496 * RETURNS:
@@ -460,8 +513,7 @@ alloc_workqueue(const char *fmt, unsigned int flags, int max_active, ...);
460513 * Pointer to the allocated workqueue on success, %NULL on failure.
461514 */
462515#define alloc_ordered_workqueue (fmt , flags , args ...) \
463- alloc_workqueue(fmt, WQ_UNBOUND | __WQ_ORDERED | \
464- __WQ_ORDERED_EXPLICIT | (flags), 1, ##args)
516+ alloc_workqueue(fmt, WQ_UNBOUND | __WQ_ORDERED | (flags), 1, ##args)
465517
466518#define create_workqueue (name ) \
467519 alloc_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, 1, (name))
@@ -471,6 +523,9 @@ alloc_workqueue(const char *fmt, unsigned int flags, int max_active, ...);
471523#define create_singlethread_workqueue (name ) \
472524 alloc_ordered_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, name)
473525
526+ #define from_work (var , callback_work , work_fieldname ) \
527+ container_of(callback_work, typeof(*var), work_fieldname)
528+
474529extern void destroy_workqueue (struct workqueue_struct * wq );
475530
476531struct workqueue_attrs * alloc_workqueue_attrs (void );
@@ -508,6 +563,8 @@ extern bool flush_rcu_work(struct rcu_work *rwork);
508563
509564extern void workqueue_set_max_active (struct workqueue_struct * wq ,
510565 int max_active );
566+ extern void workqueue_set_min_active (struct workqueue_struct * wq ,
567+ int min_active );
511568extern struct work_struct * current_work (void );
512569extern bool current_is_workqueue_rescuer (void );
513570extern bool workqueue_congested (int cpu , struct workqueue_struct * wq );
0 commit comments