All of 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, "Brahmajit Das" <bdas@suse.de>,
	syzbot+d6f123d4060990f0f3ff@syzkaller.appspotmail.com,
	dsterba@suse.cz
Subject: Re: [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied
Date: Thu, 03 Dec 2026 04:19:20 +0000	[thread overview]
Message-ID: <sashiko-outbox-247153@kernel.org> (raw)
In-Reply-To: <20261202041853.4184717-1-listout@listout.xyz>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] btrfs: NULL pointer dereference in extent_writepage() when
  GFP_NOWAIT fails
- [Medium] btrfs: subpage uptodate bitmap not synced outside
  btrfs_data_dirty_folio()
--

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

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

This commit re-attaches folio::private using GFP_NOWAIT in
btrfs_data_dirty_folio() and in the beyond-EOF branch of extent_writepage()
when a cached folio that lost its private state is dirtied.

> 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 GFP_NOWAIT failure here on a subpage or large folio inside or
straddling EOF cause a NULL pointer dereference in extent_writepage()?

set_folio_extent_mapped_gfp() only allocates when btrfs_is_subpage() is
true.  If that allocation fails, filemap_dirty_folio() still marks the
folio dirty with folio->private == NULL.  When extent_writepage() runs on a
folio that is not completely beyond EOF, it hits the !folio_test_private()
check, sets ret = -EUCLEAN, and jumps to done:, which passes the folio to
btrfs_folio_end_lock_bitmap():

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) {
...
}

Because btrfs_is_subpage() is true and bfs is NULL, atomic_read()
dereferences NULL.  Also, if another path calls set_folio_extent_mapped()
before writeback runs, a zeroed btrfs_folio_state is attached without the
fixup or dirty bits that btrfs_folio_set_fixup_dirty() skipped.

> diff --git a/fs/btrfs/subpage.c b/fs/btrfs/subpage.c
> index ebf18efe1ea31..c779bf4771b98 100644
> --- a/fs/btrfs/subpage.c
> +++ b/fs/btrfs/subpage.c
> @@ -36,7 +36,8 @@
>   */
>  
>  int btrfs_attach_folio_state(const struct btrfs_fs_info *fs_info,
> -			     struct folio *folio, enum btrfs_folio_type type)
> +			     struct folio *folio, enum btrfs_folio_type type,
> +			     gfp_t gfp)
>  {
>  	struct btrfs_folio_state *bfs;
>  
> @@ -59,7 +60,7 @@ int btrfs_attach_folio_state(const struct btrfs_fs_info *fs_info,
>  	if (type == BTRFS_SUBPAGE_DATA && !btrfs_is_subpage(fs_info, folio))
>  		return 0;
>  
> -	bfs = btrfs_alloc_folio_state(fs_info, folio_size(folio), type, GFP_NOFS);
> +	bfs = btrfs_alloc_folio_state(fs_info, folio_size(folio), type, gfp);
>  	if (IS_ERR(bfs))
>  		return PTR_ERR(bfs);

[Severity: Medium]
Should the subpage uptodate bitmap be synced with folio_test_uptodate()
here in btrfs_attach_folio_state() (when type == BTRFS_SUBPAGE_DATA) rather
than only in btrfs_data_dirty_folio()?

If an uptodate folio loses its private via btrfs_release_folio() and
another caller such as btrfs_truncate_block(), prepare_one_folio(), or
btrfs_page_mkwrite() calls set_folio_extent_mapped() first, a zeroed
btrfs_folio_state is attached while PG_uptodate is still set on the folio.
Those paths skip reading the already-uptodate folio and only update their
own sub-range (or unlock early if an ordered extent is pending in
btrfs_page_mkwrite()), leaving unmodified blocks marked not-uptodate in the
subpage bitmap and preventing a later btrfs_data_dirty_folio() from syncing
the bitmap because folio_test_private() is already true.

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


  reply	other threads:[~2026-12-03  4:19 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  4:18 [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied Brahmajit Das
2026-12-03  4:19 ` sashiko-bot [this message]
2026-12-03 13:28 ` Pedro Falcato
2026-12-03 20:43   ` Qu Wenruo
2026-12-04 10:05     ` Pedro Falcato
2026-12-04 12:48       ` Brahmajit Das
2026-12-04 21:05       ` Qu Wenruo
  -- strict thread matches above, loose matches on Subject: below --
2026-12-02  4:11 Brahmajit Das
2026-12-03  4:11 ` sashiko-bot
2026-12-02  4:08 Brahmajit Das
2026-12-02  5:51 ` sashiko-bot
2026-12-02  7:17   ` 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-247153@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bdas@suse.de \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.