[PATCH] [mfd] da9052: Add error handling for spi_setup in da9052_spi_probe

Haoran Liu posted 1 patch 2 years ago
drivers/mfd/da9052-spi.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
[PATCH] [mfd] da9052: Add error handling for spi_setup in da9052_spi_probe
Posted by Haoran Liu 2 years ago
This patch adds error handling for the spi_setup call. The need for this
error handling was identified through static analysis, which highlighted
the lack of proper handling for potential failures of spi_setup.
Previously, a failure in spi_setup could lead to unstable behavior of
the DA9052 device.

Although the error addressed by this patch may not occur in the current
environment, I still suggest implementing these error handling routines
if the function is not highly time-sensitive. As the environment evolves
or the code gets reused in different contexts, there's a possibility that
these errors might occur. In case you find this addition unnecessary, I
completely understand and respect your perspective. My intention was to
enhance the robustness of the code, but I acknowledge that practical
considerations and current functionality might not warrant this change
at this point.

Signed-off-by: Haoran Liu <liuhaoran14@163.com>
---
 drivers/mfd/da9052-spi.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/mfd/da9052-spi.c b/drivers/mfd/da9052-spi.c
index be5f2b34e18a..c32d5025a18f 100644
--- a/drivers/mfd/da9052-spi.c
+++ b/drivers/mfd/da9052-spi.c
@@ -22,6 +22,7 @@ static int da9052_spi_probe(struct spi_device *spi)
 	int ret;
 	const struct spi_device_id *id = spi_get_device_id(spi);
 	struct da9052 *da9052;
+	int ret;
 
 	da9052 = devm_kzalloc(&spi->dev, sizeof(struct da9052), GFP_KERNEL);
 	if (!da9052)
@@ -29,7 +30,11 @@ static int da9052_spi_probe(struct spi_device *spi)
 
 	spi->mode = SPI_MODE_0;
 	spi->bits_per_word = 8;
-	spi_setup(spi);
+	ret = spi_setup(spi);
+	if (ret) {
+		dev_err(&spi->dev, "spi_setup failed: %d\n", ret);
+		return ret;
+	}
 
 	da9052->dev = &spi->dev;
 	da9052->chip_irq = spi->irq;
-- 
2.17.1
Re: [PATCH] [mfd] da9052: Add error handling for spi_setup in da9052_spi_probe
Posted by kernel test robot 2 years ago
Hi Haoran,

kernel test robot noticed the following build errors:

