[PATCH v3 1/3] btrfs: don't dereference a missing folio private on writeback error
From: Brahmajit Das
Date: Thu Dec 03 2026 - 08:31:22 EST
From: Brahmajit Das <bdas@xxxxxxx>
extent_writepage() rejects a dirty folio without folio::private with
-EUCLEAN, then jumps to the common exit path, which calls
btrfs_folio_end_lock_bitmap(). For the bs < folio size case, that
function dereferences the folio private:
btrfs_folio_end_lock_bitmap()
bfs = folio_get_private(folio); /* NULL */
...
if (atomic_read(&bfs->nr_locked) == 0) /* NULL dereference */
so the error path meant to catch such a folio crashes instead.
No subpage lock can be held on a folio without private, so just set the
mapping error and unlock the folio directly.
Note: this was only build tested.
This patch was written with the help of an AI coding assistant (Claude).
Fixes: 7ca3e84980ef ("btrfs: reject out-of-band dirty folios during writeback")
Signed-off-by: Brahmajit Das <bdas@xxxxxxx>
---
fs/btrfs/extent_io.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index b2739838c646..9dded5c670de 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -2070,8 +2070,15 @@ static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl
"root %lld ino %llu folio %llu is marked dirty without notifying the fs",
btrfs_root_id(inode->root),
btrfs_ino(inode), folio_pos(folio));
- ret = -EUCLEAN;
- goto done;
+ /*
+ * Don't go through btrfs_folio_end_lock_bitmap(), which
+ * dereferences the missing folio private for the
+ * bs < folio size case. No subpage lock can be held on a
+ * folio without private, so a plain unlock is enough.
+ */
+ mapping_set_error(folio->mapping, -EUCLEAN);
+ folio_unlock(folio);
+ return -EUCLEAN;
}
ret = set_folio_extent_mapped(folio);
--
2.56.1