Skip to content

Commit e9a8e01

Browse files
committed
workqueue: Clean up enum work_bits and related constants
The bits of work->data are used for a few different purposes. How the bits are used is determined by enum work_bits. The planned disable/enable support will add another use, so let's clean it up a bit in preparation. - Let WORK_STRUCT_*_BIT's values be determined by enum definition order. - Deliminate different bit sections the same way using SHIFT and BITS values. - Rename __WORK_OFFQ_CANCELING to WORK_OFFQ_CANCELING_BIT for consistency. - Introduce WORK_STRUCT_PWQ_SHIFT and replace WORK_STRUCT_FLAG_MASK and WORK_STRUCT_WQ_DATA_MASK with WQ_STRUCT_PWQ_MASK for clarity. - Improve documentation. No functional changes. Signed-off-by: Tejun Heo <tj@kernel.org> Reviewed-by: Lai Jiangshan <jiangshanlai@gmail.com>
1 parent c5f5b94 commit e9a8e01

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

include/linux/workqueue.h

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,49 @@
2424

2525
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

3939
/*
40-
* Reserve 8 bits off of pwq pointer w/ debugobjects turned off.
41-
* This makes pwqs aligned to 256 bytes and allows 16 workqueue
42-
* flush colors.
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
4347
*/
44-
WORK_STRUCT_FLAG_BITS = WORK_STRUCT_COLOR_SHIFT +
45-
WORK_STRUCT_COLOR_BITS,
48+
WORK_STRUCT_PWQ_SHIFT = WORK_STRUCT_COLOR_SHIFT + WORK_STRUCT_COLOR_BITS,
4649

47-
/* data contains off-queue information when !WORK_STRUCT_PWQ */
48-
WORK_OFFQ_FLAG_BASE = WORK_STRUCT_COLOR_SHIFT,
49-
50-
__WORK_OFFQ_CANCELING = WORK_OFFQ_FLAG_BASE,
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,
5161

5262
/*
53-
* When a work item is off queue, its high bits point to the last
54-
* pool it was on. Cap at 31 bits and use the highest number to
55-
* indicate that no pool is associated.
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.
5666
*/
57-
WORK_OFFQ_FLAG_BITS = 1,
58-
WORK_OFFQ_POOL_SHIFT = WORK_OFFQ_FLAG_BASE + WORK_OFFQ_FLAG_BITS,
67+
WORK_OFFQ_POOL_SHIFT = WORK_OFFQ_FLAG_SHIFT + WORK_OFFQ_FLAG_BITS,
5968
WORK_OFFQ_LEFT = BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT,
6069
WORK_OFFQ_POOL_BITS = WORK_OFFQ_LEFT <= 31 ? WORK_OFFQ_LEFT : 31,
61-
6270
};
6371

6472
enum work_flags {
@@ -88,12 +96,10 @@ enum wq_misc_consts {
8896
};
8997

9098
/* Convenience constants - of type 'unsigned long', not 'enum'! */
91-
#define WORK_OFFQ_CANCELING (1ul << __WORK_OFFQ_CANCELING)
99+
#define WORK_OFFQ_CANCELING (1ul << WORK_OFFQ_CANCELING_BIT)
92100
#define WORK_OFFQ_POOL_NONE ((1ul << WORK_OFFQ_POOL_BITS) - 1)
93101
#define WORK_STRUCT_NO_POOL (WORK_OFFQ_POOL_NONE << WORK_OFFQ_POOL_SHIFT)
94-
95-
#define WORK_STRUCT_FLAG_MASK ((1ul << WORK_STRUCT_FLAG_BITS) - 1)
96-
#define WORK_STRUCT_WQ_DATA_MASK (~WORK_STRUCT_FLAG_MASK)
102+
#define WORK_STRUCT_PWQ_MASK (~((1ul << WORK_STRUCT_PWQ_SHIFT) - 1))
97103

98104
#define WORK_DATA_INIT() ATOMIC_LONG_INIT((unsigned long)WORK_STRUCT_NO_POOL)
99105
#define WORK_DATA_STATIC_INIT() \

kernel/workqueue.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ enum pool_workqueue_stats {
247247
};
248248

249249
/*
250-
* The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
250+
* The per-pool workqueue. While queued, bits below WORK_PWQ_SHIFT
251251
* of work_struct->data are used for flags and the remaining high bits
252252
* point to the pwq; thus, pwqs need to be aligned at two's power of the
253253
* number of flag bits.
@@ -294,7 +294,7 @@ struct pool_workqueue {
294294
*/
295295
struct kthread_work release_work;
296296
struct rcu_head rcu;
297-
} __aligned(1 << WORK_STRUCT_FLAG_BITS);
297+
} __aligned(1 << WORK_STRUCT_PWQ_SHIFT);
298298

299299
/*
300300
* Structure used to wait for workqueue flush.
@@ -843,7 +843,7 @@ static void clear_work_data(struct work_struct *work)
843843

844844
static inline struct pool_workqueue *work_struct_pwq(unsigned long data)
845845
{
846-
return (struct pool_workqueue *)(data & WORK_STRUCT_WQ_DATA_MASK);
846+
return (struct pool_workqueue *)(data & WORK_STRUCT_PWQ_MASK);
847847
}
848848

849849
static struct pool_workqueue *get_work_pwq(struct work_struct *work)
@@ -4851,7 +4851,7 @@ static void pwq_release_workfn(struct kthread_work *work)
48514851
static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq,
48524852
struct worker_pool *pool)
48534853
{
4854-
BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
4854+
BUG_ON((unsigned long)pwq & ~WORK_STRUCT_PWQ_MASK);
48554855

48564856
memset(pwq, 0, sizeof(*pwq));
48574857

0 commit comments

Comments
 (0)