History

workqueue: fix CPU binding of flush_delayed_work[_sync]()

delayed_work encodes the workqueue to use and the last CPU in delayed_work->work.data while it's on timer. The target CPU is implicitly recorded as the CPU the timer is queued on and delayed_work_timer_fn() queues delayed_work->work to the CPU it is running on. Unfortunately, this leaves flush_delayed_work[_sync]() no way to find out which CPU the delayed_work was queued for when they try to re-queue after killing the timer. Currently, it chooses the local CPU flush is running on. This can unexpectedly move a delayed_work queued on a specific CPU to another CPU and lead to subtle errors. There isn't much point in trying to save several bytes in struct delayed_work, which is already close to a hundred bytes on 64bit with all debug options turned off. This patch adds delayed_work->cpu to remember the CPU it's queued for. Note that if the timer is migrated during CPU down, the work item could be queued to the downed global_cwq after this change. As a detached global_cwq behaves like an unbound one, this doesn't change much for the delayed_work.

Tejun Heo committed 14 years ago

Commit 12650571 parent 41f63c52 files · +5 −3Browse files

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

Unified · Split

kernel/workqueue.c+4−3
@@ -1319,7 +1319,7 @@ void delayed_work_timer_fn(unsigned long __data) · 1318 unmodified lines
13191319 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
13201320
13211321 local_irq_disable();
1322− __queue_work(WORK_CPU_UNBOUND, cwq->wq, &dwork->work);
1322+ __queue_work(dwork->cpu, cwq->wq, &dwork->work);
13231323 local_irq_enable();
13241324 }
13251325 EXPORT_SYMBOL_GPL(delayed_work_timer_fn);
@@ -1356,6 +1356,7 @@ static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, · 30 unmodified lines
13561356
13571357 set_work_cwq(work, get_cwq(lcpu, wq), 0);
13581358
1359+ dwork->cpu = cpu;
13591360 timer->expires = jiffies + delay;
13601361
13611362 if (unlikely(cpu != WORK_CPU_UNBOUND))
@@ -2997,7 +2998,7 @@ bool flush_delayed_work(struct delayed_work *dwork) · 1635 unmodified lines
29972998 {
29982999 local_irq_disable();
29993000 if (del_timer_sync(&dwork->timer))
3000− __queue_work(WORK_CPU_UNBOUND,
3001+ __queue_work(dwork->cpu,
30013002 get_work_cwq(&dwork->work)->wq, &dwork->work);
30023003 local_irq_enable();
30033004 return flush_work(&dwork->work);
@@ -3020,7 +3021,7 @@ bool flush_delayed_work_sync(struct delayed_work *dwork) · 16 unmodified lines
30203021 {
30213022 local_irq_disable();
30223023 if (del_timer_sync(&dwork->timer))
3023− __queue_work(WORK_CPU_UNBOUND,
3024+ __queue_work(dwork->cpu,
30243025 get_work_cwq(&dwork->work)->wq, &dwork->work);
30253026 local_irq_enable();
30263027 return flush_work_sync(&dwork->work);