Commit 6b44003e authored by Andrew Morton's avatar Andrew Morton Committed by Rusty Russell
Browse files

work_on_cpu(): rewrite it to create a kernel thread on demand

Impact: circular locking bugfix

The various implemetnations and proposed implemetnations of work_on_cpu()
are vulnerable to various deadlocks because they all used queues of some

Unrelated pieces of kernel code thus gained dependencies wherein if one
work_on_cpu() caller holds a lock which some other work_on_cpu() callback
also takes, the kernel could rarely deadlock.

Fix this by creating a short-lived kernel thread for each work_on_cpu()

This is not terribly fast, but the only current caller of work_on_cpu() is

It would be nice to find some other way of doing the node-local
allocations in the PCI probe code so that we can zap work_on_cpu()
altogether.  The code there is rather nasty.  I can't think of anything
simple at this time...

Cc: Ingo Molnar <>
Signed-off-by: default avatarAndrew Morton <>
Signed-off-by: default avatarRusty Russell <>
parent 1c99315b
......@@ -966,20 +966,20 @@ undo:
static struct workqueue_struct *work_on_cpu_wq __read_mostly;
struct work_for_cpu {
struct work_struct work;
struct completion completion;
long (*fn)(void *);
void *arg;
long ret;
static void do_work_for_cpu(struct work_struct *w)
static int do_work_for_cpu(void *_wfc)
struct work_for_cpu *wfc = container_of(w, struct work_for_cpu, work);
struct work_for_cpu *wfc = _wfc;
wfc->ret = wfc->fn(wfc->arg);
return 0;
......@@ -990,17 +990,23 @@ static void do_work_for_cpu(struct work_struct *w)
* This will return the value @fn returns.
* It is up to the caller to ensure that the cpu doesn't go offline.
* The caller must not hold any locks which would prevent @fn from completing.
long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
struct work_for_cpu wfc;
INIT_WORK(&, do_work_for_cpu);
wfc.fn = fn;
wfc.arg = arg;
queue_work_on(cpu, work_on_cpu_wq, &;
struct task_struct *sub_thread;
struct work_for_cpu wfc = {
.completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
.fn = fn,
.arg = arg,
sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
if (IS_ERR(sub_thread))
return PTR_ERR(sub_thread);
kthread_bind(sub_thread, cpu);
return wfc.ret;
......@@ -1016,8 +1022,4 @@ void __init init_workqueues(void)
hotcpu_notifier(workqueue_cpu_callback, 0);
keventd_wq = create_workqueue("events");
work_on_cpu_wq = create_workqueue("work_on_cpu");
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment