Skip to content

Commit

Permalink
workqueue: allow work_on_cpu() to be called recursively
Browse files Browse the repository at this point in the history
If the @fn call work_on_cpu() again, the lockdep will complain:

> [ INFO: possible recursive locking detected ]
> 3.11.0-rc1-lockdep-fix-a #6 Not tainted
> ---------------------------------------------
> kworker/0:1/142 is trying to acquire lock:
>  ((&wfc.work)){+.+.+.}, at: [<ffffffff81077100>] flush_work+0x0/0xb0
>
> but task is already holding lock:
>  ((&wfc.work)){+.+.+.}, at: [<ffffffff81075dd9>] process_one_work+0x169/0x610
>
> other info that might help us debug this:
>  Possible unsafe locking scenario:
>
>        CPU0
>        ----
>   lock((&wfc.work));
>   lock((&wfc.work));
>
>  *** DEADLOCK ***

It is false-positive lockdep report. In this sutiation,
the two "wfc"s of the two work_on_cpu() are different,
they are both on stack. flush_work() can't be deadlock.

To fix this, we need to avoid the lockdep checking in this case,
thus we instroduce a internal __flush_work() which skip the lockdep.

tj: Minor comment adjustment.

Signed-off-by: Lai Jiangshan <[email protected]>
Reported-by: "Srivatsa S. Bhat" <[email protected]>
Reported-by: Alexander Duyck <[email protected]>
Signed-off-by: Tejun Heo <[email protected]>
  • Loading branch information
Lai Jiangshan authored and htejun committed Jul 24, 2013
1 parent ad81f05 commit c2fda50
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions kernel/workqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -2817,6 +2817,19 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
return false;
}

static bool __flush_work(struct work_struct *work)
{
struct wq_barrier barr;

if (start_flush_work(work, &barr)) {
wait_for_completion(&barr.done);
destroy_work_on_stack(&barr.work);
return true;
} else {
return false;
}
}

/**
* flush_work - wait for a work to finish executing the last queueing instance
* @work: the work to flush
Expand All @@ -2830,18 +2843,10 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
*/
bool flush_work(struct work_struct *work)
{
struct wq_barrier barr;

lock_map_acquire(&work->lockdep_map);
lock_map_release(&work->lockdep_map);

if (start_flush_work(work, &barr)) {
wait_for_completion(&barr.done);
destroy_work_on_stack(&barr.work);
return true;
} else {
return false;
}
return __flush_work(work);
}
EXPORT_SYMBOL_GPL(flush_work);

Expand Down Expand Up @@ -4756,7 +4761,14 @@ long work_on_cpu(int cpu, long (*fn)(void *), void *arg)

INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
schedule_work_on(cpu, &wfc.work);
flush_work(&wfc.work);

/*
* The work item is on-stack and can't lead to deadlock through
* flushing. Use __flush_work() to avoid spurious lockdep warnings
* when work_on_cpu()s are nested.
*/
__flush_work(&wfc.work);

return wfc.ret;
}
EXPORT_SYMBOL_GPL(work_on_cpu);
Expand Down

0 comments on commit c2fda50

Please sign in to comment.