Kernel bug: possible deadlock in ext4_writepages (2)
Subsystem: ext4
Status: open

Crash report:
loop0: detected capacity change from 0 to 512
======================================================
WARNING: possible circular locking dependency detected
syzkaller #0 Not tainted
------------------------------------------------------
syz.0.17/6053 is trying to acquire lock:
ffff8880369acc58 (&sbi->s_writepages_rwsem){++++}-{0:0}, at: percpu_down_read include/linux/percpu-rwsem.h:77 [inline]
ffff8880369acc58 (&sbi->s_writepages_rwsem){++++}-{0:0}, at: ext4_writepages_down_read fs/ext4/ext4.h:1820 [inline]
ffff8880369acc58 (&sbi->s_writepages_rwsem){++++}-{0:0}, at: ext4_writepages+0x1ca/0x350 fs/ext4/inode.c:3025

but task is already holding lock:
ffff88805a204828 (&ei->xattr_sem){++++}-{4:4}, at: ext4_write_trylock_xattr fs/ext4/xattr.h:164 [inline]
ffff88805a204828 (&ei->xattr_sem){++++}-{4:4}, at: ext4_try_to_expand_extra_isize fs/ext4/inode.c:6389 [inline]
ffff88805a204828 (&ei->xattr_sem){++++}-{4:4}, at: __ext4_mark_inode_dirty+0x3db/0x6e0 fs/ext4/inode.c:6470

which lock already depends on the new lock.


the existing dependency chain (in reverse order) is:

-> #2 (&ei->xattr_sem){++++}-{4:4}:
       down_write+0x3a/0x50 kernel/locking/rwsem.c:1590
       ext4_write_lock_xattr fs/ext4/xattr.h:157 [inline]
       ext4_xattr_set_handle+0x165/0x1590 fs/ext4/xattr.c:2371
       ext4_initxattrs+0x9f/0x110 fs/ext4/xattr_security.c:44
       security_inode_init_security+0x290/0x3d0 security/security.c:1344
       __ext4_new_inode+0x32f7/0x3c90 fs/ext4/ialloc.c:1324
       ext4_create+0x22d/0x460 fs/ext4/namei.c:2820
       lookup_open fs/namei.c:4440 [inline]
       open_last_lookups fs/namei.c:4540 [inline]
       path_openat+0x18d1/0x3df0 fs/namei.c:4784
       do_filp_open+0x1fa/0x410 fs/namei.c:4814
       do_sys_openat2+0x121/0x200 fs/open.c:1430
       do_sys_open fs/open.c:1436 [inline]
       __do_sys_openat fs/open.c:1452 [inline]
       __se_sys_openat fs/open.c:1447 [inline]
       __x64_sys_openat+0x138/0x170 fs/open.c:1447
       do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
       do_syscall_64+0xec/0xf80 arch/x86/entry/syscall_64.c:94
       entry_SYSCALL_64_after_hwframe+0x77/0x7f

-> #1 (jbd2_handle){++++}-{0:0}:
       wait_transaction_locked+0x1b6/0x270 fs/jbd2/transaction.c:151
       add_transaction_credits fs/jbd2/transaction.c:222 [inline]
       start_this_handle+0x77d/0x21c0 fs/jbd2/transaction.c:403
       jbd2__journal_start+0x2c1/0x5b0 fs/jbd2/transaction.c:501
       __ext4_journal_start_sb+0x203/0x580 fs/ext4/ext4_jbd2.c:114
       __ext4_journal_start fs/ext4/ext4_jbd2.h:242 [inline]
       ext4_do_writepages+0xf41/0x44f0 fs/ext4/inode.c:2914
       ext4_writepages+0x203/0x350 fs/ext4/inode.c:3026
       do_writepages+0x32e/0x550 mm/page-writeback.c:2598
       __writeback_single_inode+0x133/0x1240 fs/fs-writeback.c:1737
       writeback_sb_inodes+0x8c8/0x1840 fs/fs-writeback.c:2030
       __writeback_inodes_wb+0x111/0x240 fs/fs-writeback.c:2107
       wb_writeback+0x43f/0xaa0 fs/fs-writeback.c:2218
       wb_check_old_data_flush fs/fs-writeback.c:2322 [inline]
       wb_do_writeback fs/fs-writeback.c:2375 [inline]
       wb_workfn+0xad7/0xee0 fs/fs-writeback.c:2403
       process_one_work kernel/workqueue.c:3257 [inline]
       process_scheduled_works+0xad1/0x1770 kernel/workqueue.c:3340
       worker_thread+0x8a0/0xda0 kernel/workqueue.c:3421
       kthread+0x711/0x8a0 kernel/kthread.c:463
       ret_from_fork+0x510/0xa50 arch/x86/kernel/process.c:158
       ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:246

