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,
"Brahmajit Das" <bdas@suse.de>,
dsterba@suse.cz
Subject: Re: [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied
Date: Thu, 03 Dec 2026 04:11:58 +0000 [thread overview]
Message-ID: <sashiko-outbox-247147@kernel.org> (raw)
In-Reply-To: <20261202041128.4168140-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() on
GFP_NOWAIT failure
- [Medium] btrfs: unsynced subpage uptodate bitmap in
btrfs_attach_folio_state()
--
commit 0c19f450904f3d6a2c814ed2d4ee9f4abff7f475
Author: Brahmajit Das <listout@listout.xyz>
btrfs: re-attach folio private when a released folio gets dirtied
This commit re-attaches folio->private in btrfs_data_dirty_folio() using
GFP_NOWAIT when a released folio in the page cache is dirtied, with a
sleeping fallback in the beyond-EOF branch of extent_writepage().
> diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
> index b2739838c6462..9d54dbd015918 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;
> 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]
What happens during writeback when set_folio_extent_mapped_gfp() fails in
btrfs_data_dirty_folio() on a folio that is inside or straddling EOF
(range_end > page_start)?
filemap_dirty_folio() still marks the folio dirty with folio->private ==
NULL, and extent_writepage() only calls set_folio_extent_mapped() in the
strictly beyond-EOF branch. For a subpage or large folio within EOF,
extent_writepage() hits the !folio_test_private(folio) check, triggers
DEBUG_WARN(), sets ret = -EUCLEAN, and jumps to done, where
btrfs_folio_end_lock_bitmap() dereferences NULL bfs =
folio_get_private(folio):
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);
}
Even if another path attaches folio->private before writeback runs, skipping
btrfs_folio_set_fixup_dirty() leaves the fixup and dirty bitmaps empty for
the dirtied blocks.
> 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(folio)
inside btrfs_attach_folio_state() instead of only in
btrfs_data_dirty_folio()?
During an unaligned shrink truncate on a pinned folio, truncate_setsize()
detaches folio->private via truncate_inode_partial_folio(), and then
btrfs_truncate() -> btrfs_truncate_block() immediately re-attaches
folio->private while folio_test_uptodate(folio) is true:
fs/btrfs/inode.c:btrfs_truncate_block() {
...
if (!folio_test_uptodate(folio)) {
ret = btrfs_read_folio(NULL, folio);
...
}
ret = set_folio_extent_mapped(folio);
if (ret < 0)
goto out_unlock;
...
}
Because btrfs_truncate_block() (and similarly prepare_one_folio() ->
prepare_uptodate_folio()) skips reading when PG_uptodate is already set,
the newly attached btrfs_folio_state is left with an all-zero uptodate
bitmap, and a later btrfs_data_dirty_folio() sees folio_test_private(folio)
already set and skips btrfs_folio_set_uptodate() as well. If only part of
the folio is later marked uptodate, can_skip_one_ordered_range() can trip
ASSERT(btrfs_folio_test_uptodate(fs_info, folio, cur, range_len)).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202041128.4168140-1-listout@listout.xyz?part=1
next prev parent reply other threads:[~2026-12-03 4:12 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 4:11 [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied Brahmajit Das
2026-12-03 4:11 ` sashiko-bot [this message]
-- strict thread matches above, loose matches on Subject: below --
2026-12-02 4:18 Brahmajit Das
2026-12-03 4:19 ` 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-247147@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox