Re: [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied

From: Qu Wenruo

Date: Fri Dec 04 2026 - 16:05:30 EST




在 2026/12/4 20:35, Pedro Falcato 写道:
+CC willy, jack, hch

On Fri, Dec 04, 2026 at 07:13:08AM +1030, Qu Wenruo wrote:


在 2026/12/3 23:58, Pedro Falcato 写道:
[...]
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)

One assumption doesn't meet the current code.

- ->dirty_folio() relies on buffers/IFS/etc being attached to the
folios. It _cannot_ attach them itself, as dirty_folio() cannot
block.

Iomap is already attaching it:

iomap_dirty_folio()
ifs_alloc(inode, folio, 0);
gfp = GFP_NOFS | __GFP_NOFAIL;
ifs = kzalloc_flex(..., gfp);

Either it's already a bug, or we won't hit such folio without private for
the non-sleeping routes.

This is, strictly speaking, a bug. Per Documentation/filesystems/locking.rst:

locking rules:
All except dirty_folio and free_folio may block

I think currently, de facto, it's not very likely to be hit[1]. Mostly because,
for writenotify filesystems/VMAs, you'll always trip over ->page_mkwrite()
(marking the folio dirty), and cleaning will always write-protect it before
clearing the dirty flag off the folio.

[1] I _think_ there are ways to hit it, if you go down funky folio_cancel_dirty()
paths.




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()?

If a folio is beyond EOF, we have no need to write it, thus can go
invalidating it directly.

Why would you not rely on truncation itself to invalidate it?

The truncation did try to invalidate the folio, by even removing the folio private.

But the folio can not be removed from filemap because it's pinned by the direct IO.


Did you mean we just skip the write, and let future truncation to invalidate it again?

That also feels a little weird, yes the folio will eventually be truncated when evicting the inode, but still doesn't feel so correct.

And this also means, we need to manually copy the invalidating handling to release reserved space, just not try to detach the private.


I guess for now, following the not-so-correct iomap behavior is the most simple solution from the perspective of fs, but it still leave a pretty bad taste.



Or is there any better solution from the perspective of MM?

My understanding is that the truncation logic should correctly handle
the semantics you want.

As for the GUP problem, yes, this is yet another issue. It is known.
->dirty_folio() implementations cannot assume the memory allocation will
succeed. And there's no possible fallback to "failure to dirty a folio".

The real solution (in my series) is to simply not discard this at-will.
invalidate_folio() seems to be yet another variant of this issue.

For what it's worth, I observed a similar issue in mm/truncate code. I don't
know what the correct approach would be. Keep the private around for GUP pins?
Reject the truncate altogether?

(I don't think it's possible to do this last option; keeping the private is
also weird, and I don't see how you can possibly ditch it after the folio is
orphaned)

I had a not-so-good idea, that will require invalidate_folio() callback to be split into two parts:

- invalidate_folio_fs()
The usual fs specific part.

- invalidate_folio_detach()
The private detaching part.

The second part is only called after the folio is properly removed from the filemap.

So this ensures all folios in the filemap still have their private attached.

But again, I'm not an expert in MM. So this can just be a stupid idea and can conflict with other things like large folio splitting.

Thanks,
Qu