From patchwork Tue Feb 21 17:14:52 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 9585211 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 6A866600CA for ; Tue, 21 Feb 2017 17:15:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 55CBA2851A for ; Tue, 21 Feb 2017 17:15:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4AC0128619; Tue, 21 Feb 2017 17:15:08 +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=-6.9 required=2.0 tests=BAYES_00,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 92B5F2851A for ; Tue, 21 Feb 2017 17:15:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753906AbdBURPG (ORCPT ); Tue, 21 Feb 2017 12:15:06 -0500 Received: from mail.kernel.org ([198.145.29.136]:60392 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753514AbdBURO7 (ORCPT ); Tue, 21 Feb 2017 12:14:59 -0500 Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id CC5262021B for ; Tue, 21 Feb 2017 17:14:56 +0000 (UTC) Received: from debian3.lan (bl12-226-64.dsl.telepac.pt [85.245.226.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2D2E5201C8 for ; Tue, 21 Feb 2017 17:14:55 +0000 (UTC) From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH] Btrfs: fix deadlock between dedup on same file and starting writeback Date: Tue, 21 Feb 2017 17:14:52 +0000 Message-Id: <1487697292-10015-1-git-send-email-fdmanana@kernel.org> X-Mailer: git-send-email 2.7.0.rc3 X-Virus-Scanned: ClamAV using ClamSMTP 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 From: Filipe Manana If we are deduping two ranges of the same file we need to make sure that we lock all pages in ascending order, that is, lock first the pages from the range with lower offset and then the pages from the other range, as otherwise we can deadlock with a concurrent task that is starting delalloc (writeback). Example trace: [74073.052218] INFO: task kworker/u32:10:17997 blocked for more than 120 seconds. [74073.053889] Tainted: G W 4.9.0-rc7-btrfs-next-36+ #1 [74073.055071] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. [74073.056696] kworker/u32:10 D 0 17997 2 0x00000000 [74073.058606] Workqueue: writeback wb_workfn (flush-btrfs-53176) [74073.061370] ffff880031e79858 ffff8802159d2580 ffff880237004580 ffff880031e79240 [74073.064784] ffff88023f4978c0 ffffc9000817b638 ffffffff814c15e1 0000000000000000 [74073.068386] ffff88023f4978d8 ffff88023f4978c0 000000000017b620 ffff880031e79240 [74073.071712] Call Trace: [74073.072884] [] ? __schedule+0x48f/0x6f4 [74073.075395] [] ? bit_wait+0x2f/0x2f [74073.077511] [] schedule+0x8c/0xa0 [74073.079440] [] schedule_timeout+0x43/0xff [74073.081637] [] ? time_hardirqs_on+0x9/0x14 [74073.083809] [] ? trace_hardirqs_on_caller+0x16/0x197 [74073.086314] [] ? timekeeping_get_ns+0x1e/0x32 [74073.100654] [] ? ktime_get+0x41/0x52 [74073.102619] [] io_schedule_timeout+0xa0/0x102 [74073.104771] [] ? io_schedule_timeout+0xa0/0x102 [74073.106969] [] bit_wait_io+0x1b/0x39 [74073.108954] [] __wait_on_bit_lock+0x4f/0x99 [74073.110981] [] __lock_page+0x6b/0x6d [74073.112833] [] ? autoremove_wake_function+0x3a/0x3a [74073.115010] [] lock_page+0x2f/0x32 [btrfs] [74073.116999] [] lock_delalloc_pages+0xc7/0x1a0 [btrfs] [74073.119243] [] find_lock_delalloc_range+0xc3/0x1a4 [btrfs] [74073.121636] [] writepage_delalloc.isra.31+0x8b/0x134 [btrfs] [74073.124229] [] __extent_writepage+0x1c1/0x2bf [btrfs] [74073.126372] [] extent_write_cache_pages.isra.30.constprop.49+0x28b/0x36c [btrfs] [74073.129371] [] extent_writepages+0x4b/0x5c [btrfs] [74073.131440] [] ? insert_reserved_file_extent.constprop.42+0x261/0x261 [btrfs] [74073.134303] [] ? writeback_sb_inodes+0xe0/0x4a1 [74073.136298] [] btrfs_writepages+0x28/0x2a [btrfs] [74073.138248] [] do_writepages+0x23/0x2c [74073.139910] [] __writeback_single_inode+0x105/0x6d2 [74073.142003] [] writeback_sb_inodes+0x292/0x4a1 [74073.136298] [] btrfs_writepages+0x28/0x2a [btrfs] [74073.138248] [] do_writepages+0x23/0x2c [74073.139910] [] __writeback_single_inode+0x105/0x6d2 [74073.142003] [] writeback_sb_inodes+0x292/0x4a1 [74073.143911] [] __writeback_inodes_wb+0x76/0xae [74073.145787] [] wb_writeback+0x1cc/0x4d7 [74073.147452] [] wb_workfn+0x194/0x37d [74073.149084] [] ? wb_workfn+0x194/0x37d [74073.150726] [] ? process_one_work+0x154/0x4e4 [74073.152694] [] process_one_work+0x273/0x4e4 [74073.154452] [] worker_thread+0x1eb/0x2ca [74073.156138] [] ? rescuer_thread+0x2b6/0x2b6 [74073.157837] [] kthread+0xd5/0xdd [74073.159339] [] ? __kthread_unpark+0x5a/0x5a [74073.161088] [] ret_from_fork+0x27/0x40 [74073.162680] INFO: lockdep is turned off. [74073.163855] INFO: task do-dedup:30264 blocked for more than 120 seconds. [74073.181180] Tainted: G W 4.9.0-rc7-btrfs-next-36+ #1 [74073.181180] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. [74073.185296] fdm-stress D 0 30264 29974 0x00000000 [74073.186810] ffff880089595118 ffff880211b8eac0 ffff880237030380 ffff880089594b00 [74073.188998] ffff88023f2978c0 ffffc900063abb68 ffffffff814c15e1 0000000000000000 [74073.191070] ffff88023f2978d8 ffff88023f2978c0 00000000003abb50 ffff880089594b00 [74073.193286] Call Trace: [74073.193990] [] ? __schedule+0x48f/0x6f4 [74073.195418] [] ? bit_wait+0x2f/0x2f [74073.196796] [] schedule+0x8c/0xa0 [74073.198163] [] schedule_timeout+0x43/0xff [74073.199621] [] ? trace_hardirqs_on+0xd/0xf [74073.201100] [] ? timekeeping_get_ns+0x1e/0x32 [74073.202686] [] ? ktime_get+0x41/0x52 [74073.204051] [] io_schedule_timeout+0xa0/0x102 [74073.205585] [] ? io_schedule_timeout+0xa0/0x102 [74073.207123] [] bit_wait_io+0x1b/0x39 [74073.208238] [] __wait_on_bit_lock+0x4f/0x99 [74073.208871] [] __lock_page+0x6b/0x6d [74073.209430] [] ? autoremove_wake_function+0x3a/0x3a [74073.210101] [] lock_page+0x2f/0x32 [74073.210636] [] pagecache_get_page+0x5e/0x153 [74073.211270] [] gather_extent_pages+0x4e/0x109 [btrfs] [74073.212166] [] btrfs_dedupe_file_range+0x1e1/0x4dd [btrfs] [74073.213257] [] vfs_dedupe_file_range+0x1c1/0x221 [74073.214086] [] do_vfs_ioctl+0x442/0x600 [74073.214767] [] ? rcu_read_unlock+0x5b/0x5d [74073.215619] [] ? __fget+0x6b/0x77 [74073.216338] [] SyS_ioctl+0x57/0x79 [74073.217149] [] entry_SYSCALL_64_fastpath+0x18/0xad [74073.218102] [] ? time_hardirqs_off+0x9/0x14 [74073.218968] [] ? trace_hardirqs_off_caller+0x1f/0xaa [74073.219938] INFO: lockdep is turned off. What happened was the following: CPU 1 CPU 2 btrfs_dedupe_file_range() --> using same inode as source and target --> src range is [768K, 1Mb[ --> dst range is [0, 256K[ btrfs_cmp_data_prepare() --> calls gather_extent_pages() for range [768K, 1Mb[ and locks all pages in that range do_writepages() btrfs_writepages() extent_writepages() extent_write_cache_pages() __extent_writepage() writepage_delalloc() find_lock_delalloc_range() --> finds range [0, 1Mb[ lock_delalloc_pages() --> locks all pages in the range [0, 768K[ --> tries to lock page at offset 768K --> deadlock --> calls gather_extent_pages() to lock pages in the range [0, 256K[ --> deadlock, task at CPU 1 already locked that range and it's trying to lock the range we locked previously So fix this by making sure that during a dedup we always lock first the pages from the range with lower offset. Signed-off-by: Filipe Manana Reviewed-by: Liu Bo --- fs/btrfs/ioctl.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index 0c77cad..4284279 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -3047,11 +3047,21 @@ static int btrfs_cmp_data_prepare(struct inode *src, u64 loff, cmp->src_pages = src_pgarr; cmp->dst_pages = dst_pgarr; - ret = gather_extent_pages(src, cmp->src_pages, cmp->num_pages, loff); + /* + * If deduping ranges in the same inode, locking rules make it mandatory + * to always lock pages in ascending order to avoid deadlocks with + * concurrent tasks (such as starting writeback/delalloc). + */ + if (src == dst && dst_loff < loff) { + swap(src_pgarr, dst_pgarr); + swap(loff, dst_loff); + } + + ret = gather_extent_pages(src, src_pgarr, cmp->num_pages, loff); if (ret) goto out; - ret = gather_extent_pages(dst, cmp->dst_pages, cmp->num_pages, dst_loff); + ret = gather_extent_pages(dst, dst_pgarr, cmp->num_pages, dst_loff); out: if (ret)