* [PATCH v2] leds: rgb: ktd202x: allocate the LED array for the chip's LED count
@ 2026-11-25 12:57 Cong Nguyen
2026-12-02 14:16 ` Lee Jones
0 siblings, 1 reply; 2+ messages in thread
From: Cong Nguyen @ 2026-11-25 12:57 UTC (permalink / raw)
To: Lee Jones, Pavel Machek; +Cc: André Apitzsch, linux-leds, linux-kernel
ktd202x_probe() allocates chip->leds[] with one entry per LED child node.
The allocation helper sets chip->num_leds to that count, but probe then
overwrites it with the number of LEDs the chip supports (3 for KTD2026,
4 for KTD2027).
If the firmware describes fewer LEDs than the chip supports,
ktd202x_chip_in_use() reads entries past the end of the allocation each
time a brightness is set.
Allocate the array with the number of LEDs the chip supports, and set
chip->num_leds right after the allocation, because the helper only sets
the counter on compilers that support __builtin_counted_by_ref. Entries
without a firmware node stay zeroed.
Fixes: 0ebdb7210943 ("leds: Add ktd202x driver")
Signed-off-by: Cong Nguyen <congnt264@gmail.com>
---
Changes in v2:
- Set chip->num_leds explicitly after the allocation. v1 relied on
devm_kzalloc_flex() to set it, but on compilers without a working
__builtin_counted_by_ref() the helper does not set the counter, so
num_leds stayed 0 and probe failed with -EINVAL.
v1: https://lore.kernel.org/r/20261124095707.3852724-1-congnt264@gmail.com
---
drivers/leds/rgb/leds-ktd202x.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/drivers/leds/rgb/leds-ktd202x.c b/drivers/leds/rgb/leds-ktd202x.c
index f1822281c3e6..bfb28c38ba14 100644
--- a/drivers/leds/rgb/leds-ktd202x.c
+++ b/drivers/leds/rgb/leds-ktd202x.c
@@ -533,6 +533,7 @@ static int ktd202x_probe(struct i2c_client *client)
{
struct device *dev = &client->dev;
struct ktd202x *chip;
+ unsigned long num_leds;
int count;
int ret;
@@ -540,10 +541,14 @@ static int ktd202x_probe(struct i2c_client *client)
if (!count || count > KTD202X_MAX_LEDS)
return dev_err_probe(dev, -EINVAL, "Incorrect number of leds (%d)", count);
- chip = devm_kzalloc_flex(dev, *chip, leds, count);
+ num_leds = (unsigned long)i2c_get_match_data(client);
+
+ chip = devm_kzalloc_flex(dev, *chip, leds, num_leds);
if (!chip)
return -ENOMEM;
+ chip->num_leds = num_leds;
+
chip->dev = dev;
i2c_set_clientdata(client, chip);
@@ -558,8 +563,6 @@ static int ktd202x_probe(struct i2c_client *client)
if (ret)
return ret;
- chip->num_leds = (unsigned long)i2c_get_match_data(client);
-
chip->regulators[0].supply = "vin";
chip->regulators[1].supply = "vio";
ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(chip->regulators), chip->regulators);
--
2.25.1
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH v2] leds: rgb: ktd202x: allocate the LED array for the chip's LED count
2026-11-25 12:57 [PATCH v2] leds: rgb: ktd202x: allocate the LED array for the chip's LED count Cong Nguyen
@ 2026-12-02 14:16 ` Lee Jones
0 siblings, 0 replies; 2+ messages in thread
From: Lee Jones @ 2026-12-02 14:16 UTC (permalink / raw)
To: Cong Nguyen; +Cc: Pavel Machek, André Apitzsch, linux-leds, linux-kernel
On Wed, 25 Nov 2026, Cong Nguyen wrote:
> ktd202x_probe() allocates chip->leds[] with one entry per LED child node.
> The allocation helper sets chip->num_leds to that count, but probe then
> overwrites it with the number of LEDs the chip supports (3 for KTD2026,
> 4 for KTD2027).
>
> If the firmware describes fewer LEDs than the chip supports,
> ktd202x_chip_in_use() reads entries past the end of the allocation each
> time a brightness is set.
>
> Allocate the array with the number of LEDs the chip supports, and set
> chip->num_leds right after the allocation, because the helper only sets
> the counter on compilers that support __builtin_counted_by_ref. Entries
> without a firmware node stay zeroed.
>
> Fixes: 0ebdb7210943 ("leds: Add ktd202x driver")
> Signed-off-by: Cong Nguyen <congnt264@gmail.com>
> ---
> Changes in v2:
> - Set chip->num_leds explicitly after the allocation. v1 relied on
> devm_kzalloc_flex() to set it, but on compilers without a working
> __builtin_counted_by_ref() the helper does not set the counter, so
> num_leds stayed 0 and probe failed with -EINVAL.
>
> v1: https://lore.kernel.org/r/20261124095707.3852724-1-congnt264@gmail.com
> ---
> drivers/leds/rgb/leds-ktd202x.c | 9 ++++++---
> 1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/leds/rgb/leds-ktd202x.c b/drivers/leds/rgb/leds-ktd202x.c
> index f1822281c3e6..bfb28c38ba14 100644
> --- a/drivers/leds/rgb/leds-ktd202x.c
> +++ b/drivers/leds/rgb/leds-ktd202x.c
> @@ -533,6 +533,7 @@ static int ktd202x_probe(struct i2c_client *client)
> {
> struct device *dev = &client->dev;
> struct ktd202x *chip;
> + unsigned long num_leds;
> int count;
> int ret;
>
> @@ -540,10 +541,14 @@ static int ktd202x_probe(struct i2c_client *client)
> if (!count || count > KTD202X_MAX_LEDS)
> return dev_err_probe(dev, -EINVAL, "Incorrect number of leds (%d)", count);
>
> - chip = devm_kzalloc_flex(dev, *chip, leds, count);
> + num_leds = (unsigned long)i2c_get_match_data(client);
> +
> + chip = devm_kzalloc_flex(dev, *chip, leds, num_leds);
Shouldn't the array only be as large as the number of LEDs actually
defined in the firmware ('count'), rather than over-allocating for
unused channels? Looking at the driver, 'chip->num_leds' is used both
as the hardware channel limit in ktd202x_add_led() and as the array
bound in ktd202x_chip_in_use() and ktd202x_probe_fw().
Why not keep 'chip->num_leds' as the actual number of allocated LEDs
('count'), and check the hardware limit against
i2c_get_match_data(client) during probe?
> if (!chip)
> return -ENOMEM;
>
> + chip->num_leds = num_leds;
> +
> chip->dev = dev;
> i2c_set_clientdata(client, chip);
>
> @@ -558,8 +563,6 @@ static int ktd202x_probe(struct i2c_client *client)
> if (ret)
> return ret;
>
> - chip->num_leds = (unsigned long)i2c_get_match_data(client);
> -
> chip->regulators[0].supply = "vin";
> chip->regulators[1].supply = "vio";
> ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(chip->regulators), chip->regulators);
> --
> 2.25.1
>
>
--
Lee Jones
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-02 14:16 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 12:57 [PATCH v2] leds: rgb: ktd202x: allocate the LED array for the chip's LED count Cong Nguyen
2026-12-02 14:16 ` Lee Jones
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®