22
22
*/
23
23
#define work_data_bits (work ) ((unsigned long *)(&(work)->data))
24
24
25
- enum {
25
+ enum work_bits {
26
26
WORK_STRUCT_PENDING_BIT = 0 , /* work item is pending execution */
27
- WORK_STRUCT_INACTIVE_BIT = 1 , /* work item is inactive */
28
- WORK_STRUCT_PWQ_BIT = 2 , /* data points to pwq */
29
- WORK_STRUCT_LINKED_BIT = 3 , /* next work is linked to this one */
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 */
30
30
#ifdef CONFIG_DEBUG_OBJECTS_WORK
31
- WORK_STRUCT_STATIC_BIT = 4 , /* static initializer (debugobjects) */
32
- WORK_STRUCT_COLOR_SHIFT = 5 , /* color for workqueue flushing */
33
- #else
34
- WORK_STRUCT_COLOR_SHIFT = 4 , /* color for workqueue flushing */
31
+ WORK_STRUCT_STATIC_BIT , /* static initializer (debugobjects) */
35
32
#endif
33
+ WORK_STRUCT_FLAG_BITS ,
36
34
35
+ /* color for workqueue flushing */
36
+ WORK_STRUCT_COLOR_SHIFT = WORK_STRUCT_FLAG_BITS ,
37
37
WORK_STRUCT_COLOR_BITS = 4 ,
38
38
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 {
39
73
WORK_STRUCT_PENDING = 1 << WORK_STRUCT_PENDING_BIT ,
40
74
WORK_STRUCT_INACTIVE = 1 << WORK_STRUCT_INACTIVE_BIT ,
41
75
WORK_STRUCT_PWQ = 1 << WORK_STRUCT_PWQ_BIT ,
@@ -45,35 +79,14 @@ enum {
45
79
#else
46
80
WORK_STRUCT_STATIC = 0 ,
47
81
#endif
82
+ };
48
83
84
+ enum wq_misc_consts {
49
85
WORK_NR_COLORS = (1 << WORK_STRUCT_COLOR_BITS ),
50
86
51
87
/* not bound to any CPU, prefer the local CPU */
52
88
WORK_CPU_UNBOUND = NR_CPUS ,
53
89
54
- /*
55
- * Reserve 8 bits off of pwq pointer w/ debugobjects turned off.
56
- * This makes pwqs aligned to 256 bytes and allows 16 workqueue
57
- * flush colors.
58
- */
59
- WORK_STRUCT_FLAG_BITS = WORK_STRUCT_COLOR_SHIFT +
60
- WORK_STRUCT_COLOR_BITS ,
61
-
62
- /* data contains off-queue information when !WORK_STRUCT_PWQ */
63
- WORK_OFFQ_FLAG_BASE = WORK_STRUCT_COLOR_SHIFT ,
64
-
65
- __WORK_OFFQ_CANCELING = WORK_OFFQ_FLAG_BASE ,
66
-
67
- /*
68
- * When a work item is off queue, its high bits point to the last
69
- * pool it was on. Cap at 31 bits and use the highest number to
70
- * indicate that no pool is associated.
71
- */
72
- WORK_OFFQ_FLAG_BITS = 1 ,
73
- WORK_OFFQ_POOL_SHIFT = WORK_OFFQ_FLAG_BASE + WORK_OFFQ_FLAG_BITS ,
74
- WORK_OFFQ_LEFT = BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT ,
75
- WORK_OFFQ_POOL_BITS = WORK_OFFQ_LEFT <= 31 ? WORK_OFFQ_LEFT : 31 ,
76
-
77
90
/* bit mask for work_busy() return values */
78
91
WORK_BUSY_PENDING = 1 << 0 ,
79
92
WORK_BUSY_RUNNING = 1 << 1 ,
@@ -83,12 +96,10 @@ enum {
83
96
};
84
97
85
98
/* 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 )
87
100
#define WORK_OFFQ_POOL_NONE ((1ul << WORK_OFFQ_POOL_BITS) - 1)
88
101
#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))
92
103
93
104
#define WORK_DATA_INIT () ATOMIC_LONG_INIT((unsigned long)WORK_STRUCT_NO_POOL)
94
105
#define WORK_DATA_STATIC_INIT () \
@@ -347,7 +358,8 @@ static inline unsigned int work_static(struct work_struct *work) { return 0; }
347
358
* Workqueue flags and constants. For details, please refer to
348
359
* Documentation/core-api/workqueue.rst.
349
360
*/
350
- enum {
361
+ enum wq_flags {
362
+ WQ_BH = 1 << 0 , /* execute in bottom half (softirq) context */
351
363
WQ_UNBOUND = 1 << 1 , /* not bound to any cpu */
352
364
WQ_FREEZABLE = 1 << 2 , /* freeze during suspend */
353
365
WQ_MEM_RECLAIM = 1 << 3 , /* may be used for memory reclaim */
@@ -386,11 +398,22 @@ enum {
386
398
__WQ_DRAINING = 1 << 16 , /* internal: workqueue is draining */
387
399
__WQ_ORDERED = 1 << 17 , /* internal: workqueue is ordered */
388
400
__WQ_LEGACY = 1 << 18 , /* internal: create*_workqueue() */
389
- __WQ_ORDERED_EXPLICIT = 1 << 19 , /* internal: alloc_ordered_workqueue() */
390
401
402
+ /* BH wq only allows the following flags */
403
+ __WQ_BH_ALLOWS = WQ_BH | WQ_HIGHPRI ,
404
+ };
405
+
406
+ enum wq_consts {
391
407
WQ_MAX_ACTIVE = 512 , /* I like 512, better ideas? */
392
408
WQ_UNBOUND_MAX_ACTIVE = WQ_MAX_ACTIVE ,
393
409
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 ,
394
417
};
395
418
396
419
/*
@@ -420,6 +443,9 @@ enum {
420
443
* they are same as their non-power-efficient counterparts - e.g.
421
444
* system_power_efficient_wq is identical to system_wq if
422
445
* '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.
423
449
*/
424
450
extern struct workqueue_struct * system_wq ;
425
451
extern struct workqueue_struct * system_highpri_wq ;
@@ -428,16 +454,43 @@ extern struct workqueue_struct *system_unbound_wq;
428
454
extern struct workqueue_struct * system_freezable_wq ;
429
455
extern struct workqueue_struct * system_power_efficient_wq ;
430
456
extern 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 );
431
462
432
463
/**
433
464
* alloc_workqueue - allocate a workqueue
434
465
* @fmt: printf format for the name of the workqueue
435
466
* @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
437
468
* remaining args: args for @fmt
438
469
*
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
441
494
* Documentation/core-api/workqueue.rst.
442
495
*
443
496
* RETURNS:
@@ -460,8 +513,7 @@ alloc_workqueue(const char *fmt, unsigned int flags, int max_active, ...);
460
513
* Pointer to the allocated workqueue on success, %NULL on failure.
461
514
*/
462
515
#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)
465
517
466
518
#define create_workqueue (name ) \
467
519
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, ...);
471
523
#define create_singlethread_workqueue (name ) \
472
524
alloc_ordered_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, name)
473
525
526
+ #define from_work (var , callback_work , work_fieldname ) \
527
+ container_of(callback_work, typeof(*var), work_fieldname)
528
+
474
529
extern void destroy_workqueue (struct workqueue_struct * wq );
475
530
476
531
struct workqueue_attrs * alloc_workqueue_attrs (void );
@@ -508,6 +563,8 @@ extern bool flush_rcu_work(struct rcu_work *rwork);
508
563
509
564
extern void workqueue_set_max_active (struct workqueue_struct * wq ,
510
565
int max_active );
566
+ extern void workqueue_set_min_active (struct workqueue_struct * wq ,
567
+ int min_active );
511
568
extern struct work_struct * current_work (void );
512
569
extern bool current_is_workqueue_rescuer (void );
513
570
extern bool workqueue_congested (int cpu , struct workqueue_struct * wq );
0 commit comments