Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp50600imd; Wed, 31 Oct 2018 14:27:37 -0700 (PDT) X-Google-Smtp-Source: AJdET5fcXGmcsdddSyyW/DNL4e6wURKGI8wOq8op/qulUMgMuoXM/vS7LUTamZ+KuQjdyy2l5Oe4 X-Received: by 2002:a63:6045:: with SMTP id u66mr4727934pgb.204.1541021257743; Wed, 31 Oct 2018 14:27:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1541021257; cv=none; d=google.com; s=arc-20160816; b=ZrPGfnnStDKT1lCdASfe7LY3zl/qw5U/hCWtYlXthJ6XsJIMBA78PJcafnRIvmO9mK Fl33BXNIZp8zuBOJoI3fxcecz5Fk3ag19y871rUJ8Rcv9/bBXUfjVbknqSeSQS2JpnAS JEL0x6OfCDHYt7JLhywfE74Vg3bEOY5tBckOhodb7yCRKE/SiXTNX1/Bvq5kk18+Xboy Pi/0e6eb4aaUirShUgvfJt/N/8+h+91XsjY9dYe3qLchLoyydodEGemcizGBmvv91UiT fzq2V3AR+ZMfLB0DOfSL3gtr3STAiggbDSWpl7qdxGXP15tGss2fVVQzwZxLuvuz7icm t69Q== 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 :references:in-reply-to:message-id:subject:cc:to:from:date; bh=wBI1bsDWYscHKSdlkDp4L5tCExXXE6NF4KyMzC3hl3E=; b=adYP9ARlMkOGjeMAfdv1kZipj+yRoD/zlCkncppVVxnJTQ7xE3BE6DAYncD9I4ioJe 9J9/gutHqVLcPD8gU97/LOdqzcy/uqdp5QquL4Gt7ONR9SmRJeCNhyaqmgvIn5tO0zX8 g70Itn+FsE1FJwAOTtpCtKWI4gIOcG6nIcQBnpKO5fpWUxhbKAxJmhY1ACLcRcgN+Fy2 B6Got4LgW7hbgPkkQWlfXNhnGfGYQonv67Ej0qvfRqcppmoRYuGbF6sgddlmGydquFhT 7lemQsFryb7tzTyHfgwkRtTqZ323QxxsLonaM+BrBlvnqa1IgeYcgJOZk7C8LR3/3LnU G8mA== ARC-Authentication-Results: i=1; mx.google.com; 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 n1-v6si19330089pgl.407.2018.10.31.14.27.18; Wed, 31 Oct 2018 14:27:37 -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; 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 S1725940AbeKAG0X (ORCPT + 99 others); Thu, 1 Nov 2018 02:26:23 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:44320 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725736AbeKAG0X (ORCPT ); Thu, 1 Nov 2018 02:26:23 -0400 Received: from akpm3.svl.corp.google.com (unknown [104.133.8.65]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 398D4B09; Wed, 31 Oct 2018 21:26:33 +0000 (UTC) Date: Wed, 31 Oct 2018 14:26:32 -0700 From: Andrew Morton To: Larry Chen Cc: mark@fasheh.com, jlbec@evilplan.org, linux-kernel@vger.kernel.org, ocfs2-devel@oss.oracle.com, Joseph Qi , Changwei Ge Subject: Re: [PATCH V2] ocfs2: fix dead lock caused by ocfs2_defrag_extent Message-Id: <20181031142632.c523108bc137caa8cb1c6282@linux-foundation.org> In-Reply-To: <20180902091455.23862-1-lchen@suse.com> References: <20180902091455.23862-1-lchen@suse.com> X-Mailer: Sylpheed 3.6.0 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Folks, could we please review this patch for upstream inclusion? Thanks. From: Larry Chen Subject: ocfs2: fix deadlock caused by ocfs2_defrag_extent ocfs2_defrag_extent may fall into deadlock. ocfs2_ioctl_move_extents ocfs2_ioctl_move_extents ocfs2_move_extents ocfs2_defrag_extent ocfs2_lock_allocators_move_extents ocfs2_reserve_clusters inode_lock GLOBAL_BITMAP_SYSTEM_INODE __ocfs2_flush_truncate_log inode_lock GLOBAL_BITMAP_SYSTEM_INODE As backtrace shows above, ocfs2_reserve_clusters() will call inode_lock against the global bitmap if local allocator has not sufficient cluters. Once global bitmap could meet the demand, ocfs2_reserve_cluster will return success with global bitmap locked. After ocfs2_reserve_cluster(), if truncate log is full, __ocfs2_flush_truncate_log() will definitely fall into deadlock because it needs to inode_lock global bitmap, which has already been locked. To fix this bug, we could remove from ocfs2_lock_allocators_move_extents() the code which intends to lock global allocator, and put the removed code after __ocfs2_flush_truncate_log(). ocfs2_lock_allocators_move_extents() is refered by 2 places, one is here, the other does not need the data allocator context, which means this patch does not affect the caller so far. [lchen@suse.com: rename ocfs2_lock_allocators_move_extents() to ocfs2_lock_meta_allocator_move_extents(), add some comments] Link: http://lkml.kernel.org/r/20180902091455.23862-1-lchen@suse.com Link: http://lkml.kernel.org/r/20180827080121.31145-1-lchen@suse.com Signed-off-by: Larry Chen Cc: Mark Fasheh Cc: Joel Becker Cc: Junxiao Bi Cc: Joseph Qi Cc: Changwei Ge Signed-off-by: Andrew Morton --- fs/ocfs2/move_extents.c | 40 +++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) --- a/fs/ocfs2/move_extents.c~fix-dead-lock-caused-by-ocfs2_defrag_extent +++ a/fs/ocfs2/move_extents.c @@ -162,7 +162,7 @@ out: * in some cases, we don't need to reserve clusters, just let data_ac * be NULL. */ -static int ocfs2_lock_allocators_move_extents(struct inode *inode, +static int ocfs2_lock_meta_allocator_move_extents(struct inode *inode, struct ocfs2_extent_tree *et, u32 clusters_to_move, u32 extents_to_split, @@ -192,13 +192,6 @@ static int ocfs2_lock_allocators_move_ex goto out; } - if (data_ac) { - ret = ocfs2_reserve_clusters(osb, clusters_to_move, data_ac); - if (ret) { - mlog_errno(ret); - goto out; - } - } *credits += ocfs2_calc_extend_credits(osb->sb, et->et_root_el); @@ -257,10 +250,11 @@ static int ocfs2_defrag_extent(struct oc } } - ret = ocfs2_lock_allocators_move_extents(inode, &context->et, *len, 1, - &context->meta_ac, - &context->data_ac, - extra_blocks, &credits); + ret = ocfs2_lock_meta_allocator_move_extents(inode, &context->et, + *len, 1, + &context->meta_ac, + &context->data_ac, + extra_blocks, &credits); if (ret) { mlog_errno(ret); goto out; @@ -283,6 +277,21 @@ static int ocfs2_defrag_extent(struct oc } } + /* + * Make sure ocfs2_reserve_cluster is called after + * __ocfs2_flush_truncate_log, otherwise, dead lock may happen. + * + * If ocfs2_reserve_cluster is called + * before __ocfs2_flush_truncate_log, dead lock on global bitmap + * may happen. + * + */ + ret = ocfs2_reserve_clusters(osb, *len, &context->data_ac); + if (ret) { + mlog_errno(ret); + goto out_unlock_mutex; + } + handle = ocfs2_start_trans(osb, credits); if (IS_ERR(handle)) { ret = PTR_ERR(handle); @@ -600,9 +609,10 @@ static int ocfs2_move_extent(struct ocfs } } - ret = ocfs2_lock_allocators_move_extents(inode, &context->et, len, 1, - &context->meta_ac, - NULL, extra_blocks, &credits); + ret = ocfs2_lock_meta_allocator_move_extents(inode, &context->et, + len, 1, + &context->meta_ac, + NULL, extra_blocks, &credits); if (ret) { mlog_errno(ret); goto out; _