Skip to content

Commit 8594fad

Browse files
Lai Jiangshanhtejun
authored andcommitted
workqueue: pick cwq instead of pool in __queue_work()
Currently, __queue_work() chooses the pool to queue a work item to and then determines cwq from the target wq and the chosen pool. This is a bit backwards in that we can determine cwq first and simply use cwq->pool. This way, we can skip get_std_worker_pool() in queueing path which will be a hurdle when implementing custom worker pools. Update __queue_work() such that it chooses the target cwq and then use cwq->pool instead of the other way around. While at it, add missing {} in an if statement. This patch doesn't introduce any functional changes. tj: The original patch had two get_cwq() calls - the first to determine the pool by doing get_cwq(cpu, wq)->pool and the second to determine the matching cwq from get_cwq(pool->cpu, wq). Updated the function such that it chooses cwq instead of pool and removed the second call. Rewrote the description. Signed-off-by: Lai Jiangshan <laijs@cn.fujitsu.com> Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent 54d5b7d commit 8594fad

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

kernel/workqueue.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,8 +1193,6 @@ static bool is_chained_work(struct workqueue_struct *wq)
11931193
static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
11941194
struct work_struct *work)
11951195
{
1196-
bool highpri = wq->flags & WQ_HIGHPRI;
1197-
struct worker_pool *pool;
11981196
struct cpu_workqueue_struct *cwq;
11991197
struct list_head *worklist;
12001198
unsigned int work_flags;
@@ -1215,7 +1213,7 @@ static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
12151213
WARN_ON_ONCE(!is_chained_work(wq)))
12161214
return;
12171215

1218-
/* determine pool to use */
1216+
/* determine the cwq to use */
12191217
if (!(wq->flags & WQ_UNBOUND)) {
12201218
struct worker_pool *last_pool;
12211219

@@ -1228,37 +1226,36 @@ static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
12281226
* work needs to be queued on that cpu to guarantee
12291227
* non-reentrancy.
12301228
*/
1231-
pool = get_std_worker_pool(cpu, highpri);
1229+
cwq = get_cwq(cpu, wq);
12321230
last_pool = get_work_pool(work);
12331231

1234-
if (last_pool && last_pool != pool) {
1232+
if (last_pool && last_pool != cwq->pool) {
12351233
struct worker *worker;
12361234

12371235
spin_lock(&last_pool->lock);
12381236

12391237
worker = find_worker_executing_work(last_pool, work);
12401238

1241-
if (worker && worker->current_cwq->wq == wq)
1242-
pool = last_pool;
1243-
else {
1239+
if (worker && worker->current_cwq->wq == wq) {
1240+
cwq = get_cwq(last_pool->cpu, wq);
1241+
} else {
12441242
/* meh... not running there, queue here */
12451243
spin_unlock(&last_pool->lock);
1246-
spin_lock(&pool->lock);
1244+
spin_lock(&cwq->pool->lock);
12471245
}
12481246
} else {
1249-
spin_lock(&pool->lock);
1247+
spin_lock(&cwq->pool->lock);
12501248
}
12511249
} else {
1252-
pool = get_std_worker_pool(WORK_CPU_UNBOUND, highpri);
1253-
spin_lock(&pool->lock);
1250+
cwq = get_cwq(WORK_CPU_UNBOUND, wq);
1251+
spin_lock(&cwq->pool->lock);
12541252
}
12551253

1256-
/* pool determined, get cwq and queue */
1257-
cwq = get_cwq(pool->cpu, wq);
1254+
/* cwq determined, queue */
12581255
trace_workqueue_queue_work(req_cpu, cwq, work);
12591256

12601257
if (WARN_ON(!list_empty(&work->entry))) {
1261-
spin_unlock(&pool->lock);
1258+
spin_unlock(&cwq->pool->lock);
12621259
return;
12631260
}
12641261

@@ -1276,7 +1273,7 @@ static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
12761273

12771274
insert_work(cwq, work, worklist, work_flags);
12781275

1279-
spin_unlock(&pool->lock);
1276+
spin_unlock(&cwq->pool->lock);
12801277
}
12811278

12821279
/**

0 commit comments

Comments
 (0)