Skip to contentSkip to conversation

btrfs: setup the page before calling any subpage helper

PATCH In review Qu Wenruo 2 participants 4 messages Jul 30, 2021 – Aug 12, 2021 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH 1/1 btrfs: setup the page before calling any subpage helper

Function set_page_extent_mapped() will setup the data page cache so that for subpage cases those pages will have page->private to store subpage specific info.

Normally this happens when we create a new page for the page cache. But there is a special call site, __extent_writepage(), as we have special cases where upper layer can mark some page dirty without going through set_page_dirty() interface.

I haven't yet seen any real world case for this, but if that's possible then in __extent_writepage() we will call btrfs_page_clear_error() before setting up the page->private, which can lead to NULL pointer dereference.

Fix it by moving set_page_extent_mapped() call before btrfs_page_clear_error().
And make sure in the error path we won't call anything subpage helper.

Fixes 32443de3382b ("btrfs: introduce btrfs_subpage for data inodes")

---
I really hope we can have a more explicit comment about in exactly which cases we can have such page, and maybe some test cases for it.

In fact, I haven't really seen any case like this, and it doesn't really make sense for me to make some MM layer code to mark a page dirty without going through set_page_dirty() interface.

---
 fs/btrfs/extent_io.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
fs/btrfs/extent_io.c +7 -6
index e665779c046d..e82328bcb281 100644
@@ -4065,6 +4065,13 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc,
 
 	WARN_ON(!PageLocked(page));
 
+	ret = set_page_extent_mapped(page);
+	if (ret < 0) {
+		SetPageError(page);
+		unlock_page(page);
+		return ret;
+	}
+
 	btrfs_page_clear_error(btrfs_sb(inode->i_sb), page,
 			       page_offset(page), PAGE_SIZE);
 
@@ -4081,12 +4088,6 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc,
 		flush_dcache_page(page);
 	}
 