[auto build test ERROR on lee-mfd/for-mfd-next]
[also build test ERROR on lee-leds/for-leds-next lee-mfd/for-mfd-fixes linus/master v6.7-rc4 next-20231205]
[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/Haoran-Liu/da9052-Add-error-handling-for-spi_setup-in-da9052_spi_probe/20231203-132410
base:   https://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd.git for-mfd-next
patch link:    https://lore.kernel.org/r/20231203052125.37334-1-liuhaoran14%40163.com
patch subject: [PATCH] [mfd] da9052: Add error handling for spi_setup in da9052_spi_probe
config: x86_64-randconfig-123-20231203 (https://download.01.org/0day-ci/archive/20231206/202312060001.AnSLILR4-lkp@intel.com/config)
compiler: clang version 16.0.4 (https://github.com/llvm/llvm-project.git ae42196bc493ffe877a7e3dff8be32035dea4d07)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231206/202312060001.AnSLILR4-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202312060001.AnSLILR4-lkp@intel.com/

All errors (new ones prefixed by >>):

>> drivers/mfd/da9052-spi.c:25:6: error: redefinition of 'ret'
           int ret;
               ^
   drivers/mfd/da9052-spi.c:22:6: note: previous definition is here
           int ret;
               ^
   1 error generated.


vim +/ret +25 drivers/mfd/da9052-spi.c

    18	
    19	static int da9052_spi_probe(struct spi_device *spi)
    20	{
    21		struct regmap_config config;
    22		int ret;
    23		const struct spi_device_id *id = spi_get_device_id(spi);
    24		struct da9052 *da9052;
  > 25		int ret;
    26	
    27		da9052 = devm_kzalloc(&spi->dev, sizeof(struct da9052), GFP_KERNEL);
    28		if (!da9052)
    29			return -ENOMEM;
    30	
    31		spi->mode = SPI_MODE_0;
    32		spi->bits_per_word = 8;
    33		ret = spi_setup(spi);
    34		if (ret) {
    35			dev_err(&spi->dev, "spi_setup failed: %d\n", ret);
    36			return ret;
    37		}
    38	
    39		da9052->dev = &spi->dev;
    40		da9052->chip_irq = spi->irq;
    41	
    42		spi_set_drvdata(spi, da9052);
    43	
    44		config = da9052_regmap_config;
    45		config.read_flag_mask = 1;
    46		config.reg_bits = 7;
    47		config.pad_bits = 1;
    48		config.val_bits = 8;
    49		config.use_single_read = true;
    50		config.use_single_write = true;
    51	
    52		da9052->regmap = devm_regmap_init_spi(spi, &config);
    53		if (IS_ERR(da9052->regmap)) {
    54			ret = PTR_ERR(da9052->regmap);
    55			dev_err(&spi->dev, "Failed to allocate register map: %d\n",
    56				ret);
    57			return ret;
    58		}
    59	
    60		return da9052_device_init(da9052, id->driver_data);
    61	}
    62	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH] [mfd] da9052: Add error handling for spi_setup in da9052_spi_probe
Posted by Geert Uytterhoeven 2 years ago
On Sun, Dec 3, 2023 at 6:22 AM Haoran Liu <liuhaoran14@163.com> wrote:
> This patch adds error handling for the spi_setup call. The need for this
> error handling was identified through static analysis, which highlighted
> the lack of proper handling for potential failures of spi_setup.
> Previously, a failure in spi_setup could lead to unstable behavior of
> the DA9052 device.
>
> Although the error addressed by this patch may not occur in the current
> environment, I still suggest implementing these error handling routines
> if the function is not highly time-sensitive. As the environment evolves
> or the code gets reused in different contexts, there's a possibility that
> these errors might occur. In case you find this addition unnecessary, I
> completely understand and respect your perspective. My intention was to
> enhance the robustness of the code, but I acknowledge that practical
> considerations and current functionality might not warrant this change
> at this point.
>
> Signed-off-by: Haoran Liu <liuhaoran14@163.com>
> ---
>  drivers/mfd/da9052-spi.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/mfd/da9052-spi.c b/drivers/mfd/da9052-spi.c
> index be5f2b34e18a..c32d5025a18f 100644
> --- a/drivers/mfd/da9052-spi.c
> +++ b/drivers/mfd/da9052-spi.c
> @@ -22,6 +22,7 @@ static int da9052_spi_probe(struct spi_device *spi)
>         int ret;
>         const struct spi_device_id *id = spi_get_device_id(spi);
>         struct da9052 *da9052;
> +       int ret;

As reported by the kernel test robot:

    error: redeclaration of 'ret' with no linkage

your patch fails to build (again!).

Please stop submitting completely untested patches.
Thank you!

Gr{oetje,eeting}s,

                        Geert


--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Re: [PATCH] [mfd] da9052: Add error handling for spi_setup in da9052_spi_probe
Posted by kernel test robot 2 years ago
Hi Haoran,

kernel test robot noticed the following build errors:

[auto build test ERROR on lee-mfd/for-mfd-next]
[also build test ERROR on lee-leds/for-leds-next lee-mfd/for-mfd-fixes linus/master v6.7-rc4 next-20231205]
[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/Haoran-Liu/da9052-Add-error-handling-for-spi_setup-in-da9052_spi_probe/20231203-132410
base:   https://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd.git for-mfd-next
patch link:    https://lore.kernel.org/r/20231203052125.37334-1-liuhaoran14%40163.com
patch subject: [PATCH] [mfd] da9052: Add error handling for spi_setup in da9052_spi_probe
config: i386-buildonly-randconfig-004-20231203 (https://download.01.org/0day-ci/archive/20231205/202312051908.qxoHjGkx-lkp@intel.com/config)
compiler: gcc-7 (Ubuntu 7.5.0-6ubuntu2) 7.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231205/202312051908.qxoHjGkx-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202312051908.qxoHjGkx-lkp@intel.com/

All errors (new ones prefixed by >>):

   drivers/mfd/da9052-spi.c: In function 'da9052_spi_probe':
>> drivers/mfd/da9052-spi.c:25:6: error: redeclaration of 'ret' with no linkage
     int ret;
         ^~~
   drivers/mfd/da9052-spi.c:22:6: note: previous declaration of 'ret' was here
     int ret;
         ^~~


vim +/ret +25 drivers/mfd/da9052-spi.c

    18	
    19	static int da9052_spi_probe(struct spi_device *spi)
    20	{
    21		struct regmap_config config;
    22		int ret;
    23		const struct spi_device_id *id = spi_get_device_id(spi);
    24		struct da9052 *da9052;
  > 25		int ret;
    26	
    27		da9052 = devm_kzalloc(&spi->dev, sizeof(struct da9052), GFP_KERNEL);
    28		if (!da9052)
    29			return -ENOMEM;
    30	
    31		spi->mode = SPI_MODE_0;
    32		spi->bits_per_word = 8;
    33		ret = spi_setup(spi);
    34		if (ret) {
    35			dev_err(&spi->dev, "spi_setup failed: %d\n", ret);
    36			return ret;
    37		}
    38	
    39		da9052->dev = &spi->dev;
    40		da9052->chip_irq = spi->irq;
    41	
    42		spi_set_drvdata(spi, da9052);
    43	
    44		config = da9052_regmap_config;
    45		config.read_flag_mask = 1;
    46		config.reg_bits = 7;
    47		config.pad_bits = 1;
    48		config.val_bits = 8;
    49		config.use_single_read = true;
    50		config.use_single_write = true;
    51	
    52		da9052->regmap = devm_regmap_init_spi(spi, &config);
    53		if (IS_ERR(da9052->regmap)) {
    54			ret = PTR_ERR(da9052->regmap);
    55			dev_err(&spi->dev, "Failed to allocate register map: %d\n",
    56				ret);
    57			return ret;
    58		}
    59	
    60		return da9052_device_init(da9052, id->driver_data);
    61	}
    62	

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