Re: [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied
From: Pedro Falcato
Date: Thu Dec 03 2026 - 08:28:39 EST
On Wed, Dec 02, 2026 at 09:48:53AM +0530, Brahmajit Das wrote:
> From: Brahmajit Das <bdas@xxxxxxx>
>
> syzbot reported an assertion failure in wait_subpage_spinlock():
>
> assertion failed: folio_test_private(folio) && folio_get_private(folio),
> in fs/btrfs/inode.c:7616
> kernel BUG at fs/btrfs/inode.c:7616!
> Call Trace:
> btrfs_invalidate_folio+0x26e/0xc50
> extent_writepage fs/btrfs/extent_io.c:2058 [inline]
> extent_write_cache_pages fs/btrfs/extent_io.c:2843 [inline]
> btrfs_writepages+0x129f/0x28c0
> do_writepages+0x338/0x560
> filemap_fdatawrite_range+0x1ef/0x2f0
> btrfs_start_ordered_extent_nowriteback+0x305/0x6e0
> btrfs_page_mkwrite+0x856/0x1a40
> ...
> __gup_longterm_locked+0x1032/0x15c0
> ...
> blkdev_direct_IO+0x121d/0x17b0
> aio_read+0x33a/0x4d0
>
> The program does an O_DIRECT read into a MAP_SHARED mapping of a btrfs
> file, while the same file is being truncated.
>
> A dirty folio of a btrfs data inode is expected to have folio::private,
> but a folio can lose it while it stays in the page cache and is pinned
> by an in-flight O_DIRECT read, e.g.:
>
> truncate_inode_partial_folio()
> folio_invalidate(folio, offset, length)
> btrfs_invalidate_folio()
> btrfs_release_folio() /* folio is clean */
> __btrfs_release_folio()
> clear_folio_extent_mapped()
> folio_split_or_unmap()
> folio_split() /* fails, the folio is pinned */
>
> or from any failed large folio split, as __folio_freeze_split_file()
> calls filemap_release_folio() before folio_ref_freeze().
>
> When the read completes, the folio is re-dirtied without notifying the
> fs:
>
> bio_release_pages()
> folio_mark_dirty()
> btrfs_data_dirty_folio()
> filemap_dirty_folio()
FWIW, this sounds like a variant of
https://lore.kernel.org/linux-mm/20261119-gup-dirty-what-v1-0-236d69f25c24@xxxxxxx/.
(which I will respin soon)
>
> If the folio is beyond EOF by then, btrfs_data_dirty_folio() does not
> touch any btrfs state, and the next writeback calls folio_invalidate()
> on a folio without private, triggering the above ASSERT() in the
? why is writeback calling folio_invalidate()?
> bs < folio size case. If the folio straddles EOF,
> btrfs_folio_set_fixup_dirty() dereferences the NULL folio private
> instead.
>
> Fix it by re-attaching the folio private in btrfs_data_dirty_folio()
> if it is missing. ->dirty_folio() may not block, so use GFP_NOWAIT.
NAK to this approach. I need to think about folio_invalidate() and its
interaction with GUP a little harder.
--
Pedro