iommu: Mark dev_iommu_priv_set() with a lockdep
authorJason Gunthorpe <jgg@nvidia.com>
Thu, 7 Dec 2023 18:03:12 +0000 (14:03 -0400)
committerJoerg Roedel <jroedel@suse.de>
Tue, 12 Dec 2023 09:18:49 +0000 (10:18 +0100)
A perfect driver would only call dev_iommu_priv_set() from its probe
callback. We've made it functionally correct to call it from the of_xlate
by adding a lock around that call.

lockdep assert that iommu_probe_device_lock is held to discourage misuse.

Exclude PPC kernels with CONFIG_FSL_PAMU turned on because FSL_PAMU uses a
global static for its priv and abuses priv for its domain.

Remove the pointless stores of NULL, all these are on paths where the core
code will free dev->iommu after the op returns.

Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: Jerry Snitselaar <jsnitsel@redhat.com>
Tested-by: Hector Martin <marcan@marcan.st>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Link: https://lore.kernel.org/r/5-v2-16e4def25ebb+820-iommu_fwspec_p1_jgg@nvidia.com
Signed-off-by: Joerg Roedel <jroedel@suse.de>
drivers/iommu/amd/iommu.c
drivers/iommu/apple-dart.c
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
drivers/iommu/arm/arm-smmu/arm-smmu.c
drivers/iommu/intel/iommu.c
drivers/iommu/iommu.c
drivers/iommu/omap-iommu.c
include/linux/iommu.h

index fcc987f5d4edc3ae87335fceed57eb26d7ba9b00..8199c678c2dc2a3ce32cddb85f117620852f40ca 100644 (file)
@@ -551,8 +551,6 @@ static void amd_iommu_uninit_device(struct device *dev)
        if (dev_data->domain)
                detach_device(dev);
 
-       dev_iommu_priv_set(dev, NULL);
-
        /*
         * We keep dev_data around for unplugged devices and reuse it when the
         * device is re-plugged - not doing so would introduce a ton of races.
index ee05f4824bfad1d6515fd506e9c1c2fd6760e18f..56cfc33042e0b5f55df3d4ed7387d6ea6a72094f 100644 (file)
@@ -740,7 +740,6 @@ static void apple_dart_release_device(struct device *dev)
 {
        struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);
 
-       dev_iommu_priv_set(dev, NULL);
        kfree(cfg);
 }
 
index fc4317c25b6d535155a81a990b608bbfd5dbe57a..1855d3892b15f82eb4aeed0eb9ae05bb938feea4 100644 (file)
@@ -2695,7 +2695,6 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev)
 
 err_free_master:
        kfree(master);
-       dev_iommu_priv_set(dev, NULL);
        return ERR_PTR(ret);
 }
 
index 4d09c00478927465cac6e748b74d3243add1f8f8..adc7937fd8a3a3774002da551a109bd0373d8700 100644 (file)
@@ -1420,7 +1420,6 @@ static void arm_smmu_release_device(struct device *dev)
 
        arm_smmu_rpm_put(cfg->smmu);
 
-       dev_iommu_priv_set(dev, NULL);
        kfree(cfg);
 }
 
index 897159dba47de4f863f57b365bd025cda0c8cd57..511589341074f094a2ff497da605b6d0aee7012e 100644 (file)
@@ -4461,7 +4461,6 @@ static struct iommu_device *intel_iommu_probe_device(struct device *dev)
                ret = intel_pasid_alloc_table(dev);
                if (ret) {
                        dev_err(dev, "PASID table allocation failed\n");
-                       dev_iommu_priv_set(dev, NULL);
                        kfree(info);
                        return ERR_PTR(ret);
                }
@@ -4479,7 +4478,6 @@ static void intel_iommu_release_device(struct device *dev)
        dmar_remove_one_dev_info(dev);
        intel_pasid_free_table(dev);
        intel_iommu_debugfs_remove_dev(info);
-       dev_iommu_priv_set(dev, NULL);
        kfree(info);
        set_dma_ops(dev, NULL);
 }
index df58025c001b332db8117582de360d80fa42f3f5..68e648b55767060204a8f42d1927c09ebacad39a 100644 (file)
@@ -387,6 +387,15 @@ static u32 dev_iommu_get_max_pasids(struct device *dev)
        return min_t(u32, max_pasids, dev->iommu->iommu_dev->max_pasids);
 }
 
+void dev_iommu_priv_set(struct device *dev, void *priv)
+{
+       /* FSL_PAMU does something weird */
+       if (!IS_ENABLED(CONFIG_FSL_PAMU))
+               lockdep_assert_held(&iommu_probe_device_lock);
+       dev->iommu->priv = priv;
+}
+EXPORT_SYMBOL_GPL(dev_iommu_priv_set);
+
 /*
  * Init the dev->iommu and dev->iommu_group in the struct device and get the
  * driver probed
index c66b070841dd41e0c322f12515c7d8f919e5bd16..c9528065a59afac738a6f06ba89ef11c90082a72 100644 (file)
@@ -1719,7 +1719,6 @@ static void omap_iommu_release_device(struct device *dev)
        if (!dev->of_node || !arch_data)
                return;
 
-       dev_iommu_priv_set(dev, NULL);
        kfree(arch_data);
 
 }
index c6bbbe0901d0cc30d5d069b5ab8b05afadd75f75..3a556996fea7f505c44381a09294dcfbcace0adf 100644 (file)
@@ -850,10 +850,7 @@ static inline void *dev_iommu_priv_get(struct device *dev)
                return NULL;
 }
 
-static inline void dev_iommu_priv_set(struct device *dev, void *priv)
-{
-       dev->iommu->priv = priv;
-}
+void dev_iommu_priv_set(struct device *dev, void *priv);
 
 extern struct mutex iommu_probe_device_lock;
 int iommu_probe_device(struct device *dev);