[PATCH] leds: lp55xx: Fix check for invalid channel number

Michal Vokáč posted 1 patch 1 month, 2 weeks ago
drivers/leds/leds-lp55xx-common.c | 11 ++++-------
1 file changed, 4 insertions(+), 7 deletions(-)
[PATCH] leds: lp55xx: Fix check for invalid channel number
Posted by Michal Vokáč 1 month, 2 weeks ago
Prior to commit 92a81562e695 ("leds: lp55xx: Add multicolor framework
support to lp55xx") the reg property (chan_nr) was parsed and stored
as it was. Then, in lp55xx_init_led() function, it was checked if it
is within valid range. In case it was not, an error message was
printed and the driver probe failed.

With the mentioned commit the reg property is checked right after it
is read from the device tree. Comparison to the upper range is not
correct though. Valid reg values are 0 to (max_channel - 1). So in
case the parsed value is out of this (wrong) range the probe just
fails and no error message is shown.

Fix it by using proper comparison and print a message in case of
an error. The check that is done in lp55xx_init_led() function is now
redundant and can be removed.

Fixes: 92a81562e695 ("leds: lp55xx: Add multicolor framework support to lp55xx")
Cc: <stable@vger.kernel.org>
Signed-off-by: Michal Vokáč <michal.vokac@ysoft.com>
---
 drivers/leds/leds-lp55xx-common.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
index 5a2e259679cf..055ee77455f9 100644
--- a/drivers/leds/leds-lp55xx-common.c
+++ b/drivers/leds/leds-lp55xx-common.c
@@ -512,12 +512,6 @@ static int lp55xx_init_led(struct lp55xx_led *led,
 	led->max_current = pdata->led_config[chan].max_current;
 	led->chan_nr = pdata->led_config[chan].chan_nr;
 
-	if (led->chan_nr >= max_channel) {
-		dev_err(dev, "Use channel numbers between 0 and %d\n",
-			max_channel - 1);
-		return -EINVAL;
-	}
-
 	if (pdata->led_config[chan].num_colors > 1)
 		ret = devm_led_classdev_multicolor_register(dev, &led->mc_cdev);
 	else
@@ -1132,8 +1126,11 @@ static int lp55xx_parse_common_child(struct device_node *np,
 	if (ret)
 		return ret;
 
-	if (*chan_nr < 0 || *chan_nr > cfg->max_channel)
+	if (*chan_nr < 0 || *chan_nr >= cfg->max_channel) {
+		dev_err(dev, "Use channel numbers between 0 and %d\n",
+			cfg->max_channel - 1);
 		return -EINVAL;
+	}
 
 	return 0;
 }
-- 
2.1.4

Re: [PATCH] leds: lp55xx: Fix check for invalid channel number
Posted by kernel test robot 1 month, 2 weeks ago
Hi Michal,

kernel test robot noticed the following build errors:

[auto build test ERROR on lee-leds/for-leds-next]
[also build test ERROR on linus/master v6.12-rc2 next-20241009]
[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/Michal-Vok/leds-lp55xx-Fix-check-for-invalid-channel-number/20241009-171340
base:   https://git.kernel.org/pub/scm/linux/kernel/git/lee/leds.git for-leds-next
patch link:    https://lore.kernel.org/r/1728464547-31722-1-git-send-email-michal.vokac%40ysoft.com
patch subject: [PATCH] leds: lp55xx: Fix check for invalid channel number
config: xtensa-randconfig-r071-20241010 (https://download.01.org/0day-ci/archive/20241010/202410101313.hQc9I8AL-lkp@intel.com/config)
compiler: xtensa-linux-gcc (GCC) 14.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241010/202410101313.hQc9I8AL-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/202410101313.hQc9I8AL-lkp@intel.com/

All errors (new ones prefixed by >>):

   In file included from include/linux/device.h:15,
                    from include/linux/acpi.h:14,
                    from include/linux/i2c.h:13,
                    from drivers/leds/leds-lp55xx-common.c:17:
   drivers/leds/leds-lp55xx-common.c: In function 'lp55xx_parse_common_child':
>> drivers/leds/leds-lp55xx-common.c:1130:25: error: 'dev' undeclared (first use in this function); did you mean 'cdev'?
    1130 |                 dev_err(dev, "Use channel numbers between 0 and %d\n",
         |                         ^~~
   include/linux/dev_printk.h:110:25: note: in definition of macro 'dev_printk_index_wrap'
     110 |                 _p_func(dev, fmt, ##__VA_ARGS__);                       \
         |                         ^~~
   drivers/leds/leds-lp55xx-common.c:1130:17: note: in expansion of macro 'dev_err'
    1130 |                 dev_err(dev, "Use channel numbers between 0 and %d\n",
         |                 ^~~~~~~
   drivers/leds/leds-lp55xx-common.c:1130:25: note: each undeclared identifier is reported only once for each function it appears in
    1130 |                 dev_err(dev, "Use channel numbers between 0 and %d\n",
         |                         ^~~
   include/linux/dev_printk.h:110:25: note: in definition of macro 'dev_printk_index_wrap'
     110 |                 _p_func(dev, fmt, ##__VA_ARGS__);                       \
         |                         ^~~
   drivers/leds/leds-lp55xx-common.c:1130:17: note: in expansion of macro 'dev_err'
    1130 |                 dev_err(dev, "Use channel numbers between 0 and %d\n",
         |                 ^~~~~~~


vim +1130 drivers/leds/leds-lp55xx-common.c

  1111	
  1112	static int lp55xx_parse_common_child(struct device_node *np,
  1113					     struct lp55xx_led_config *cfg,
  1114					     int led_number, int *chan_nr)
  1115	{
  1116		int ret;
  1117	
  1118		of_property_read_string(np, "chan-name",
  1119					&cfg[led_number].name);
  1120		of_property_read_u8(np, "led-cur",
  1121				    &cfg[led_number].led_current);
  1122		of_property_read_u8(np, "max-cur",
  1123				    &cfg[led_number].max_current);
  1124	
  1125		ret = of_property_read_u32(np, "reg", chan_nr);
  1126		if (ret)
  1127			return ret;
  1128	
  1129		if (*chan_nr < 0 || *chan_nr >= cfg->max_channel) {
> 1130			dev_err(dev, "Use channel numbers between 0 and %d\n",
  1131				cfg->max_channel - 1);
  1132			return -EINVAL;
  1133		}
  1134	
  1135		return 0;
  1136	}
  1137	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH] leds: lp55xx: Fix check for invalid channel number
Posted by Michal Vokáč 1 month, 2 weeks ago
On 10. 10. 24 8:00, kernel test robot wrote:

> vim +1130 drivers/leds/leds-lp55xx-common.c
> 
>    1111	
>    1112	static int lp55xx_parse_common_child(struct device_node *np,
>    1113					     struct lp55xx_led_config *cfg,
>    1114					     int led_number, int *chan_nr)
>    1115	{
>    1116		int ret;
>    1117	
>    1118		of_property_read_string(np, "chan-name",
>    1119					&cfg[led_number].name);
>    1120		of_property_read_u8(np, "led-cur",
>    1121				    &cfg[led_number].led_current);
>    1122		of_property_read_u8(np, "max-cur",
>    1123				    &cfg[led_number].max_current);
>    1124	
>    1125		ret = of_property_read_u32(np, "reg", chan_nr);
>    1126		if (ret)
>    1127			return ret;
>    1128	
>    1129		if (*chan_nr < 0 || *chan_nr >= cfg->max_channel) {
>> 1130			dev_err(dev, "Use channel numbers between 0 and %d\n",

Ahh, rookie mistake. Of course the dev is not available here. I feel dumb
as I think I at least compile tested this..

Anyway, the comparison is wrong and I still think it is not nice to
the user/DT developer to quietly fail here. I suggest to remove this
check here completely and keep the one in the lp55xx_init_led().

Michal

>    1131				cfg->max_channel - 1);
>    1132			return -EINVAL;
>    1133		}
>    1134	
>    1135		return 0;
>    1136	}
>    1137	
> 
Re: [PATCH] leds: lp55xx: Fix check for invalid channel number
Posted by Michal Vokáč 1 month, 2 weeks ago
+Cc Christian Marangi as I see he contributed a lot to this driver recently.

I also see that Dan Murphy can not be reached on the ti.com e-mail as he
works for Abbott since 2021 according to linkedin..

On 09. 10. 24 11:02, Michal Vokáč wrote:
> Prior to commit 92a81562e695 ("leds: lp55xx: Add multicolor framework
> support to lp55xx") the reg property (chan_nr) was parsed and stored
> as it was. Then, in lp55xx_init_led() function, it was checked if it
> is within valid range. In case it was not, an error message was
> printed and the driver probe failed.
> 
> With the mentioned commit the reg property is checked right after it
> is read from the device tree. Comparison to the upper range is not
> correct though. Valid reg values are 0 to (max_channel - 1). So in
> case the parsed value is out of this (wrong) range the probe just
> fails and no error message is shown.
> 
> Fix it by using proper comparison and print a message in case of
> an error. The check that is done in lp55xx_init_led() function is now
> redundant and can be removed.
> 
> Fixes: 92a81562e695 ("leds: lp55xx: Add multicolor framework support to lp55xx")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Michal Vokáč <michal.vokac@ysoft.com>
> ---
>   drivers/leds/leds-lp55xx-common.c | 11 ++++-------
>   1 file changed, 4 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
> index 5a2e259679cf..055ee77455f9 100644
> --- a/drivers/leds/leds-lp55xx-common.c
> +++ b/drivers/leds/leds-lp55xx-common.c
> @@ -512,12 +512,6 @@ static int lp55xx_init_led(struct lp55xx_led *led,
>   	led->max_current = pdata->led_config[chan].max_current;
>   	led->chan_nr = pdata->led_config[chan].chan_nr;
>   
> -	if (led->chan_nr >= max_channel) {
> -		dev_err(dev, "Use channel numbers between 0 and %d\n",
> -			max_channel - 1);
> -		return -EINVAL;
> -	}
> -
>   	if (pdata->led_config[chan].num_colors > 1)
>   		ret = devm_led_classdev_multicolor_register(dev, &led->mc_cdev);
>   	else
> @@ -1132,8 +1126,11 @@ static int lp55xx_parse_common_child(struct device_node *np,
>   	if (ret)
>   		return ret;
>   
> -	if (*chan_nr < 0 || *chan_nr > cfg->max_channel)
> +	if (*chan_nr < 0 || *chan_nr >= cfg->max_channel) {
> +		dev_err(dev, "Use channel numbers between 0 and %d\n",
> +			cfg->max_channel - 1);
>   		return -EINVAL;
> +	}
>   
>   	return 0;
>   }