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

From: Pedro Falcato

Date: Fri Dec 04 2026 - 05:06:06 EST


+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?

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

--
Pedro