Re: [PATCHv2] workqueue: Catch more locking problems with flush_work()

From: Yong Zhang
Date: Fri Apr 20 2012 - 20:34:02 EST


On Sat, Apr 21, 2012 at 8:28 AM, Stephen Boyd <sboyd@xxxxxxxxxxxxxx> wrote:
> If a workqueue is flushed with flush_work() lockdep checking can
> be circumvented. For example:
>
> Âstatic DEFINE_MUTEX(mutex);
>
> Âstatic void my_work(struct work_struct *w)
> Â{
> Â Â Â Â mutex_lock(&mutex);
> Â Â Â Â mutex_unlock(&mutex);
> Â}
>
> Âstatic DECLARE_WORK(work, my_work);
>
> Âstatic int __init start_test_module(void)
> Â{
> Â Â Â Â schedule_work(&work);
> Â Â Â Â return 0;
> Â}
> Âmodule_init(start_test_module);
>
> Âstatic void __exit stop_test_module(void)
> Â{
> Â Â Â Â mutex_lock(&mutex);
> Â Â Â Â flush_work(&work);
> Â Â Â Â mutex_unlock(&mutex);
> Â}
> Âmodule_exit(stop_test_module);
>
> would not always print a warning when flush_work() was called.
> In this trivial example nothing could go wrong since we are
> guaranteed module_init() and module_exit() don't run concurrently,
> but if the work item is schedule asynchronously we could have a
> scenario where the work item is running just at the time flush_work()
> is called resulting in a classic ABBA locking problem.
>
> Add a lockdep hint by acquiring and releasing the work item
> lockdep_map in flush_work() so that we always catch this
> potential deadlock scenario.
>
> Signed-off-by: Stephen Boyd <sboyd@xxxxxxxxxxxxxx>

Reviewed-by: Yong Zhang <yong.zhang0@xxxxxxxxx>

> ---
> Âkernel/workqueue.c | Â Â3 +++
> Â1 file changed, 3 insertions(+)
>
> diff --git a/kernel/workqueue.c b/kernel/workqueue.c
> index 5abf42f..038cf64 100644
> --- a/kernel/workqueue.c
> +++ b/kernel/workqueue.c
> @@ -2506,6 +2506,9 @@ 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, true)) {
> Â Â Â Â Â Â Â Âwait_for_completion(&barr.done);
> Â Â Â Â Â Â Â Âdestroy_work_on_stack(&barr.work);
> --
> Sent by an employee of the Qualcomm Innovation Center, Inc.
> The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum.
>



--
Only stand for myself
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/