[PATCH] Regulator: stm32-pwr: fix of_iomap leak

YAN SHI posted 1 patch 2 years, 10 months ago
There is a newer version of this series
drivers/regulator/stm32-pwr.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
[PATCH] Regulator: stm32-pwr: fix of_iomap leak
Posted by YAN SHI 2 years, 10 months ago
Smatch reports:
drivers/regulator/stm32-pwr.c:166 stm32_pwr_regulator_probe() warn:
'base' from of_iomap() not released on lines: 151,166.

In stm32_pwr_regulator_probe(), base is not released
when devm_kzalloc() fails to allocate memory or
devm_regulator_register() fails to register a new regulator device, 
which may cause a leak.

To fix this issue, replace of_iomap() with 
devm_platform_ioremap_resource(). devm_platform_ioremap_resource()
is a specialized function for platform devices. 
It allows 'base' to be automatically released whether the probe
function succeeds or fails.

Besides, use IS_ERR(base) instead of !base 
as the return value of devm_platform_ioremap_resource() 
can either be a pointer to the remapped memory or 
an ERR_PTR() encoded error code if the operation fails.

Fixes: dc62f951a6a8 ("regulator: stm32-pwr: Fix return value check in stm32_pwr_regulator_probe()")
Signed-off-by: YAN SHI <m202071378@hust.edu.cn>
Reviewed-by: Dongliang Mu <dzm91@hust.edu.cn>
---
The issue is found by static analysis, and the patch remains untest.
---
 drivers/regulator/stm32-pwr.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/regulator/stm32-pwr.c b/drivers/regulator/stm32-pwr.c
index 2a42acb7c24e..1df64e3513c6 100644
--- a/drivers/regulator/stm32-pwr.c
+++ b/drivers/regulator/stm32-pwr.c
@@ -136,10 +136,10 @@ static int stm32_pwr_regulator_probe(struct platform_device *pdev)
 	struct regulator_config config = { };
 	int i, ret = 0;
 
-	base = of_iomap(np, 0);
-	if (!base) {
+	base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(base)) {
 		dev_err(&pdev->dev, "Unable to map IO memory\n");
-		return -ENOMEM;
+		return PTR_ERR(base);
 	}
 
 	config.dev = &pdev->dev;
-- 
2.34.1
Re: [PATCH] Regulator: stm32-pwr: fix of_iomap leak
Posted by kernel test robot 2 years, 10 months ago
Hi YAN,

kernel test robot noticed the following build warnings:

[auto build test WARNING on broonie-regulator/for-next]
[also build test WARNING on linus/master v6.3-rc6 next-20230411]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/YAN-SHI/Regulator-stm32-pwr-fix-of_iomap-leak/20230410-182833
base:   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-next
patch link:    https://lore.kernel.org/r/20230410102501.13246-1-m202071378%40hust.edu.cn
patch subject: [PATCH] Regulator: stm32-pwr: fix of_iomap leak
config: i386-buildonly-randconfig-r006-20230410 (https://download.01.org/0day-ci/archive/20230411/202304111750.o2643eJN-lkp@intel.com/config)
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/b1d07f5aa3b7411da258693d1860b56fe82b3777
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review YAN-SHI/Regulator-stm32-pwr-fix-of_iomap-leak/20230410-182833
        git checkout b1d07f5aa3b7411da258693d1860b56fe82b3777
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 olddefconfig
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash drivers/regulator/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>
| Link: https://lore.kernel.org/oe-kbuild-all/202304111750.o2643eJN-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> drivers/regulator/stm32-pwr.c:132:22: warning: unused variable 'np' [-Wunused-variable]
           struct device_node *np = pdev->dev.of_node;
                               ^
   1 warning generated.


vim +/np +132 drivers/regulator/stm32-pwr.c

6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  129  
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  130  static int stm32_pwr_regulator_probe(struct platform_device *pdev)
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  131  {
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15 @132  	struct device_node *np = pdev->dev.of_node;
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  133  	struct stm32_pwr_reg *priv;
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  134  	void __iomem *base;
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  135  	struct regulator_dev *rdev;
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  136  	struct regulator_config config = { };
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  137  	int i, ret = 0;
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  138  
b1d07f5aa3b741 YAN SHI            2023-04-10  139  	base = devm_platform_ioremap_resource(pdev, 0);
b1d07f5aa3b741 YAN SHI            2023-04-10  140  	if (IS_ERR(base)) {
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  141  		dev_err(&pdev->dev, "Unable to map IO memory\n");
b1d07f5aa3b741 YAN SHI            2023-04-10  142  		return PTR_ERR(base);
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  143  	}
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  144  
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  145  	config.dev = &pdev->dev;
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  146  
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  147  	for (i = 0; i < STM32PWR_REG_NUM_REGS; i++) {
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  148  		priv = devm_kzalloc(&pdev->dev, sizeof(struct stm32_pwr_reg),
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  149  				    GFP_KERNEL);
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  150  		if (!priv)
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  151  			return -ENOMEM;
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  152  		priv->base = base;
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  153  		priv->ready_mask = ready_mask_table[i];
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  154  		config.driver_data = priv;
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  155  
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  156  		rdev = devm_regulator_register(&pdev->dev,
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  157  					       &stm32_pwr_desc[i],
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  158  					       &config);
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  159  		if (IS_ERR(rdev)) {
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  160  			ret = PTR_ERR(rdev);
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  161  			dev_err(&pdev->dev,
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  162  				"Failed to register regulator: %d\n", ret);
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  163  			break;
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  164  		}
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  165  	}
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  166  	return ret;
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  167  }
6cdae8173f6771 Pascal PAILLET-LME 2019-04-15  168  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests