mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Qu Wenruo <quwenruo.btrfs@gmx.com>
To: Brahmajit Das <listout@listout.xyz>,
	dsterba@suse.com, linux-btrfs@vger.kernel.org
Cc: clm@fb.com, mason@kernel.org, linux-kernel@vger.kernel.org,
	syzkaller-bugs@googlegroups.com,
	syzbot+d6f123d4060990f0f3ff@syzkaller.appspotmail.com,
	Brahmajit Das <bdas@suse.de>
Subject: Re: [PATCH] btrfs: don't invalidate beyond-EOF folios without private in extent_writepage()
Date: Tue, 1 Dec 2026 07:17:25 +1030	[thread overview]
Message-ID: <38aa11f8-b9c2-4f3e-a7fb-ac385cee42ce@gmx.com> (raw)
In-Reply-To: <20261130182637.468143-1-listout@listout.xyz>



在 2026/12/1 04:56, Brahmajit Das 写道:
> 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 (creat() with O_TRUNC).
> 
> extent_writepage() unconditionally calls folio_invalidate() on a dirty
> folio that is fully beyond EOF.  However such a folio can be dirty
> without folio::private: a pinned folio can lose its private once it is
> clean,

Mind to explain this a little more? On exact which path the folio's 
private is cleared?

Better with a calltrace showing the situation.

> and then be re-dirtied through btrfs_data_dirty_folio(), which
> intentionally does not touch any btrfs state for blocks beyond EOF.

I'm not a huge fan of breaking the assumption that all btrfs filemap 
folios should have its private properly set.

Can we fix the bug by ensuring even filemap folios beyond EOF will have 
proper private set?

> 
> For the bs < folio size case (which includes large folios),
> btrfs_invalidate_folio() requires folio::private, both in
> wait_subpage_spinlock() and in the subpage bitmap helpers, so we hit the
> above ASSERT() (or a NULL pointer dereference without
> CONFIG_BTRFS_ASSERT).
> 
> The MM layer never calls ->invalidate_folio() for such folios, as
> truncate_cleanup_folio() only does so if folio_needs_release().  A folio
> without private has no btrfs per-folio state to tear down, and since
> try_release_extent_mapping() must have succeeded to detach it, there is
> no delalloc range for it either.
> 
> So follow the same rule in extent_writepage(): only invalidate the
> beyond-EOF folio if it has private, otherwise just clear its dirty flag
> and dirty tag.
> 
> 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 and console log.
> 
> Reported-by: syzbot+d6f123d4060990f0f3ff@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=d6f123d4060990f0f3ff
> Fixes: cc38d178ff33 ("btrfs: enable large data folio support under CONFIG_BTRFS_EXPERIMENTAL")
> Signed-off-by: Brahmajit Das <bdas@suse.de>
> ---
>   fs/btrfs/extent_io.c | 19 ++++++++++++++++++-
>   1 file changed, 18 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
> index b2739838c646..166034ec0c3b 100644
> --- a/fs/btrfs/extent_io.c
> +++ b/fs/btrfs/extent_io.c
> @@ -2046,7 +2046,24 @@ 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)) {
> -		folio_invalidate(folio, 0, folio_size(folio));
> +		/*
> +		 * A folio beyond EOF can be dirtied without folio::private,
> +		 * e.g. a pinned folio that lost its private and was then
> +		 * re-dirtied by btrfs_data_dirty_folio(), which leaves blocks
> +		 * beyond EOF alone.
> +		 *
> +		 * Such a folio has no btrfs state to tear down, and
> +		 * btrfs_invalidate_folio() requires folio::private for the
> +		 * bs < folio size case.  So just like truncate_cleanup_folio()
> +		 * only invalidate the folio if it has private, otherwise just
> +		 * clear the dirty flag and tag.
> +		 */
> +		if (folio_test_private(folio)) {
> +			folio_invalidate(folio, 0, folio_size(folio));
> +		} else {
> +			folio_clear_dirty_for_io(folio);
> +			btrfs_clear_folio_dirty_tag(folio);
> +		}
>   		folio_unlock(folio);
>   		return 0;
>   	}


      reply	other threads:[~2026-11-30 20:48 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-22 19:05 [syzbot] [btrfs?] kernel BUG in wait_subpage_spinlock syzbot
2026-11-30 18:26 ` [PATCH] btrfs: don't invalidate beyond-EOF folios without private in extent_writepage() Brahmajit Das
2026-11-30 20:47   ` Qu Wenruo [this message]

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=38aa11f8-b9c2-4f3e-a7fb-ac385cee42ce@gmx.com \
    --to=quwenruo.btrfs@gmx.com \
    --cc=bdas@suse.de \
    --cc=clm@fb.com \
    --cc=dsterba@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=listout@listout.xyz \
    --cc=mason@kernel.org \
    --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

all inboxes | Powered by JetHome®