iommu/vt-d: Fix lockdep splat in sva bind()/unbind()
authorLu Baolu <baolu.lu@linux.intel.com>
Thu, 31 Dec 2020 00:53:23 +0000 (08:53 +0800)
committerWill Deacon <will@kernel.org>
Thu, 7 Jan 2021 13:27:14 +0000 (13:27 +0000)
Lock(&iommu->lock) without disabling irq causes lockdep warnings.

========================================================
WARNING: possible irq lock inversion dependency detected
5.11.0-rc1+ #828 Not tainted
--------------------------------------------------------
kworker/0:1H/120 just changed the state of lock:
ffffffffad9ea1b8 (device_domain_lock){..-.}-{2:2}, at:
iommu_flush_dev_iotlb.part.0+0x32/0x120
but this lock took another, SOFTIRQ-unsafe lock in the past:
 (&iommu->lock){+.+.}-{2:2}

and interrupts could create inverse lock ordering between them.

other info that might help us debug this:
 Possible interrupt unsafe locking scenario:

       CPU0                    CPU1
       ----                    ----
  lock(&iommu->lock);
                               local_irq_disable();
                               lock(device_domain_lock);
                               lock(&iommu->lock);
  <Interrupt>
    lock(device_domain_lock);

 *** DEADLOCK ***

Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Link: https://lore.kernel.org/r/20201231005323.2178523-5-baolu.lu@linux.intel.com
Signed-off-by: Will Deacon <will@kernel.org>
drivers/iommu/intel/svm.c

index 4fa248b98031cde564692b9bc5642cae2aeda04e..9bcedd360235d75535810ea124533576265686af 100644 (file)
@@ -281,6 +281,7 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
        struct dmar_domain *dmar_domain;
        struct device_domain_info *info;
        struct intel_svm *svm = NULL;
+       unsigned long iflags;
        int ret = 0;
 
        if (WARN_ON(!iommu) || !data)
@@ -381,12 +382,12 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
         * each bind of a new device even with an existing PASID, we need to
         * call the nested mode setup function here.
         */
-       spin_lock(&iommu->lock);
+       spin_lock_irqsave(&iommu->lock, iflags);
        ret = intel_pasid_setup_nested(iommu, dev,
                                       (pgd_t *)(uintptr_t)data->gpgd,
                                       data->hpasid, &data->vendor.vtd, dmar_domain,
                                       data->addr_width);
-       spin_unlock(&iommu->lock);
+       spin_unlock_irqrestore(&iommu->lock, iflags);
        if (ret) {
                dev_err_ratelimited(dev, "Failed to set up PASID %llu in nested mode, Err %d\n",
                                    data->hpasid, ret);
@@ -486,6 +487,7 @@ intel_svm_bind_mm(struct device *dev, unsigned int flags,
        struct device_domain_info *info;
        struct intel_svm_dev *sdev;
        struct intel_svm *svm = NULL;
+       unsigned long iflags;
        int pasid_max;
        int ret;
 
@@ -605,14 +607,14 @@ intel_svm_bind_mm(struct device *dev, unsigned int flags,
                        }
                }
 
-               spin_lock(&iommu->lock);
+               spin_lock_irqsave(&iommu->lock, iflags);
                ret = intel_pasid_setup_first_level(iommu, dev,
                                mm ? mm->pgd : init_mm.pgd,
                                svm->pasid, FLPT_DEFAULT_DID,
                                (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) |
                                (cpu_feature_enabled(X86_FEATURE_LA57) ?
                                 PASID_FLAG_FL5LP : 0));
-               spin_unlock(&iommu->lock);
+               spin_unlock_irqrestore(&iommu->lock, iflags);
                if (ret) {
                        if (mm)
                                mmu_notifier_unregister(&svm->notifier, mm);
@@ -632,14 +634,14 @@ intel_svm_bind_mm(struct device *dev, unsigned int flags,
                 * Binding a new device with existing PASID, need to setup
                 * the PASID entry.
                 */
-               spin_lock(&iommu->lock);
+               spin_lock_irqsave(&iommu->lock, iflags);
                ret = intel_pasid_setup_first_level(iommu, dev,
                                                mm ? mm->pgd : init_mm.pgd,
                                                svm->pasid, FLPT_DEFAULT_DID,
                                                (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) |
                                                (cpu_feature_enabled(X86_FEATURE_LA57) ?
                                                PASID_FLAG_FL5LP : 0));
-               spin_unlock(&iommu->lock);
+               spin_unlock_irqrestore(&iommu->lock, iflags);
                if (ret) {
                        kfree(sdev);
                        goto out;