Re: [PATCH] btrfs: don't invalidate beyond-EOF folios without private in extent_writepage()

From: listout

Date: Tue Dec 01 2026 - 08:14:32 EST


On 2026/12/1 Qu Wenruo wrote:
> Mind to explain this a little more? On exact which path the folio's
> private is cleared?
>
> Better with a calltrace showing the situation.

Unfortunately the console log doesn't show where private was dropped, and
syzbot has no reproducer, so I can't give you a trace from the actual
crash. From code reading, a pinned folio can lose its private but stay in
the page cache in two ways:

1) Partial invalidation in btrfs itself. For a range smaller than the
whole folio, btrfs_invalidate_folio() calls btrfs_release_folio(),
which only checks dirty/writeback:

truncate_pagecache()
truncate_inode_pages_range()
truncate_inode_partial_folio()
folio_invalidate(folio, offset, length)
btrfs_invalidate_folio()
btrfs_release_folio()
__btrfs_release_folio()
clear_folio_extent_mapped()
folio_split_or_unmap()
folio_split() <- fails, folio pinned by an in-flight O_DIRECT read

2) Any failed large folio split. __folio_freeze_split_file() calls
filemap_release_folio() before folio_ref_freeze(), with no pin check,
so a split failing on a pinned folio leaves it without private
(reachable from truncate and from migration).

When the O_DIRECT read completes, bio_release_pages() -> folio_mark_dirty()
-> btrfs_data_dirty_folio() re-dirties the folio. If it's beyond EOF by
then, nothing is attached and we get the reported crash. If it straddles
EOF, btrfs_subpage_set_fixup_dirty() would dereference the NULL private
instead.

> Can we fix the bug by ensuring even filemap folios beyond EOF will have
> proper private set?

Agreed, that's better. For v2 I'm thinking of making btrfs_release_folio()
refuse to release while folio_maybe_dma_pinned(), so any folio that can
still be re-dirtied without btrfs knowing keeps its private. That covers
both paths above.

The remaining case is the full invalidation extent_writepage() does on
beyond-EOF folios, which clears private unconditionally while the folio
may stay in the page cache until truncate removes it. Would you prefer
to skip that invalidation for pinned folios, or to re-attach private
there before invalidating?

Thanks,
Brahmajit