From patchwork Tue Aug 14 05:01:14 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: bo liu X-Patchwork-Id: 1318131 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id D86E7DF266 for ; Tue, 14 Aug 2012 05:02:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752164Ab2HNFCf (ORCPT ); Tue, 14 Aug 2012 01:02:35 -0400 Received: from mail-gg0-f174.google.com ([209.85.161.174]:64743 "EHLO mail-gg0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751684Ab2HNFCe (ORCPT ); Tue, 14 Aug 2012 01:02:34 -0400 Received: by ggdk6 with SMTP id k6so4129826ggd.19 for ; Mon, 13 Aug 2012 22:02:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer; bh=b8E2+8kwngyOV8fV+Lz79ArnIowhUSr/GBKX4T9nlgc=; b=qHklq0dTJZwPhMDv7M93iwVIBOW5iEQyk9srmKNK5zMGC8yBP26xiool6yaRdwuy5r xOIuDLtG0W6J9Lt54Cqa5WIJSdHeTIZ+9K5muPEXkmTxd40m3TBvWm0CaOcmrcJQgFzp fqyRaiiPiOyEqjB71hnKTHOGaIeL5nQFKv+6OEEs/HXeHL9r9qSqSyZP9wG6SaLpBKcd bh1UuZwVfYECWYhGTVJ7a8aO9nZ8lsDtJWsX2n1rQVS0oi/Ltir7Ni5/z/yUfCKy/nLT Y3fk/lEHFYzWlsG+OlxcEeL+kBptz1iY1EuQD3+t51kkSTPOMNsw6k/dU48VBXb05Kwj uXIw== Received: by 10.66.76.130 with SMTP id k2mr19832174paw.19.1344920553425; Mon, 13 Aug 2012 22:02:33 -0700 (PDT) Received: from localhost.localdomain ([219.145.42.157]) by mx.google.com with ESMTPS id rd7sm466638pbc.70.2012.08.13.22.02.30 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 13 Aug 2012 22:02:32 -0700 (PDT) From: To: Cc: Subject: [PATCH RFC] Btrfs: fix deadlock between sys_sync and freeze Date: Tue, 14 Aug 2012 13:01:14 +0800 Message-Id: <1344920474-10014-1-git-send-email-liub.liubo@gmail.com> X-Mailer: git-send-email 1.7.9.5 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org From: Liu Bo I found this while testing xfstests 068, the story is t1 t2 sys_sync thaw_super iterate_supers down_read(sb->s_umount) down_write(sb->s_umount) --->wait for t1 sync_fs (with wait mode) start_transaction sb_start_intwrite --------------------> wait for t2 to set s_writers.frozen to SB_UNFROZEN In this patch, I add an helper sb_start_intwrite_trylock() and use it before we start_transaction in sync_fs() with wait mode so that we won't hit the deadlock. Signed-off-by: Liu Bo --- fs/btrfs/super.c | 15 +++++++++++++++ include/linux/fs.h | 5 +++++ 2 files changed, 20 insertions(+), 0 deletions(-) diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c index f2eb24c..1e04b41 100644 --- a/fs/btrfs/super.c +++ b/fs/btrfs/super.c @@ -847,6 +847,21 @@ int btrfs_sync_fs(struct super_block *sb, int wait) return 0; } + /* + * sys_sync can cause an ABBA deadlock with freeze/thaw + * o freeze_super() grabs s_umount lock and set sb to SB_FREEZE_FS. + * o thaw_super() grabs s_umount lock and set sb to SB_UNFROZEN. + * o iterate_supers() grabs s_umount lock, and sync fs, during which + * we need to do sb_start_intwrite() in starting a + * new transaction. + * so iterate_supers() will wait for thaw_super() to reset sb's frozen + * state, while thaw_super() will wait for iterate_supers() to drop the + * s_umount lock. This is an ABBA deadlock. + */ + if (!sb_start_intwrite_trylock(sb)) + return 0; + sb_end_intwrite(sb); + btrfs_wait_ordered_extents(root, 0, 0); trans = btrfs_start_transaction(root, 0); diff --git a/include/linux/fs.h b/include/linux/fs.h index aa11047..8a3efd0 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1700,6 +1700,11 @@ static inline void sb_start_intwrite(struct super_block *sb) __sb_start_write(sb, SB_FREEZE_FS, true); } +static inline int sb_start_intwrite_trylock(struct super_block *sb) +{ + return __sb_start_write(sb, SB_FREEZE_FS, false); +} + extern bool inode_owner_or_capable(const struct inode *inode);