Kernel bug: possible deadlock in blkdev_read_iter
Subsystem: block
Status: fixed

Crash report:
WARNING: possible circular locking dependency detected
syzkaller #0 Not tainted
------------------------------------------------------
syz.0.17/6091 is trying to acquire lock:
ffff888106d71328 (&sb->s_type->i_mutex_key#8){++++}-{4:4}, at: inode_lock_shared include/linux/fs.h:1042 [inline]
ffff888106d71328 (&sb->s_type->i_mutex_key#8){++++}-{4:4}, at: blkdev_read_iter+0x2b6/0x4f0 block/fops.c:855

but task is already holding lock:
ffff88803cd6c088 (vm_lock){++++}-{0:0}, at: lock_next_vma+0x10a/0xf40 mm/mmap_lock.c:334

which lock already depends on the new lock.


the existing dependency chain (in reverse order) is:

-> #2 (vm_lock){++++}-{0:0}:
       __vma_enter_locked+0x23a/0x770 mm/mmap_lock.c:72
       __vma_start_write+0x21/0x170 mm/mmap_lock.c:104
       vma_start_write include/linux/mmap_lock.h:213 [inline]
       mprotect_fixup+0x345/0xb40 mm/mprotect.c:768
       setup_arg_pages+0x4b6/0xb60 fs/exec.c:670
       load_elf_binary+0xb75/0x5110 fs/binfmt_elf.c:1028
       search_binary_handler fs/exec.c:1669 [inline]
       exec_binprm fs/exec.c:1701 [inline]
       bprm_execve fs/exec.c:1753 [inline]
       bprm_execve+0x8fb/0x1620 fs/exec.c:1729
       kernel_execve+0x32b/0x3d0 fs/exec.c:1919
       try_to_run_init_process+0x14/0x60 init/main.c:1506
       kernel_init+0x14b/0x1e0 init/main.c:1634
       ret_from_fork+0x754/0xaf0 arch/x86/kernel/process.c:158
       ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:246

-> #1 (&mm->mmap_lock){++++}-{4:4}:
       __might_fault+0xde/0x140 mm/memory.c:7177
       _copy_to_iter+0x117/0x1720 lib/iov_iter.c:196
       copy_page_to_iter lib/iov_iter.c:374 [inline]
       copy_page_to_iter+0x12a/0x1e0 lib/iov_iter.c:361
       copy_folio_to_iter include/linux/uio.h:204 [inline]
       filemap_read+0x7a9/0x10a0 mm/filemap.c:2851
       blkdev_read_iter+0x2c4/0x4f0 block/fops.c:856
       new_sync_read fs/read_write.c:491 [inline]
       vfs_read+0x825/0xb30 fs/read_write.c:572
       ksys_read+0x12a/0x250 fs/read_write.c:715
       do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
       do_syscall_64+0xc9/0xf80 arch/x86/entry/syscall_64.c:94
       entry_SYSCALL_64_after_hwframe+0x77/0x7f

-> #0 (&sb->s_type->i_mutex_key#8){++++}-{4:4}:
       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+0x14b8/0x2630 kernel/locking/lockdep.c:5237
       lock_acquire kernel/locking/lockdep.c:5868 [inline]
       lock_acquire+0x17c/0x330 kernel/locking/lockdep.c:5825
       down_read+0x99/0x460 kernel/locking/rwsem.c:1537
       inode_lock_shared include/linux/fs.h:1042 [inline]
       blkdev_read_iter+0x2b6/0x4f0 block/fops.c:855
       __kernel_read+0x397/0xac0 fs/read_write.c:530
       freader_fetch+0x1d3/0xa70 lib/buildid.c:100
       __build_id_parse.isra.0+0xe4/0x6c0 lib/buildid.c:297
       do_procmap_query+0xaf6/0x1050 fs/proc/task_mmu.c:733
       procfs_procmap_ioctl+0x9d/0xe0 fs/proc/task_mmu.c:813
       vfs_ioctl fs/ioctl.c:51 [inline]
       __do_sys_ioctl fs/ioctl.c:597 [inline]
       __se_sys_ioctl fs/ioctl.c:583 [inline]
       __x64_sys_ioctl+0x18e/0x210 fs/ioctl.c:583
       do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
       do_syscall_64+0xc9/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:
  &sb->s_type->i_mutex_key#8 --> &mm->mmap_lock --> vm_lock

 Possible unsafe locking scenario:

       CPU0                    CPU1
       ----                    ----
  rlock(vm_lock);
                               lock(&mm->mmap_lock);
                               lock(vm_lock);
  rlock(&sb->s_type->i_mutex_key#8);

 *** DEADLOCK ***

1 lock held by syz.0.17/6091:
 #0: ffff88803cd6c088 (vm_lock){++++}-{0:0}, at: lock_next_vma+0x10a/0xf40 mm/mmap_lock.c:334

stack backtrace:
CPU: 1 UID: 0 PID: 6091 Comm: syz.0.17 Not tainted syzkaller #0 PREEMPT(full) 
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
Call Trace:
 <TASK>
 __dump_stack lib/dump_stack.c:94 [inline]
 dump_stack_lvl+0x100/0x190 lib/dump_stack.c:120
 print_circular_bug.cold+0x178/0x1c7 kernel/locking/lockdep.c:2043
 check_noncircular+0x146/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 kernel/locking/lockdep.c:3908 [inline]
 __lock_acquire+0x14b8/0x2630 kernel/locking/lockdep.c:5237
 lock_acquire kernel/locking/lockdep.c:5868 [inline]
 lock_acquire+0x17c/0x330 kernel/locking/lockdep.c:5825
 down_read+0x99/0x460 kernel/locking/rwsem.c:1537
 inode_lock_shared include/linux/fs.h:1042 [inline]
 blkdev_read_iter+0x2b6/0x4f0 block/fops.c:855
 __kernel_read+0x397/0xac0 fs/read_write.c:530
 freader_fetch+0x1d3/0xa70 lib/buildid.c:100
 __build_id_parse.isra.0+0xe4/0x6c0 lib/buildid.c:297
 do_procmap_query+0xaf6/0x1050 fs/proc/task_mmu.c:733
 procfs_procmap_ioctl+0x9d/0xe0 fs/proc/task_mmu.c:813
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:597 [inline]
 __se_sys_ioctl fs/ioctl.c:583 [inline]
 __x64_sys_ioctl+0x18e/0x210 fs/ioctl.c:583
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0xc9/0xf80 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7fe93fb9aeb9
Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 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 e8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007fff4c053408 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
RAX: ffffffffffffffda RBX: 00007fe93fe15fa0 RCX: 00007fe93fb9aeb9
RDX: 0000200000000180 RSI: 00000000c0686611 RDI: 0000000000000003
RBP: 00007fe93fc08c1f R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 00007fe93fe15fac R14: 00007fe93fe15fa0 R15: 00007fe93fe15fa0
 </TASK>