Merge branch 'fix-usage-counter-leak-by-adding-a-general-sync-ops'
authorJakub Kicinski <kuba@kernel.org>
Mon, 16 Nov 2020 17:37:13 +0000 (09:37 -0800)
committerJakub Kicinski <kuba@kernel.org>
Mon, 16 Nov 2020 17:37:13 +0000 (09:37 -0800)
Zhang Qilong says:

====================
Fix usage counter leak by adding a general sync ops

In many case, we need to check return value of pm_runtime_get_sync,
but it brings a trouble to the usage counter processing. Many callers
forget to decrease the usage counter when it failed, which could
resulted in reference leak. It has been discussed a lot[0][1]. So we
add a function to deal with the usage counter for better coding and
view. Then, we replace pm_runtime_resume_and_get with it in fec_main.c
to avoid it.

[0] https://lkml.org/lkml/2020/6/14/88
[1] https://patchwork.ozlabs.org/project/linux-tegra/list/?series=178139
====================

Link: https://lore.kernel.org/r/20201110092933.3342784-1-zhangqilong3@huawei.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
drivers/net/ethernet/freescale/fec_main.c
include/linux/pm_runtime.h

index d7919555250dd34fcf372c85dd1c03aa13d76d03..04f24c66cf3668113014208d8a2dedc76180f131 100644 (file)
@@ -1808,7 +1808,7 @@ static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
        int ret = 0, frame_start, frame_addr, frame_op;
        bool is_c45 = !!(regnum & MII_ADDR_C45);
 
-       ret = pm_runtime_get_sync(dev);
+       ret = pm_runtime_resume_and_get(dev);
        if (ret < 0)
                return ret;
 
@@ -1867,11 +1867,9 @@ static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
        int ret, frame_start, frame_addr;
        bool is_c45 = !!(regnum & MII_ADDR_C45);
 
-       ret = pm_runtime_get_sync(dev);
+       ret = pm_runtime_resume_and_get(dev);
        if (ret < 0)
                return ret;
-       else
-               ret = 0;
 
        if (is_c45) {
                frame_start = FEC_MMFR_ST_C45;
@@ -2275,7 +2273,7 @@ static void fec_enet_get_regs(struct net_device *ndev,
        u32 i, off;
        int ret;
 
-       ret = pm_runtime_get_sync(dev);
+       ret = pm_runtime_resume_and_get(dev);
        if (ret < 0)
                return;
 
@@ -2976,7 +2974,7 @@ fec_enet_open(struct net_device *ndev)
        int ret;
        bool reset_again;
 
-       ret = pm_runtime_get_sync(&fep->pdev->dev);
+       ret = pm_runtime_resume_and_get(&fep->pdev->dev);
        if (ret < 0)
                return ret;
 
@@ -3770,7 +3768,7 @@ fec_drv_remove(struct platform_device *pdev)
        struct device_node *np = pdev->dev.of_node;
        int ret;
 
-       ret = pm_runtime_get_sync(&pdev->dev);
+       ret = pm_runtime_resume_and_get(&pdev->dev);
        if (ret < 0)
                return ret;
 
index 4b708f4e8eed9df4d7f98ddcc8482f97dba63d2f..b492ae00cc9088e61f61042a0c1b0fa576c6c18b 100644 (file)
@@ -386,6 +386,27 @@ static inline int pm_runtime_get_sync(struct device *dev)
        return __pm_runtime_resume(dev, RPM_GET_PUT);
 }
 
+/**
+ * pm_runtime_resume_and_get - Bump up usage counter of a device and resume it.
+ * @dev: Target device.
+ *
+ * Resume @dev synchronously and if that is successful, increment its runtime
+ * PM usage counter. Return 0 if the runtime PM usage counter of @dev has been
+ * incremented or a negative error code otherwise.
+ */
+static inline int pm_runtime_resume_and_get(struct device *dev)
+{
+       int ret;
+
+       ret = __pm_runtime_resume(dev, RPM_GET_PUT);
+       if (ret < 0) {
+               pm_runtime_put_noidle(dev);
+               return ret;
+       }
+
+       return 0;
+}
+
 /**
  * pm_runtime_put - Drop device usage counter and queue up "idle check" if 0.
  * @dev: Target device.