From patchwork Mon Feb 10 09:07:16 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Bo X-Patchwork-Id: 3617041 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id E607D9F2F6 for ; Mon, 10 Feb 2014 09:07:33 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 10B132012F for ; Mon, 10 Feb 2014 09:07:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 14C9E2010C for ; Mon, 10 Feb 2014 09:07:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752292AbaBJJH2 (ORCPT ); Mon, 10 Feb 2014 04:07:28 -0500 Received: from aserp1040.oracle.com ([141.146.126.69]:42262 "EHLO aserp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752148AbaBJJH0 (ORCPT ); Mon, 10 Feb 2014 04:07:26 -0500 Received: from acsinet21.oracle.com (acsinet21.oracle.com [141.146.126.237]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id s1A97Pe9028120 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 10 Feb 2014 09:07:26 GMT Received: from aserz7022.oracle.com (aserz7022.oracle.com [141.146.126.231]) by acsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id s1A97OJ7005429 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Mon, 10 Feb 2014 09:07:25 GMT Received: from abhmp0018.oracle.com (abhmp0018.oracle.com [141.146.116.24]) by aserz7022.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id s1A97Olf019870 for ; Mon, 10 Feb 2014 09:07:24 GMT Received: from localhost.localdomain.com (/10.182.228.124) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 10 Feb 2014 01:07:24 -0800 From: Liu Bo To: linux-btrfs@vger.kernel.org Subject: [PATCH] Btrfs: fix possible deadlock in btrfs_cleanup_transaction Date: Mon, 10 Feb 2014 17:07:16 +0800 Message-Id: <1392023236-25797-1-git-send-email-bo.li.liu@oracle.com> X-Mailer: git-send-email 1.8.1.4 X-Source-IP: acsinet21.oracle.com [141.146.126.237] Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP [13654.480669] ====================================================== [13654.480905] [ INFO: possible circular locking dependency detected ] [13654.481003] 3.12.0+ #4 Tainted: G W O [13654.481060] ------------------------------------------------------- [13654.481060] btrfs-transacti/9347 is trying to acquire lock: [13654.481060] (&(&root->ordered_extent_lock)->rlock){+.+...}, at: [] btrfs_cleanup_transaction+0x271/0x570 [btrfs] [13654.481060] but task is already holding lock: [13654.481060] (&(&fs_info->ordered_root_lock)->rlock){+.+...}, at: [] btrfs_cleanup_transaction+0x1e5/0x570 [btrfs] [13654.481060] which lock already depends on the new lock. [13654.481060] the existing dependency chain (in reverse order) is: [13654.481060] -> #1 (&(&fs_info->ordered_root_lock)->rlock){+.+...}: [13654.481060] [] lock_acquire+0x93/0x130 [13654.481060] [] _raw_spin_lock+0x41/0x50 [13654.481060] [] __btrfs_add_ordered_extent+0x39b/0x450 [btrfs] [13654.481060] [] btrfs_add_ordered_extent+0x32/0x40 [btrfs] [13654.481060] [] run_delalloc_nocow+0x78a/0x9d0 [btrfs] [13654.481060] [] run_delalloc_range+0x31d/0x390 [btrfs] [13654.481060] [] __extent_writepage+0x310/0x780 [btrfs] [13654.481060] [] extent_write_cache_pages.isra.29.constprop.48+0x29a/0x410 [btrfs] [13654.481060] [] extent_writepages+0x4d/0x70 [btrfs] [13654.481060] [] btrfs_writepages+0x28/0x30 [btrfs] [13654.481060] [] do_writepages+0x21/0x50 [13654.481060] [] __filemap_fdatawrite_range+0x59/0x60 [13654.481060] [] filemap_fdatawrite_range+0x13/0x20 [13654.481060] [] btrfs_wait_ordered_range+0x49/0x140 [btrfs] [13654.481060] [] __btrfs_write_out_cache+0x682/0x8b0 [btrfs] [13654.481060] [] btrfs_write_out_cache+0x8d/0xe0 [btrfs] [13654.481060] [] btrfs_write_dirty_block_groups+0x593/0x680 [btrfs] [13654.481060] [] commit_cowonly_roots+0x14b/0x20d [btrfs] [13654.481060] [] btrfs_commit_transaction+0x43a/0x9d0 [btrfs] [13654.481060] [] btrfs_create_uuid_tree+0x5a/0x100 [btrfs] [13654.481060] [] open_ctree+0x21da/0x2210 [btrfs] [13654.481060] [] btrfs_mount+0x68e/0x870 [btrfs] [13654.481060] [] mount_fs+0x39/0x1b0 [13654.481060] [] vfs_kern_mount+0x63/0xf0 [13654.481060] [] do_mount+0x23e/0xa90 [13654.481060] [] SyS_mount+0x83/0xc0 [13654.481060] [] system_call_fastpath+0x16/0x1b [13654.481060] -> #0 (&(&root->ordered_extent_lock)->rlock){+.+...}: [13654.481060] [] __lock_acquire+0x150a/0x1a70 [13654.481060] [] lock_acquire+0x93/0x130 [13654.481060] [] _raw_spin_lock+0x41/0x50 [13654.481060] [] btrfs_cleanup_transaction+0x271/0x570 [btrfs] [13654.481060] [] transaction_kthread+0x22e/0x270 [btrfs] [13654.481060] [] kthread+0xea/0xf0 [13654.481060] [] ret_from_fork+0x7c/0xb0 [13654.481060] other info that might help us debug this: [13654.481060] Possible unsafe locking scenario: [13654.481060] CPU0 CPU1 [13654.481060] ---- ---- [13654.481060] lock(&(&fs_info->ordered_root_lock)->rlock); [13654.481060] lock(&(&root->ordered_extent_lock)->rlock); [13654.481060] lock(&(&fs_info->ordered_root_lock)->rlock); [13654.481060] lock(&(&root->ordered_extent_lock)->rlock); [13654.481060] *** DEADLOCK *** [...] ====================================================== btrfs_destroy_all_ordered_extents() gets &fs_info->ordered_root_lock __BEFORE__ acquiring &root->ordered_extent_lock, while btrfs_[add,remove]_ordered_extent() acquires &fs_info->ordered_root_lock __AFTER__ getting &root->ordered_extent_lock. This patch fixes the above problem. Signed-off-by: Liu Bo --- fs/btrfs/disk-io.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index 3903bd3..8cd48c3 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -3794,9 +3794,11 @@ static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) list_move_tail(&root->ordered_root, &fs_info->ordered_roots); + spin_unlock(&fs_info->ordered_root_lock); btrfs_destroy_ordered_extents(root); - cond_resched_lock(&fs_info->ordered_root_lock); + cond_resched(); + spin_lock(&fs_info->ordered_root_lock); } spin_unlock(&fs_info->ordered_root_lock); }