Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp619572img; Fri, 22 Mar 2019 05:17:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqzWi0o2evMcJVQ85rks5eN265ZKXNilIvrIp2ULWPaGWPLRQw/Gb1noWIMPyx1HBU5kIGvB X-Received: by 2002:a62:6e05:: with SMTP id j5mr9150895pfc.158.1553257052265; Fri, 22 Mar 2019 05:17:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553257052; cv=none; d=google.com; s=arc-20160816; b=rCfR5y2LLgzbnrx3oe/CtnRUqHYxVpaSbw+cu1bxidny147GbI7a97aeJ9Qpu+1Z/Z lMtUHBZLqnR229dYH40UpUs6/ztEusncDKHi918Dqu2vzgraiB5JjXigFfZTs/2wVhC1 EiuRwnSclEwygxrsmgSExUUDpwyKc8VdF0Bzu+58NPbiNokw8NCKk7XtmX7iiBjzp3Jm O81F7ztc8FehBZ4dxWefzxsdlUTcV7tosw69RT3hQ/ye4IRcYMjqtlLjOsGBNWSN2L7G NmAgVB7zRE2Gh1VXbA6UGbZjMcj8mioJ6K6OK2gIuE3nL+OeWp2yhj9i2WvJTGfGcvUX l74Q== 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=hGWQ54RYweSZMOZ1rpHeGpDNPpn7xThF455CgHAStsU=; b=bR8AaKpsYkJgfJXP+lZfiev8YB66ZBZl6RJRRUbaovo8QSVEssDoTW54eyU7on6OM9 T13a/fgPN8PJ5j6knMJH/C5lzP8k3f5dbHDVxrwPAJzRwP/F/IOdr/mDBKNk7+wix/HD ZO2PWK26jf2dtOZ9O/gSlsU/3i9PH8/SoLHdsuDPPWJpSS6wywOurHBX7h2e/HM7ypjq +khu7rVZV8OJ9zyReV406zp5UMRurb0ltxgI1YbOP5o/UJ70Li5WrJ9NmEwGq+XxZ69h IyuTA1ss8JyAlbrpLplroV/gDMq130N3nd0FKZnZ20hjnl8YZ1YfaA/sRxABXDHxg6I4 5ifQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=rWBzs3SM; 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 124si6720172pfw.148.2019.03.22.05.17.17; Fri, 22 Mar 2019 05:17:32 -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=rWBzs3SM; 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 S2390217AbfCVMQ3 (ORCPT + 99 others); Fri, 22 Mar 2019 08:16:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:55112 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390211AbfCVMQ1 (ORCPT ); Fri, 22 Mar 2019 08:16:27 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 79A6A2083D; Fri, 22 Mar 2019 12:16:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553256986; bh=P27Cx0S3UEZY+SSGVQn0Ze0F2UeM7CCb+qjgzdqGKkI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rWBzs3SM5MOBj3/dS9DaLcA3hGQ58MzUX492Vqju32Lk0ZpV1MRoLTM8wOxqYe2LG qUlywnc3iIbvpCukLcR1QTli0Ih9cGNW1DsMl74l6xpdy0uEIye8QkYH1r9cEk9GCi /c+v6SI8E2zKBmURxbkRL0W8xj1bQWcy+bjZG7nc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zygo Blaxell , Filipe Manana , David Sterba Subject: [PATCH 5.0 098/238] Btrfs: fix deadlock between clone/dedupe and rename Date: Fri, 22 Mar 2019 12:15:17 +0100 Message-Id: <20190322111304.345951751@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190322111258.383569278@linuxfoundation.org> References: <20190322111258.383569278@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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 5.0-stable review patch. If anyone has any objections, please let me know. ------------------ From: Filipe Manana commit 4ea748e1d2c9f8a27332b949e8210dbbf392987e upstream. Reflinking (clone/dedupe) and rename are operations that operate on two inodes and therefore need to lock them in the same order to avoid ABBA deadlocks. It happens that Btrfs' reflink implementation always locked them in a different order from VFS's lock_two_nondirectories() helper, which is used by the rename code in VFS, resulting in ABBA type deadlocks. Btrfs' locking order: static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2) { if (inode1 < inode2) swap(inode1, inode2); inode_lock_nested(inode1, I_MUTEX_PARENT); inode_lock_nested(inode2, I_MUTEX_CHILD); } VFS's locking order: void lock_two_nondirectories(struct inode *inode1, struct inode *inode2) { if (inode1 > inode2) swap(inode1, inode2); if (inode1 && !S_ISDIR(inode1->i_mode)) inode_lock(inode1); if (inode2 && !S_ISDIR(inode2->i_mode) && inode2 != inode1) inode_lock_nested(inode2, I_MUTEX_NONDIR2); } Fix this by killing the btrfs helper function that does the double inode locking and replace it with VFS's helper lock_two_nondirectories(). Reported-by: Zygo Blaxell Fixes: 416161db9b63e3 ("btrfs: offline dedupe") CC: stable@vger.kernel.org # 4.4+ Signed-off-by: Filipe Manana Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/ioctl.c | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -3206,21 +3206,6 @@ out: return ret; } -static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2) -{ - inode_unlock(inode1); - inode_unlock(inode2); -} - -static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2) -{ - if (inode1 < inode2) - swap(inode1, inode2); - - inode_lock_nested(inode1, I_MUTEX_PARENT); - inode_lock_nested(inode2, I_MUTEX_CHILD); -} - static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1, struct inode *inode2, u64 loff2, u64 len) { @@ -3989,7 +3974,7 @@ static int btrfs_remap_file_range_prep(s if (same_inode) inode_lock(inode_in); else - btrfs_double_inode_lock(inode_in, inode_out); + lock_two_nondirectories(inode_in, inode_out); /* * Now that the inodes are locked, we need to start writeback ourselves @@ -4039,7 +4024,7 @@ static int btrfs_remap_file_range_prep(s if (same_inode) inode_unlock(inode_in); else - btrfs_double_inode_unlock(inode_in, inode_out); + unlock_two_nondirectories(inode_in, inode_out); return ret; } @@ -4069,7 +4054,7 @@ loff_t btrfs_remap_file_range(struct fil if (same_inode) inode_unlock(src_inode); else - btrfs_double_inode_unlock(src_inode, dst_inode); + unlock_two_nondirectories(src_inode, dst_inode); return ret < 0 ? ret : len; }