i2c: omap: fix reference leak when pm_runtime_get_sync fails
authorQinglang Miao <miaoqinglang@huawei.com>
Tue, 1 Dec 2020 09:31:43 +0000 (17:31 +0800)
committerWolfram Sang <wsa@kernel.org>
Wed, 14 Apr 2021 07:49:17 +0000 (09:49 +0200)
The PM reference count is not expected to be incremented on
return in omap_i2c_probe() and omap_i2c_remove().

However, pm_runtime_get_sync will increment the PM reference
count even failed. Forgetting to putting operation will result
in a reference leak here. I Replace it with pm_runtime_resume_and_get
to keep usage counter balanced.

What's more, error path 'err_free_mem' seems not like a proper
name any more. So I change the name to err_disable_pm and move
pm_runtime_disable below, for pm_runtime of 'pdev->dev' should
be disabled when pm_runtime_resume_and_get fails.

Fixes: 3b0fb97c8dc4 ("I2C: OMAP: Handle error check for pm runtime")
Reported-by: Hulk Robot <hulkci@huawei.com>
Signed-off-by: Qinglang Miao <miaoqinglang@huawei.com>
Reviewed-by: Grygorii Strashko <grygorii.strashko@ti.com>
Reviewed-by: Vignesh Raghavendra <vigneshr@ti.com>
Signed-off-by: Wolfram Sang <wsa@kernel.org>
drivers/i2c/busses/i2c-omap.c

index 12ac421..d4f6c6d 100644 (file)
@@ -1404,9 +1404,9 @@ omap_i2c_probe(struct platform_device *pdev)
        pm_runtime_set_autosuspend_delay(omap->dev, OMAP_I2C_PM_TIMEOUT);
        pm_runtime_use_autosuspend(omap->dev);
 
-       r = pm_runtime_get_sync(omap->dev);
+       r = pm_runtime_resume_and_get(omap->dev);
        if (r < 0)
-               goto err_free_mem;
+               goto err_disable_pm;
 
        /*
         * Read the Rev hi bit-[15:14] ie scheme this is 1 indicates ver2.
@@ -1513,8 +1513,8 @@ err_unuse_clocks:
        omap_i2c_write_reg(omap, OMAP_I2C_CON_REG, 0);
        pm_runtime_dont_use_autosuspend(omap->dev);
        pm_runtime_put_sync(omap->dev);
+err_disable_pm:
        pm_runtime_disable(&pdev->dev);
-err_free_mem:
 
        return r;
 }
@@ -1525,7 +1525,7 @@ static int omap_i2c_remove(struct platform_device *pdev)
        int ret;
 
        i2c_del_adapter(&omap->adapter);
-       ret = pm_runtime_get_sync(&pdev->dev);
+       ret = pm_runtime_resume_and_get(&pdev->dev);
        if (ret < 0)
                return ret;