From patchwork Thu Aug 30 17:42:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 10582457 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 01BE75A4 for ; Thu, 30 Aug 2018 17:43:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E483B28673 for ; Thu, 30 Aug 2018 17:43:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D899F28681; Thu, 30 Aug 2018 17:43:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C661F284DA for ; Thu, 30 Aug 2018 17:43:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727592AbeH3VqP (ORCPT ); Thu, 30 Aug 2018 17:46:15 -0400 Received: from mail-qt0-f194.google.com ([209.85.216.194]:44382 "EHLO mail-qt0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727067AbeH3VqP (ORCPT ); Thu, 30 Aug 2018 17:46:15 -0400 Received: by mail-qt0-f194.google.com with SMTP id k38-v6so11283492qtk.11 for ; Thu, 30 Aug 2018 10:42:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references; bh=BgDRhtJ88t7ugG2XXq1rTUKV8IL4k8Uo9zOi0L9ZWBs=; b=qPMYGDJ1JuxdsezjIyoxY2VolmDM4ON2S38UZy1WhGxliwZd4TYD/TkY+nRM+h75fQ E3eBQgw0LM9Lce1KzSes2DCVMrlM5YTwGC5X7qjlbsa9O7uc26R7BepBvGeX8ndGO+PU OPBcVi0RMKUSVu81qKPKIQ9G52lHO2TBcCbD2ahFzyNkulSToayO8S5yegepo4YhEKNT nHxyRRZuqv99bCef781E1xY4mKeZ8pxKUzf5wCOtDwhUxy0FOkcPLck1zkjh7s2Q7miz whUErWv8d+VF+Rk3dBHANIAxl37kVTxVekb11ktIaF4DmwrvU0kV1M/4cLHob4UWkw0Q yt0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=BgDRhtJ88t7ugG2XXq1rTUKV8IL4k8Uo9zOi0L9ZWBs=; b=mgiJBWPtcLzBrnMJsjTWysJczVdq+DFxN6drds4EOLTcwfzx8hY+DhrRr2QklJhdTp 8uqUBE04ds4bGXFK7HipNz8X5EnFAq858XMZ331plpKrXjVKt+JHa9RiowOTIfRchZZQ 8WQTFIUS+QZ+Zl5cIz0oMorUz/1qupwpmaIof+RqhxqrSfGDPNVNlmjhabMUDIvSyuxo BrdA2G8lSoIv6IPMEznXM3QqgkFPkmcoPCw/cdp84iPGzs+wQ/SEnd221BBlJp2i4XkR HPGQ+DG4/SbJkn46i+6oqhWh7tWAlAsdfozOrIxmnAJZYzNFlPpVu50p3E7oliLlQObN ZliA== X-Gm-Message-State: APzg51DRJ4ct/NVlyuJ7ZRARvA7hOddT6dROERVsSOlDF7mOWPWx56dN 9hoAQAfTlC3s3l/jR/ayrpUdEmwGAF11bQ== X-Google-Smtp-Source: ANB0VdaeuyXWGZdlP28yZMY/akDSh+cv67rIFwyKZkDa+gMFQ5wmwViOb95mYR+W9WnaHtPIJF+IpA== X-Received: by 2002:ac8:4807:: with SMTP id g7-v6mr12322584qtq.301.1535650978971; Thu, 30 Aug 2018 10:42:58 -0700 (PDT) Received: from localhost ([107.15.81.208]) by smtp.gmail.com with ESMTPSA id p5-v6sm4147494qkd.81.2018.08.30.10.42.57 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 30 Aug 2018 10:42:58 -0700 (PDT) From: Josef Bacik To: linux-btrfs@vger.kernel.org Subject: [PATCH 17/35] btrfs: move the dio_sem higher up the callchain Date: Thu, 30 Aug 2018 13:42:07 -0400 Message-Id: <20180830174225.2200-18-josef@toxicpanda.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180830174225.2200-1-josef@toxicpanda.com> References: <20180830174225.2200-1-josef@toxicpanda.com> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP We're getting a lockdep splat because we take the dio_sem under the log_mutex. What we really need is to protect fsync() from logging an extent map for an extent we never waited on higher up, so just guard the whole thing with dio_sem. Signed-off-by: Josef Bacik --- fs/btrfs/file.c | 12 ++++++++++++ fs/btrfs/tree-log.c | 2 -- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c index 095f0bb86bb7..c07110edb9de 100644 --- a/fs/btrfs/file.c +++ b/fs/btrfs/file.c @@ -2079,6 +2079,14 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) goto out; inode_lock(inode); + + /* + * We take the dio_sem here because the tree log stuff can race with + * lockless dio writes and get an extent map logged for an extent we + * never waited on. We need it this high up for lockdep reasons. + */ + down_write(&BTRFS_I(inode)->dio_sem); + atomic_inc(&root->log_batch); /* @@ -2087,6 +2095,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) */ ret = btrfs_wait_ordered_range(inode, start, len); if (ret) { + up_write(&BTRFS_I(inode)->dio_sem); inode_unlock(inode); goto out; } @@ -2110,6 +2119,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) * checked called fsync. */ ret = filemap_check_wb_err(inode->i_mapping, file->f_wb_err); + up_write(&BTRFS_I(inode)->dio_sem); inode_unlock(inode); goto out; } @@ -2128,6 +2138,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) trans = btrfs_start_transaction(root, 0); if (IS_ERR(trans)) { ret = PTR_ERR(trans); + up_write(&BTRFS_I(inode)->dio_sem); inode_unlock(inode); goto out; } @@ -2149,6 +2160,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) * file again, but that will end up using the synchronization * inside btrfs_sync_log to keep things safe. */ + up_write(&BTRFS_I(inode)->dio_sem); inode_unlock(inode); /* diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c index 1650dc44a5e3..66b7e059b765 100644 --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -4374,7 +4374,6 @@ static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans, INIT_LIST_HEAD(&extents); - down_write(&inode->dio_sem); write_lock(&tree->lock); test_gen = root->fs_info->last_trans_committed; logged_start = start; @@ -4440,7 +4439,6 @@ static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans, } WARN_ON(!list_empty(&extents)); write_unlock(&tree->lock); - up_write(&inode->dio_sem); btrfs_release_path(path); if (!ret)