Message ID | 7026d8edc3d1ea5b0ef7307298149d06210807ce.1741144890.git.wqu@suse.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | [v2] btrfs: reject out-of-band dirty folios during writeback | expand |
On Wed, Mar 05, 2025 at 01:51:39PM +1030, Qu Wenruo wrote: > [OUT-OF-BAND DIRTY FOLIOS] > An out-of-band folio means the folio is marked dirty but without > notifying the filesystem. > > This can lead to various problems, not limited to: > > - No folio::private to track per block status > > - No proper space reserved for such a dirty folio > > [HISTORY IN BTRFS] > This used to be a problem related to get_user_page(), but with the > introduction of pin_user_pages*(), we should no longer hit such > case anymore. > > In btrfs, we have a long history of catching such out-of-band dirty > folios by: > > - Mark the folio ordered during delayed allocation > > - Check the folio ordered flag during writeback > If the folio has no ordered flag, it means it doesn't go through > delayed allocation, thus it's definitely an out-of-band > one. > > If we got one, we go through COW fixup, which will re-dirty the folio > with proper handling in another workqueue. > > [PROBLEMS OF COW-FIXUP] > Such workaround is a blockage for us to migrate to iomap (it requires > extra flags to trace if a folio is dirtied by the fs or not) and I'd > argue it's not data checksum safe, since if a folio can be marked dirty > without informing the fs, the content can also change at any time. > > But with the introduction of pin_user_pages*() during v5.8 merge > window, such out-of-band dirty folio such be treated as a bug. > Ext4 has treated such case by warning and erroring out even before > pin_user_pages*(). > > Furthermore, there are already proofs that such folio ordered flag > tracking can be screwed up by incorrect error handling, check the commit > messages of the following commits: > > 06f364284794 ("btrfs: do proper folio cleanup when cow_file_range() failed") > c2b47df81c8e ("btrfs: do proper folio cleanup when run_delalloc_nocow() failed") > > [FIXES] > Unlike btrfs, ext4 and xfs (iomap) never bother handling such > out-of-band dirty folios. > > - Ext4 just warns and errors out > - Iomap always follows the folio/block dirty flags > > And there is nothing really COW specific, xfs also supports COW too. > > Here we take one step towards ext4 by doing warning and erroring out. > But since the cow fixup thing is introduced from the beginning, we keep > the old behavior for non-experimental builds, and only do the new warning > for experimental builds before we're 100% sure and remove cow fixup. > > Signed-off-by: Qu Wenruo <wqu@suse.com> Thanks, this is a good intermediate step before we remove the cow fixup for good. Reviewed-by: David Sterba <dsterba@suse.com>
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c index 57906c226220..365be9eb4e04 100644 --- a/fs/btrfs/extent_io.c +++ b/fs/btrfs/extent_io.c @@ -1624,12 +1624,14 @@ static noinline_for_stack int extent_writepage_io(struct btrfs_inode *inode, start + len <= folio_start + folio_size(folio)); ret = btrfs_writepage_cow_fixup(folio); - if (ret) { + if (ret == -EAGAIN) { /* Fixup worker will requeue */ folio_redirty_for_writepage(bio_ctrl->wbc, folio); folio_unlock(folio); return 1; } + if (ret < 0) + return ret; for (cur = start; cur < start + len; cur += fs_info->sectorsize) set_bit((cur - folio_start) >> fs_info->sectorsize_bits, &range_bitmap); @@ -1734,6 +1736,30 @@ static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl * The proper bitmap can only be initialized until writepage_delalloc(). */ bio_ctrl->submit_bitmap = (unsigned long)-1; + + /* + * If the page is dirty but without private set, it's marked dirty + * without informing the fs. + * Nowadays that is a bug, since the introduction of + * pin_user_pages*(). + * + * So here we check if the page has private set to rule out such + * case. + * But we also have a long history of relying on the COW fixup, + * so here we only enable this check for experimental builds until + * we're sure it's safe. + */ + if (IS_ENABLED(CONFIG_BTRFS_EXPERIMENTAL) && + unlikely(!folio_test_private(folio))) { + WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); + btrfs_err_rl(fs_info, + "root %lld ino %llu folio %llu is marked dirty without notifying the fs", + inode->root->root_key.objectid, + btrfs_ino(inode), folio_pos(folio)); + ret = -EUCLEAN; + goto done; + } + ret = set_folio_extent_mapped(folio); if (ret < 0) goto done; diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index f9bd9242acd3..467550c477d8 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -2890,6 +2890,21 @@ int btrfs_writepage_cow_fixup(struct folio *folio) if (folio_test_ordered(folio)) return 0; + /* + * For experimental build, we error out instead of EAGAIN. + * + * We should not hit such out-of-band dirty folios anymore. + */ + if (IS_ENABLED(CONFIG_BTRFS_EXPERIMENTAL)) { + WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); + btrfs_err_rl(fs_info, + "root %lld ino %llu folio %llu is marked dirty without notifying the fs", + BTRFS_I(inode)->root->root_key.objectid, + btrfs_ino(BTRFS_I(inode)), + folio_pos(folio)); + return -EUCLEAN; + } + /* * folio_checked is set below when we create a fixup worker for this * folio, don't try to create another one if we're already
[OUT-OF-BAND DIRTY FOLIOS] An out-of-band folio means the folio is marked dirty but without notifying the filesystem. This can lead to various problems, not limited to: - No folio::private to track per block status - No proper space reserved for such a dirty folio [HISTORY IN BTRFS] This used to be a problem related to get_user_page(), but with the introduction of pin_user_pages*(), we should no longer hit such case anymore. In btrfs, we have a long history of catching such out-of-band dirty folios by: - Mark the folio ordered during delayed allocation - Check the folio ordered flag during writeback If the folio has no ordered flag, it means it doesn't go through delayed allocation, thus it's definitely an out-of-band one. If we got one, we go through COW fixup, which will re-dirty the folio with proper handling in another workqueue. [PROBLEMS OF COW-FIXUP] Such workaround is a blockage for us to migrate to iomap (it requires extra flags to trace if a folio is dirtied by the fs or not) and I'd argue it's not data checksum safe, since if a folio can be marked dirty without informing the fs, the content can also change at any time. But with the introduction of pin_user_pages*() during v5.8 merge window, such out-of-band dirty folio such be treated as a bug. Ext4 has treated such case by warning and erroring out even before pin_user_pages*(). Furthermore, there are already proofs that such folio ordered flag tracking can be screwed up by incorrect error handling, check the commit messages of the following commits: 06f364284794 ("btrfs: do proper folio cleanup when cow_file_range() failed") c2b47df81c8e ("btrfs: do proper folio cleanup when run_delalloc_nocow() failed") [FIXES] Unlike btrfs, ext4 and xfs (iomap) never bother handling such out-of-band dirty folios. - Ext4 just warns and errors out - Iomap always follows the folio/block dirty flags And there is nothing really COW specific, xfs also supports COW too. Here we take one step towards ext4 by doing warning and erroring out. But since the cow fixup thing is introduced from the beginning, we keep the old behavior for non-experimental builds, and only do the new warning for experimental builds before we're 100% sure and remove cow fixup. Signed-off-by: Qu Wenruo <wqu@suse.com> --- Changelog: v2: - Rebased to the latest for-next - Slightly change the commit message To mention that bugs in error handling can lead to false COW fixup routine. --- fs/btrfs/extent_io.c | 28 +++++++++++++++++++++++++++- fs/btrfs/inode.c | 15 +++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-)