syzbot reported a deadlock in mark_as_free_ex. [1] Reset the order of mft and used locks to avoid this issue. [1] WARNING: possible circular locking dependency detected 6.13.0-rc2-syzkaller-00036-g231825b2e1ff #0 Not tainted ------------------------------------------------------ syz-executor149/6544 is trying to acquire lock: ffff88807e17a270 (&wnd->rw_lock){++++}-{4:4}, at: mark_as_free_ex+0x3e/0x390 fs/ntfs3/fsntfs.c:2484 but task is already holding lock: ffff8880705e43c8 (&ni->file.run_lock#2){++++}-{4:4}, at: ntfs_truncate fs/ntfs3/file.c:505 [inline] ffff8880705e43c8 (&ni->file.run_lock#2){++++}-{4:4}, at: ntfs_setattr+0x6d3/0xb80 fs/ntfs3/file.c:824 which lock already depends on the new lock. the existing dependency chain (in reverse order) is: -> #1 (&ni->file.run_lock#2){++++}-{4:4}: lock_acquire+0x1ed/0x550 kernel/locking/lockdep.c:5849 down_read+0xb1/0xa40 kernel/locking/rwsem.c:1524 run_unpack_ex+0x55e/0x9e0 fs/ntfs3/run.c:1119 ntfs_read_mft fs/ntfs3/inode.c:401 [inline] ntfs_iget5+0x1f9a/0x37b0 fs/ntfs3/inode.c:537 ntfs_dir_emit fs/ntfs3/dir.c:335 [inline] ntfs_read_hdr+0x700/0xb80 fs/ntfs3/dir.c:383 ntfs_readdir+0x91f/0xf00 fs/ntfs3/dir.c:494 iterate_dir+0x571/0x800 fs/readdir.c:108 __do_sys_getdents64 fs/readdir.c:403 [inline] __se_sys_getdents64+0x1e2/0x4b0 fs/readdir.c:389 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x77/0x7f -> #0 (&wnd->rw_lock){++++}-{4:4}: check_prev_add kernel/locking/lockdep.c:3161 [inline] check_prevs_add kernel/locking/lockdep.c:3280 [inline] validate_chain+0x18ef/0x5920 kernel/locking/lockdep.c:3904 __lock_acquire+0x1397/0x2100 kernel/locking/lockdep.c:5226 lock_acquire+0x1ed/0x550 kernel/locking/lockdep.c:5849 down_write_nested+0xa2/0x220 kernel/locking/rwsem.c:1693 mark_as_free_ex+0x3e/0x390 fs/ntfs3/fsntfs.c:2484 run_deallocate_ex+0x244/0x5f0 fs/ntfs3/attrib.c:122 attr_set_size+0x168d/0x4300 fs/ntfs3/attrib.c:753 ntfs_truncate fs/ntfs3/file.c:506 [inline] ntfs_setattr+0x7a4/0xb80 fs/ntfs3/file.c:824 notify_change+0xbca/0xe90 fs/attr.c:552 do_truncate+0x220/0x310 fs/open.c:65 handle_truncate fs/namei.c:3449 [inline] do_open fs/namei.c:3832 [inline] path_openat+0x2e1e/0x3590 fs/namei.c:3987 do_filp_open+0x27f/0x4e0 fs/namei.c:4014 do_sys_openat2+0x13e/0x1d0 fs/open.c:1402 do_sys_open fs/open.c:1417 [inline] __do_sys_open fs/open.c:1425 [inline] __se_sys_open fs/open.c:1421 [inline] __x64_sys_open+0x225/0x270 fs/open.c:1421 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x77/0x7f other info that might help us debug this: Possible unsafe locking scenario: CPU0 CPU1 ---- ---- lock(&ni->file.run_lock#2); lock(&wnd->rw_lock); lock(&ni->file.run_lock#2); lock(&wnd->rw_lock); *** DEADLOCK *** Fixes: 5fc982fe7eca ("fs/ntfs3: Fix case when unmarked clusters intersect with zone") Reported-by: syzbot+8df514c431bd240c5644@xxxxxxxxxxxxxxxxxxxxxxxxx Closes: https://syzkaller.appspot.com/bug?extid=8df514c431bd240c5644 Tested-by: syzbot+8df514c431bd240c5644@xxxxxxxxxxxxxxxxxxxxxxxxx Signed-off-by: Lizhi Xu <lizhi.xu@xxxxxxxxxxxxx> --- fs/ntfs3/run.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/fs/ntfs3/run.c b/fs/ntfs3/run.c index 6e86d66197ef..fdac1f941379 100644 --- a/fs/ntfs3/run.c +++ b/fs/ntfs3/run.c @@ -1096,9 +1096,17 @@ int run_unpack_ex(struct runs_tree *run, struct ntfs_sb_info *sbi, CLST ino, /* Looks like volume is corrupted. */ ntfs_set_state(sbi, NTFS_DIRTY_ERROR); - - if (!down_write_trylock(&wnd->rw_lock)) + struct rw_semaphore *lock = + is_mounted(sbi) ? &sbi->mft.ni->file.run_lock : + NULL; + if (zone && lock) + down_read(lock); + + if (!down_write_trylock(&wnd->rw_lock)) { + if (zone && lock) + up_read(lock); continue; + } if (zone) { /* @@ -1112,16 +1120,12 @@ int run_unpack_ex(struct runs_tree *run, struct ntfs_sb_info *sbi, CLST ino, err = wnd_set_used_safe(wnd, lcn, len, &done); if (zone) { /* Restore zone. Lock mft run. */ - struct rw_semaphore *lock = - is_mounted(sbi) ? &sbi->mft.ni->file.run_lock : - NULL; - if (lock) - down_read(lock); ntfs_refresh_zone(sbi); - if (lock) - up_read(lock); } up_write(&wnd->rw_lock); + if (zone && lock) + up_read(lock); + if (err) return err; } -- 2.43.0