Merge tag 'irq-urgent-2023-03-05' of git://git.kernel.org/pub/scm/linux/kernel/git...
[platform/kernel/linux-rpi.git] / kernel / irq / msi.c
index efd21b7..7a97bcb 100644 (file)
@@ -1109,14 +1109,35 @@ int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev,
        return 0;
 
 fail:
-       for (--virq; virq >= virq_base; virq--)
+       for (--virq; virq >= virq_base; virq--) {
+               msi_domain_depopulate_descs(dev, virq, 1);
                irq_domain_free_irqs_common(domain, virq, 1);
+       }
        msi_domain_free_descs(dev, &ctrl);
 unlock:
        msi_unlock_descs(dev);
        return ret;
 }
 
+void msi_domain_depopulate_descs(struct device *dev, int virq_base, int nvec)
+{
+       struct msi_ctrl ctrl = {
+               .domid  = MSI_DEFAULT_DOMAIN,
+               .first  = virq_base,
+               .last   = virq_base + nvec - 1,
+       };
+       struct msi_desc *desc;
+       struct xarray *xa;
+       unsigned long idx;
+
+       if (!msi_ctrl_valid(dev, &ctrl))
+               return;
+
+       xa = &dev->msi.data->__domains[ctrl.domid].store;
+       xa_for_each_range(xa, idx, desc, ctrl.first, ctrl.last)
+               desc->irq = 0;
+}
+
 /*
  * Carefully check whether the device can use reservation mode. If
  * reservation mode is enabled then the early activation will assign a
@@ -1627,3 +1648,30 @@ struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain)
 {
        return (struct msi_domain_info *)domain->host_data;
 }
+
+/**
+ * msi_device_has_isolated_msi - True if the device has isolated MSI
+ * @dev: The device to check
+ *
+ * Isolated MSI means that HW modeled by an irq_domain on the path from the
+ * initiating device to the CPU will validate that the MSI message specifies an
+ * interrupt number that the device is authorized to trigger. This must block
+ * devices from triggering interrupts they are not authorized to trigger.
+ * Currently authorization means the MSI vector is one assigned to the device.
+ *
+ * This is interesting for securing VFIO use cases where a rouge MSI (eg created
+ * by abusing a normal PCI MemWr DMA) must not allow the VFIO userspace to
+ * impact outside its security domain, eg userspace triggering interrupts on
+ * kernel drivers, a VM triggering interrupts on the hypervisor, or a VM
+ * triggering interrupts on another VM.
+ */
+bool msi_device_has_isolated_msi(struct device *dev)
+{
+       struct irq_domain *domain = dev_get_msi_domain(dev);
+
+       for (; domain; domain = domain->parent)
+               if (domain->flags & IRQ_DOMAIN_FLAG_ISOLATED_MSI)
+                       return true;
+       return arch_is_isolated_msi();
+}
+EXPORT_SYMBOL_GPL(msi_device_has_isolated_msi);