iommu: amd: Fix uninitialized dev_data on probe failure
Subsystems (2)
PATCH 1/1 iommu: amd: Fix uninitialized dev_data on probe failure
amd_iommu_probe_device() may jump to the out_err path when iommu_init_device() fails. In that case, dev_data has not been initialized yet, but the out_err path unconditionally dereferences dev_data, leading to an uninitialized pointer dereference.
The IOMMU core explicitly allows ->probe_device() callbacks to return ERR_PTR() on failure. Return immediately when device initialization fails instead of falling through the shared error path, which assumes that per-device IOMMU state exists.
This avoids dereferencing dev_data on error paths where the device was not successfully initialized.
---
Testing note:
Compile tested only.
Build and Analysis:
This patch was compiled against the configuration file reported by 0day CI in the above link (config: x86_64-randconfig-r073-20251215) using gcc version 15.2.0 (Ubuntu 15.2.0-4ubuntu4).
Static analysis was performed with Smatch to ensure the reported warning no longer reproduces after applying this fix.
Command using for testing:
~/project/smatch/smatch_scripts/kchecker ./drivers/iommu/amd/iommu.c
drivers/iommu/amd/iommu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +1 -1
@@ -2426,7 +2426,7 @@ static struct iommu_device *amd_iommu_probe_device(struct device *dev)dev_err (dev,"Failed to initialize - trying to proceed anyway\n" ); iommu_dev =ERR_PTR (ret);iommu_ignore_device (iommu, dev); - goto out_err; +return iommu_dev; }amd_iommu_set_pci_msi_domain (dev, iommu);
Signature · 1 line
2.51.0
Jörg Rödel
maintainerdrivers/iommu/amd/iommu.c static struct iommu_device *amd_iommu_probe_device(struct device *dev) · line 2430
Quoted 11 lines · Rakuram Eswaran
drivers/iommu/amd/iommu.c +1
-1@@ -2426,7 +2426,7 @@ static struct iommu_device *amd_iommu_probe_device(struct device *dev)dev_err (dev,"Failed to initialize - trying to proceed anyway\n" ); iommu_dev =ERR_PTR (ret);iommu_ignore_device (iommu, dev); - goto out_err; +return iommu_dev; }
This is not the right fix. It makes the function omit further initialization steps which are needed event when iommu_init_device() fails (which it only does on out-of-memory).
The right fix is to initialize dev_data to NULL and check for that value before dereferencing it further down to keep the current logic.
-Joerg
Vasant Hegde
maintainerQuoted 17 lines · Jörg Rödel
drivers/iommu/amd/iommu.c static struct iommu_device *amd_iommu_probe_device(struct device *dev) · line 2430
Quoted 11 lines · Rakuram Eswaran
drivers/iommu/amd/iommu.c +1
-1@@ -2426,7 +2426,7 @@ static struct iommu_device *amd_iommu_probe_device(struct device *dev)dev_err (dev,"Failed to initialize - trying to proceed anyway\n" ); iommu_dev =ERR_PTR (ret);iommu_ignore_device (iommu, dev); - goto out_err; +return iommu_dev; }This is not the right fix. It makes the function omit further initialization steps which are needed event when
iommu_init_device()fails (which it only does on out-of-memory).
Quoted 2 lines
The right fix is to initialize
dev_datato NULL and check for that value before dereferencing it further down to keep the current logic.
Ack. I think this function needs rewrite. PCIe device capability check/enablement is scattered between this one and iommu_init_device(). I will fix it as part of other PASID related fixes/cleanups.
I think for now below fix is good enough.
@Rakuram, @Joerg, let me know if you want me to send proper patch?
---
drivers/iommu/amd/iommu.c +0 -0
@@ -2983,8 +2983,6 @@ static struct iommu_device *amd_iommu_probe_device(struct
device *dev)
goto out_err;
}
-out_err:
-
iommu_completion_wait(iommu);
if (FEATURE_NUM_INT_REMAP_SUP_2K(amd_iommu_efr2))
@@ -2995,6 +2993,7 @@ static struct iommu_device *amd_iommu_probe_device(struct device *dev)
if (dev_is_pci(dev))
pci_prepare_ats(to_pci_dev(dev), PAGE_SHIFT);
+out_err:
return iommu_dev;
}
-Vasant
Hi Vasant,
Quoted 22 lines · Vasant Hegde
Quoted 17 lines · Jörg Rödel
drivers/iommu/amd/iommu.c static struct iommu_device *amd_iommu_probe_device(struct device *dev) · line 2430
Quoted 11 lines · Rakuram Eswaran
drivers/iommu/amd/iommu.c +1
-1@@ -2426,7 +2426,7 @@ static struct iommu_device *amd_iommu_probe_device(struct device *dev)dev_err (dev,"Failed to initialize - trying to proceed anyway\n" ); iommu_dev =ERR_PTR (ret);iommu_ignore_device (iommu, dev); - goto out_err; +return iommu_dev; }This is not the right fix. It makes the function omit further initialization steps which are needed event when
iommu_init_device()fails (which it only does on out-of-memory).Quoted 2 lines
The right fix is to initialize
dev_datato NULL and check for that value before dereferencing it further down to keep the current logic.
Quoted 8 lines
Ack. I think this function needs rewrite. PCIe device capability check/enablement is scattered between this one and
iommu_init_device(). I will fix it as part of other PASID related fixes/cleanups.I think for now below fix is good enough.
@Rakuram, @Joerg, let me know if you want me to send proper patch?
Ok.
Best Regards,
Rakuram
Vasant Hegde
maintainerHi Rakuram,
Quoted 29 lines · Rakuram Eswaran
Hi Vasant,
Quoted 26 lines · Vasant Hegde
Quoted 17 lines · Jörg Rödel
drivers/iommu/amd/iommu.c static struct iommu_device *amd_iommu_probe_device(struct device *dev) · line 2430
Quoted 11 lines · Rakuram Eswaran
drivers/iommu/amd/iommu.c +1
-1@@ -2426,7 +2426,7 @@ static struct iommu_device *amd_iommu_probe_device(struct device *dev)dev_err (dev,"Failed to initialize - trying to proceed anyway\n" ); iommu_dev =ERR_PTR (ret);iommu_ignore_device (iommu, dev); - goto out_err; +return iommu_dev; }This is not the right fix. It makes the function omit further initialization steps which are needed event when
iommu_init_device()fails (which it only does on out-of-memory).Quoted 2 lines
The right fix is to initialize
dev_datato NULL and check for that value before dereferencing it further down to keep the current logic.Ack. I think this function needs rewrite. PCIe device capability check/enablement is scattered between this one and
iommu_init_device(). I will fix it as part of other PASID related fixes/cleanups.
Quoted 6 lines
Quoted 4 lines
I think for now below fix is good enough.
@Rakuram, @Joerg, let me know if you want me to send proper patch?
Ok.
Done. Thanks.
-Vasant