Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp666386ybv; Thu, 13 Feb 2020 07:26:45 -0800 (PST) X-Google-Smtp-Source: APXvYqyTzk9/zt+XY1Zgl/rEvLEpmfxTMSVTgjdWaN/lCQiCrrFUPPCy1stIRiz2cSIyxr6Auxi6 X-Received: by 2002:aca:c415:: with SMTP id u21mr3285567oif.49.1581607604933; Thu, 13 Feb 2020 07:26:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581607604; cv=none; d=google.com; s=arc-20160816; b=00x/ksSzmeO4GDllC70kgxNIK+FaFRLOmlScqf0Hk46Sjf6dl8aZwFpPIEO3jwYxhG 0I1MEwsUEA3by7rPPGIJAnw3SREBSdG3Qv7tlnYrIg7Gy6K5yqL0d5Zmcnenkgx+fAcz JBEE0PzErEQlCdDv6r1WUsZyGrsvOsr0++KXVqwG7GLIjk9Dmx2ES7MHiHPsoCutT/yC bwiqpmJNX6SfLVfZVK18OeNR8PGrVYE63+UQipw1+D6yRb0BVB3tiqyn17OzIobsEKL1 fttk3eF71LM3MZLntYKbJuxEEDSwHBl885xbW+CEBemp+sDEu6WxP1A4p44GqqzPd7jh wPTQ== 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=B4IgRFchfXq4tlho4mrgJQJUwww3SWKNYNSGjscJxzc=; b=ZH1BaVoVMcWGSAVyzUM/hr5V/dTxgzaOffRiIcOiTXS1AIpQPHHqqfYzvjKmfobk8M 0qKmSeHd5la2gVFfWDG3f7npOukgyWx6AnmcgNB95rVcIK+eDbj8tjVDuMqM26CS84pW 41Ry+3Dv4/V1eSDCRa5RgqFjXPWC5UbDmbvDzU4NRHHmxjqtR+o+0HDQE70EayqU/0v6 CFSZ3mtW36yWw05NMENbKPPziAeLE/h7auA+320wXio+IHmKecSE9C5ZryktnmdDcZ8B rxC1xfUMN4z/83VbLGS6sb6V+y1zCxbcWxzm1PGdLctZXT4ChaCpiaSZ7gVCPFMdLlj5 JKGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=cah7D5CU; 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 w7si1339722otq.250.2020.02.13.07.26.32; Thu, 13 Feb 2020 07:26:44 -0800 (PST) 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=cah7D5CU; 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 S1728612AbgBMPZw (ORCPT + 99 others); Thu, 13 Feb 2020 10:25:52 -0500 Received: from mail.kernel.org ([198.145.29.99]:36772 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728592AbgBMPYH (ORCPT ); Thu, 13 Feb 2020 10:24:07 -0500 Received: from localhost (unknown [104.132.1.104]) (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 ECCFD24691; Thu, 13 Feb 2020 15:24:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581607447; bh=usdD7E2v7ryHJezLF4KmGqTvXV8BRUodlwhHkqQprBU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cah7D5CUzIx/h3sZ2MXiiGDZXnIaxJIAhe8K3/IySsnVzJcV9n+vOCG61lH3L+Uka zaWnFP94M4NBL0jhBz/68KxKj7le1Z2/Z+7vZRPnPYGsT1aNc14ZYeNx8fbEbWZJWY 3hSvX5kbbvCSvD8pOCFoM/SuEMpMC3gOGa2tFIjA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Filipe Manana , Josef Bacik , David Sterba , Sasha Levin Subject: [PATCH 4.9 089/116] btrfs: flush write bio if we loop in extent_write_cache_pages Date: Thu, 13 Feb 2020 07:20:33 -0800 Message-Id: <20200213151917.511897953@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200213151842.259660170@linuxfoundation.org> References: <20200213151842.259660170@linuxfoundation.org> User-Agent: quilt/0.66 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 From: Josef Bacik [ Upstream commit 96bf313ecb33567af4cb53928b0c951254a02759 ] There exists a deadlock with range_cyclic that has existed forever. If we loop around with a bio already built we could deadlock with a writer who has the page locked that we're attempting to write but is waiting on a page in our bio to be written out. The task traces are as follows PID: 1329874 TASK: ffff889ebcdf3800 CPU: 33 COMMAND: "kworker/u113:5" #0 [ffffc900297bb658] __schedule at ffffffff81a4c33f #1 [ffffc900297bb6e0] schedule at ffffffff81a4c6e3 #2 [ffffc900297bb6f8] io_schedule at ffffffff81a4ca42 #3 [ffffc900297bb708] __lock_page at ffffffff811f145b #4 [ffffc900297bb798] __process_pages_contig at ffffffff814bc502 #5 [ffffc900297bb8c8] lock_delalloc_pages at ffffffff814bc684 #6 [ffffc900297bb900] find_lock_delalloc_range at ffffffff814be9ff #7 [ffffc900297bb9a0] writepage_delalloc at ffffffff814bebd0 #8 [ffffc900297bba18] __extent_writepage at ffffffff814bfbf2 #9 [ffffc900297bba98] extent_write_cache_pages at ffffffff814bffbd PID: 2167901 TASK: ffff889dc6a59c00 CPU: 14 COMMAND: "aio-dio-invalid" #0 [ffffc9003b50bb18] __schedule at ffffffff81a4c33f #1 [ffffc9003b50bba0] schedule at ffffffff81a4c6e3 #2 [ffffc9003b50bbb8] io_schedule at ffffffff81a4ca42 #3 [ffffc9003b50bbc8] wait_on_page_bit at ffffffff811f24d6 #4 [ffffc9003b50bc60] prepare_pages at ffffffff814b05a7 #5 [ffffc9003b50bcd8] btrfs_buffered_write at ffffffff814b1359 #6 [ffffc9003b50bdb0] btrfs_file_write_iter at ffffffff814b5933 #7 [ffffc9003b50be38] new_sync_write at ffffffff8128f6a8 #8 [ffffc9003b50bec8] vfs_write at ffffffff81292b9d #9 [ffffc9003b50bf00] ksys_pwrite64 at ffffffff81293032 I used drgn to find the respective pages we were stuck on page_entry.page 0xffffea00fbfc7500 index 8148 bit 15 pid 2167901 page_entry.page 0xffffea00f9bb7400 index 7680 bit 0 pid 1329874 As you can see the kworker is waiting for bit 0 (PG_locked) on index 7680, and aio-dio-invalid is waiting for bit 15 (PG_writeback) on index 8148. aio-dio-invalid has 7680, and the kworker epd looks like the following crash> struct extent_page_data ffffc900297bbbb0 struct extent_page_data { bio = 0xffff889f747ed830, tree = 0xffff889eed6ba448, extent_locked = 0, sync_io = 0 } Probably worth mentioning as well that it waits for writeback of the page to complete while holding a lock on it (at prepare_pages()). Using drgn I walked the bio pages looking for page 0xffffea00fbfc7500 which is the one we're waiting for writeback on bio = Object(prog, 'struct bio', address=0xffff889f747ed830) for i in range(0, bio.bi_vcnt.value_()): bv = bio.bi_io_vec[i] if bv.bv_page.value_() == 0xffffea00fbfc7500: print("FOUND IT") which validated what I suspected. The fix for this is simple, flush the epd before we loop back around to the beginning of the file during writeout. Fixes: b293f02e1423 ("Btrfs: Add writepages support") CC: stable@vger.kernel.org # 4.4+ Reviewed-by: Filipe Manana Signed-off-by: Josef Bacik Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/extent_io.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c index 37a28e2369b91..1372d3e5d90bb 100644 --- a/fs/btrfs/extent_io.c +++ b/fs/btrfs/extent_io.c @@ -4060,6 +4060,14 @@ static int extent_write_cache_pages(struct extent_io_tree *tree, */ scanned = 1; index = 0; + + /* + * If we're looping we could run into a page that is locked by a + * writer and that writer could be waiting on writeback for a + * page in our current bio, and thus deadlock, so flush the + * write bio here. + */ + flush_write_bio(data); goto retry; } -- 2.20.1