VMCI: Use threaded irqs instead of tasklets

The vmci_dispatch_dgs() tasklet function calls vmci_read_data()
which uses wait_event() resulting in invalid sleep in an atomic
context (and therefore potentially in a deadlock).

Use threaded irqs to fix this issue and completely remove usage
of tasklets.

[ 20.264639] BUG: sleeping function called from invalid context at drivers/misc/vmw_vmci/vmci_guest.c:145
[ 20.264643] in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 762, name: vmtoolsd
[ 20.264645] preempt_count: 101, expected: 0
[ 20.264646] RCU nest depth: 0, expected: 0
[ 20.264647] 1 lock held by vmtoolsd/762:
[ 20.264648] #0: ffff0000874ae440 (sk_lock-AF_VSOCK){+.+.}-{0:0}, at: vsock_connect+0x60/0x330 [vsock]
[ 20.264658] Preemption disabled at:
[ 20.264659] [<ffff80000151d7d8>] vmci_send_datagram+0x44/0xa0 [vmw_vmci]
[ 20.264665] CPU: 0 PID: 762 Comm: vmtoolsd Not tainted 5.19.0-0.rc8.20220727git39c3c396f813.60.fc37.aarch64 #1
[ 20.264667] Hardware name: VMware, Inc. VBSA/VBSA, BIOS VEFI 12/31/2020
[ 20.264668] Call trace:
[ 20.264669] dump_backtrace+0xc4/0x130
[ 20.264672] show_stack+0x24/0x80
[ 20.264673] dump_stack_lvl+0x88/0xb4
[ 20.264676] dump_stack+0x18/0x34
[ 20.264677] __might_resched+0x1a0/0x280
[ 20.264679] __might_sleep+0x58/0x90
[ 20.264681] vmci_read_data+0x74/0x120 [vmw_vmci]
[ 20.264683] vmci_dispatch_dgs+0x64/0x204 [vmw_vmci]
[ 20.264686] tasklet_action_common.constprop.0+0x13c/0x150
[ 20.264688] tasklet_action+0x40/0x50
[ 20.264689] __do_softirq+0x23c/0x6b4
[ 20.264690] __irq_exit_rcu+0x104/0x214
[ 20.264691] irq_exit_rcu+0x1c/0x50
[ 20.264693] el1_interrupt+0x38/0x6c
[ 20.264695] el1h_64_irq_handler+0x18/0x24
[ 20.264696] el1h_64_irq+0x68/0x6c
[ 20.264697] preempt_count_sub+0xa4/0xe0
[ 20.264698] _raw_spin_unlock_irqrestore+0x64/0xb0
[ 20.264701] vmci_send_datagram+0x7c/0xa0 [vmw_vmci]
[ 20.264703] vmci_datagram_dispatch+0x84/0x100 [vmw_vmci]
[ 20.264706] vmci_datagram_send+0x2c/0x40 [vmw_vmci]
[ 20.264709] vmci_transport_send_control_pkt+0xb8/0x120 [vmw_vsock_vmci_transport]
[ 20.264711] vmci_transport_connect+0x40/0x7c [vmw_vsock_vmci_transport]
[ 20.264713] vsock_connect+0x278/0x330 [vsock]
[ 20.264715] __sys_connect_file+0x8c/0xc0
[ 20.264718] __sys_connect+0x84/0xb4
[ 20.264720] __arm64_sys_connect+0x2c/0x3c
[ 20.264721] invoke_syscall+0x78/0x100
[ 20.264723] el0_svc_common.constprop.0+0x68/0x124
[ 20.264724] do_el0_svc+0x38/0x4c
[ 20.264725] el0_svc+0x60/0x180
[ 20.264726] el0t_64_sync_handler+0x11c/0x150
[ 20.264728] el0t_64_sync+0x190/0x194

Signed-off-by: Vishnu Dasa <vdasa@vmware.com>
Suggested-by: Zack Rusin <zackr@vmware.com>
Reported-by: Nadav Amit <namit@vmware.com>
Reported-by: Nathan Chancellor <nathan@kernel.org>
Tested-by: Nathan Chancellor <nathan@kernel.org>
Fixes: 463713eb6164 ("VMCI: dma dg: add support for DMA datagrams receive")
Cc: <stable@vger.kernel.org> # v5.18+
Cc: VMware PV-Drivers Reviewers <pv-drivers@vmware.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Bryan Tan <bryantan@vmware.com>
Reviewed-by: Bryan Tan <bryantan@vmware.com>
Reviewed-by: Zack Rusin <zackr@vmware.com>
Link: https://lore.kernel.org/r/20221130070511.46558-1-vdasa@vmware.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by Vishnu Dasa and committed by Greg Kroah-Hartman 3daed634 aaca766c