-> #0 (&sbi->s_writepages_rwsem){++++}-{0:0}:
       check_prev_add kernel/locking/lockdep.c:3165 [inline]
       check_prevs_add kernel/locking/lockdep.c:3284 [inline]
       validate_chain kernel/locking/lockdep.c:3908 [inline]
       __lock_acquire+0x15a6/0x2cf0 kernel/locking/lockdep.c:5237
       lock_acquire+0x107/0x340 kernel/locking/lockdep.c:5868
       percpu_down_read_internal+0x48/0x1d0 include/linux/percpu-rwsem.h:53
       percpu_down_read include/linux/percpu-rwsem.h:77 [inline]
       ext4_writepages_down_read fs/ext4/ext4.h:1820 [inline]
       ext4_writepages+0x1ca/0x350 fs/ext4/inode.c:3025
       do_writepages+0x32e/0x550 mm/page-writeback.c:2598
       __writeback_single_inode+0x133/0x1240 fs/fs-writeback.c:1737
       writeback_single_inode+0x488/0xd60 fs/fs-writeback.c:1858
       write_inode_now+0x160/0x1d0 fs/fs-writeback.c:2924
       iput_final fs/inode.c:1941 [inline]
       iput+0xa77/0x1030 fs/inode.c:2003
       ext4_xattr_block_set+0x1fce/0x2ac0 fs/ext4/xattr.c:2203
       ext4_xattr_move_to_block fs/ext4/xattr.c:2668 [inline]
       ext4_xattr_make_inode_space fs/ext4/xattr.c:2743 [inline]
       ext4_expand_extra_isize_ea+0x12da/0x1ea0 fs/ext4/xattr.c:2831
       __ext4_expand_extra_isize+0x30d/0x400 fs/ext4/inode.c:6349
       ext4_try_to_expand_extra_isize fs/ext4/inode.c:6392 [inline]
       __ext4_mark_inode_dirty+0x45c/0x6e0 fs/ext4/inode.c:6470
       ext4_evict_inode+0x79c/0xe60 fs/ext4/inode.c:253
       evict+0x5f4/0xae0 fs/inode.c:837
       ext4_orphan_cleanup+0xc20/0x1460 fs/ext4/orphan.c:472
       __ext4_fill_super fs/ext4/super.c:5658 [inline]
       ext4_fill_super+0x58ad/0x6170 fs/ext4/super.c:5777
       get_tree_bdev_flags+0x40e/0x4d0 fs/super.c:1691
       vfs_get_tree+0x92/0x2a0 fs/super.c:1751
       fc_mount fs/namespace.c:1199 [inline]
       do_new_mount_fc fs/namespace.c:3636 [inline]
       do_new_mount+0x302/0xa10 fs/namespace.c:3712
       do_mount fs/namespace.c:4035 [inline]
       __do_sys_mount fs/namespace.c:4224 [inline]
       __se_sys_mount+0x313/0x410 fs/namespace.c:4201
       do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
       do_syscall_64+0xec/0xf80 arch/x86/entry/syscall_64.c:94
       entry_SYSCALL_64_after_hwframe+0x77/0x7f

other info that might help us debug this:

Chain exists of:
  &sbi->s_writepages_rwsem --> jbd2_handle --> &ei->xattr_sem

 Possible unsafe locking scenario:

       CPU0                    CPU1
       ----                    ----
  lock(&ei->xattr_sem);
                               lock(jbd2_handle);
                               lock(&ei->xattr_sem);
  rlock(&sbi->s_writepages_rwsem);

 *** DEADLOCK ***

3 locks held by syz.0.17/6053:
 #0: ffff8880369ae0d0 (&type->s_umount_key#28/1){+.+.}-{4:4}, at: alloc_super+0x28c/0xab0 fs/super.c:344
 #1: ffff8880369ae770 (sb_internal){.+.+}-{0:0}, at: percpu_down_read_freezable include/linux/percpu-rwsem.h:83 [inline]
 #1: ffff8880369ae770 (sb_internal){.+.+}-{0:0}, at: __sb_start_write include/linux/fs/super.h:19 [inline]
 #1: ffff8880369ae770 (sb_internal){.+.+}-{0:0}, at: sb_start_intwrite include/linux/fs/super.h:177 [inline]
 #1: ffff8880369ae770 (sb_internal){.+.+}-{0:0}, at: ext4_evict_inode+0x26f/0xe60 fs/ext4/inode.c:214
 #2: ffff88805a204828 (&ei->xattr_sem){++++}-{4:4}, at: ext4_write_trylock_xattr fs/ext4/xattr.h:164 [inline]
 #2: ffff88805a204828 (&ei->xattr_sem){++++}-{4:4}, at: ext4_try_to_expand_extra_isize fs/ext4/inode.c:6389 [inline]
 #2: ffff88805a204828 (&ei->xattr_sem){++++}-{4:4}, at: __ext4_mark_inode_dirty+0x3db/0x6e0 fs/ext4/inode.c:6470

