Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp264845ybi; Wed, 29 May 2019 20:58:36 -0700 (PDT) X-Google-Smtp-Source: APXvYqybgXcFYVVGvQKwT/CEq0DWS+Myl/bxo4wynvLez/VaBlrM9dyerwL+HFRWNFRbrA+Ox9ii X-Received: by 2002:a17:90a:9503:: with SMTP id t3mr1527307pjo.47.1559188715933; Wed, 29 May 2019 20:58:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559188715; cv=none; d=google.com; s=arc-20160816; b=FbHpGLSFhD6IwWzYGVPCnWjFDsfxjCdc/ME8FExutfU4d2aFn0N3kttuCvo57xGfYW nVGkK45OKG+ncVJhZX0Xi1vBzBIwcA+q+gp0SJClETDGxTwYE9jqEjmpMhCGm4A2GRut mf5s3pbfgWOITHL3XhjbDzt9LApIjjq8QzjI3V+Tog79cHxtW8xAr4NaODBW+c6N5J+W x2OXqLhIypyA5aN96uhp8sEzISOPdFn+6F0ReRhxUagT2YlELw6t5oUGVsHzVv59SDPC VCIihSrSGGxhPRoYItM56XyOWx4S8j36rAFzjbema6r1flOsZd+LuVMKyXncmqAM34gQ QB5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=KsCqbbq6QsRj6kvU2KxW/porLdnVbsY1Swe73glyrZo=; b=gGHehTnTJi8rCHTiC61qDwwkFnyeFHgtP57XeCGUmoZNy1gpabukyKMPflZd0anOdw jtPI8nitan3M/BcD8eZKdl5nNiSbwlVqnd7WgKK3Wnasu92me6ZZ6ozb8ZBqIU2DvETr Ud31Z4xV23yhDd6fGafoaR8Jnxl+rwC1ILNH1jdmnjEv9NJfG/Ho6vG27PMagKz+OO4E vmMefoG/VxDPex6qDfQ8homW0uiM994bAyNj4+Ax3iu+Wg397Jy2SgyOC5CBLmMpPCS9 VXQYLn1d2UFFkkv75DkCLVMer7Zwi55xUkLeV1u8Ai+sWpxeHHfbkkYEFk+ncx2FIOVw BXqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=UZZPG0eB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v89si1936597pjb.14.2019.05.29.20.58.20; Wed, 29 May 2019 20:58:35 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=UZZPG0eB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387947AbfE3D4j (ORCPT + 99 others); Wed, 29 May 2019 23:56:39 -0400 Received: from mail.kernel.org ([198.145.29.99]:53108 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731758AbfE3DS6 (ORCPT ); Wed, 29 May 2019 23:18:58 -0400 Received: from localhost (ip67-88-213-2.z213-88-67.customer.algx.net [67.88.213.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A4C1724807; Thu, 30 May 2019 03:18:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1559186337; bh=CLzmkCeDUTXeQSAoIWSMUtN2s/dle3ux9x8AjJvHwHA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UZZPG0eB9TvTz1R/qwTSy7srtMXvmd2xiMG8B/iyObW09CqUdA8tSvkLI78rEa7mE uv9GGKjQK+ZkQRkikrAB+HdqpKd/kFJns0mIcF37SGUdP6WXPwzyJSoVEPmT+zLOai sQV5oYZZNhja+4gjiYoTMuDroPFZVK6oGRqGfqTQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Filipe Manana , Qu Wenruo , Josef Bacik , David Sterba Subject: [PATCH 4.14 025/193] btrfs: honor path->skip_locking in backref code Date: Wed, 29 May 2019 20:04:39 -0700 Message-Id: <20190530030452.374190769@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190530030446.953835040@linuxfoundation.org> References: <20190530030446.953835040@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Josef Bacik commit 38e3eebff643db725633657d1d87a3be019d1018 upstream. Qgroups will do the old roots lookup at delayed ref time, which could be while walking down the extent root while running a delayed ref. This should be fine, except we specifically lock eb's in the backref walking code irrespective of path->skip_locking, which deadlocks the system. Fix up the backref code to honor path->skip_locking, nobody will be modifying the commit_root when we're searching so it's completely safe to do. This happens since fb235dc06fac ("btrfs: qgroup: Move half of the qgroup accounting time out of commit trans"), kernel may lockup with quota enabled. There is one backref trace triggered by snapshot dropping along with write operation in the source subvolume. The example can be reliably reproduced: btrfs-cleaner D 0 4062 2 0x80000000 Call Trace: schedule+0x32/0x90 btrfs_tree_read_lock+0x93/0x130 [btrfs] find_parent_nodes+0x29b/0x1170 [btrfs] btrfs_find_all_roots_safe+0xa8/0x120 [btrfs] btrfs_find_all_roots+0x57/0x70 [btrfs] btrfs_qgroup_trace_extent_post+0x37/0x70 [btrfs] btrfs_qgroup_trace_leaf_items+0x10b/0x140 [btrfs] btrfs_qgroup_trace_subtree+0xc8/0xe0 [btrfs] do_walk_down+0x541/0x5e3 [btrfs] walk_down_tree+0xab/0xe7 [btrfs] btrfs_drop_snapshot+0x356/0x71a [btrfs] btrfs_clean_one_deleted_snapshot+0xb8/0xf0 [btrfs] cleaner_kthread+0x12b/0x160 [btrfs] kthread+0x112/0x130 ret_from_fork+0x27/0x50 When dropping snapshots with qgroup enabled, we will trigger backref walk. However such backref walk at that timing is pretty dangerous, as if one of the parent nodes get WRITE locked by other thread, we could cause a dead lock. For example: FS 260 FS 261 (Dropped) node A node B / \ / \ node C node D node E / \ / \ / \ leaf F|leaf G|leaf H|leaf I|leaf J|leaf K The lock sequence would be: Thread A (cleaner) | Thread B (other writer) ----------------------------------------------------------------------- write_lock(B) | write_lock(D) | ^^^ called by walk_down_tree() | | write_lock(A) | write_lock(D) << Stall read_lock(H) << for backref walk | read_lock(D) << lock owner is | the same thread A | so read lock is OK | read_lock(A) << Stall | So thread A hold write lock D, and needs read lock A to unlock. While thread B holds write lock A, while needs lock D to unlock. This will cause a deadlock. This is not only limited to snapshot dropping case. As the backref walk, even only happens on commit trees, is breaking the normal top-down locking order, makes it deadlock prone. Fixes: fb235dc06fac ("btrfs: qgroup: Move half of the qgroup accounting time out of commit trans") CC: stable@vger.kernel.org # 4.14+ Reported-and-tested-by: David Sterba Reported-by: Filipe Manana Reviewed-by: Qu Wenruo Signed-off-by: Josef Bacik Reviewed-by: Filipe Manana [ rebase to latest branch and fix lock assert bug in btrfs/007 ] [ backport to linux-4.19.y branch, solve minor conflicts ] Signed-off-by: Qu Wenruo [ copy logs and deadlock analysis from Qu's patch ] Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/backref.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) --- a/fs/btrfs/backref.c +++ b/fs/btrfs/backref.c @@ -718,7 +718,7 @@ out: * read tree blocks and add keys where required. */ static int add_missing_keys(struct btrfs_fs_info *fs_info, - struct preftrees *preftrees) + struct preftrees *preftrees, bool lock) { struct prelim_ref *ref; struct extent_buffer *eb; @@ -742,12 +742,14 @@ static int add_missing_keys(struct btrfs free_extent_buffer(eb); return -EIO; } - btrfs_tree_read_lock(eb); + if (lock) + btrfs_tree_read_lock(eb); if (btrfs_header_level(eb) == 0) btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0); else btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0); - btrfs_tree_read_unlock(eb); + if (lock) + btrfs_tree_read_unlock(eb); free_extent_buffer(eb); prelim_ref_insert(fs_info, &preftrees->indirect, ref, NULL); cond_resched(); @@ -1228,7 +1230,7 @@ again: btrfs_release_path(path); - ret = add_missing_keys(fs_info, &preftrees); + ret = add_missing_keys(fs_info, &preftrees, path->skip_locking == 0); if (ret) goto out; @@ -1290,9 +1292,14 @@ again: } btrfs_tree_read_lock(eb); btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); + if (!path->skip_locking) { + btrfs_tree_read_lock(eb); + btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); + } ret = find_extent_in_eb(eb, bytenr, *extent_item_pos, &eie); - btrfs_tree_read_unlock_blocking(eb); + if (!path->skip_locking) + btrfs_tree_read_unlock_blocking(eb); free_extent_buffer(eb); if (ret < 0) goto out;