-	ret = set_page_extent_mapped(page);
-	if (ret < 0) {
-		SetPageError(page);
-		goto done;
-	}
-
 	if (!epd->extent_locked) {
 		ret = writepage_delalloc(BTRFS_I(inode), page, wbc, start,
 					 &nr_written);
Signature · 1 line
2.32.0
#1 lore raw
Quoted 9 lines · Qu Wenruo

Function set_page_extent_mapped() will setup the data page cache so that for subpage cases those pages will have page->private to store subpage specific info.

Normally this happens when we create a new page for the page cache. But there is a special call site, __extent_writepage(), as we have special cases where upper layer can mark some page dirty without going through set_page_dirty() interface.

Quoted 4 lines

I haven't yet seen any real world case for this, but if that's possible then in __extent_writepage() we will call btrfs_page_clear_error() before setting up the page->private, which can lead to NULL pointer dereference.

Yeah it's hard to believe, but it's been there since almost the beginning. Back then there was a hard BUG() in the fixup worker, I've hit it randomly on x86_64,

https://lore.kernel.org/linux-btrfs/[email protected]/

you could find a lot of other reports where it crashed inside btrfs_writepage_fixup_worker.

Quoted 3 lines

Fix it by moving set_page_extent_mapped() call before btrfs_page_clear_error().
And make sure in the error path we won't call anything subpage helper.

I'm not sure about the fix, because the whole fixup thing is not entirely clear.

Quoted 5 lines
Fixes 32443de3382b ("btrfs: introduce btrfs_subpage for data inodes")

---
I really hope we can have a more explicit comment about in exactly which cases we can have such page, and maybe some test cases for it.

The only reliable test case was on s390 with a particular seed for fsx, I still have it stored somewhere. On x86_64 it's very hard to hit.

Quoted 3 lines

In fact, I haven't really seen any case like this, and it doesn't really make sense for me to make some MM layer code to mark a page dirty without going through set_page_dirty() interface.

On s390 it's quick because the page state bits are stored in 2 places and need to be synced. On x86_64 it's very unclear and low level arch specific MM stuff but it is still a problem.

author
#2 lore raw
Quoted 15 lines · David Sterba
Quoted 9 lines · Qu Wenruo

Function set_page_extent_mapped() will setup the data page cache so that for subpage cases those pages will have page->private to store subpage specific info.

Normally this happens when we create a new page for the page cache. But there is a special call site, __extent_writepage(), as we have special cases where upper layer can mark some page dirty without going through set_page_dirty() interface.

Quoted 4 lines

I haven't yet seen any real world case for this, but if that's possible then in __extent_writepage() we will call btrfs_page_clear_error() before setting up the page->private, which can lead to NULL pointer dereference.

Quoted 8 lines

Yeah it's hard to believe, but it's been there since almost the beginning. Back then there was a hard BUG() in the fixup worker, I've hit it randomly on x86_64,

https://lore.kernel.org/linux-btrfs/[email protected]/

you could find a lot of other reports where it crashed inside btrfs_writepage_fixup_worker.

Well, over 10 years ago.

But finally I have seen a real world report for it.

This makes me wonder, wouldn't other sites like iomap, which also utilize a private structure for subpage bitmaps, also hit such crash?

Quoted 7 lines
Quoted 4 lines

Fix it by moving set_page_extent_mapped() call before btrfs_page_clear_error().
And make sure in the error path we won't call anything subpage helper.

I'm not sure about the fix, because the whole fixup thing is not entirely clear.

Indeed, but the idea should be straightfoward:

Don't call any subpage helper before set_page_extent_mapped().

So that such page without private get passed in, we can still handle it well.

Quoted 9 lines
Quoted 6 lines
Fixes 32443de3382b ("btrfs: introduce btrfs_subpage for data inodes")

---
I really hope we can have a more explicit comment about in exactly which cases we can have such page, and maybe some test cases for it.

The only reliable test case was on s390 with a particular seed for fsx, I still have it stored somewhere. On x86_64 it's very hard to hit.

Can it be reproduced by S390 qemu tcc emulation?

And by S390, did you mean modern Power8/9/10 systems too?

Quoted 9 lines
Quoted 4 lines

In fact, I haven't really seen any case like this, and it doesn't really make sense for me to make some MM layer code to mark a page dirty without going through set_page_dirty() interface.

On s390 it's quick because the page state bits are stored in 2 places and need to be synced. On x86_64 it's very unclear and low level arch specific MM stuff but it is still a problem.

The hard to hit part is really harming our test coverage...

Thanks,
Qu

#3 lore raw
Quoted 11 lines · Qu Wenruo
Quoted 9 lines · David Sterba

On Fri, Jul 30, 2021 at 01:58:57PM +0800, Qu Wenruo wrote:
Yeah it's hard to believe, but it's been there since almost the beginning. Back then there was a hard BUG() in the fixup worker, I've hit it randomly on x86_64,

https://lore.kernel.org/linux-btrfs/[email protected]/

you could find a lot of other reports where it crashed inside btrfs_writepage_fixup_worker.

Quoted 6 lines

Well, over 10 years ago.

But finally I have seen a real world report for it.

This makes me wonder, wouldn't other sites like iomap, which also utilize a private structure for subpage bitmaps, also hit such crash?

I don't know.

Quoted 21 lines
Quoted 6 lines
Quoted 3 lines

Fix it by moving set_page_extent_mapped() call before btrfs_page_clear_error().
And make sure in the error path we won't call anything subpage helper.

I'm not sure about the fix, because the whole fixup thing is not entirely clear.

Indeed, but the idea should be straightfoward:

Don't call any subpage helper before set_page_extent_mapped().

So that such page without private get passed in, we can still handle it well.

Quoted 7 lines
Quoted 6 lines
Fixes 32443de3382b ("btrfs: introduce btrfs_subpage for data inodes")

---
I really hope we can have a more explicit comment about in exactly which cases we can have such page, and maybe some test cases for it.

Quoted 4 lines
Quoted 2 lines

The only reliable test case was on s390 with a particular seed for fsx, I still have it stored somewhere. On x86_64 it's very hard to hit.

Can it be reproduced by S390 qemu tcc emulation?

I think the reproducer was deterministic, depending on the fsx seed it created a file layout that at some point triggered the desync between hw page and memory management page, requiring the fixup.

So even if the emulation is slow, it should work to verify the test. If you set that up, you could also restore the BUG() inside the fixup worker to see for yourself that this mysterious fixup thing is real.

Quoted 1 line

And by S390, did you mean modern Power8/9/10 systems too?

I haven't been testing on Power machines a lot so I don't know, it's possible that the success rate hitting that would be similar as on x86_64.

Quoted 9 lines
Quoted 7 lines
Quoted 3 lines

In fact, I haven't really seen any case like this, and it doesn't really make sense for me to make some MM layer code to mark a page dirty without going through set_page_dirty() interface.

On s390 it's quick because the page state bits are stored in 2 places and need to be synced. On x86_64 it's very unclear and low level arch specific MM stuff but it is still a problem.

The hard to hit part is really harming our test coverage...

I don't disagree with that.