stack backtrace:
CPU: 1 UID: 0 PID: 6053 Comm: syz.0.17 Not tainted syzkaller #0 PREEMPT_{RT,(full)} 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/25/2025
Call Trace:
 <TASK>
 dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120
 print_circular_bug+0x2e2/0x300 kernel/locking/lockdep.c:2043
 check_noncircular+0x12e/0x150 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 kernel/locking/lockdep.c:3908 [inline]
 __lock_acquire+0x15a6/0x2cf0 kernel/locking/lockdep.c:5237
 lock_acquire+0x107/0x340 kernel/locking/lockdep.c:5868
 percpu_down_read_internal+0x48/0x1d0 include/linux/percpu-rwsem.h:53
 percpu_down_read include/linux/percpu-rwsem.h:77 [inline]
 ext4_writepages_down_read fs/ext4/ext4.h:1820 [inline]
 ext4_writepages+0x1ca/0x350 fs/ext4/inode.c:3025
 do_writepages+0x32e/0x550 mm/page-writeback.c:2598
 __writeback_single_inode+0x133/0x1240 fs/fs-writeback.c:1737
 writeback_single_inode+0x488/0xd60 fs/fs-writeback.c:1858
 write_inode_now+0x160/0x1d0 fs/fs-writeback.c:2924
 iput_final fs/inode.c:1941 [inline]
 iput+0xa77/0x1030 fs/inode.c:2003
 ext4_xattr_block_set+0x1fce/0x2ac0 fs/ext4/xattr.c:2203
 ext4_xattr_move_to_block fs/ext4/xattr.c:2668 [inline]
 ext4_xattr_make_inode_space fs/ext4/xattr.c:2743 [inline]
 ext4_expand_extra_isize_ea+0x12da/0x1ea0 fs/ext4/xattr.c:2831
 __ext4_expand_extra_isize+0x30d/0x400 fs/ext4/inode.c:6349
 ext4_try_to_expand_extra_isize fs/ext4/inode.c:6392 [inline]
 __ext4_mark_inode_dirty+0x45c/0x6e0 fs/ext4/inode.c:6470
 ext4_evict_inode+0x79c/0xe60 fs/ext4/inode.c:253
 evict+0x5f4/0xae0 fs/inode.c:837
 ext4_orphan_cleanup+0xc20/0x1460 fs/ext4/orphan.c:472
 __ext4_fill_super fs/ext4/super.c:5658 [inline]
 ext4_fill_super+0x58ad/0x6170 fs/ext4/super.c:5777
 get_tree_bdev_flags+0x40e/0x4d0 fs/super.c:1691
 vfs_get_tree+0x92/0x2a0 fs/super.c:1751
 fc_mount fs/namespace.c:1199 [inline]
 do_new_mount_fc fs/namespace.c:3636 [inline]
 do_new_mount+0x302/0xa10 fs/namespace.c:3712
 do_mount fs/namespace.c:4035 [inline]
 __do_sys_mount fs/namespace.c:4224 [inline]
 __se_sys_mount+0x313/0x410 fs/namespace.c:4201
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0xec/0xf80 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f362dd40eea
Code: d8 64 89 02 48 c7 c0 ff ff ff ff eb a6 e8 de 1a 00 00 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 40 00 49 89 ca b8 a5 00 00 00 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:00007ffc008111c8 EFLAGS: 00000246 ORIG_RAX: 00000000000000a5
RAX: ffffffffffffffda RBX: 00007ffc00811250 RCX: 00007f362dd40eea
RDX: 0000200000000180 RSI: 00002000000001c0 RDI: 00007ffc00811210
RBP: 0000200000000180 R08: 00007ffc00811250 R09: 0000000000800700
R10: 0000000000800700 R11: 0000000000000246 R12: 00002000000001c0
R13: 00007ffc00811210 R14: 000000000000046f R15: 000000000000002c
 </TASK>
