Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] virtio: dcp_ipcc_handle_rx() conflates power ACK and resp_vq
  IRQs
- [Medium] virtio: unsafe restore in dcp_virtio_transport_suspend()
  error path
- [Medium] virtio: concurrent vring_interrupt() on resp_vq from two IRQ
  lines
- [Medium] virtio: missing synchronize_irq() in dcp_ipcc_synchronize()
- [Low] virtio: duplicated MMIO init in dcp_virtio_init_device()
--

commit 857816350f7692694cd906a59f8f5d13188d9138
Author: Jayant Shekhar <[email protected]>

virtio: Add Qualcomm VirtIO transport wrapper

This commit adds a Qualcomm DCP VirtIO transport driver that reuses
virtio_mmio_config_ops and integrates an IPCC mailbox client to send power
notifications and forward response virtqueue interrupts.

> diff --git a/drivers/virtio/virtio_qcom_dcp.c 
> b/drivers/virtio/virtio_qcom_dcp.c
> new file mode 100644
> index 0000000000000..a5443f2dbc4b0
> --- /dev/null
> +++ b/drivers/virtio/virtio_qcom_dcp.c
> @@ -0,0 +1,273 @@

[ ... ]

> +static int dcp_virtio_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
> +                            struct virtqueue *vqs[],
> +                            struct virtqueue_info vqs_info[],
> +                            struct irq_affinity *desc)
> +{
> +     struct virtio_mmio_device *mmio =
> +             container_of(vdev, struct virtio_mmio_device, vdev);
> +     struct dcp_virtio_transport *transport = to_dcp_virtio_transport(mmio);
> +     unsigned int i;
> +     int ret;
> +
> +     ret = virtio_mmio_config_ops.find_vqs(vdev, nvqs, vqs, vqs_info, desc);
> +     if (ret)
> +             return ret;
> +
> +     for (i = 0; i < nvqs; i++) {
> +             if (vqs[i] && vqs_info[i].name && !strcmp(vqs_info[i].name, 
> "resp")) {
> +                     dcp_ipcc_set_resp_vq(&transport->ipcc, vqs[i]);
> +                     break;
> +             }
> +     }
> +
> +     return 0;
> +}

[Severity: Medium]
Can vm_interrupt() and dcp_ipcc_handle_rx() run vring_interrupt() on
resp_vq concurrently across two IRQ lines?

virtio_mmio_config_ops.find_vqs() registers vm_interrupt() on IRQ 0 and
keeps every created virtqueue, including "resp", on vdev->vqs.  
dcp_virtio_find_vqs()
then also assigns that queue to transport->ipcc.resp_vq, which is serviced
on IRQ 1 by dcp_ipcc_power_notify_irq():

CPU0 (IRQ 0)                      CPU1 (IRQ 1)
vm_interrupt()                    dcp_ipcc_power_notify_irq()
  virtio_device_for_each_vq()       dcp_ipcc_handle_rx()
    vring_interrupt(irq, vq)          spin_lock_irqsave(&ipcc->state_lock)
      vq->vq.callback(&vq->vq)        vring_interrupt(0, ipcc->resp_vq)
                                        vq->vq.callback(&vq->vq)
                                          <-- concurrent callback execution

vring_interrupt() has no internal lock, so if both interrupts fire at the
same time while a used buffer is pending, resp_vq->callback() runs on two
CPUs simultaneously.

[ ... ]

