Kernel bug: possible deadlock in mark_as_free_ex (2)
Subsystem: ntfs3
Status: open

Crash report:
loop0: detected capacity change from 0 to 4096
======================================================
WARNING: possible circular locking dependency detected
syzkaller #0 Not tainted
------------------------------------------------------
syz.0.28/6110 is trying to acquire lock:
ffff888056140250 (&wnd->rw_lock){++++}-{4:4}, at: mark_as_free_ex+0x3e/0x390 fs/ntfs3/fsntfs.c:2464

but task is already holding lock:
ffff88805b6c2fb0 (&ni->file.run_lock#2){++++}-{4:4}, at: ntfs_truncate fs/ntfs3/file.c:483 [inline]
ffff88805b6c2fb0 (&ni->file.run_lock#2){++++}-{4:4}, at: ntfs_setattr+0x70e/0xbe0 fs/ntfs3/file.c:806

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+0x120/0x360 kernel/locking/lockdep.c:5868
       down_read+0x97/0x1f0 kernel/locking/rwsem.c:1537
       run_unpack_ex+0x7e5/0xba0 fs/ntfs3/run.c:1124
       ntfs_read_mft fs/ntfs3/inode.c:401 [inline]
       ntfs_iget5+0x232b/0x37e0 fs/ntfs3/inode.c:541
       ntfs_dir_emit fs/ntfs3/dir.c:337 [inline]
       ntfs_read_hdr+0x7cf/0xc30 fs/ntfs3/dir.c:385
       ntfs_readdir+0xa5f/0xdd0 fs/ntfs3/dir.c:496
       iterate_dir+0x3a5/0x580 fs/readdir.c:108
       __do_sys_getdents64 fs/readdir.c:410 [inline]
       __se_sys_getdents64+0xe4/0x260 fs/readdir.c:396
       do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
       do_syscall_64+0xfa/0xfa0 arch/x86/entry/syscall_64.c:94
       entry_SYSCALL_64_after_hwframe+0x77/0x7f

-> #0 (&wnd->rw_lock){++++}-{4:4}:
       check_prev_add kernel/locking/lockdep.c:3165 [inline]
       check_prevs_add kernel/locking/lockdep.c:3284 [inline]
       validate_chain+0xb9b/0x2140 kernel/locking/lockdep.c:3908
       __lock_acquire+0xab9/0xd20 kernel/locking/lockdep.c:5237
       lock_acquire+0x120/0x360 kernel/locking/lockdep.c:5868
       down_write_nested+0x3e/0x60 kernel/locking/rwsem.c:1706
       mark_as_free_ex+0x3e/0x390 fs/ntfs3/fsntfs.c:2464
       run_deallocate_ex+0x187/0x2f0 fs/ntfs3/attrib.c:122
       attr_set_size+0xeb9/0x2c70 fs/ntfs3/attrib.c:753
       ntfs_truncate fs/ntfs3/file.c:484 [inline]
       ntfs_setattr+0x7d0/0xbe0 fs/ntfs3/file.c:806
       notify_change+0xc18/0xf60 fs/attr.c:546
       do_truncate+0x1a4/0x220 fs/open.c:68
       vfs_truncate+0x493/0x520 fs/open.c:118
       do_sys_truncate+0xdb/0x190 fs/open.c:141
       __do_sys_truncate fs/open.c:153 [inline]
       __se_sys_truncate fs/open.c:151 [inline]
       __x64_sys_truncate+0x5b/0x70 fs/open.c:151
       do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
       do_syscall_64+0xfa/0xfa0 arch/x86/entry/syscall_64.c:94
       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 ***

4 locks held by syz.0.28/6110:
 #0: ffff888056142480 (sb_writers#12){.+.+}-{0:0}, at: mnt_want_write+0x41/0x90 fs/namespace.c:508
 #1: ffff88805b6c31c8 (&sb->s_type->i_mutex_key#20){+.+.}-{4:4}, at: inode_lock_killable include/linux/fs.h:985 [inline]
 #1: ffff88805b6c31c8 (&sb->s_type->i_mutex_key#20){+.+.}-{4:4}, at: do_truncate+0x171/0x220 fs/open.c:63
 #2: ffff88805b6c2f10 (&ni->ni_lock#2/5){+.+.}-{4:4}, at: ni_lock fs/ntfs3/ntfs_fs.h:1113 [inline]
 #2: ffff88805b6c2f10 (&ni->ni_lock#2/5){+.+.}-{4:4}, at: ntfs_truncate fs/ntfs3/file.c:481 [inline]
 #2: ffff88805b6c2f10 (&ni->ni_lock#2/5){+.+.}-{4:4}, at: ntfs_setattr+0x6fd/0xbe0 fs/ntfs3/file.c:806
 #3: ffff88805b6c2fb0 (&ni->file.run_lock#2){++++}-{4:4}, at: ntfs_truncate fs/ntfs3/file.c:483 [inline]
 #3: ffff88805b6c2fb0 (&ni->file.run_lock#2){++++}-{4:4}, at: ntfs_setattr+0x70e/0xbe0 fs/ntfs3/file.c:806

stack backtrace:
CPU: 0 UID: 0 PID: 6110 Comm: syz.0.28 Not tainted syzkaller #0 PREEMPT_{RT,(full)} 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/02/2025
Call Trace:
 <TASK>
 dump_stack_lvl+0x189/0x250 lib/dump_stack.c:120
 print_circular_bug+0x2ee/0x310 kernel/locking/lockdep.c:2043
 check_noncircular+0x134/0x160 kernel/locking/lockdep.c:2175
 check_prev_add kernel/locking/lockdep.c:3165 [inline]
 check_prevs_add kernel/locking/lockdep.c:3284 [inline]
 validate_chain+0xb9b/0x2140 kernel/locking/lockdep.c:3908
 __lock_acquire+0xab9/0xd20 kernel/locking/lockdep.c:5237
 lock_acquire+0x120/0x360 kernel/locking/lockdep.c:5868
 down_write_nested+0x3e/0x60 kernel/locking/rwsem.c:1706
 mark_as_free_ex+0x3e/0x390 fs/ntfs3/fsntfs.c:2464
 run_deallocate_ex+0x187/0x2f0 fs/ntfs3/attrib.c:122
 attr_set_size+0xeb9/0x2c70 fs/ntfs3/attrib.c:753
 ntfs_truncate fs/ntfs3/file.c:484 [inline]
 ntfs_setattr+0x7d0/0xbe0 fs/ntfs3/file.c:806
 notify_change+0xc18/0xf60 fs/attr.c:546
 do_truncate+0x1a4/0x220 fs/open.c:68
 vfs_truncate+0x493/0x520 fs/open.c:118
 do_sys_truncate+0xdb/0x190 fs/open.c:141
 __do_sys_truncate fs/open.c:153 [inline]
 __se_sys_truncate fs/open.c:151 [inline]
 __x64_sys_truncate+0x5b/0x70 fs/open.c:151
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0xfa/0xfa0 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7fca2976f6c9
Code: ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 40 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 a8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007ffeb7252248 EFLAGS: 00000246 ORIG_RAX: 000000000000004c
RAX: ffffffffffffffda RBX: 00007fca299c5fa0 RCX: 00007fca2976f6c9
RDX: 0000000000000000 RSI: 000000000001bf00 RDI: 00002000000000c0
RBP: 00007fca297f1f91 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 00007fca299c5fa0 R14: 00007fca299c5fa0 R15: 0000000000000002
 </TASK>