Linux LED subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: jiegu <jie.gu@leica-geosystems.com>
Cc: linux-leds@vger.kernel.org, lee@kernel.org
Subject: Re: [PATCH 1/2] leds: tps6131x: cancel torch refresh on flash mode switch
Date: Tue, 17 Nov 2026 07:04:17 +0000	[thread overview]
Message-ID: <sashiko-outbox-218290@kernel.org> (raw)
In-Reply-To: <20261117064328.4096518-2-jie.gu@leica-geosystems.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] leds: tps6131x: username handle used in Signed-off-by trailer
- [Medium] leds: tps6131x: torch_refresh_work race in
  tps6131x_strobe_set()
- [Low] leds: tps6131x: missing Fixes trailer for commit b338a2ae9b31
- [Low] leds: tps6131x: uncapitalized commit subject description
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Critical] leds: tps6131x: use-after-free in
  tps6131x_torch_refresh_handler() [1]
--

commit eca7ab46a5d8dad2ca9e23fede2225cbcb7e5c34
Author: gujie <jie.gu@leica-geosystems.com.cn>

> leds: tps6131x: cancel torch refresh on flash mode switch

[Severity: Low]
In the LED subsystem, the commit subject description after the prefix
should be capitalized:

  leds: tps6131x: Cancel torch refresh on flash mode switch

>
> The TPS6131X torch mode uses a delayed work item to refresh the watchdog
> timer and keep torch mode active when driven over I2C. If userspace enables
> torch and then the camera stack switches the LED into flash or external
> strobe mode, the pending refresh work can still run later and force the
> device back into torch mode.
>
> Cancel the pending torch refresh work before switching into flash mode from
> both the LED flash class strobe path and the V4L2 external strobe path.
>
> Signed-off-by: gujie <jie.gu@leica-geosystems.com.cn>

[Severity: High]
The Author header and Signed-off-by trailer use an account handle (gujie)
rather than a full real human name (for example, Jie Gu
<jie.gu@leica-geosystems.com.cn>), which is required for Developer's
Certificate of Origin compliance.

[Severity: Low]
Since this fixes a bug in the initial driver commit, should it include a
Fixes tag?

  Fixes: b338a2ae9b31 ("leds: tps6131x: Add support for Texas Instruments TPS6131X flash LED driver")