> +static int dcp_virtio_init_device(struct dcp_virtio_transport *transport,
> +                               struct platform_device *pdev)
> +{
> +     struct virtio_mmio_device *mmio = &transport->mmio;
> +     unsigned long magic;
> +     int ret;
> +
> +     mmio->vdev.dev.parent = &pdev->dev;
> +     mmio->vdev.config = &transport->config_ops;
> +     mmio->pdev = pdev;
> +
> +     mmio->base = devm_platform_ioremap_resource(pdev, 0);
> +     if (IS_ERR(mmio->base))
> +             return PTR_ERR(mmio->base);
> +
> +     magic = readl(mmio->base + VIRTIO_MMIO_MAGIC_VALUE);
> +     if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) {
> +             dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic);
> +             return -ENODEV;
> +     }
> +
> +     mmio->version = readl(mmio->base + VIRTIO_MMIO_VERSION);
> +     if (mmio->version < 1 || mmio->version > 3) {
> +             dev_err(&pdev->dev, "Version %ld not supported!\n", 
> mmio->version);
> +             return -ENXIO;
> +     }
> +
> +     mmio->vdev.id.device = readl(mmio->base + VIRTIO_MMIO_DEVICE_ID);
> +     if (!mmio->vdev.id.device)
> +             return -ENODEV;
> +
> +     mmio->vdev.id.vendor = readl(mmio->base + VIRTIO_MMIO_VENDOR_ID);
> +
> +     if (mmio->version == 1) {
> +             writel(PAGE_SIZE, mmio->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
> +             ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
> +             if (!ret)
> +                     dma_set_coherent_mask(&pdev->dev,
> +                                           DMA_BIT_MASK(32 + PAGE_SHIFT));
> +     } else {
> +             ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
> +     }
> +     if (ret)
> +             ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
> +     if (ret)
> +             dev_warn(&pdev->dev,
> +                      "Failed to enable 64-bit or 32-bit DMA. Trying to 
> continue, but this might not work.\n");
> +
> +     return 0;
> +}

[Severity: Low]
This isn't a bug, but dcp_virtio_init_device() duplicates the MMIO register
validation and DMA mask setup from virtio_mmio_probe() in
drivers/virtio/virtio_mmio.c.  Since this patch already introduces
drivers/virtio/virtio_mmio.h to share struct virtio_mmio_device and
virtio_mmio_config_ops, could this initialization be factored into a shared
helper in virtio_mmio.c?

Also, the commit message says the transport implements MMIO configuration
access, feature negotiation, and reset, whereas it reuses
virtio_mmio_config_ops for those operations.

[ ... ]

> +#ifdef CONFIG_PM_SLEEP
> +static int dcp_virtio_transport_suspend(struct device *dev)
> +{
> +     struct dcp_virtio_transport *transport = dev_get_drvdata(dev);
> +     int ret;
> +
> +     ret = virtio_device_freeze(&transport->mmio.vdev);
> +     if (ret)
> +             return ret;
> +
> +     ret = dcp_ipcc_enable_irq_wake(&transport->ipcc);
> +     if (ret)
> +             goto err_restore;
> +
> +     ret = dcp_ipcc_send_power_notify(&transport->ipcc);
> +     if (ret) {
> +             dcp_ipcc_disable_irq_wake(&transport->ipcc);
> +             goto err_restore;
> +     }
> +
> +     return 0;
> +
> +err_restore:
> +     virtio_device_restore(&transport->mmio.vdev);
> +     return ret;
> +}

[Severity: Medium]
Is it safe for err_restore to call virtio_device_restore() directly after
dcp_ipcc_send_power_notify() fails?

If dcp_ipcc_send_power_notify() times out after mbox_send_message() has
already sent the sleep notification, DCP may already be powering down or
inaccessible when virtio_device_restore() accesses mmio->base to reset the
device and recreate virtqueues.  In addition, unlike
dcp_virtio_transport_resume(), err_restore does not write PAGE_SIZE to
VIRTIO_MMIO_GUEST_PAGE_SIZE when mmio->version == 1 before calling
virtio_device_restore().

> diff --git a/drivers/virtio/virtio_qcom_dcp_ipcc.c 
> b/drivers/virtio/virtio_qcom_dcp_ipcc.c
> new file mode 100644
> index 0000000000000..1d83bcc69d5cf
> --- /dev/null
> +++ b/drivers/virtio/virtio_qcom_dcp_ipcc.c
> @@ -0,0 +1,193 @@

[ ... ]

> +static void dcp_ipcc_handle_rx(struct dcp_ipcc *ipcc)
> +{
> +     unsigned long flags;
> +
> +     spin_lock_irqsave(&ipcc->state_lock, flags);
> +     if (ipcc->shutting_down) {
> +             spin_unlock_irqrestore(&ipcc->state_lock, flags);
> +             return;
> +     }
> +
> +     complete(&ipcc->power_notify_done);
> +     if (ipcc->resp_vq)
> +             vring_interrupt(0, ipcc->resp_vq);
> +
> +     spin_unlock_irqrestore(&ipcc->state_lock, flags);
> +}

[Severity: Medium]
Does dcp_ipcc_handle_rx() conflate power-notification acknowledgments with
response virtqueue interrupts during suspend and resume?

ipcc->resp_vq is only cleared in dcp_virtio_del_vqs() and
dcp_ipcc_deinit().  If the bound VirtIO driver does not call del_vqs() in
its freeze callback (for example, virtio_rtc keeps its virtqueues across
freeze and deletes them in restore), ipcc->resp_vq remains non-NULL while
the device is frozen.  When DCP acknowledges dcp_ipcc_send_power_notify()
during dcp_virtio_transport_suspend() (after virtio_device_freeze()) or
during dcp_virtio_transport_resume() (before virtio_device_restore()),
dcp_ipcc_handle_rx() invokes vring_interrupt(0, ipcc->resp_vq) on the
frozen device.

Conversely, because dcp_ipcc_send_power_notify() calls
reinit_completion(&ipcc->power_notify_done) while ipcc->power_notify_irq is
also used for response queue notifications, an in-flight response queue
interrupt arriving after reinit_completion() completes power_notify_done
and causes dcp_ipcc_send_power_notify() to return before DCP has
acknowledged the power transition.

[ ... ]

> +void dcp_ipcc_synchronize(struct dcp_ipcc *ipcc)
> +{
> +     unsigned long flags;
> +
> +     spin_lock_irqsave(&ipcc->state_lock, flags);
> +     spin_unlock_irqrestore(&ipcc->state_lock, flags);
> +}

[Severity: Medium]
Should dcp_ipcc_synchronize() call synchronize_irq(ipcc->power_notify_irq)
instead of only cycling ipcc->state_lock?

When virtio_reset_device() calls virtio_synchronize_cbs() ->
dcp_virtio_synchronize_cbs(), ipcc->shutting_down is still false and
ipcc->resp_vq is still set until del_vqs() is called later.  If
power_notify_irq has fired on another CPU (or woken its IRQ thread under
threadirqs or PREEMPT_RT) but has not yet acquired ipcc->state_lock, cycling
the lock returns immediately and the handler then enters
dcp_ipcc_handle_rx() and calls vring_interrupt(0, ipcc->resp_vq) after
virtio_reset_device() has returned:

CPU0                              CPU1 (<hardirq> / IRQ thread)
virtio_reset_device()
  dev->config->reset(dev)
                                  dcp_ipcc_power_notify_irq()
                                    dcp_ipcc_handle_rx()
  virtio_synchronize_cbs()
    dcp_virtio_synchronize_cbs()
      dcp_ipcc_synchronize()
        spin_lock_irqsave(&ipcc->state_lock)
        spin_unlock_irqrestore(&ipcc->state_lock)
                                      spin_lock_irqsave(&ipcc->state_lock)
                                      vring_interrupt(0, ipcc->resp_vq)
                                        <-- callback runs after reset

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/20261126-jshekhar-qcom-virtio-dcp-v1-0-7b49bda74...@oss.qualcomm.com?part=3

Reply via email to