[PATCH 7/7] power: supply: ingenic-battery: free scale buffer after use

Matteo Martelli posted 7 patches 1 month, 3 weeks ago
There is a newer version of this series
[PATCH 7/7] power: supply: ingenic-battery: free scale buffer after use
Posted by Matteo Martelli 1 month, 3 weeks ago
The iio_read_avail_channel_attribute() iio interface now allocates a
copy of the available info buffer that must be freed after use.

Signed-off-by: Matteo Martelli <matteomartelli3@gmail.com>
---
 drivers/power/supply/ingenic-battery.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/drivers/power/supply/ingenic-battery.c b/drivers/power/supply/ingenic-battery.c
index 0a40f425c27723ccec49985b8b5e14a737b6a7eb..fa6d6898f8722cc8e06a888a762a3edeb0474a6e 100644
--- a/drivers/power/supply/ingenic-battery.c
+++ b/drivers/power/supply/ingenic-battery.c
@@ -79,8 +79,10 @@ static int ingenic_battery_set_scale(struct ingenic_battery *bat)
 		dev_err(bat->dev, "Unable to read channel avail scale\n");
 		return ret;
 	}
-	if (ret != IIO_AVAIL_LIST || scale_type != IIO_VAL_FRACTIONAL_LOG2)
-		return -EINVAL;
+	if (ret != IIO_AVAIL_LIST || scale_type != IIO_VAL_FRACTIONAL_LOG2) {
+		ret = -EINVAL;
+		goto out;
+	}
 
 	max_mV = bat->info->voltage_max_design_uv / 1000;
 
@@ -99,7 +101,8 @@ static int ingenic_battery_set_scale(struct ingenic_battery *bat)
 
 	if (best_idx < 0) {
 		dev_err(bat->dev, "Unable to find matching voltage scale\n");
-		return -EINVAL;
+		ret = -EINVAL;
+		goto out;
 	}
 
 	/* Only set scale if there is more than one (fractional) entry */
@@ -109,10 +112,13 @@ static int ingenic_battery_set_scale(struct ingenic_battery *bat)
 						  scale_raw[best_idx + 1],
 						  IIO_CHAN_INFO_SCALE);
 		if (ret)
-			return ret;
+			goto out;
 	}
 
