Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Brahmajit Das" <listout@listout.xyz>, "David Sterba" <dsterba@suse.com>
Cc: "Qu Wenruo" <quwenruo.btrfs@gmx.com>,
	"Chris Mason" <mason@kernel.org>,
	"David Hildenbrand" <david@kernel.org>,
	"Jason Gunthorpe" <jgg@nvidia.com>,
	"John Hubbard" <jhubbard@nvidia.com>,
	"Peter Xu" <peterx@redhat.com>,
	linux-mm@kvack.org, syzkaller-bugs@googlegroups.com,
	syzbot+d6f123d4060990f0f3ff@syzkaller.appspotmail.com,
	"Brahmajit Das" <bdas@suse.de>,
	dsterba@suse.cz
Subject: Re: [PATCH v3 3/3] btrfs: re-attach folio private when a released folio gets dirtied
Date: Fri, 04 Dec 2026 13:31:43 +0000	[thread overview]
Message-ID: <sashiko-outbox-249872@kernel.org> (raw)
In-Reply-To: <20261203133059.2193197-4-listout@listout.xyz>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] btrfs: GFP_NOWAIT failure in btrfs_data_dirty_folio() loses
  data or wedges WB
- [High] btrfs: unlocked folio private attach in
  btrfs_data_dirty_folio()
- [Medium] btrfs: -ENOMEM in beyond-EOF extent_writepage() aborts
  batched in-EOF writes
--

commit e78785cc7760f906528c4b79c739cf40ed3170e4
Author: Brahmajit Das <bdas@suse.de>

btrfs: re-attach folio private when a released folio gets dirtied

This commit re-attaches folio::private with GFP_NOWAIT in
btrfs_data_dirty_folio() when a dirtying path encounters a folio whose
private was previously released.  If that allocation fails, it skips
marking the folio for COW fixup and attaches folio::private in the
beyond-EOF branch of extent_writepage() before calling folio_invalidate().

> diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
> index 9dded5c670de1..e2a6a4bf7f4b8 100644
> --- a/fs/btrfs/extent_io.c
> +++ b/fs/btrfs/extent_io.c

[ ... ]

