Kernel bug: possible deadlock in pcpu_alloc_noprof (2)
Subsystem: nbd
Status: open

Crash report:
======================================================
WARNING: possible circular locking dependency detected
syzkaller #0 Not tainted
------------------------------------------------------
syz.0.17/6079 is trying to acquire lock:
ffffffff8e53b568 (pcpu_alloc_mutex){+.+.}-{4:4}, at: pcpu_alloc_noprof+0xb5a/0x1470 mm/percpu.c:1782

but task is already holding lock:
ffff8880278dc318 (&q->q_usage_counter(io)#49){++++}-{0:0}, at: blk_mq_freeze_queue_nomemsave block/blk-mq.c:205 [inline]
ffff8880278dc318 (&q->q_usage_counter(io)#49){++++}-{0:0}, at: __blk_mq_update_nr_hw_queues block/blk-mq.c:5119 [inline]
ffff8880278dc318 (&q->q_usage_counter(io)#49){++++}-{0:0}, at: blk_mq_update_nr_hw_queues+0x8ad/0x1280 block/blk-mq.c:5173

which lock already depends on the new lock.


the existing dependency chain (in reverse order) is:

-> #2 (&q->q_usage_counter(io)#49){++++}-{0:0}:
       blk_alloc_queue+0x610/0x750 block/blk-core.c:461
       blk_mq_alloc_queue+0x172/0x280 block/blk-mq.c:4402
       __blk_mq_alloc_disk+0x29/0x120 block/blk-mq.c:4449
       nbd_dev_add+0x492/0xbb0 drivers/block/nbd.c:1954
       nbd_init+0x181/0x320 drivers/block/nbd.c:2692
       do_one_initcall+0x123/0x680 init/main.c:1283
       do_initcall_level init/main.c:1345 [inline]
       do_initcalls init/main.c:1361 [inline]
       do_basic_setup init/main.c:1380 [inline]
       kernel_init_freeable+0x5c8/0x920 init/main.c:1593
       kernel_init+0x1c/0x2b0 init/main.c:1483
       ret_from_fork+0x983/0xb10 arch/x86/kernel/process.c:158
       ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:246

-> #1 (fs_reclaim){+.+.}-{0:0}:
       __fs_reclaim_acquire mm/page_alloc.c:4301 [inline]
       fs_reclaim_acquire+0x102/0x150 mm/page_alloc.c:4315
       might_alloc include/linux/sched/mm.h:317 [inline]
       prepare_alloc_pages+0x162/0x670 mm/page_alloc.c:4978
       __alloc_frozen_pages_noprof+0x18b/0x2430 mm/page_alloc.c:5199
       __alloc_pages_noprof+0xa/0x30 mm/page_alloc.c:5244
       __alloc_pages_node_noprof include/linux/gfp.h:285 [inline]
       alloc_pages_node_noprof include/linux/gfp.h:312 [inline]
       pcpu_alloc_pages mm/percpu-vm.c:95 [inline]
       pcpu_populate_chunk+0x110/0xb10 mm/percpu-vm.c:285
       pcpu_alloc_noprof+0x86b/0x1470 mm/percpu.c:1870
       iommu_dma_init_fq_percpu drivers/iommu/dma-iommu.c:320 [inline]
       iommu_dma_init_fq+0x202/0x8a0 drivers/iommu/dma-iommu.c:346
       iommu_dma_init_domain drivers/iommu/dma-iommu.c:709 [inline]
       iommu_setup_dma_ops+0x1336/0x1700 drivers/iommu/dma-iommu.c:2108
       bus_iommu_probe drivers/iommu/iommu.c:1952 [inline]
       iommu_device_register+0x3e3/0x7d0 drivers/iommu/iommu.c:278
       intel_iommu_init+0x25e7/0x3780 drivers/iommu/intel/iommu.c:2662
       pci_iommu_init+0x31/0x90 arch/x86/kernel/pci-dma.c:174
       do_one_initcall+0x123/0x680 init/main.c:1283
       do_initcall_level init/main.c:1345 [inline]
       do_initcalls init/main.c:1361 [inline]
       do_basic_setup init/main.c:1380 [inline]
       kernel_init_freeable+0x5c8/0x920 init/main.c:1593
       kernel_init+0x1c/0x2b0 init/main.c:1483
       ret_from_fork+0x983/0xb10 arch/x86/kernel/process.c:158
       ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:246

-> #0 (pcpu_alloc_mutex){+.+.}-{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+0x1542/0x22f0 kernel/locking/lockdep.c:5237
       lock_acquire kernel/locking/lockdep.c:5868 [inline]
       lock_acquire+0x179/0x330 kernel/locking/lockdep.c:5825
       __mutex_lock_common kernel/locking/mutex.c:614 [inline]
       __mutex_lock+0x1aa/0x1b10 kernel/locking/mutex.c:776
       pcpu_alloc_noprof+0xb5a/0x1470 mm/percpu.c:1782
       init_alloc_hint lib/sbitmap.c:16 [inline]
       sbitmap_init_node+0x2ff/0x770 lib/sbitmap.c:126
       sbitmap_queue_init_node+0x40/0x4a0 lib/sbitmap.c:454
       bt_alloc block/blk-mq-tag.c:546 [inline]
       blk_mq_init_tags+0x17f/0x320 block/blk-mq-tag.c:571
       blk_mq_alloc_rq_map block/blk-mq.c:3545 [inline]
       blk_mq_alloc_map_and_rqs+0x222/0xeb0 block/blk-mq.c:4100
       __blk_mq_alloc_map_and_rqs block/blk-mq.c:4122 [inline]
       __blk_mq_alloc_map_and_rqs+0x128/0x1f0 block/blk-mq.c:4113
       blk_mq_realloc_tag_set_tags block/blk-mq.c:4790 [inline]
       __blk_mq_update_nr_hw_queues block/blk-mq.c:5121 [inline]
       blk_mq_update_nr_hw_queues+0xad5/0x1280 block/blk-mq.c:5173
       nbd_start_device+0x1b0/0xd70 drivers/block/nbd.c:1489
       nbd_start_device_ioctl drivers/block/nbd.c:1548 [inline]
       __nbd_ioctl drivers/block/nbd.c:1623 [inline]
       nbd_ioctl+0x219/0xda0 drivers/block/nbd.c:1663
       blkdev_ioctl+0x5b0/0x6e0 block/ioctl.c:707
       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+0xcd/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:
  pcpu_alloc_mutex --> fs_reclaim --> &q->q_usage_counter(io)#49

 Possible unsafe locking scenario:

       CPU0                    CPU1
       ----                    ----
  lock(&q->q_usage_counter(io)#49);
                               lock(fs_reclaim);
                               lock(&q->q_usage_counter(io)#49);
  lock(pcpu_alloc_mutex);

 *** DEADLOCK ***

4 locks held by syz.0.17/6079:
 #0: ffff888027a739d0 (&set->update_nr_hwq_lock){++++}-{4:4}, at: blk_mq_update_nr_hw_queues+0x91/0x1280 block/blk-mq.c:5171
 #1: ffff888027a738d8 (&set->tag_list_lock){+.+.}-{4:4}, at: blk_mq_update_nr_hw_queues+0xa4/0x1280 block/blk-mq.c:5172
 #2: ffff8880278dc318 (&q->q_usage_counter(io)#49){++++}-{0:0}, at: blk_mq_freeze_queue_nomemsave block/blk-mq.c:205 [inline]
 #2: ffff8880278dc318 (&q->q_usage_counter(io)#49){++++}-{0:0}, at: __blk_mq_update_nr_hw_queues block/blk-mq.c:5119 [inline]
 #2: ffff8880278dc318 (&q->q_usage_counter(io)#49){++++}-{0:0}, at: blk_mq_update_nr_hw_queues+0x8ad/0x1280 block/blk-mq.c:5173
 #3: ffff8880278dc350 (&q->q_usage_counter(queue)#33){+.+.}-{0:0}, at: blk_mq_freeze_queue_nomemsave block/blk-mq.c:205 [inline]
 #3: ffff8880278dc350 (&q->q_usage_counter(queue)#33){+.+.}-{0:0}, at: __blk_mq_update_nr_hw_queues block/blk-mq.c:5119 [inline]
 #3: ffff8880278dc350 (&q->q_usage_counter(queue)#33){+.+.}-{0:0}, at: blk_mq_update_nr_hw_queues+0x8ad/0x1280 block/blk-mq.c:5173

stack backtrace:
CPU: 3 UID: 0 PID: 6079 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~bpo12+1 04/01/2014
Call Trace:
 <TASK>
 __dump_stack lib/dump_stack.c:94 [inline]
 dump_stack_lvl+0x116/0x1f0 lib/dump_stack.c:120
 print_circular_bug+0x2db/0x410 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+0x1542/0x22f0 kernel/locking/lockdep.c:5237
 lock_acquire kernel/locking/lockdep.c:5868 [inline]
 lock_acquire+0x179/0x330 kernel/locking/lockdep.c:5825
 __mutex_lock_common kernel/locking/mutex.c:614 [inline]
 __mutex_lock+0x1aa/0x1b10 kernel/locking/mutex.c:776
 pcpu_alloc_noprof+0xb5a/0x1470 mm/percpu.c:1782
 init_alloc_hint lib/sbitmap.c:16 [inline]
 sbitmap_init_node+0x2ff/0x770 lib/sbitmap.c:126
 sbitmap_queue_init_node+0x40/0x4a0 lib/sbitmap.c:454
 bt_alloc block/blk-mq-tag.c:546 [inline]
 blk_mq_init_tags+0x17f/0x320 block/blk-mq-tag.c:571
 blk_mq_alloc_rq_map block/blk-mq.c:3545 [inline]
 blk_mq_alloc_map_and_rqs+0x222/0xeb0 block/blk-mq.c:4100
 __blk_mq_alloc_map_and_rqs block/blk-mq.c:4122 [inline]
 __blk_mq_alloc_map_and_rqs+0x128/0x1f0 block/blk-mq.c:4113
 blk_mq_realloc_tag_set_tags block/blk-mq.c:4790 [inline]
 __blk_mq_update_nr_hw_queues block/blk-mq.c:5121 [inline]
 blk_mq_update_nr_hw_queues+0xad5/0x1280 block/blk-mq.c:5173
 nbd_start_device+0x1b0/0xd70 drivers/block/nbd.c:1489
 nbd_start_device_ioctl drivers/block/nbd.c:1548 [inline]
 __nbd_ioctl drivers/block/nbd.c:1623 [inline]
 nbd_ioctl+0x219/0xda0 drivers/block/nbd.c:1663
 blkdev_ioctl+0x5b0/0x6e0 block/ioctl.c:707
 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+0xcd/0xf80 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f2ccd38f7c9
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:00007ffc01c968a8 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
RAX: ffffffffffffffda RBX: 00007f2ccd5e5fa0 RCX: 00007f2ccd38f7c9
RDX: 0000000000000000 RSI: 000000000000ab03 RDI: 0000000000000003
RBP: 00007f2ccd413f91 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 00007f2ccd5e5fa0 R14: 00007f2ccd5e5fa0 R15: 0000000000000002
 </TASK>
block nbd0: shutting down sockets