-	return 0;
+	ret = 0;
+out:
+	kfree(scale_raw);
+	return ret;
 }
 
 static enum power_supply_property ingenic_battery_properties[] = {

-- 
2.46.2
Re: [PATCH 7/7] power: supply: ingenic-battery: free scale buffer after use
Posted by kernel test robot 1 month, 3 weeks ago
Hi Matteo,

kernel test robot noticed the following build errors:

[auto build test ERROR on fec496684388685647652ab4213454fbabdab099]

url:    https://github.com/intel-lab-lkp/linux/commits/Matteo-Martelli/iio-core-add-read_avail_release_resource-callback-to-fix-race/20241004-013654
base:   fec496684388685647652ab4213454fbabdab099
patch link:    https://lore.kernel.org/r/20241003-iio-read-avail-release-v1-7-c70cc7d9c2e0%40gmail.com
patch subject: [PATCH 7/7] power: supply: ingenic-battery: free scale buffer after use
config: hexagon-allmodconfig (https://download.01.org/0day-ci/archive/20241005/202410050737.0PgqTuD1-lkp@intel.com/config)
compiler: clang version 20.0.0git (https://github.com/llvm/llvm-project fef3566a25ff0e34fb87339ba5e13eca17cec00f)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241005/202410050737.0PgqTuD1-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/202410050737.0PgqTuD1-lkp@intel.com/

All errors (new ones prefixed by >>):

>> drivers/power/supply/ingenic-battery.c:120:2: error: call to undeclared function 'kfree'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     120 |         kfree(scale_raw);
         |         ^
   1 error generated.


vim +/kfree +120 drivers/power/supply/ingenic-battery.c

    59	
    60	/* Set the most appropriate IIO channel voltage reference scale
    61	 * based on the battery's max voltage.
    62	 */
    63	static int ingenic_battery_set_scale(struct ingenic_battery *bat)
    64	{
    65		const int *scale_raw;
    66		int scale_len, scale_type, best_idx = -1, best_mV, max_raw, i, ret;
    67		u64 max_mV;
    68	
    69		ret = iio_read_max_channel_raw(bat->channel, &max_raw);
    70		if (ret) {
    71			dev_err(bat->dev, "Unable to read max raw channel value\n");
    72			return ret;
    73		}
    74	
    75		ret = iio_read_avail_channel_attribute(bat->channel, &scale_raw,
    76						       &scale_type, &scale_len,
    77						       IIO_CHAN_INFO_SCALE);
    78		if (ret < 0) {
    79			dev_err(bat->dev, "Unable to read channel avail scale\n");
    80			return ret;
    81		}
    82		if (ret != IIO_AVAIL_LIST || scale_type != IIO_VAL_FRACTIONAL_LOG2) {
    83			ret = -EINVAL;
    84			goto out;
    85		}
    86	
    87		max_mV = bat->info->voltage_max_design_uv / 1000;
    88	
    89		for (i = 0; i < scale_len; i += 2) {
    90			u64 scale_mV = (max_raw * scale_raw[i]) >> scale_raw[i + 1];
    91	
    92			if (scale_mV < max_mV)
    93				continue;
    94	
    95			if (best_idx >= 0 && scale_mV > best_mV)
    96				continue;
    97	
    98			best_mV = scale_mV;
    99			best_idx = i;
   100		}
   101	
   102		if (best_idx < 0) {
   103			dev_err(bat->dev, "Unable to find matching voltage scale\n");
   104			ret = -EINVAL;
   105			goto out;
   106		}
   107	
   108		/* Only set scale if there is more than one (fractional) entry */
   109		if (scale_len > 2) {
   110			ret = iio_write_channel_attribute(bat->channel,
   111							  scale_raw[best_idx],
   112							  scale_raw[best_idx + 1],
   113							  IIO_CHAN_INFO_SCALE);
   114			if (ret)
   115				goto out;
   116		}
   117	
   118		ret = 0;
   119	out:
 > 120		kfree(scale_raw);
   121		return ret;
   122	}
   123	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH 7/7] power: supply: ingenic-battery: free scale buffer after use
Posted by kernel test robot 1 month, 3 weeks ago
Hi Matteo,

kernel test robot noticed the following build errors:

[auto build test ERROR on fec496684388685647652ab4213454fbabdab099]

url:    https://github.com/intel-lab-lkp/linux/commits/Matteo-Martelli/iio-core-add-read_avail_release_resource-callback-to-fix-race/20241004-013654
base:   fec496684388685647652ab4213454fbabdab099
patch link:    https://lore.kernel.org/r/20241003-iio-read-avail-release-v1-7-c70cc7d9c2e0%40gmail.com
patch subject: [PATCH 7/7] power: supply: ingenic-battery: free scale buffer after use
config: m68k-allmodconfig (https://download.01.org/0day-ci/archive/20241005/202410050547.Pybj1FLp-lkp@intel.com/config)
compiler: m68k-linux-gcc (GCC) 14.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241005/202410050547.Pybj1FLp-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/202410050547.Pybj1FLp-lkp@intel.com/

All errors (new ones prefixed by >>):

   drivers/power/supply/ingenic-battery.c: In function 'ingenic_battery_set_scale':
>> drivers/power/supply/ingenic-battery.c:120:9: error: implicit declaration of function 'kfree'; did you mean 'kvfree'? [-Wimplicit-function-declaration]
     120 |         kfree(scale_raw);
         |         ^~~~~
         |         kvfree


vim +120 drivers/power/supply/ingenic-battery.c

    59	
    60	/* Set the most appropriate IIO channel voltage reference scale
    61	 * based on the battery's max voltage.
    62	 */
    63	static int ingenic_battery_set_scale(struct ingenic_battery *bat)
    64	{
    65		const int *scale_raw;
    66		int scale_len, scale_type, best_idx = -1, best_mV, max_raw, i, ret;
    67		u64 max_mV;
    68	
    69		ret = iio_read_max_channel_raw(bat->channel, &max_raw);
    70		if (ret) {
    71			dev_err(bat->dev, "Unable to read max raw channel value\n");
    72			return ret;
    73		}
    74	
    75		ret = iio_read_avail_channel_attribute(bat->channel, &scale_raw,
    76						       &scale_type, &scale_len,
    77						       IIO_CHAN_INFO_SCALE);
    78		if (ret < 0) {
    79			dev_err(bat->dev, "Unable to read channel avail scale\n");
    80			return ret;
    81		}
    82		if (ret != IIO_AVAIL_LIST || scale_type != IIO_VAL_FRACTIONAL_LOG2) {
    83			ret = -EINVAL;
    84			goto out;
    85		}
    86	
    87		max_mV = bat->info->voltage_max_design_uv / 1000;
    88	
    89		for (i = 0; i < scale_len; i += 2) {
    90			u64 scale_mV = (max_raw * scale_raw[i]) >> scale_raw[i + 1];
    91	
    92			if (scale_mV < max_mV)
    93				continue;
    94	
    95			if (best_idx >= 0 && scale_mV > best_mV)
    96				continue;
    97	
    98			best_mV = scale_mV;
    99			best_idx = i;
   100		}
   101	
   102		if (best_idx < 0) {
   103			dev_err(bat->dev, "Unable to find matching voltage scale\n");
   104			ret = -EINVAL;
   105			goto out;
   106		}
   107	
   108		/* Only set scale if there is more than one (fractional) entry */
   109		if (scale_len > 2) {
   110			ret = iio_write_channel_attribute(bat->channel,
   111							  scale_raw[best_idx],
   112							  scale_raw[best_idx + 1],
   113							  IIO_CHAN_INFO_SCALE);
   114			if (ret)
   115				goto out;
   116		}
   117	
   118		ret = 0;
   119	out:
 > 120		kfree(scale_raw);
   121		return ret;
   122	}
   123	

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