btrfs: set the block size before starting a transaction
PATCH 1/1 btrfs: set the block size before starting a transaction
From: Qing Zhang <[email protected]>
When adding a device, btrfs_init_new_device() calls set_blocksize() with a transaction handle held. If the block size changes, this takes the bdev inode's i_rwsem and triggers a lockdep dependency cycle:
bdev i_rwsem -> mmap_lock -> trans_num_writers
-> trans_num_extwriters -> bdev i_rwsem
The buffered read path nests mmap_lock under the bdev i_rwsem, while mmapping a btrfs file can join a transaction through atime updates. Since commit c0e473a0d226 ("block: fix race between set_blocksize and read paths"), set_blocksize() takes the bdev inode's i_rwsem when changing the block size.
Move set_blocksize() before btrfs_start_transaction() to break the cycle. Also check its return value to reject devices whose logical block size exceeds BTRFS_BDEV_BLOCKSIZE.
---
#syz test: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
fs/btrfs/volumes.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
fs/btrfs/volumes.c +4 -1
@@ -3019,6 +3019,10 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_pathif (ret) goto error_free_device; + ret =set_blocksize (device->bdev_file,BTRFS_BDEV_BLOCKSIZE ); +if (ret) + goto error_free_zone; + trans =btrfs_start_transaction (root,0 );if (IS_ERR (trans)) { ret =PTR_ERR (trans);
@@ -3043,7 +3047,6 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path*/ atomic_inc (&device->dev_stats_ccnt); device->dev_stats_valid =1 ; -set_blocksize (device->bdev_file,BTRFS_BDEV_BLOCKSIZE );if (seeding_dev) {/* GFP_KERNEL allocation must not be under device_list_mutex */
Signature · 1 line
2.43.7
syzbot
syzbotRe: [syzbot] [btrfs?] possible deadlock in set_blocksize
syzbot has tested the proposed patch but the reproducer is still triggering an issue: WARNING in __btrfs_free_extent
Hello,
syzbot has tested the proposed patch but the reproducer is still triggering an issue:
WARNING in __btrfs_free_extent
------------[ cut here ]------------ ret == -E ret == -ENOENT WARNING: fs/btrfs/extent-tree.c:3418 at __btrfs_free_extent+0x1745/0x38a0 fs/btrfs/extent-tree.c:3418, CPU#1: btrfs-transacti/6038 Modules linked in: CPU: 1 UID: 0 PID: 6038 Comm: btrfs-transacti Not tainted syzkaller #0 PREEMPT(full) Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/15/2026 RIP: 0010:__btrfs_free_extent+0x1745/0x38a0 fs/btrfs/extent-tree.c:3418 Code: 84 c0 0f 84 01 03 00 00 e8 d8 88 a8 fd e9 30 0b 00 00 e8 1e ef 8c fd 84 c0 0f 84 0c 03 00 00 e8 c1 88 a8 fd e9 9d 0c 00 00 90 <0f> 0b 90 48 8b 44 24 28 42 80 3c 20 00 4d 8d 75 60 74 08 4c 89 f7 RSP: 0018:ffffc900034d7580 EFLAGS: 00010246 RAX: 00000000fffffffe RBX: ffff88801faee720 RCX: 0000000000000005 RDX: ffffffff8420790e RSI: ffffffff905d9c00 RDI: 00000000fffffffe RBP: ffffc900034d7730 R08: ffff888021381f40 R09: 0000000000000002 R10: 00000000fffffffe R11: 0000000000000000 R12: dffffc0000000000 R13: ffff88805ad54178 R14: ffff888053e7ddc0 R15: 00000000fffffffe FS: 0000000000000000(0000) GS:ffff88812372b000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000560567923d51 CR3: 000000007caed000 CR4: 0000000000350ef0 Call Trace: <TASK> run_delayed_tree_ref fs/btrfs/extent-tree.c:1824 [inline] run_one_delayed_ref fs/btrfs/extent-tree.c:1851 [inline] btrfs_run_delayed_refs_for_head fs/btrfs/extent-tree.c:2058 [inline] __btrfs_run_delayed_refs+0x1669/0x43a0 fs/btrfs/extent-tree.c:2134 btrfs_run_delayed_refs+0xdc/0x2a0 fs/btrfs/extent-tree.c:2246 btrfs_commit_transaction+0x287/0x3010 fs/btrfs/transaction.c:2283 transaction_kthread+0x2c1/0x460 fs/btrfs/disk-io.c:1586 kthread+0x38b/0x480 kernel/kthread.c:436 ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245 </TASK>
Tested on:
commit: 65ba8f34 Merge tag 'xtensa-20261027' of https://github.. git tree: upstream console output: https://syzkaller.appspot.com/x/log.txt?x=1439e469580000 kernel config: https://syzkaller.appspot.com/x/.config?x=fbcd9c8641bd149b dashboard link: https://syzkaller.appspot.com/bug?extid=5c01398781fb51df783a compiler: Debian clang version 22.1.8 (1~deb13u4), Debian LLD 22.1.8 patch: https://syzkaller.appspot.com/x/patch.diff?x=11607721580000
Quoted 15 lines · Qing Zhang
From: Qing Zhang <[email protected]>
When adding a device,
btrfs_init_new_device()callsset_blocksize()with a transaction handle held. If the block size changes, this takes the bdev inode'si_rwsemand triggers a lockdep dependency cycle:bdev i_rwsem -> mmap_lock -> trans_num_writers -> trans_num_extwriters -> bdev i_rwsemThe buffered read path nests
mmap_lockunder the bdevi_rwsem, while mmapping a btrfs file can join a transaction through atime updates. Since commitc0e473a0d226("block: fix race betweenset_blocksizeand read paths"),set_blocksize()takes the bdev inode'si_rwsemwhen changing the block size.
Quoted 7 lines
Move
set_blocksize()beforebtrfs_start_transaction()to break the cycle. Also check its return value to reject devices whose logical block size exceedsBTRFS_BDEV_BLOCKSIZE.Fixes c0e473a0d226 ("block: fix race between set_blocksize and read paths")Reported-by [email protected]
There is already a better solution by not calling the stupid ste_blocksize() at all:
Quoted 29 lines
Signed-off-by Qing Zhang <[email protected]>---
#syz test:git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.gitmasterfs/btrfs/volumes.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)fs/btrfs/volumes.c +4
-1@@ -3019,6 +3019,10 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_pathif (ret) goto error_free_device; + ret =set_blocksize (device->bdev_file,BTRFS_BDEV_BLOCKSIZE ); +if (ret) + goto error_free_zone; + trans =btrfs_start_transaction (root,0 );if (IS_ERR (trans)) { ret =PTR_ERR (trans);@@ -3043,7 +3047,6 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path*/ atomic_inc (&device->dev_stats_ccnt); device->dev_stats_valid =1 ; -set_blocksize (device->bdev_file,BTRFS_BDEV_BLOCKSIZE );if (seeding_dev) {/* GFP_KERNEL allocation must not be under device_list_mutex */
Quoted 20 lines · Qu Wenruo
Quoted 19 lines · Qing Zhang
From: Qing Zhang <[email protected]>
When adding a device,
btrfs_init_new_device()callsset_blocksize()with a transaction handle held. If the block size changes, this takes the bdev inode'si_rwsemand triggers a lockdep dependency cycle:bdev i_rwsem -> mmap_lock -> trans_num_writers -> trans_num_extwriters -> bdev i_rwsemThe buffered read path nests
mmap_lockunder the bdevi_rwsem, while mmapping a btrfs file can join a transaction through atime updates. Since commitc0e473a0d226("block: fix race betweenset_blocksizeand read paths"),set_blocksize()takes the bdev inode'si_rwsemwhen changing the block size.Move
set_blocksize()beforebtrfs_start_transaction()to break the cycle. Also check its return value to reject devices whose logical block size exceedsBTRFS_BDEV_BLOCKSIZE.
Quoted 8 lines
Quoted 3 lines
Fixes c0e473a0d226 ("block: fix race between set_blocksize and read paths")Reported-by [email protected]There is already a better solution by not calling the stupid
ste_blocksize()at all:
Thanks, agreed: removing the call breaks the cycle at the source.
Quoted 29 lines
Quoted 29 lines
Signed-off-by Qing Zhang <[email protected]>---
#syz test:git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.gitmasterfs/btrfs/volumes.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)fs/btrfs/volumes.c +4
-1@@ -3019,6 +3019,10 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_pathif (ret) goto error_free_device; + ret =set_blocksize (device->bdev_file,BTRFS_BDEV_BLOCKSIZE ); +if (ret) + goto error_free_zone; + trans =btrfs_start_transaction (root,0 );if (IS_ERR (trans)) { ret =PTR_ERR (trans);@@ -3043,7 +3047,6 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path*/ atomic_inc (&device->dev_stats_ccnt); device->dev_stats_valid =1 ; -set_blocksize (device->bdev_file,BTRFS_BDEV_BLOCKSIZE );if (seeding_dev) {/* GFP_KERNEL allocation must not be under device_list_mutex */