Kernel bug: possible deadlock in ext4_xattr_inode_lookup_create
Subsystem: ext4
Status: open

Crash report:
loop0: detected capacity change from 0 to 1024
======================================================
WARNING: possible circular locking dependency detected
syzkaller #0 Not tainted
------------------------------------------------------
syz.0.57/6273 is trying to acquire lock:
ffff8880588cd060 (&sb->s_type->i_mutex_key#9/1){+.+.}-{4:4}, at: inode_lock include/linux/fs.h:869 [inline]
ffff8880588cd060 (&sb->s_type->i_mutex_key#9/1){+.+.}-{4:4}, at: ext4_xattr_inode_create fs/ext4/xattr.c:1506 [inline]
ffff8880588cd060 (&sb->s_type->i_mutex_key#9/1){+.+.}-{4:4}, at: ext4_xattr_inode_lookup_create+0x17d4/0x1c20 fs/ext4/xattr.c:1588

but task is already holding lock:
ffff888058921590 (&ei->i_data_sem/3){++++}-{4:4}, at: ext4_truncate+0x93f/0x1100 fs/ext4/inode.c:4639

which lock already depends on the new lock.


the existing dependency chain (in reverse order) is:

-> #1 (&ei->i_data_sem/3){++++}-{4:4}:
       lock_acquire+0x120/0x360 kernel/locking/lockdep.c:5868
       down_write+0x96/0x1f0 kernel/locking/rwsem.c:1590
       ext4_update_i_disksize fs/ext4/ext4.h:3431 [inline]
       ext4_xattr_inode_write fs/ext4/xattr.c:1444 [inline]
       ext4_xattr_inode_lookup_create+0x14d1/0x1c20 fs/ext4/xattr.c:1594
       ext4_xattr_ibody_set+0x202/0x6a0 fs/ext4/xattr.c:2258
       ext4_xattr_set_handle+0xc9a/0x1590 fs/ext4/xattr.c:2435
       ext4_xattr_set+0x230/0x320 fs/ext4/xattr.c:2549
       __vfs_setxattr+0x43c/0x480 fs/xattr.c:200
       __vfs_setxattr_noperm+0x12d/0x660 fs/xattr.c:234
       vfs_setxattr+0x16b/0x2f0 fs/xattr.c:321
       do_setxattr fs/xattr.c:636 [inline]
       filename_setxattr+0x274/0x600 fs/xattr.c:665
       path_setxattrat+0x364/0x3a0 fs/xattr.c:713
       __do_sys_setxattr fs/xattr.c:747 [inline]
       __se_sys_setxattr fs/xattr.c:743 [inline]
       __x64_sys_setxattr+0xbc/0xe0 fs/xattr.c:743
       do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
       do_syscall_64+0xfa/0x3b0 arch/x86/entry/syscall_64.c:94
       entry_SYSCALL_64_after_hwframe+0x77/0x7f

-> #0 (&sb->s_type->i_mutex_key#9/1){+.+.}-{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+0x96/0x1f0 kernel/locking/rwsem.c:1590
       inode_lock include/linux/fs.h:869 [inline]
       ext4_xattr_inode_create fs/ext4/xattr.c:1506 [inline]
       ext4_xattr_inode_lookup_create+0x17d4/0x1c20 fs/ext4/xattr.c:1588
       ext4_xattr_block_set+0x223/0x2ac0 fs/ext4/xattr.c:1908
       ext4_xattr_move_to_block fs/ext4/xattr.c:2659 [inline]
       ext4_xattr_make_inode_space fs/ext4/xattr.c:2734 [inline]
       ext4_expand_extra_isize_ea+0x12d5/0x1ea0 fs/ext4/xattr.c:2822
       __ext4_expand_extra_isize+0x30d/0x400 fs/ext4/inode.c:6385
       ext4_try_to_expand_extra_isize fs/ext4/inode.c:6428 [inline]
       __ext4_mark_inode_dirty+0x46c/0x700 fs/ext4/inode.c:6506
       ext4_ext_truncate+0xac/0x300 fs/ext4/extents.c:4475
       ext4_truncate+0x9c0/0x1100 fs/ext4/inode.c:4643
       ext4_evict_inode+0x8cf/0xee0 fs/ext4/inode.c:261
       evict+0x504/0x9c0 fs/inode.c:810
       do_unlinkat+0x39f/0x560 fs/namei.c:4657
       __do_sys_unlink fs/namei.c:4696 [inline]
       __se_sys_unlink fs/namei.c:4694 [inline]
       __x64_sys_unlink+0x47/0x50 fs/namei.c:4694
       do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
       do_syscall_64+0xfa/0x3b0 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(&ei->i_data_sem/3);
                               lock(&sb->s_type->i_mutex_key#9/1);
                               lock(&ei->i_data_sem/3);
  lock(&sb->s_type->i_mutex_key#9/1);

 *** DEADLOCK ***

4 locks held by syz.0.57/6273:
 #0: ffff88807d068428 (sb_writers#4){.+.+}-{0:0}, at: mnt_want_write+0x41/0x90 fs/namespace.c:557
 #1: ffff88807d068618 (sb_internal){.+.+}-{0:0}, at: percpu_down_read_freezable include/linux/percpu-rwsem.h:83 [inline]
 #1: ffff88807d068618 (sb_internal){.+.+}-{0:0}, at: __sb_start_write include/linux/fs.h:1798 [inline]
 #1: ffff88807d068618 (sb_internal){.+.+}-{0:0}, at: sb_start_intwrite include/linux/fs.h:1981 [inline]
 #1: ffff88807d068618 (sb_internal){.+.+}-{0:0}, at: ext4_evict_inode+0x2d6/0xee0 fs/ext4/inode.c:215
 #2: ffff888058921590 (&ei->i_data_sem/3){++++}-{4:4}, at: ext4_truncate+0x93f/0x1100 fs/ext4/inode.c:4639
 #3: ffff8880589213e8 (&ei->xattr_sem){++++}-{4:4}, at: ext4_write_trylock_xattr fs/ext4/xattr.h:164 [inline]
 #3: ffff8880589213e8 (&ei->xattr_sem){++++}-{4:4}, at: ext4_try_to_expand_extra_isize fs/ext4/inode.c:6425 [inline]
 #3: ffff8880589213e8 (&ei->xattr_sem){++++}-{4:4}, at: __ext4_mark_inode_dirty+0x3eb/0x700 fs/ext4/inode.c:6506

stack backtrace:
CPU: 0 UID: 0 PID: 6273 Comm: syz.0.57 Not tainted syzkaller #0 PREEMPT(full) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 07/12/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+0x96/0x1f0 kernel/locking/rwsem.c:1590
 inode_lock include/linux/fs.h:869 [inline]
 ext4_xattr_inode_create fs/ext4/xattr.c:1506 [inline]
 ext4_xattr_inode_lookup_create+0x17d4/0x1c20 fs/ext4/xattr.c:1588
 ext4_xattr_block_set+0x223/0x2ac0 fs/ext4/xattr.c:1908
 ext4_xattr_move_to_block fs/ext4/xattr.c:2659 [inline]
 ext4_xattr_make_inode_space fs/ext4/xattr.c:2734 [inline]
 ext4_expand_extra_isize_ea+0x12d5/0x1ea0 fs/ext4/xattr.c:2822
 __ext4_expand_extra_isize+0x30d/0x400 fs/ext4/inode.c:6385
 ext4_try_to_expand_extra_isize fs/ext4/inode.c:6428 [inline]
 __ext4_mark_inode_dirty+0x46c/0x700 fs/ext4/inode.c:6506
 ext4_ext_truncate+0xac/0x300 fs/ext4/extents.c:4475
 ext4_truncate+0x9c0/0x1100 fs/ext4/inode.c:4643
 ext4_evict_inode+0x8cf/0xee0 fs/ext4/inode.c:261
 evict+0x504/0x9c0 fs/inode.c:810
 do_unlinkat+0x39f/0x560 fs/namei.c:4657
 __do_sys_unlink fs/namei.c:4696 [inline]
 __se_sys_unlink fs/namei.c:4694 [inline]
 __x64_sys_unlink+0x47/0x50 fs/namei.c:4694
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0xfa/0x3b0 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f15e1f8ebe9
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:00007fffc6559138 EFLAGS: 00000246 ORIG_RAX: 0000000000000057
RAX: ffffffffffffffda RBX: 00007f15e21b5fa0 RCX: 00007f15e1f8ebe9
RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000200000000080
RBP: 00007f15e2011e19 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 00007f15e21b5fa0 R14: 00007f15e21b5fa0 R15: 0000000000000001
 </TASK>