* [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied
@ 2026-12-02 4:18 Brahmajit Das
2026-12-03 4:19 ` sashiko-bot
2026-12-03 13:28 ` Pedro Falcato
0 siblings, 2 replies; 12+ messages in thread
From: Brahmajit Das @ 2026-12-02 4:18 UTC (permalink / raw)
To: dsterba, linux-btrfs
Cc: quwenruo.btrfs, clm, mason, david, jgg, jhubbard, peterx,
linux-mm, linux-kernel, syzkaller-bugs, Brahmajit Das,
syzbot+d6f123d4060990f0f3ff
From: Brahmajit Das <bdas@suse.de>
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()")
Signed-off-by: Brahmajit Das <bdas@suse.de>
---
Notes:
v2:
- Re-attach the folio private in btrfs_data_dirty_folio() instead of
skipping folio_invalidate() for folios without private (Qu)
- Attach private only inside the beyond-EOF branch of extent_writepage(),
leaving the unexpected dirty folio check alone (Qu)
- Sync the uptodate bitmap of the new folio state with the folio flag
- Explain where the folio private is dropped in the changelog
- Cc GUP maintainers for the GUP part of the reasoning (Qu)
- v1: https://lore.kernel.org/linux-btrfs/20261130182637.468143-1-listout@listout.xyz/
Open: if the GFP_NOWAIT allocation fails for a folio inside EOF, the
blocks are not marked for fixup and writeback hits the existing "marked
dirty without notifying the fs" error path.
---
fs/btrfs/extent_io.c | 24 +++++++++++++++++++++---
fs/btrfs/extent_io.h | 1 +
fs/btrfs/inode.c | 22 +++++++++++++++++++++-
fs/btrfs/subpage.c | 5 +++--
fs/btrfs/subpage.h | 3 ++-
5 files changed, 48 insertions(+), 7 deletions(-)
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index b2739838c646..9d54dbd01591 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -923,11 +923,12 @@ static int attach_extent_buffer_folio(struct extent_buffer *eb,
folio_attach_private(folio, prealloc);
else
/* Do new allocation to attach subpage */
- ret = btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_METADATA);
+ ret = btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_METADATA,
+ GFP_NOFS);
return ret;
}
-int set_folio_extent_mapped(struct folio *folio)
+int set_folio_extent_mapped_gfp(struct folio *folio, gfp_t gfp)
{
struct btrfs_fs_info *fs_info;
@@ -939,12 +940,18 @@ int set_folio_extent_mapped(struct folio *folio)
fs_info = folio_to_fs_info(folio);
if (btrfs_is_subpage(fs_info, folio))
- return btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_DATA);
+ return btrfs_attach_folio_state(fs_info, folio,
+ BTRFS_SUBPAGE_DATA, gfp);
folio_attach_private(folio, (void *)EXTENT_FOLIO_PRIVATE);
return 0;
}
+int set_folio_extent_mapped(struct folio *folio)
+{
+ return set_folio_extent_mapped_gfp(folio, GFP_NOFS);
+}
+
void clear_folio_extent_mapped(struct folio *folio)
{
struct btrfs_fs_info *fs_info;
@@ -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/extent_io.h b/fs/btrfs/extent_io.h
index c55b37cb24b7..1da7606efaad 100644
--- a/fs/btrfs/extent_io.h
+++ b/fs/btrfs/extent_io.h
@@ -285,6 +285,7 @@ int btrfs_writepages(struct address_space *mapping, struct writeback_control *wb
int btree_writepages(struct address_space *mapping, struct writeback_control *wbc);
void btrfs_btree_wait_writeback_range(struct btrfs_fs_info *fs_info, u64 start, u64 end);
void btrfs_readahead(struct readahead_control *rac);
+int set_folio_extent_mapped_gfp(struct folio *folio, gfp_t gfp);
int set_folio_extent_mapped(struct folio *folio);
void clear_folio_extent_mapped(struct folio *folio);
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 95c9530554d0..f30f334a099e 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);
diff --git a/fs/btrfs/subpage.c b/fs/btrfs/subpage.c
index ebf18efe1ea3..c779bf4771b9 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);
diff --git a/fs/btrfs/subpage.h b/fs/btrfs/subpage.h
index 9b106a73d682..83e758af4e4e 100644
--- a/fs/btrfs/subpage.h
+++ b/fs/btrfs/subpage.h
@@ -104,7 +104,8 @@ static inline bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info,
}
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);
void btrfs_detach_folio_state(const struct btrfs_fs_info *fs_info, struct folio *folio,
enum btrfs_folio_type type);
--
2.56.1
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied
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
2026-12-03 13:28 ` Pedro Falcato
1 sibling, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-12-03 4:19 UTC (permalink / raw)
To: Brahmajit Das, dsterba
Cc: quwenruo.btrfs, clm, mason, david, jgg, jhubbard, peterx,
linux-mm, syzkaller-bugs, Brahmajit Das,
syzbot+d6f123d4060990f0f3ff, dsterba
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
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied
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
@ 2026-12-03 13:28 ` Pedro Falcato
2026-12-03 20:43 ` Qu Wenruo
1 sibling, 1 reply; 12+ messages in thread
From: Pedro Falcato @ 2026-12-03 13:28 UTC (permalink / raw)
To: Brahmajit Das
Cc: dsterba, linux-btrfs, quwenruo.btrfs, clm, mason, david, jgg,
jhubbard, peterx, linux-mm, linux-kernel, syzkaller-bugs,
Brahmajit Das, syzbot+d6f123d4060990f0f3ff
On Wed, Dec 02, 2026 at 09:48:53AM +0530, Brahmajit Das wrote:
> From: Brahmajit Das <bdas@suse.de>
>
> 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()
FWIW, this sounds like a variant of
https://lore.kernel.org/linux-mm/20261119-gup-dirty-what-v1-0-236d69f25c24@suse.de/.
(which I will respin soon)
>
> 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
? why is writeback calling folio_invalidate()?
> 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.
NAK to this approach. I need to think about folio_invalidate() and its
interaction with GUP a little harder.
--
Pedro
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied
2026-12-03 13:28 ` Pedro Falcato
@ 2026-12-03 20:43 ` Qu Wenruo
2026-12-04 10:05 ` Pedro Falcato
0 siblings, 1 reply; 12+ messages in thread
From: Qu Wenruo @ 2026-12-03 20:43 UTC (permalink / raw)
To: Pedro Falcato, Brahmajit Das
Cc: dsterba, linux-btrfs, clm, mason, david, jgg, jhubbard, peterx,
linux-mm, linux-kernel, syzkaller-bugs, Brahmajit Das,
syzbot+d6f123d4060990f0f3ff
在 2026/12/3 23:58, Pedro Falcato 写道:
[...]
>> 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()
>
> FWIW, this sounds like a variant of
> https://lore.kernel.org/linux-mm/20261119-gup-dirty-what-v1-0-236d69f25c24@suse.de/.
> (which I will respin soon)
One assumption doesn't meet the current code.
> - ->dirty_folio() relies on buffers/IFS/etc being attached to the
> folios. It _cannot_ attach them itself, as dirty_folio() cannot
> block.
Iomap is already attaching it:
iomap_dirty_folio()
ifs_alloc(inode, folio, 0);
gfp = GFP_NOFS | __GFP_NOFAIL;
ifs = kzalloc_flex(..., gfp);
Either it's already a bug, or we won't hit such folio without private
for the non-sleeping routes.
>
>>
>> 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
>
> ? why is writeback calling folio_invalidate()?
If a folio is beyond EOF, we have no need to write it, thus can go
invalidating it directly.
Or is there any better solution from the perspective of MM?
Thanks,
Qu
>
>> 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.
>
> NAK to this approach. I need to think about folio_invalidate() and its
> interaction with GUP a little harder.
>
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied
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
0 siblings, 2 replies; 12+ messages in thread
From: Pedro Falcato @ 2026-12-04 10:05 UTC (permalink / raw)
To: Qu Wenruo
Cc: Brahmajit Das, dsterba, linux-btrfs, clm, mason, david, jgg,
jhubbard, peterx, linux-mm, linux-kernel, syzkaller-bugs,
Brahmajit Das, syzbot+d6f123d4060990f0f3ff, willy, jack, hch
+CC willy, jack, hch
On Fri, Dec 04, 2026 at 07:13:08AM +1030, Qu Wenruo wrote:
>
>
> 在 2026/12/3 23:58, Pedro Falcato 写道:
> [...]
> > > 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()
> >
> > FWIW, this sounds like a variant of
> > https://lore.kernel.org/linux-mm/20261119-gup-dirty-what-v1-0-236d69f25c24@suse.de/.
> > (which I will respin soon)
>
> One assumption doesn't meet the current code.
>
> > - ->dirty_folio() relies on buffers/IFS/etc being attached to the
> > folios. It _cannot_ attach them itself, as dirty_folio() cannot
> > block.
>
> Iomap is already attaching it:
>
> iomap_dirty_folio()
> ifs_alloc(inode, folio, 0);
> gfp = GFP_NOFS | __GFP_NOFAIL;
> ifs = kzalloc_flex(..., gfp);
>
> Either it's already a bug, or we won't hit such folio without private for
> the non-sleeping routes.
This is, strictly speaking, a bug. Per Documentation/filesystems/locking.rst:
locking rules:
All except dirty_folio and free_folio may block
I think currently, de facto, it's not very likely to be hit[1]. Mostly because,
for writenotify filesystems/VMAs, you'll always trip over ->page_mkwrite()
(marking the folio dirty), and cleaning will always write-protect it before
clearing the dirty flag off the folio.
[1] I _think_ there are ways to hit it, if you go down funky folio_cancel_dirty()
paths.
>
> >
> > >
> > > 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
> >
> > ? why is writeback calling folio_invalidate()?
>
> If a folio is beyond EOF, we have no need to write it, thus can go
> invalidating it directly.
Why would you not rely on truncation itself to invalidate it?
>
> Or is there any better solution from the perspective of MM?
My understanding is that the truncation logic should correctly handle
the semantics you want.
As for the GUP problem, yes, this is yet another issue. It is known.
->dirty_folio() implementations cannot assume the memory allocation will
succeed. And there's no possible fallback to "failure to dirty a folio".
The real solution (in my series) is to simply not discard this at-will.
invalidate_folio() seems to be yet another variant of this issue.
For what it's worth, I observed a similar issue in mm/truncate code. I don't
know what the correct approach would be. Keep the private around for GUP pins?
Reject the truncate altogether?
(I don't think it's possible to do this last option; keeping the private is
also weird, and I don't see how you can possibly ditch it after the folio is
orphaned)
--
Pedro
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied
2026-12-04 10:05 ` Pedro Falcato
@ 2026-12-04 12:48 ` Brahmajit Das
2026-12-04 21:05 ` Qu Wenruo
1 sibling, 0 replies; 12+ messages in thread
From: Brahmajit Das @ 2026-12-04 12:48 UTC (permalink / raw)
To: Pedro Falcato
Cc: Qu Wenruo, dsterba, linux-btrfs, clm, mason, david, jgg, jhubbard,
peterx, linux-mm, linux-kernel, syzkaller-bugs,
syzbot+d6f123d4060990f0f3ff, willy, jack, hch
On Fri, Dec 04, 2026 at 10:05:49 +0000, Pedro Falcato wrote:
> +CC willy, jack, hch
>
> On Fri, Dec 04, 2026 at 07:13:08AM +1030, Qu Wenruo wrote:
> >
> >
> > 在 2026/12/3 23:58, Pedro Falcato 写道:
> > [...]
> > > > 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()
> > >
> > > FWIW, this sounds like a variant of
> > > https://lore.kernel.org/linux-mm/20261119-gup-dirty-what-v1-0-236d69f25c24@suse.de/.
> > > (which I will respin soon)
> >
> > One assumption doesn't meet the current code.
> >
> > > - ->dirty_folio() relies on buffers/IFS/etc being attached to the
> > > folios. It _cannot_ attach them itself, as dirty_folio() cannot
> > > block.
> >
> > Iomap is already attaching it:
> >
> > iomap_dirty_folio()
> > ifs_alloc(inode, folio, 0);
> > gfp = GFP_NOFS | __GFP_NOFAIL;
> > ifs = kzalloc_flex(..., gfp);
> >
> > Either it's already a bug, or we won't hit such folio without private for
> > the non-sleeping routes.
>
> This is, strictly speaking, a bug. Per Documentation/filesystems/locking.rst:
>
> locking rules:
> All except dirty_folio and free_folio may block
>
> I think currently, de facto, it's not very likely to be hit[1]. Mostly because,
> for writenotify filesystems/VMAs, you'll always trip over ->page_mkwrite()
> (marking the folio dirty), and cleaning will always write-protect it before
> clearing the dirty flag off the folio.
>
> [1] I _think_ there are ways to hit it, if you go down funky folio_cancel_dirty()
> paths.
>
> >
> > >
> > > >
> > > > 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
> > >
> > > ? why is writeback calling folio_invalidate()?
> >
> > If a folio is beyond EOF, we have no need to write it, thus can go
> > invalidating it directly.
>
> Why would you not rely on truncation itself to invalidate it?
>
> >
> > Or is there any better solution from the perspective of MM?
>
> My understanding is that the truncation logic should correctly handle
> the semantics you want.
>
> As for the GUP problem, yes, this is yet another issue. It is known.
> ->dirty_folio() implementations cannot assume the memory allocation will
> succeed. And there's no possible fallback to "failure to dirty a folio".
>
> The real solution (in my series) is to simply not discard this at-will.
> invalidate_folio() seems to be yet another variant of this issue.
>
> For what it's worth, I observed a similar issue in mm/truncate code. I don't
> know what the correct approach would be. Keep the private around for GUP pins?
> Reject the truncate altogether?
>
> (I don't think it's possible to do this last option; keeping the private is
> also weird, and I don't see how you can possibly ditch it after the folio is
> orphaned)
>
> --
> Pedro
Just to note by mistake I created v2 a separate thread. v4 is at:
https://lore.kernel.org/all/20261204063235.1033893-1-listout@listout.xyz/
--
Brahmajit Das
SUSE L3
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied
2026-12-04 10:05 ` Pedro Falcato
2026-12-04 12:48 ` Brahmajit Das
@ 2026-12-04 21:05 ` Qu Wenruo
1 sibling, 0 replies; 12+ messages in thread
From: Qu Wenruo @ 2026-12-04 21:05 UTC (permalink / raw)
To: Pedro Falcato
Cc: Brahmajit Das, dsterba, linux-btrfs, clm, mason, david, jgg,
jhubbard, peterx, linux-mm, linux-kernel, syzkaller-bugs,
Brahmajit Das, syzbot+d6f123d4060990f0f3ff, willy, jack, hch
在 2026/12/4 20:35, Pedro Falcato 写道:
> +CC willy, jack, hch
>
> On Fri, Dec 04, 2026 at 07:13:08AM +1030, Qu Wenruo wrote:
>>
>>
>> 在 2026/12/3 23:58, Pedro Falcato 写道:
>> [...]
>>>> 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()
>>>
>>> FWIW, this sounds like a variant of
>>> https://lore.kernel.org/linux-mm/20261119-gup-dirty-what-v1-0-236d69f25c24@suse.de/.
>>> (which I will respin soon)
>>
>> One assumption doesn't meet the current code.
>>
>>> - ->dirty_folio() relies on buffers/IFS/etc being attached to the
>>> folios. It _cannot_ attach them itself, as dirty_folio() cannot
>>> block.
>>
>> Iomap is already attaching it:
>>
>> iomap_dirty_folio()
>> ifs_alloc(inode, folio, 0);
>> gfp = GFP_NOFS | __GFP_NOFAIL;
>> ifs = kzalloc_flex(..., gfp);
>>
>> Either it's already a bug, or we won't hit such folio without private for
>> the non-sleeping routes.
>
> This is, strictly speaking, a bug. Per Documentation/filesystems/locking.rst:
>
> locking rules:
> All except dirty_folio and free_folio may block
>
> I think currently, de facto, it's not very likely to be hit[1]. Mostly because,
> for writenotify filesystems/VMAs, you'll always trip over ->page_mkwrite()
> (marking the folio dirty), and cleaning will always write-protect it before
> clearing the dirty flag off the folio.
>
> [1] I _think_ there are ways to hit it, if you go down funky folio_cancel_dirty()
> paths.
>
>>
>>>
>>>>
>>>> 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
>>>
>>> ? why is writeback calling folio_invalidate()?
>>
>> If a folio is beyond EOF, we have no need to write it, thus can go
>> invalidating it directly.
>
> Why would you not rely on truncation itself to invalidate it?
The truncation did try to invalidate the folio, by even removing the
folio private.
But the folio can not be removed from filemap because it's pinned by the
direct IO.
Did you mean we just skip the write, and let future truncation to
invalidate it again?
That also feels a little weird, yes the folio will eventually be
truncated when evicting the inode, but still doesn't feel so correct.
And this also means, we need to manually copy the invalidating handling
to release reserved space, just not try to detach the private.
I guess for now, following the not-so-correct iomap behavior is the most
simple solution from the perspective of fs, but it still leave a pretty
bad taste.
>
>>
>> Or is there any better solution from the perspective of MM?
>
> My understanding is that the truncation logic should correctly handle
> the semantics you want.
>
> As for the GUP problem, yes, this is yet another issue. It is known.
> ->dirty_folio() implementations cannot assume the memory allocation will
> succeed. And there's no possible fallback to "failure to dirty a folio".
>
> The real solution (in my series) is to simply not discard this at-will.
> invalidate_folio() seems to be yet another variant of this issue.
>
> For what it's worth, I observed a similar issue in mm/truncate code. I don't
> know what the correct approach would be. Keep the private around for GUP pins?
> Reject the truncate altogether?
>
> (I don't think it's possible to do this last option; keeping the private is
> also weird, and I don't see how you can possibly ditch it after the folio is
> orphaned)
I had a not-so-good idea, that will require invalidate_folio() callback
to be split into two parts:
- invalidate_folio_fs()
The usual fs specific part.
- invalidate_folio_detach()
The private detaching part.
The second part is only called after the folio is properly removed from
the filemap.
So this ensures all folios in the filemap still have their private attached.
But again, I'm not an expert in MM. So this can just be a stupid idea
and can conflict with other things like large folio splitting.
Thanks,
Qu
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied
@ 2026-12-02 4:11 Brahmajit Das
2026-12-03 4:11 ` sashiko-bot
0 siblings, 1 reply; 12+ messages in thread
From: Brahmajit Das @ 2026-12-02 4:11 UTC (permalink / raw)
To: dsterba, linux-btrfs
Cc: quwenruo.btrfs, clm, mason, david, jgg, jhubbard, peterx,
linux-mm, linux-kernel, syzkaller-bugs,
syzbot+d6f123d4060990f0f3ff, 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.
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()")
Signed-off-by: Brahmajit Das <bdas@suse.de>
---
fs/btrfs/extent_io.c | 24 +++++++++++++++++++++---
fs/btrfs/extent_io.h | 1 +
fs/btrfs/inode.c | 22 +++++++++++++++++++++-
fs/btrfs/subpage.c | 5 +++--
fs/btrfs/subpage.h | 3 ++-
5 files changed, 48 insertions(+), 7 deletions(-)
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index b2739838c646..9d54dbd01591 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -923,11 +923,12 @@ static int attach_extent_buffer_folio(struct extent_buffer *eb,
folio_attach_private(folio, prealloc);
else
/* Do new allocation to attach subpage */
- ret = btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_METADATA);
+ ret = btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_METADATA,
+ GFP_NOFS);
return ret;
}
-int set_folio_extent_mapped(struct folio *folio)
+int set_folio_extent_mapped_gfp(struct folio *folio, gfp_t gfp)
{
struct btrfs_fs_info *fs_info;
@@ -939,12 +940,18 @@ int set_folio_extent_mapped(struct folio *folio)
fs_info = folio_to_fs_info(folio);
if (btrfs_is_subpage(fs_info, folio))
- return btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_DATA);
+ return btrfs_attach_folio_state(fs_info, folio,
+ BTRFS_SUBPAGE_DATA, gfp);
folio_attach_private(folio, (void *)EXTENT_FOLIO_PRIVATE);
return 0;
}
+int set_folio_extent_mapped(struct folio *folio)
+{
+ return set_folio_extent_mapped_gfp(folio, GFP_NOFS);
+}
+
void clear_folio_extent_mapped(struct folio *folio)
{
struct btrfs_fs_info *fs_info;
@@ -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/extent_io.h b/fs/btrfs/extent_io.h
index c55b37cb24b7..1da7606efaad 100644
--- a/fs/btrfs/extent_io.h
+++ b/fs/btrfs/extent_io.h
@@ -285,6 +285,7 @@ int btrfs_writepages(struct address_space *mapping, struct writeback_control *wb
int btree_writepages(struct address_space *mapping, struct writeback_control *wbc);
void btrfs_btree_wait_writeback_range(struct btrfs_fs_info *fs_info, u64 start, u64 end);
void btrfs_readahead(struct readahead_control *rac);
+int set_folio_extent_mapped_gfp(struct folio *folio, gfp_t gfp);
int set_folio_extent_mapped(struct folio *folio);
void clear_folio_extent_mapped(struct folio *folio);
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 95c9530554d0..f30f334a099e 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);
diff --git a/fs/btrfs/subpage.c b/fs/btrfs/subpage.c
index ebf18efe1ea3..c779bf4771b9 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);
diff --git a/fs/btrfs/subpage.h b/fs/btrfs/subpage.h
index 9b106a73d682..83e758af4e4e 100644
--- a/fs/btrfs/subpage.h
+++ b/fs/btrfs/subpage.h
@@ -104,7 +104,8 @@ static inline bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info,
}
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);
void btrfs_detach_folio_state(const struct btrfs_fs_info *fs_info, struct folio *folio,
enum btrfs_folio_type type);
--
2.56.1
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied
2026-12-02 4:11 Brahmajit Das
@ 2026-12-03 4:11 ` sashiko-bot
0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-12-03 4:11 UTC (permalink / raw)
To: Brahmajit Das, dsterba
Cc: quwenruo.btrfs, clm, mason, david, jgg, jhubbard, peterx,
linux-mm, syzkaller-bugs, syzbot+d6f123d4060990f0f3ff,
Brahmajit Das, dsterba
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
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied
@ 2026-12-02 4:08 Brahmajit Das
2026-12-02 5:51 ` sashiko-bot
0 siblings, 1 reply; 12+ messages in thread
From: Brahmajit Das @ 2026-12-02 4:08 UTC (permalink / raw)
To: dsterba, linux-btrfs
Cc: quwenruo.btrfs, clm, mason, david, jgg, jhubbard, peterx,
linux-mm, linux-kernel, syzkaller-bugs,
syzbot+d6f123d4060990f0f3ff
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()")
---
Notes:
v2:
- Re-attach the folio private in btrfs_data_dirty_folio() instead of
skipping folio_invalidate() for folios without private (Qu)
- Attach private only inside the beyond-EOF branch of extent_writepage(),
leaving the unexpected dirty folio check alone (Qu)
- Sync the uptodate bitmap of the new folio state with the folio flag
- Explain where the folio private is dropped in the changelog
- Cc GUP maintainers for the GUP part of the reasoning (Qu)
- v1: https://lore.kernel.org/linux-btrfs/20261130182637.468143-1-listout@listout.xyz/
Open: if the GFP_NOWAIT allocation fails for a folio inside EOF, the
blocks are not marked for fixup and writeback hits the existing "marked
dirty without notifying the fs" error path.
fs/btrfs/extent_io.c | 24 +++++++++++++++++++++---
fs/btrfs/extent_io.h | 1 +
fs/btrfs/inode.c | 22 +++++++++++++++++++++-
fs/btrfs/subpage.c | 5 +++--
fs/btrfs/subpage.h | 3 ++-
5 files changed, 48 insertions(+), 7 deletions(-)
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index b2739838c646..9d54dbd01591 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -923,11 +923,12 @@ static int attach_extent_buffer_folio(struct extent_buffer *eb,
folio_attach_private(folio, prealloc);
else
/* Do new allocation to attach subpage */
- ret = btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_METADATA);
+ ret = btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_METADATA,
+ GFP_NOFS);
return ret;
}
-int set_folio_extent_mapped(struct folio *folio)
+int set_folio_extent_mapped_gfp(struct folio *folio, gfp_t gfp)
{
struct btrfs_fs_info *fs_info;
@@ -939,12 +940,18 @@ int set_folio_extent_mapped(struct folio *folio)
fs_info = folio_to_fs_info(folio);
if (btrfs_is_subpage(fs_info, folio))
- return btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_DATA);
+ return btrfs_attach_folio_state(fs_info, folio,
+ BTRFS_SUBPAGE_DATA, gfp);
folio_attach_private(folio, (void *)EXTENT_FOLIO_PRIVATE);
return 0;
}
+int set_folio_extent_mapped(struct folio *folio)
+{
+ return set_folio_extent_mapped_gfp(folio, GFP_NOFS);
+}
+
void clear_folio_extent_mapped(struct folio *folio)
{
struct btrfs_fs_info *fs_info;
@@ -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/extent_io.h b/fs/btrfs/extent_io.h
index c55b37cb24b7..1da7606efaad 100644
--- a/fs/btrfs/extent_io.h
+++ b/fs/btrfs/extent_io.h
@@ -285,6 +285,7 @@ int btrfs_writepages(struct address_space *mapping, struct writeback_control *wb
int btree_writepages(struct address_space *mapping, struct writeback_control *wbc);
void btrfs_btree_wait_writeback_range(struct btrfs_fs_info *fs_info, u64 start, u64 end);
void btrfs_readahead(struct readahead_control *rac);
+int set_folio_extent_mapped_gfp(struct folio *folio, gfp_t gfp);
int set_folio_extent_mapped(struct folio *folio);
void clear_folio_extent_mapped(struct folio *folio);
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 95c9530554d0..f30f334a099e 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);
diff --git a/fs/btrfs/subpage.c b/fs/btrfs/subpage.c
index ebf18efe1ea3..c779bf4771b9 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);
diff --git a/fs/btrfs/subpage.h b/fs/btrfs/subpage.h
index 9b106a73d682..83e758af4e4e 100644
--- a/fs/btrfs/subpage.h
+++ b/fs/btrfs/subpage.h
@@ -104,7 +104,8 @@ static inline bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info,
}
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);
void btrfs_detach_folio_state(const struct btrfs_fs_info *fs_info, struct folio *folio,
enum btrfs_folio_type type);
--
2.56.1
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied
2026-12-02 4:08 Brahmajit Das
@ 2026-12-02 5:51 ` sashiko-bot
2026-12-02 7:17 ` Qu Wenruo
0 siblings, 1 reply; 12+ messages in thread
From: sashiko-bot @ 2026-12-02 5:51 UTC (permalink / raw)
To: Brahmajit Das, dsterba
Cc: quwenruo.btrfs, clm, mason, david, jgg, jhubbard, peterx,
linux-mm, syzkaller-bugs, syzbot+d6f123d4060990f0f3ff, dsterba
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
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied
2026-12-02 5:51 ` sashiko-bot
@ 2026-12-02 7:17 ` Qu Wenruo
0 siblings, 0 replies; 12+ messages in thread
From: Qu Wenruo @ 2026-12-02 7:17 UTC (permalink / raw)
To: sashiko-reviews, Brahmajit Das, dsterba
Cc: clm, mason, david, jgg, jhubbard, peterx, linux-mm,
syzkaller-bugs, syzbot+d6f123d4060990f0f3ff, dsterba
在 2026/12/2 16:21, sashiko-bot@kernel.org 写道:
> 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?
In this case, we can follow iomap, by using GFP_NOFS | __GFP_NOFAIL.
I know NOFAIL usage is not recommended, but at least there is already
the iomap example to follow.
Furthermore, with the NOFAIL here, we do not need the fallback in
extent_writepage() anymore.
Thanks,
Qu
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2026-12-04 21:05 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox