From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796221005; b=gsd0cQ3qI3U6ySvMTRQzdCRQ9Zq9uWH18Pb41P/zylfc36zJzFFxKUsCVdJSicpwqdoo F36KjwVvgbeACz7Vb8UugTMoVN+ZO5JWsCAWHuvLyN5IWdkwntr9nqMYOcfL2sXLVCw61 WbUqrRqdX4o0bu0jkhqnjPbFZzLkafWiDrKx5O36NI7osmLYDT3ISqfM5gR+5epsDcuEf HfyfRE3+4+CrN4oYqHSCRconkkXtdq+RZk5SIth7TFq1bzf9WU4oK8IiuqUeC+4uJ3AI1 PtleGmXL1ZOGuVBePQ2lcDWL4zKLSWjJ6zKjsTFzgfkjfunaJgDAwYmbhtiwaLCqcVg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796221005; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To: X-AI-Review-Draft; bh=nwyS5uf3fye1EWmEdT3jodJraPQDTCsMg8HeD94+3o0=; b=Lwkk3cWjLujx50hG4VBA++ZBty9WJDOpRUy0RvzFQHQnEw2bbtwQbY/a89IQ7YvpEXq1 H4hgvXMEszcxfxmsckVSrwtCDQTn7fK6dgM8hVyo1muA1lTzz3pbnlwswD5n47AoNgu3P u/VOwuPnLRCrNkHbXudcTTeIOaMgrAC+X9HvxnDHO+cB9+YxBZa6HwLWjq1gFtoLqIoGJ BI5oZb5eMfylaSGfZ6DdsaAH5oeMBMkFPfckvIkvCQM6I491GH/8/9r7VlKkLjVeRdJL1 J1XLulOJzLcKcWW3/u6zTNEjzaXj6DR4/NIkXBMp/WK0Q2muEH/8n6s5syPg3Zbwp5w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=RkZOHL+7; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E8056606A332; Wed, 2 Dec 2026 14:16:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=RkZOHL+7 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E4D331F00929; Wed, 2 Dec 2026 14:16:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796221004; bh=nwyS5uf3fye1EWmEdT3jodJraPQDTCsMg8HeD94+3o0=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=RkZOHL+745sLcT+E82DSppfjv4ikOYKuMvi9BU3XaDHOO6fMty1I4JD7qXJ8FPEKZ P5PHdUPdHMXIda8X1O378QFb2iS7zV7MtsGms6vC3Ro71LTbMyjzzcNUxeZMnb28uP ZOB0ZwtVCLCRVMqgy5r4JRyWB9APjq3SiqzxEU5DDIRaMvtJvQBMAMgiDDEZ0oiz6v Jn8qm35qtHfhuYwf01dyA8L4MwVCLebe6AWp3s55S6WPnOXOq1GGMKj/H9EvWXyoCv a6v0Dvxu/7DnqL8EILToGUUrKJTiGPdWmtkpMQfQ/YG6MU8bWPPVVRnkF5yPvqfsMO 8QPeJJgLwNV/w== Date: Wed, 2 Dec 2026 14:16:40 +0000 From: Lee Jones To: Cong Nguyen Cc: Pavel Machek , =?iso-8859-1?Q?Andr=E9?= Apitzsch , linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] leds: rgb: ktd202x: allocate the LED array for the chip's LED count Message-ID: <179568526384.32537.11533501956407076839@kernel.org> References: <20261125125719.2565828-1-congnt264@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261125125719.2565828-1-congnt264@gmail.com> X-AI-Review-Draft: <20261125125719.2565828-1-congnt264@gmail.com> 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 > --- > 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