linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 1/2] btrfs: don't try to flush data write bio if we hit error preparing it
Date: Mon,  2 Aug 2021 14:54:46 +0800	[thread overview]
Message-ID: <20210802065447.178726-2-wqu@suse.com> (raw)
In-Reply-To: <20210802065447.178726-1-wqu@suse.com>

[BUG]
When running with 64K page size and 4K sectorsize (aka, subpage), there
is a chance that generic/475 would crash with the following BUG_ON() in
btrfs_csum_one_bio() triggered:

		if (!ordered) {
			ordered = btrfs_lookup_ordered_extent(inode, offset);
			BUG_ON(!ordered); /* Logic error */
		}

[CAUSE]
Test case generic/475 is doing dm-error testing, while for subpage case
we could hit error caused by dm-errors.

In that case, we will proper call end_extent_writepage() with @err = 1
to do the cleanup, including finishing the ordered extent.

In that case, the assembled bio still needs to be finished, by
end_write_bio() function.

But there are call sites that doesn't properly call end_write_bio(), but
go flush_write_bio() to submit the assembled bio.

In that case, we will call btrfs_csum_one_bio() even the ordered extent
is already cleaned up, and trigger the BUG_ON().

[FIX]
There are two call sites where we still try to call flush_write_bio()
even if we hit error:

- extent_write_cache_pages()
- extent_write_locked_range()

Fix both of them by calling end_write_bio() if we hit error.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/extent_io.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index e82328bcb281..49f7dbbb2d73 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -5035,9 +5035,13 @@ static int extent_write_cache_pages(struct address_space *mapping,
 		 * page in our current bio, and thus deadlock, so flush the
 		 * write bio here.
 		 */
-		ret = flush_write_bio(epd);
-		if (!ret)
-			goto retry;
+		if (ret < 0) {
+			end_write_bio(epd, ret);
+		} else {
+			ret = flush_write_bio(epd);
+			if (!ret)
+				goto retry;
+		}
 	}
 
 	if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
@@ -5095,9 +5099,11 @@ int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
 	wbc_attach_fdatawrite_inode(&wbc_writepages, inode);
 	while (start <= end) {
 		page = find_get_page(mapping, start >> PAGE_SHIFT);
-		if (clear_page_dirty_for_io(page))
+		if (clear_page_dirty_for_io(page)) {
 			ret = __extent_writepage(page, &wbc_writepages, &epd);
-		else {
+			if (ret < 0)
+				goto out;
+		} else {
 			btrfs_writepage_endio_finish_ordered(BTRFS_I(inode),
 					page, start, start + PAGE_SIZE - 1, true);
 			unlock_page(page);
@@ -5106,6 +5112,7 @@ int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
 		start += PAGE_SIZE;
 	}
 
+out:
 	ASSERT(ret <= 0);
 	if (ret == 0)
 		ret = flush_write_bio(&epd);
-- 
2.32.0


  reply	other threads:[~2021-08-02  6:54 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-02  6:54 [PATCH 0/2] btrfs: fix the generic/475 crash for subpage case Qu Wenruo
2021-08-02  6:54 ` Qu Wenruo [this message]
2021-08-02  6:54 ` [PATCH 2/2] btrfs: replace BUG_ON() in btrfs_csum_one_bio() with proper error handling Qu Wenruo
2021-08-02  7:53   ` Nikolay Borisov
2021-08-02  8:03     ` Qu Wenruo
2021-08-02  8:49       ` Nikolay Borisov
2021-08-16 14:21         ` David Sterba
2021-08-16 23:30           ` Qu Wenruo
2021-08-03  5:31 ` [PATCH 0/2] btrfs: fix the generic/475 crash for subpage case Qu Wenruo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210802065447.178726-2-wqu@suse.com \
    --to=wqu@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).