Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Brahmajit Das" <listout@listout.xyz>, dsterba@suse.com
Cc: quwenruo.btrfs@gmx.com, clm@fb.com, mason@kernel.org,
	david@kernel.org, jgg@ziepe.ca, jhubbard@nvidia.com,
	peterx@redhat.com, linux-mm@kvack.org,
	syzkaller-bugs@googlegroups.com,
	syzbot+d6f123d4060990f0f3ff@syzkaller.appspotmail.com,
	dsterba@suse.cz
Subject: Re: [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied
Date: Wed, 02 Dec 2026 05:51:29 +0000	[thread overview]
Message-ID: <sashiko-outbox-244930@kernel.org> (raw)
In-Reply-To: <20261202040816.4163572-1-listout@listout.xyz>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] btrfs: missing Signed-off-by trailer in commit message
- [High] btrfs: NULL deref in btrfs_folio_end_lock_bitmap() after
  GFP_NOWAIT failure
--

commit 95b6c92f6e43e75b16963da0d7b8d1fc848e76eb
Author: Brahmajit Das <listout@listout.xyz>

> btrfs: re-attach folio private when a released folio gets dirtied
>
> 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()
>
> 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
> 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.
> The new folio state has empty bitmaps while the folio, which was only
> released and not invalidated, may still be uptodate, so sync the
> uptodate bitmap with the folio flag.
>
> If the allocation fails, skip marking the blocks for fixup rather than
> dereferencing a NULL private, and attach the private in the beyond-EOF
> branch of extent_writepage(), which can sleep, before calling
> folio_invalidate().
>
> Note: syzbot has no reproducer for this issue.  The fix was only build
> tested, the race has not been reproduced locally.
>
> This patch was written with the help of an AI coding assistant (Claude),
> from the syzbot report, console log and review feedback.
>
> Reported-by: syzbot+d6f123d4060990f0f3ff@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=d6f123d4060990f0f3ff
> Fixes: 0680cbbf39ca ("btrfs: trigger cow fixup via dirty_folio()")

[Severity: High]
The commit message is missing a Signed-off-by: trailer.

> diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
> index 95c9530554d02..f30f334a099e9 100644
> --- a/fs/btrfs/inode.c
> +++ b/fs/btrfs/inode.c
> @@ -10739,7 +10739,27 @@ 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, writeback
> +	 * attaches it later for folios beyond EOF.
> +	 *
> +	 * The new folio state has empty bitmaps.  The folio has not been
> +	 * invalidated, only released, thus can still be uptodate, so sync
> +	 * the uptodate bitmap with the folio flag.
> +	 */
> +	if (unlikely(!folio_test_private(folio)) &&
> +	    set_folio_extent_mapped_gfp(folio, GFP_NOWAIT | __GFP_NOWARN) == 0 &&
> +	    folio_test_uptodate(folio))
> +		btrfs_folio_set_uptodate(fs_info, folio, page_start,
> +					 folio_size(folio));
> +
> +	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]
Can a failed GFP_NOWAIT allocation here cause a NULL pointer dereference
in btrfs_folio_end_lock_bitmap() during writeback?

set_folio_extent_mapped_gfp() only allocates when btrfs_is_subpage() is
true.  If that allocation fails on a folio inside or straddling EOF,
btrfs_data_dirty_folio() skips btrfs_folio_set_fixup_dirty() and marks
the folio dirty with folio->private still NULL.  On writeback,
extent_writepage() hits the !folio_test_private(folio) check, logs a
false "marked dirty without notifying the fs" error with -EUCLEAN, and
jumps to done:

fs/btrfs/extent_io.c:extent_writepage() {
    ...
	if (unlikely(!folio_test_private(folio))) {
		DEBUG_WARN();
		...
		ret = -EUCLEAN;
		goto done;
	}
    ...
done:
	if (ret < 0)
		mapping_set_error(folio->mapping, ret);
	btrfs_folio_end_lock_bitmap(fs_info, folio, bio_ctrl->submit_bitmap);
}

In btrfs_folio_end_lock_bitmap(), btrfs_is_subpage(fs_info, folio) is
true while bfs = folio_get_private(folio) is NULL, so
atomic_read(&bfs->nr_locked) dereferences NULL:

fs/btrfs/subpage.c:btrfs_folio_end_lock_bitmap() {
	struct btrfs_folio_state *bfs = folio_get_private(folio);
	...
	if (!btrfs_is_subpage(fs_info, folio)) {
		folio_unlock(folio);
		return;
	}

	if (atomic_read(&bfs->nr_locked) == 0) {
}

Also, if another partial buffered write or btrfs_truncate_block()
attaches a fresh btrfs_folio_state before writeback runs, the blocks
dirtied when the GFP_NOWAIT allocation failed were never marked for fixup
or dirty in the subpage bitmap and will be silently lost.

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


  reply	other threads:[~2026-12-02  5:51 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  4:08 [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied Brahmajit Das
2026-12-02  5:51 ` sashiko-bot [this message]
  -- strict thread matches above, loose matches on Subject: below --
2026-12-02  4:11 Brahmajit Das
2026-12-02  4:18 Brahmajit Das

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-244930@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=clm@fb.com \
    --cc=david@kernel.org \
    --cc=dsterba@suse.com \
    --cc=dsterba@suse.cz \
    --cc=jgg@ziepe.ca \
    --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