> diff --git a/drivers/leds/flash/leds-tps6131x.c b/drivers/leds/flash/leds-tps6131x.c
> index 7f2c3895e6e98..704c3e7dfcda0 100644
> --- a/drivers/leds/flash/leds-tps6131x.c
> +++ b/drivers/leds/flash/leds-tps6131x.c
> @@ -378,6 +378,7 @@ static int tps6131x_strobe_set(struct led_classdev_flash *fled_cdev, bool state)
>  	struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev);
>  	int ret;
>  
> +	cancel_delayed_work_sync(&tps6131x->torch_refresh_work);
>  	guard(mutex)(&tps6131x->lock);
>  
>  	ret = tps6131x_set_mode(tps6131x, state ? TPS6131X_MODE_FLASH : TPS6131X_MODE_SHUTDOWN,
> @@ -697,6 +698,7 @@ static int tps6131x_flash_external_strobe_set(struct v4l2_flash *v4l2_flash, boo
>  	struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
>  	struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev);
>  
> +	cancel_delayed_work_sync(&tps6131x->torch_refresh_work);
>  	guard(mutex)(&tps6131x->lock);
>  
>  	return tps6131x_set_mode(tps6131x, enable ? TPS6131X_MODE_FLASH : TPS6131X_MODE_SHUTDOWN,

[Severity: Medium]
Can tps6131x_brightness_set() still re-arm torch_refresh_work after
tps6131x_strobe_set() or tps6131x_flash_external_strobe_set() cancels it?

tps6131x_brightness_set() is registered as brightness_set_blocking, so
writing to the brightness sysfs attribute queues set_brightness_work and
returns before tps6131x_brightness_set() runs.  Inside
tps6131x_brightness_set(), torch_refresh_work is scheduled at the end of
the tps6131x->lock critical section after blocking I2C writes:

drivers/leds/flash/leds-tps6131x.c:tps6131x_brightness_set() {
    ...
	cancel_delayed_work_sync(&tps6131x->torch_refresh_work);
    ...
	guard(mutex)(&tps6131x->lock);
    ...
	if (brightness)
		schedule_delayed_work(&tps6131x->torch_refresh_work,
				      TPS6131X_TORCH_REFRESH_INTERVAL_JIFFIES);
}

Because the strobe callbacks call cancel_delayed_work_sync() before
acquiring tps6131x->lock, they can interleave with an in-flight
tps6131x_brightness_set():

CPU0 (leds_wq)                    CPU1 (strobe)
tps6131x_brightness_set()
  mutex_lock(&tps6131x->lock)
  tps6131x_set_mode(..., TORCH)
                                  tps6131x_strobe_set()
                                    cancel_delayed_work_sync()
                                    mutex_lock(&tps6131x->lock)
  schedule_delayed_work()
  mutex_unlock(&tps6131x->lock)
                                    tps6131x_set_mode(..., FLASH)
                                    mutex_unlock(&tps6131x->lock)
                                  tps6131x_torch_refresh_handler()
                                    tps6131x_set_mode(..., TORCH)
                                      <-- forces LED back into torch mode

When torch_refresh_work fires 10 seconds later,
tps6131x_torch_refresh_handler() unconditionally switches the controller
back to TPS6131X_MODE_TORCH and re-schedules itself.

[Severity: Critical]
This problem wasn't introduced by this patch, but can torch_refresh_work
cause a use-after-free in tps6131x_torch_refresh_handler() during device
removal [1]?

tps6131x_remove() calls cancel_delayed_work_sync() before returning, while
the LED class device is registered with
devm_led_classdev_flash_register_ext() and therefore stays exposed in
sysfs until devres runs after tps6131x_remove():

drivers/leds/flash/leds-tps6131x.c:tps6131x_remove() {
	struct tps6131x *tps6131x = i2c_get_clientdata(client);

	v4l2_flash_release(tps6131x->v4l2_flash);

	cancel_delayed_work_sync(&tps6131x->torch_refresh_work);
}

A sysfs brightness write during that window can re-arm torch_refresh_work:

CPU0                              CPU1
tps6131x_remove()
  cancel_delayed_work_sync()
                                  tps6131x_brightness_set()
                                    schedule_delayed_work()
devres_release_all()
  led_classdev_unregister()
  kfree(tps6131x)
                                  tps6131x_torch_refresh_handler()
                                    <-- use-after-free

The same issue occurs if tps6131x_v4l2_setup() fails in tps6131x_probe(),
which returns without canceling torch_refresh_work before devres frees
tps6131x.  Registering torch_refresh_work with devm_delayed_work_autocancel()
after devm_led_classdev_flash_register_ext() would ensure the work is
canceled after the LED class device is unregistered.

[1] https://sashiko.dev/#/bug/linux-fe5d04b6-6727-4f3e-ba44-48a355c973f5

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117064328.4096518-1-jie.gu@leica-geosystems.com?part=1

  reply	other threads:[~2026-11-17  7:04 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  6:43 [PATCH 0/2] leds: tps6131x: Fix torch-to-flash transitions jiegu
2026-11-17  6:43 ` [PATCH 1/2] leds: tps6131x: cancel torch refresh on flash mode switch jiegu
2026-11-17  7:04   ` sashiko-bot [this message]
2026-11-17  6:43 ` [PATCH 2/2] leds: tps6131x: clear torch brightness cache on flash takeover jiegu
2026-11-17  7:03   ` sashiko-bot

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=sashiko-outbox-218290@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=jie.gu@leica-geosystems.com \
    --cc=lee@kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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