From: Lee Jones <lee@kernel.org>
To: Mert Seftali <mertsftl@gmail.com>
Cc: Pavel Machek <pavel@kernel.org>, Kees Cook <kees@kernel.org>,
"Gustavo A . R . Silva" <gustavoars@kernel.org>,
linux-leds@vger.kernel.org, linux-hardening@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] leds: pwm: Annotate leds[] with __counted_by()
Date: Thu, 2 Jul 2026 15:49:17 +0100 [thread overview]
Message-ID: <20260702144917.GR2108533@google.com> (raw)
In-Reply-To: <20260619162113.416864-1-mertsftl@gmail.com>
On Fri, 19 Jun 2026, Mert Seftali wrote:
> Add the __counted_by() attribute to the flexible array member leds[] in
> struct led_pwm_priv so the compiler and runtime (e.g. FORTIFY_SOURCE,
> UBSAN_BOUNDS) can bounds-check accesses against num_leds.
>
> For the annotation to be correct, num_leds must reflect the number of
> allocated elements before leds[] is accessed. The driver already
> allocates space for device_get_child_node_count() elements up front, so
> set num_leds to that count right after allocation and fill the array by
> explicit index, instead of incrementing num_leds as each LED is added.
>
> No functional change intended.
>
> Signed-off-by: Mert Seftali <mertsftl@gmail.com>
> ---
> Build-tested only (also with FORTIFY_SOURCE and UBSAN_BOUNDS enabled).
>
> drivers/leds/leds-pwm.c | 14 ++++++++------
> 1 file changed, 8 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/leds/leds-pwm.c b/drivers/leds/leds-pwm.c
> index 6c1f2f50ff85..4c99a07da576 100644
> --- a/drivers/leds/leds-pwm.c
> +++ b/drivers/leds/leds-pwm.c
> @@ -36,7 +36,7 @@ struct led_pwm_data {
>
> struct led_pwm_priv {
> int num_leds;
> - struct led_pwm_data leds[];
> + struct led_pwm_data leds[] __counted_by(num_leds);
> };
>
> static int led_pwm_set(struct led_classdev *led_cdev,
> @@ -82,9 +82,10 @@ static int led_pwm_default_brightness_get(struct fwnode_handle *fwnode,
>
> __attribute__((nonnull))
> static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
> - struct led_pwm *led, struct fwnode_handle *fwnode)
> + int idx, struct led_pwm *led,
> + struct fwnode_handle *fwnode)
> {
> - struct led_pwm_data *led_data = &priv->leds[priv->num_leds];
> + struct led_pwm_data *led_data = &priv->leds[idx];
> struct led_init_data init_data = { .fwnode = fwnode };
> int ret;
>
> @@ -167,14 +168,13 @@ static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
> }
> }
>
> - priv->num_leds++;
> return 0;
> }
>
> static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv)
> {
> struct led_pwm led;
> - int ret;
> + int ret, i = 0;
> device_for_each_child_node_scoped(dev, fwnode) {
> memset(&led, 0, sizeof(led));
> @@ -193,7 +193,7 @@ static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv)
>
> led.default_state = led_init_default_state_get(fwnode);
>
> - ret = led_pwm_add(dev, priv, &led, fwnode);
> + ret = led_pwm_add(dev, priv, i++, &led, fwnode);
Nit, why not keep the variable names the same rather than use the for
iterator? i's and j's are usually for throw-away variables, but this is
counting something real.
Or if you wanted to be even smarter, why not just pass the priv->leds[n]
that you want to operate on and increment num_leds here instead?
Less args == more better.
> if (ret)
> return ret;
> }
> @@ -217,6 +217,8 @@ static int led_pwm_probe(struct platform_device *pdev)
> if (!priv)
> return -ENOMEM;
>
> + priv->num_leds = count;
> +
> ret = led_pwm_create_fwnode(&pdev->dev, priv);
>
> if (ret)
> --
> 2.54.0
>
--
Lee Jones
next prev parent reply other threads:[~2026-07-02 14:49 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-19 16:21 [PATCH] leds: pwm: Annotate leds[] with __counted_by() Mert Seftali
2026-07-02 14:49 ` Lee Jones [this message]
2026-07-02 15:39 ` Mert Seftali
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260702144917.GR2108533@google.com \
--to=lee@kernel.org \
--cc=gustavoars@kernel.org \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-leds@vger.kernel.org \
--cc=mertsftl@gmail.com \
--cc=pavel@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox