History

workqueue: replace get_pwq() with explicit per_cpu_ptr() accesses and first_pwq()

get_pwq() takes @cpu, which can also be WORK_CPU_UNBOUND, and @wq and returns the matching pwq (pool_workqueue). We want to move away from using @cpu for identifying pools and pwqs for unbound pools with custom attributes and there is only one user - workqueue_congested() - which makes use of the WQ_UNBOUND conditional in get_pwq(). All other users already know whether they're dealing with a per-cpu or unbound workqueue. Replace get_pwq() with explicit per_cpu_ptr(wq->cpu_pwqs, cpu) for per-cpu workqueues and first_pwq() for unbound ones, and open-code WQ_UNBOUND conditional in workqueue_congested(). Note that this makes workqueue_congested() behave sligntly differently when @cpu other than WORK_CPU_UNBOUND is specified. It ignores @cpu for unbound workqueues and always uses the first pwq instead of oopsing.

Tejun Heo committed 13 years ago

Commit 7fb98ea1 parent 420c0dd1 file · +14 −15Browse files

Showing the changes to kernel/workqueue.c (1 of 1 file). Show all

Unified · Split

kernel/workqueue.c+14−15
@@ -463,16 +463,9 @@ static struct worker_pool *get_std_worker_pool(int cpu, bool highpri) · 462 unmodified lines
463463 return &pools[highpri];
464464 }
465465
466−static struct pool_workqueue *get_pwq(int cpu, struct workqueue_struct *wq)
466+static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
467467 {
468− if (!(wq->flags & WQ_UNBOUND)) {
469− if (likely(cpu < nr_cpu_ids))
470− return per_cpu_ptr(wq->cpu_pwqs, cpu);
471− } else if (likely(cpu == WORK_CPU_UNBOUND)) {
472− return list_first_entry(&wq->pwqs, struct pool_workqueue,
473− pwqs_node);
474− }
475− return NULL;
468+ return list_first_entry(&wq->pwqs, struct pool_workqueue, pwqs_node);
476469 }
477470
478471 static unsigned int work_color_to_flags(int color)
@@ -1191,7 +1184,7 @@ static void __queue_work(int cpu, struct workqueue_struct *wq, · 712 unmodified lines
11911184 * work needs to be queued on that cpu to guarantee
11921185 * non-reentrancy.
11931186 */
1194− pwq = get_pwq(cpu, wq);
1187+ pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
11951188 last_pool = get_work_pool(work);
11961189
11971190 if (last_pool && last_pool != pwq->pool) {
@@ -1202,7 +1195,7 @@ static void __queue_work(int cpu, struct workqueue_struct *wq, · 4 unmodified lines
12021195 worker = find_worker_executing_work(last_pool, work);
12031196
12041197 if (worker && worker->current_pwq->wq == wq) {
1205− pwq = get_pwq(last_pool->cpu, wq);
1198+ pwq = per_cpu_ptr(wq->cpu_pwqs, last_pool->cpu);
12061199 } else {
12071200 /* meh... not running there, queue here */
12081201 spin_unlock(&last_pool->lock);
@@ -1212,7 +1205,7 @@ static void __queue_work(int cpu, struct workqueue_struct *wq, · 3 unmodified lines
12121205 spin_lock(&pwq->pool->lock);
12131206 }
12141207 } else {
1215− pwq = get_pwq(WORK_CPU_UNBOUND, wq);
1208+ pwq = first_pwq(wq);
12161209 spin_lock(&pwq->pool->lock);
12171210 }
12181211
@@ -1650,7 +1643,7 @@ static void rebind_workers(struct worker_pool *pool) · 431 unmodified lines
16501643 else
16511644 wq = system_wq;
16521645
1653− insert_work(get_pwq(pool->cpu, wq), rebind_work,
1646+ insert_work(per_cpu_ptr(wq->cpu_pwqs, pool->cpu), rebind_work,
16541647 worker->scheduled.next,
16551648 work_color_to_flags(WORK_NO_COLOR));
16561649 }
@@ -3088,7 +3081,8 @@ static int alloc_and_link_pwqs(struct workqueue_struct *wq) · 1431 unmodified lines
30883081 return -ENOMEM;
30893082
30903083 for_each_possible_cpu(cpu) {
3091− struct pool_workqueue *pwq = get_pwq(cpu, wq);
3084+ struct pool_workqueue *pwq =
3085+ per_cpu_ptr(wq->cpu_pwqs, cpu);
30923086
30933087 pwq->pool = get_std_worker_pool(cpu, highpri);
30943088 list_add_tail(&pwq->pwqs_node, &wq->pwqs);
@@ -3343,7 +3337,12 @@ EXPORT_SYMBOL_GPL(workqueue_set_max_active); · 248 unmodified lines
33433337 */
33443338 bool workqueue_congested(int cpu, struct workqueue_struct *wq)
33453339 {
3346− struct pool_workqueue *pwq = get_pwq(cpu, wq);
3340+ struct pool_workqueue *pwq;
3341+
3342+ if (!(wq->flags & WQ_UNBOUND))
3343+ pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
3344+ else
3345+ pwq = first_pwq(wq);
33473346
33483347 return !list_empty(&pwq->delayed_works);
33493348 }