Changed files
+19 -30
drivers
misc
vmw_vmci
+19 -30
drivers/misc/vmw_vmci/vmci_guest.c
··· 56 56 57 57 bool exclusive_vectors; 58 58 59 - struct tasklet_struct datagram_tasklet; 60 - struct tasklet_struct bm_tasklet; 61 59 struct wait_queue_head inout_wq; 62 60 63 61 void *data_buffer; ··· 302 304 * This function assumes that it has exclusive access to the data 303 305 * in register(s) for the duration of the call. 304 306 */ 305 - static void vmci_dispatch_dgs(unsigned long data) 307 + static void vmci_dispatch_dgs(struct vmci_guest_device *vmci_dev) 306 308 { 307 - struct vmci_guest_device *vmci_dev = (struct vmci_guest_device *)data; 308 309 u8 *dg_in_buffer = vmci_dev->data_buffer; 309 310 struct vmci_datagram *dg; 310 311 size_t dg_in_buffer_size = VMCI_MAX_DG_SIZE; ··· 462 465 * Scans the notification bitmap for raised flags, clears them 463 466 * and handles the notifications. 464 467 */ 465 - static void vmci_process_bitmap(unsigned long data) 468 + static void vmci_process_bitmap(struct vmci_guest_device *dev) 466 469 { 467 - struct vmci_guest_device *dev = (struct vmci_guest_device *)data; 468 - 469 470 if (!dev->notification_bitmap) { 470 471 dev_dbg(dev->dev, "No bitmap present in %s\n", __func__); 471 472 return; ··· 481 486 struct vmci_guest_device *dev = _dev; 482 487 483 488 /* 484 - * If we are using MSI-X with exclusive vectors then we simply schedule 485 - * the datagram tasklet, since we know the interrupt was meant for us. 489 + * If we are using MSI-X with exclusive vectors then we simply call 490 + * vmci_dispatch_dgs(), since we know the interrupt was meant for us. 486 491 * Otherwise we must read the ICR to determine what to do. 487 492 */ 488 493 489 494 if (dev->exclusive_vectors) { 490 - tasklet_schedule(&dev->datagram_tasklet); 495 + vmci_dispatch_dgs(dev); 491 496 } else { 492 497 unsigned int icr; 493 498 ··· 497 502 return IRQ_NONE; 498 503 499 504 if (icr & VMCI_ICR_DATAGRAM) { 500 - tasklet_schedule(&dev->datagram_tasklet); 505 + vmci_dispatch_dgs(dev); 501 506 icr &= ~VMCI_ICR_DATAGRAM; 502 507 } 503 508 504 509 if (icr & VMCI_ICR_NOTIFICATION) { 505 - tasklet_schedule(&dev->bm_tasklet); 510 + vmci_process_bitmap(dev); 506 511 icr &= ~VMCI_ICR_NOTIFICATION; 507 512 } 508 513 ··· 531 536 struct vmci_guest_device *dev = _dev; 532 537 533 538 /* For MSI-X we can just assume it was meant for us. */ 534 - tasklet_schedule(&dev->bm_tasklet); 539 + vmci_process_bitmap(dev); 535 540 536 541 return IRQ_HANDLED; 537 542 } ··· 633 638 vmci_dev->iobase = iobase; 634 639 vmci_dev->mmio_base = mmio_base; 635 640 636 - tasklet_init(&vmci_dev->datagram_tasklet, 637 - vmci_dispatch_dgs, (unsigned long)vmci_dev); 638 - tasklet_init(&vmci_dev->bm_tasklet, 639 - vmci_process_bitmap, (unsigned long)vmci_dev); 640 641 init_waitqueue_head(&vmci_dev->inout_wq); 641 642 642 643 if (mmio_base != NULL) { ··· 799 808 * Request IRQ for legacy or MSI interrupts, or for first 800 809 * MSI-X vector. 801 810 */ 802 - error = request_irq(pci_irq_vector(pdev, 0), vmci_interrupt, 803 - IRQF_SHARED, KBUILD_MODNAME, vmci_dev); 811 + error = request_threaded_irq(pci_irq_vector(pdev, 0), NULL, 812 + vmci_interrupt, IRQF_SHARED, 813 + KBUILD_MODNAME, vmci_dev); 804 814 if (error) { 805 815 dev_err(&pdev->dev, "Irq %u in use: %d\n", 806 816 pci_irq_vector(pdev, 0), error); ··· 815 823 * between the vectors. 816 824 */ 817 825 if (vmci_dev->exclusive_vectors) { 818 - error = request_irq(pci_irq_vector(pdev, 1), 819 - vmci_interrupt_bm, 0, KBUILD_MODNAME, 820 - vmci_dev); 826 + error = request_threaded_irq(pci_irq_vector(pdev, 1), NULL, 827 + vmci_interrupt_bm, 0, 828 + KBUILD_MODNAME, vmci_dev); 821 829 if (error) { 822 830 dev_err(&pdev->dev, 823 831 "Failed to allocate irq %u: %d\n", ··· 825 833 goto err_free_irq; 826 834 } 827 835 if (caps_in_use & VMCI_CAPS_DMA_DATAGRAM) { 828 - error = request_irq(pci_irq_vector(pdev, 2), 829 - vmci_interrupt_dma_datagram, 830 - 0, KBUILD_MODNAME, vmci_dev); 836 + error = request_threaded_irq(pci_irq_vector(pdev, 2), 837 + NULL, 838 + vmci_interrupt_dma_datagram, 839 + 0, KBUILD_MODNAME, 840 + vmci_dev); 831 841 if (error) { 832 842 dev_err(&pdev->dev, 833 843 "Failed to allocate irq %u: %d\n", ··· 865 871 866 872 err_free_irq: 867 873 free_irq(pci_irq_vector(pdev, 0), vmci_dev); 868 - tasklet_kill(&vmci_dev->datagram_tasklet); 869 - tasklet_kill(&vmci_dev->bm_tasklet); 870 874 871 875 err_disable_msi: 872 876 pci_free_irq_vectors(pdev); ··· 934 942 } 935 943 free_irq(pci_irq_vector(pdev, 0), vmci_dev); 936 944 pci_free_irq_vectors(pdev); 937 - 938 - tasklet_kill(&vmci_dev->datagram_tasklet); 939 - tasklet_kill(&vmci_dev->bm_tasklet); 940 945 941 946 if (vmci_dev->notification_bitmap) { 942 947 /*