On 2022-06-16 06:42, Yong Wu wrote:
The mtk_iommu_mm_dts_parse will parse the smi larbs nodes. if the i+1
larb is parsed fail(return -EINVAL), we should of_node_put for the 0..i
larbs. In the fail path, one of_node_put matches with of_parse_phandle in
it.

Fixes: d2e9a1102cfc ("iommu/mediatek: Contain MM IOMMU flow with the MM TYPE")
Signed-off-by: Yong Wu <yong...@mediatek.com>
---
  drivers/iommu/mtk_iommu.c | 21 ++++++++++++++++-----
  1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/drivers/iommu/mtk_iommu.c b/drivers/iommu/mtk_iommu.c
index 3b2489e8a6dd..ab24078938bf 100644
--- a/drivers/iommu/mtk_iommu.c
+++ b/drivers/iommu/mtk_iommu.c
@@ -1071,12 +1071,12 @@ static int mtk_iommu_mm_dts_parse(struct device *dev, 
struct component_match **m
plarbdev = of_find_device_by_node(larbnode);
                if (!plarbdev) {
-                       of_node_put(larbnode);
-                       return -ENODEV;
+                       ret = -ENODEV;
+                       goto err_larbnode_put;
                }
                if (!plarbdev->dev.driver) {
-                       of_node_put(larbnode);
-                       return -EPROBE_DEFER;
+                       ret = -EPROBE_DEFER;
+                       goto err_larbnode_put;
                }
                data->larb_imu[id].dev = &plarbdev->dev;
@@ -1107,9 +1107,20 @@ static int mtk_iommu_mm_dts_parse(struct device *dev, struct component_match **m
                               DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME);
        if (!link) {
                dev_err(dev, "Unable to link %s.\n", 
dev_name(data->smicomm_dev));
-               return -EINVAL;
+               ret = -EINVAL;
+               goto err_larbnode_put;
        }
        return 0;
+
+err_larbnode_put:
+       while (i--) {
+               larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i);
+               if (larbnode && of_device_is_available(larbnode)) {
+                       of_node_put(larbnode);
+                       of_node_put(larbnode);
+               }

This looks a bit awkward - could we not just iterate through data->larb_imu and put dev->of_node for each valid dev?

Also, of_find_device_by_node() takes a reference on the struct device itself, so strictly we should be doing put_device() on those as well if we're bailing out.

Robin.

+       }
+       return ret;
  }
static int mtk_iommu_probe(struct platform_device *pdev)
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

Reply via email to