Skip to content

Commit ed48ece

Browse files
committed
workqueue: reimplement work_on_cpu() using system_wq
The existing work_on_cpu() implementation is hugely inefficient. It creates a new kthread, execute that single function and then let the kthread die on each invocation. Now that system_wq can handle concurrent executions, there's no advantage of doing this. Reimplement work_on_cpu() using system_wq which makes it simpler and way more efficient. stable: While this isn't a fix in itself, it's needed to fix a workqueue related bug in cpufreq/powernow-k8. AFAICS, this shouldn't break other existing users. Signed-off-by: Tejun Heo <[email protected]> Acked-by: Jiri Kosina <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Bjorn Helgaas <[email protected]> Cc: Len Brown <[email protected]> Cc: Rafael J. Wysocki <[email protected]> Cc: [email protected]
1 parent 960bd11 commit ed48ece

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

kernel/workqueue.c

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3576,18 +3576,17 @@ static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
35763576
#ifdef CONFIG_SMP
35773577

35783578
struct work_for_cpu {
3579-
struct completion completion;
3579+
struct work_struct work;
35803580
long (*fn)(void *);
35813581
void *arg;
35823582
long ret;
35833583
};
35843584

3585-
static int do_work_for_cpu(void *_wfc)
3585+
static void work_for_cpu_fn(struct work_struct *work)
35863586
{
3587-
struct work_for_cpu *wfc = _wfc;
3587+
struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3588+
35883589
wfc->ret = wfc->fn(wfc->arg);
3589-
complete(&wfc->completion);
3590-
return 0;
35913590
}
35923591

35933592
/**
@@ -3602,19 +3601,11 @@ static int do_work_for_cpu(void *_wfc)
36023601
*/
36033602
long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
36043603
{
3605-
struct task_struct *sub_thread;
3606-
struct work_for_cpu wfc = {
3607-
.completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3608-
.fn = fn,
3609-
.arg = arg,
3610-
};
3604+
struct work_for_cpu wfc = { .fn = fn, .arg = arg };
36113605

3612-
sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3613-
if (IS_ERR(sub_thread))
3614-
return PTR_ERR(sub_thread);
3615-
kthread_bind(sub_thread, cpu);
3616-
wake_up_process(sub_thread);
3617-
wait_for_completion(&wfc.completion);
3606+
INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3607+
schedule_work_on(cpu, &wfc.work);
3608+
flush_work(&wfc.work);
36183609
return wfc.ret;
36193610
}
36203611
EXPORT_SYMBOL_GPL(work_on_cpu);

0 commit comments

Comments
 (0)