> @@ -2046,6 +2053,17 @@ static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl
>  	pg_offset = offset_in_folio(folio, i_size);
>  	if (folio->index > end_index ||
>  	   (folio->index == end_index && !pg_offset)) {
> +		/*
> +		 * btrfs_invalidate_folio() requires folio::private for the
> +		 * bs < folio size case.  A folio can still get here without
> +		 * it, if btrfs_data_dirty_folio() failed its GFP_NOWAIT
> +		 * allocation.  We can sleep here, so attach it now.
> +		 */
> +		ret = set_folio_extent_mapped(folio);
> +		if (unlikely(ret < 0)) {
> +			folio_unlock(folio);
> +			return ret;
> +		}
>  		folio_invalidate(folio, 0, folio_size(folio));
>  		folio_unlock(folio);
>  		return 0;

[Severity: Medium]
Can an -ENOMEM here discard valid in-EOF writes that are already queued in
bio_ctrl->bbio?

extent_write_cache_pages() walks dirty folios in ascending order, so earlier
in-EOF folios can already have PG_dirty cleared, PG_writeback set, and their
sectors queued in bio_ctrl->bbio.  Returning -ENOMEM here skips submitting
that bio.

extent_write_cache_pages() then breaks out and btrfs_writepages() passes
-ENOMEM to submit_write_bio(), which calls btrfs_bio_end_io() with an error
and fails the ordered extents for those valid in-EOF writes.

[ ... ]

> diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
> index 95c9530554d02..a8fdd1b834f21 100644
> --- a/fs/btrfs/inode.c
> +++ b/fs/btrfs/inode.c
> @@ -10739,7 +10739,21 @@ static bool btrfs_data_dirty_folio(struct address_space *mapping,
>  				    round_up(i_size_read(&inode->vfs_inode),
>  					     fs_info->sectorsize));
>  
> -	if (range_end > page_start)
> +	/*
> +	 * The folio may have lost its private while it stayed in the page
> +	 * cache, e.g. btrfs_release_folio() from a partial invalidation or
> +	 * from a failed large folio split, while it was pinned by GUP.  When
> +	 * the pin is released, the folio is dirtied through here, so
> +	 * re-attach the private, as every dirty folio is expected to have it.
> +	 *
> +	 * We may not block here, thus GFP_NOWAIT.  If that fails, the blocks
> +	 * can't be marked for fixup.  Writeback attaches the private later
> +	 * for folios beyond EOF, and rejects folios inside EOF with -EUCLEAN.
> +	 */
> +	if (unlikely(!folio_test_private(folio)))
> +		set_folio_extent_mapped_gfp(folio, GFP_NOWAIT | __GFP_NOWARN);
> +
> +	if (range_end > page_start && folio_test_private(folio))
>  		btrfs_folio_set_fixup_dirty(fs_info, folio, page_start,
>  					    range_end - page_start);
>  	return filemap_dirty_folio(mapping, folio);

[Severity: High]
What happens if set_folio_extent_mapped_gfp() fails under memory pressure on
a subpage or large folio inside or straddling EOF?

btrfs_data_dirty_folio() ignores -ENOMEM, skips
btrfs_folio_set_fixup_dirty(), and still calls filemap_dirty_folio(),
leaving the folio dirty with folio->private NULL.

If a partial buffered write (prepare_one_folio()) runs before writeback,
set_folio_extent_mapped() attaches a fresh btrfs_folio_state with zeroed
dirty and fixup bitmaps.  Writeback then writes only the newly dirtied
blocks and silently drops the GUP-dirtied blocks, with fsync() returning 0.

If writeback runs first, extent_writepage() hits the !folio_test_private()
-EUCLEAN path and unlocks the folio without clearing PG_dirty or attaching
private.  Every later writeback pass repeats DEBUG_WARN() and -EUCLEAN,
aborting batched bios and blocking higher-offset dirty folios in the inode.

[Severity: High]
Is folio_lock() guaranteed to be held when btrfs_data_dirty_folio() calls
set_folio_extent_mapped_gfp()?

a_ops->dirty_folio() does not require the folio lock; for example,
netfs_rreq_assess_dio() calls set_page_dirty() on GUP-pinned pages without
holding folio_lock().  On subpage or large folios, btrfs_attach_folio_state()
then hits ASSERT(folio_test_locked(folio)).

Even without CONFIG_BTRFS_ASSERT, attaching folio->private before
filemap_dirty_folio() sets PG_dirty races with folio_unmap_invalidate():

CPU0                                  CPU1
netfs_rreq_assess_dio()
  set_page_dirty()
    btrfs_data_dirty_folio()
                                      folio_unmap_invalidate()
                                        filemap_release_folio()
                                        spin_lock(&mapping->host->i_lock)
                                        xa_lock_irq(&mapping->i_pages)
                                        folio_test_dirty() /* false */
      set_folio_extent_mapped_gfp()
        folio_attach_private()
                                        BUG_ON(folio_has_private(folio))
                                          <-- kernel panic

A concurrent btrfs_release_folio() can similarly free the newly attached
btrfs_folio_state before btrfs_subpage_set_fixup_dirty() locks it, causing a
use-after-free.

Concurrent unlocked folio_attach_private() calls also leak a folio refcount
and btrfs_folio_state, and concurrent truncation clearing folio->mapping can
cause a NULL pointer dereference in set_folio_extent_mapped_gfp().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203133059.2193197-1-listout@listout.xyz?part=3


  parent reply	other threads:[~2026-12-04 13:31 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <6ab2d179.e8212013.2d060.0001.GAE@google.com>
2026-12-03 13:30 ` [PATCH v3 0/3] btrfs: fix dirtying of folios that lost their private Brahmajit Das
2026-12-03 13:30   ` [PATCH v3 1/3] btrfs: don't dereference a missing folio private on writeback error Brahmajit Das
2026-12-03 13:30   ` [PATCH v3 2/3] btrfs: sync the uptodate bitmap when attaching a data folio state Brahmajit Das
2026-12-03 20:36     ` Qu Wenruo
2026-12-04 13:31     ` sashiko-bot
2026-12-03 13:30   ` [PATCH v3 3/3] btrfs: re-attach folio private when a released folio gets dirtied Brahmajit Das
2026-12-03 20:36     ` Qu Wenruo
2026-12-04  6:36       ` Brahmajit Das
2026-12-04 13:31     ` sashiko-bot [this message]
2026-12-04  6:32   ` [PATCH v4 0/2] btrfs: fix dirtying of folios that lost their private listout
2026-12-04  6:32     ` [PATCH v4 1/2] btrfs: don't dereference a missing folio private on writeback error listout
2026-12-04  7:54       ` Qu Wenruo
2026-12-04  6:32     ` [PATCH v4 2/2] btrfs: re-attach folio private when a released folio gets dirtied listout
2026-12-04  7:52       ` Qu Wenruo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-249872@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bdas@suse.de \
    --cc=david@kernel.org \
    --cc=dsterba@suse.com \
    --cc=dsterba@suse.cz \
    --cc=jgg@nvidia.com \
    --cc=jhubbard@nvidia.com \
    --cc=linux-mm@kvack.org \
    --cc=listout@listout.xyz \
    --cc=mason@kernel.org \
    --cc=peterx@redhat.com \
    --cc=quwenruo.btrfs@gmx.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=syzbot+d6f123d4060990f0f3ff@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox