Kernel bug: possible deadlock in gup_fast_fallback (2)
Subsystem: mm
Status: fixed

Crash report:
loop0: detected capacity change from 0 to 32768
XFS (loop0): Mounting V5 Filesystem bfdc47fc-10d8-4eed-a562-11a831b3f791
XFS (loop0): Ending clean mount
======================================================
WARNING: possible circular locking dependency detected
syzkaller #0 Not tainted
------------------------------------------------------
syz.0.17/5488 is trying to acquire lock:
ffff888044e06540 (&mm->mmap_lock){++++}-{4:4}, at: gup_fast_fallback+0x20d/0x22e0 mm/gup.c:3204

but task is already holding lock:
ffff8880467da130 (&sb->s_type->i_mutex_key#24){++++}-{4:4}, at: xfs_ilock_demote+0xbd/0x290 fs/xfs/xfs_inode.c:285

which lock already depends on the new lock.


the existing dependency chain (in reverse order) is:

-> #2 (&sb->s_type->i_mutex_key#24){++++}-{4:4}:
       down_read_nested+0x49/0x2e0 kernel/locking/rwsem.c:1662
       xfs_ilock+0x109/0x360 fs/xfs/xfs_inode.c:152
       xfs_ilock_iocb fs/xfs/xfs_file.c:195 [inline]
       xfs_file_buffered_read+0x15b/0x330 fs/xfs/xfs_file.c:284
       xfs_file_read_iter+0x280/0x510 fs/xfs/xfs_file.c:312
       __kernel_read+0x504/0x9b0 fs/read_write.c:530
       freader_fetch+0x1cb/0xa00 lib/buildid.c:100
       __build_id_parse+0x168/0x870 lib/buildid.c:297
       do_procmap_query fs/proc/task_mmu.c:733 [inline]
       procfs_procmap_ioctl+0x7ae/0xd50 fs/proc/task_mmu.c:813
       vfs_ioctl fs/ioctl.c:51 [inline]
       __do_sys_ioctl fs/ioctl.c:597 [inline]
       __se_sys_ioctl+0xfc/0x170 fs/ioctl.c:583
       do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
       do_syscall_64+0xe2/0xf80 arch/x86/entry/syscall_64.c:94
       entry_SYSCALL_64_after_hwframe+0x77/0x7f

-> #1 (vm_lock){++++}-{0:0}:
       __vma_enter_locked+0x243/0x710 mm/mmap_lock.c:72
       __vma_start_write+0x23/0x140 mm/mmap_lock.c:104
       vma_start_write include/linux/mmap_lock.h:213 [inline]
       mprotect_fixup+0x5e1/0xa50 mm/mprotect.c:768
       setup_arg_pages+0x565/0xae0 fs/exec.c:670
       load_elf_binary+0xc5e/0x2980 fs/binfmt_elf.c:1028
       search_binary_handler fs/exec.c:1669 [inline]
       exec_binprm fs/exec.c:1701 [inline]
       bprm_execve+0x93d/0x1410 fs/exec.c:1753
       kernel_execve+0x8ef/0x9e0 fs/exec.c:1919
       try_to_run_init_process+0x13/0x60 init/main.c:1506
       kernel_init+0xad/0x1d0 init/main.c:1634
       ret_from_fork+0x51b/0xa40 arch/x86/kernel/process.c:158
       ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:246

-> #0 (&mm->mmap_lock){++++}-{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+0x15a5/0x2cf0 kernel/locking/lockdep.c:5237
       lock_acquire+0x106/0x330 kernel/locking/lockdep.c:5868
       gup_fast_fallback+0x226/0x22e0 mm/gup.c:3204
       iov_iter_extract_user_pages lib/iov_iter.c:1763 [inline]
       iov_iter_extract_pages+0x37b/0x5f0 lib/iov_iter.c:1826
       __bio_iov_iter_get_pages block/bio.c:1237 [inline]
       bio_iov_iter_get_pages+0x4a8/0x14b0 block/bio.c:1359
       iomap_dio_bio_iter+0xcb3/0x14d0 fs/iomap/direct-io.c:460
       iomap_dio_iter fs/iomap/direct-io.c:-1 [inline]
       __iomap_dio_rw+0xf57/0x1e10 fs/iomap/direct-io.c:753
       iomap_dio_rw+0x45/0xb0 fs/iomap/direct-io.c:847
       xfs_file_dio_write_aligned+0x36a/0x450 fs/xfs/xfs_file.c:707
       xfs_file_dio_write fs/xfs/xfs_file.c:910 [inline]
       xfs_file_write_iter+0x7dd/0x920 fs/xfs/xfs_file.c:1122
       new_sync_write fs/read_write.c:593 [inline]
       vfs_write+0x61d/0xb90 fs/read_write.c:686
       ksys_write+0x150/0x270 fs/read_write.c:738
       do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
       do_syscall_64+0xe2/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:
  &mm->mmap_lock --> vm_lock --> &sb->s_type->i_mutex_key#24

 Possible unsafe locking scenario:

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

 *** DEADLOCK ***

2 locks held by syz.0.17/5488:
 #0: ffff888031ac6420 (sb_writers#12){.+.+}-{0:0}, at: file_start_write include/linux/fs.h:2683 [inline]
 #0: ffff888031ac6420 (sb_writers#12){.+.+}-{0:0}, at: vfs_write+0x227/0xb90 fs/read_write.c:682
 #1: ffff8880467da130 (&sb->s_type->i_mutex_key#24){++++}-{4:4}, at: xfs_ilock_demote+0xbd/0x290 fs/xfs/xfs_inode.c:285

stack backtrace:
CPU: 0 UID: 0 PID: 5488 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_lvl+0xe8/0x150 lib/dump_stack.c:120
 print_circular_bug+0x2e1/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+0x15a5/0x2cf0 kernel/locking/lockdep.c:5237
 lock_acquire+0x106/0x330 kernel/locking/lockdep.c:5868
 gup_fast_fallback+0x226/0x22e0 mm/gup.c:3204
 iov_iter_extract_user_pages lib/iov_iter.c:1763 [inline]
 iov_iter_extract_pages+0x37b/0x5f0 lib/iov_iter.c:1826
 __bio_iov_iter_get_pages block/bio.c:1237 [inline]
 bio_iov_iter_get_pages+0x4a8/0x14b0 block/bio.c:1359
 iomap_dio_bio_iter+0xcb3/0x14d0 fs/iomap/direct-io.c:460
 iomap_dio_iter fs/iomap/direct-io.c:-1 [inline]
 __iomap_dio_rw+0xf57/0x1e10 fs/iomap/direct-io.c:753
 iomap_dio_rw+0x45/0xb0 fs/iomap/direct-io.c:847
 xfs_file_dio_write_aligned+0x36a/0x450 fs/xfs/xfs_file.c:707
 xfs_file_dio_write fs/xfs/xfs_file.c:910 [inline]
 xfs_file_write_iter+0x7dd/0x920 fs/xfs/xfs_file.c:1122
 new_sync_write fs/read_write.c:593 [inline]
 vfs_write+0x61d/0xb90 fs/read_write.c:686
 ksys_write+0x150/0x270 fs/read_write.c:738
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0xe2/0xf80 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f85b679aeb9
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:00007ffd910b1588 EFLAGS: 00000246 ORIG_RAX: 0000000000000001
RAX: ffffffffffffffda RBX: 00007f85b6a15fa0 RCX: 00007f85b679aeb9
RDX: 000000000000f000 RSI: 0000200000000200 RDI: 0000000000000007
RBP: 00007f85b6808c1f R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 00007f85b6a15fac R14: 00007f85b6a15fa0 R15: 00007f85b6a15fa0
 </TASK>