------------[ cut here ]------------
EA inode 11 i_nlink=2
WARNING: fs/ext4/xattr.c:1058 at ext4_xattr_inode_update_ref+0x494/0x570 fs/ext4/xattr.c:1056, CPU#0: syz.0.17/6053
Modules linked in:
CPU: 0 UID: 0 PID: 6053 Comm: syz.0.17 Not tainted syzkaller #0 PREEMPT_{RT,(full)} 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/25/2025
RIP: 0010:ext4_xattr_inode_update_ref+0x4dc/0x570 fs/ext4/xattr.c:1056
Code: 74 08 4c 89 ef e8 04 b3 9f ff 4d 8b 6d 00 48 b8 00 00 00 00 00 fc ff df 41 0f b6 04 06 84 c0 75 77 41 8b 17 4c 89 e7 4c 89 ee <67> 48 0f b9 3a 4c 8b 6c 24 28 e9 bd fe ff ff e8 60 61 4f 08 44 89
RSP: 0018:ffffc90003ff7100 EFLAGS: 00010246
RAX: 0000000000000000 RBX: 1ffff110477e78ab RCX: dffffc0000000000
RDX: 0000000000000002 RSI: 000000000000000b RDI: ffffffff8ee14520
RBP: ffffc90003ff71f8 R08: 0000000000000000 R09: 0000000000000000
R10: dffffc0000000000 R11: ffffed10477e78ac R12: ffffffff8ee14520
R13: 000000000000000b R14: 1ffff110477e7870 R15: ffff88823bf3c380
FS:  0000555577d05500(0000) GS:ffff888126cef000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00005577306ad040 CR3: 00000000422f4000 CR4: 00000000003526f0
Call Trace:
 <TASK>
 ext4_xattr_inode_dec_ref fs/ext4/xattr.c:1081 [inline]
 ext4_xattr_set_entry+0xabb/0x1e20 fs/ext4/xattr.c:1723
 ext4_xattr_ibody_set+0x254/0x6a0 fs/ext4/xattr.c:2272
 ext4_xattr_move_to_block fs/ext4/xattr.c:2675 [inline]
 ext4_xattr_make_inode_space fs/ext4/xattr.c:2743 [inline]
 ext4_expand_extra_isize_ea+0x13ad/0x1ea0 fs/ext4/xattr.c:2831
 __ext4_expand_extra_isize+0x30d/0x400 fs/ext4/inode.c:6349
 ext4_try_to_expand_extra_isize fs/ext4/inode.c:6392 [inline]
 __ext4_mark_inode_dirty+0x45c/0x6e0 fs/ext4/inode.c:6470
 ext4_evict_inode+0x79c/0xe60 fs/ext4/inode.c:253
 evict+0x5f4/0xae0 fs/inode.c:837
 ext4_orphan_cleanup+0xc20/0x1460 fs/ext4/orphan.c:472
 __ext4_fill_super fs/ext4/super.c:5658 [inline]
 ext4_fill_super+0x58ad/0x6170 fs/ext4/super.c:5777
 get_tree_bdev_flags+0x40e/0x4d0 fs/super.c:1691
 vfs_get_tree+0x92/0x2a0 fs/super.c:1751
 fc_mount fs/namespace.c:1199 [inline]
 do_new_mount_fc fs/namespace.c:3636 [inline]
 do_new_mount+0x302/0xa10 fs/namespace.c:3712
 do_mount fs/namespace.c:4035 [inline]
 __do_sys_mount fs/namespace.c:4224 [inline]
 __se_sys_mount+0x313/0x410 fs/namespace.c:4201
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0xec/0xf80 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f362dd40eea
Code: d8 64 89 02 48 c7 c0 ff ff ff ff eb a6 e8 de 1a 00 00 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 40 00 49 89 ca b8 a5 00 00 00 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:00007ffc008111c8 EFLAGS: 00000246 ORIG_RAX: 00000000000000a5
RAX: ffffffffffffffda RBX: 00007ffc00811250 RCX: 00007f362dd40eea
RDX: 0000200000000180 RSI: 00002000000001c0 RDI: 00007ffc00811210
RBP: 0000200000000180 R08: 00007ffc00811250 R09: 0000000000800700
R10: 0000000000800700 R11: 0000000000000246 R12: 00002000000001c0
R13: 00007ffc00811210 R14: 000000000000046f R15: 000000000000002c
 </TASK>
----------------
Code disassembly (best guess):
   0:	74 08                	je     0xa
   2:	4c 89 ef             	mov    %r13,%rdi
   5:	e8 04 b3 9f ff       	call   0xff9fb30e
   a:	4d 8b 6d 00          	mov    0x0(%r13),%r13
   e:	48 b8 00 00 00 00 00 	movabs $0xdffffc0000000000,%rax
  15:	fc ff df
  18:	41 0f b6 04 06       	movzbl (%r14,%rax,1),%eax
  1d:	84 c0                	test   %al,%al
  1f:	75 77                	jne    0x98
  21:	41 8b 17             	mov    (%r15),%edx
  24:	4c 89 e7             	mov    %r12,%rdi
  27:	4c 89 ee             	mov    %r13,%rsi
* 2a:	67 48 0f b9 3a       	ud1    (%edx),%rdi <-- trapping instruction
  2f:	4c 8b 6c 24 28       	mov    0x28(%rsp),%r13
  34:	e9 bd fe ff ff       	jmp    0xfffffef6
  39:	e8 60 61 4f 08       	call   0x84f619e
  3e:	44                   	rex.R
  3f:	89                   	.byte 0x89