From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753837Ab2DUA2y (ORCPT ); Fri, 20 Apr 2012 20:28:54 -0400 Received: from wolverine02.qualcomm.com ([199.106.114.251]:56335 "EHLO wolverine02.qualcomm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751991Ab2DUA2w (ORCPT ); Fri, 20 Apr 2012 20:28:52 -0400 X-IronPort-AV: E=McAfee;i="5400,1158,6687"; a="181400834" From: Stephen Boyd To: Tejun Heo Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, Ben Dooks , Yong Zhang Subject: [PATCHv2] workqueue: Catch more locking problems with flush_work() Date: Fri, 20 Apr 2012 17:28:50 -0700 Message-Id: <1334968130-20724-1-git-send-email-sboyd@codeaurora.org> X-Mailer: git-send-email 1.7.10.207.g0bb2e In-Reply-To: <20120420173529.GD32324@google.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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.