From: Lee Jones <lee@kernel.org>
To: Fenglin Wu <fenglin.wu@oss.qualcomm.com>
Cc: linux-arm-msm@vger.kernel.org, "Pavel Machek" <pavel@kernel.org>,
"Griffin Kroah-Hartman" <griffin@kroah.com>,
"Marijn Suijten" <marijn.suijten@somainline.org>,
"Bjorn Andersson" <andersson@kernel.org>,
"Anjelique Melendez" <quic_amelende@quicinc.com>,
"Guru Das Srinagesh" <linux@gurudas.dev>,
"Uwe Kleine-König" <ukleinek@kernel.org>,
"Jonathan Corbet" <corbet@lwn.net>,
"Shuah Khan" <skhan@linuxfoundation.org>,
"Randy Dunlap" <rdunlap@infradead.org>,
"David Collins" <david.collins@oss.qualcomm.com>,
"Subbaraman Narayanamurthy"
<subbaraman.narayanamurthy@oss.qualcomm.com>,
"Kamal Wadhwa" <kamal.wadhwa@oss.qualcomm.com>,
"Pavel Machek" <pavel@ucw.cz>,
linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-pwm@vger.kernel.org, linux-doc@vger.kernel.org
Subject: Re: [PATCH v3 1/4] leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak
Date: Tue, 1 Dec 2026 14:23:10 +0000 [thread overview]
Message-ID: <179568565555.32537.11528409319338694015@kernel.org> (raw)
In-Reply-To: <20261122-led-qcom-leds-multiple-fixes-and-pwm-fm-support-v3-1-f911edd40a31@oss.qualcomm.com>
On Sun, 22 Nov 2026, Fenglin Wu wrote:
> The LUT resource cleanup path currently treats identical high and
> low indices as an unallocated range and skips clearing the bitmap
> allocation. However, with a 2-step palindrome pattern, the high
> and low indices are identical, which can leak the LUT resource
> because the bitmap allocation remains uncleared.
>
> Resolve this by tracking whether each channel uses a LUT separately
> from the index range. Mark every channel using the shared allocation,
> release the bitmap allocation once through the first channel, and clear
> the LUT state for all channels during cleanup.
>
> Also, avoid ping-pong compression for two-step patterns to prevent
> programming an unsafe single-entry LUT range.
>
> Fixes: 24e2d05d1b689 ("leds: Add driver for Qualcomm LPG")
> Closes: https://lore.kernel.org/linux-leds/7e474cdf-fcb2-468d-844d-f8afb5976d16@oss.qualcomm.com/
> Signed-off-by: Fenglin Wu <fenglin.wu@oss.qualcomm.com>
> ---
> drivers/leds/rgb/leds-qcom-lpg.c | 30 +++++++++++++++++++-----------
> 1 file changed, 19 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/leds/rgb/leds-qcom-lpg.c b/drivers/leds/rgb/leds-qcom-lpg.c
> index 24ffe079f221..5a63f3bc5dcf 100644
> --- a/drivers/leds/rgb/leds-qcom-lpg.c
> +++ b/drivers/leds/rgb/leds-qcom-lpg.c
> @@ -111,6 +111,7 @@ struct lpg_data;
> * @ramp_hi_pause_ms: pause (in milliseconds) after iterating over pattern
> * @pattern_lo_idx: start index of associated pattern
> * @pattern_hi_idx: last index of associated pattern
> + * @has_lut: channel uses the associated LUT allocation
> */
> struct lpg_channel {
> struct lpg *lpg;
> @@ -147,6 +148,7 @@ struct lpg_channel {
>
> unsigned int pattern_lo_idx;
> unsigned int pattern_hi_idx;
> + bool has_lut;
> };
>
> /**
> @@ -393,15 +395,19 @@ static int lpg_lut_store(struct lpg *lpg, struct led_pattern *pattern,
> return 0;
> }
>
> -static void lpg_lut_free(struct lpg *lpg, unsigned int lo_idx, unsigned int hi_idx)
> +static void lpg_lut_free(struct lpg_channel *chan)
> {
> + struct lpg *lpg = chan->lpg;
> int len;
>
> - len = hi_idx - lo_idx + 1;
> - if (len == 1)
> - return;
> + if (chan->has_lut) {
> + len = chan->pattern_hi_idx - chan->pattern_lo_idx + 1;
> + bitmap_clear(lpg->lut_bitmap, chan->pattern_lo_idx, len);
> + }
>
> - bitmap_clear(lpg->lut_bitmap, lo_idx, len);
> + chan->pattern_lo_idx = 0;
> + chan->pattern_hi_idx = 0;
> + chan->has_lut = false;
> }
>
> static int lpg_lut_sync(struct lpg *lpg, unsigned int mask)
> @@ -624,7 +630,7 @@ static void lpg_sdam_apply_lut_control(struct lpg_channel *chan)
> unsigned int hi_pause, lo_pause;
> struct lpg *lpg = chan->lpg;
>
> - if (!chan->ramp_enabled || chan->pattern_lo_idx == chan->pattern_hi_idx)
> + if (!chan->ramp_enabled || !chan->has_lut)
> return;
>
> hi_pause = DIV_ROUND_UP(chan->ramp_hi_pause_ms, chan->ramp_tick_ms);
> @@ -668,7 +674,7 @@ static void lpg_apply_lut_control(struct lpg_channel *chan)
> unsigned int hi_idx = chan->pattern_hi_idx;
> u16 step = chan->ramp_tick_ms;
>
> - if (!chan->ramp_enabled || chan->pattern_lo_idx == chan->pattern_hi_idx)
> + if (!chan->ramp_enabled || !chan->has_lut)
> return;
>
> hi_pause = DIV_ROUND_UP(chan->ramp_hi_pause_ms, step);
> @@ -709,7 +715,7 @@ static void lpg_apply_control(struct lpg_channel *chan)
> if (chan->enabled)
> ctrl |= LPG_ENABLE_CONTROL_OUTPUT;
>
> - if (chan->pattern_lo_idx != chan->pattern_hi_idx)
> + if (chan->has_lut)
> ctrl |= LPG_ENABLE_CONTROL_RAMP_GEN;
> else
> ctrl |= LPG_ENABLE_CONTROL_SRC_PWM;
> @@ -818,7 +824,7 @@ static void lpg_brightness_set(struct lpg_led *led, struct led_classdev *cdev,
> if (brightness == LED_OFF) {
> chan->enabled = false;
> chan->ramp_enabled = false;
> - } else if (chan->pattern_lo_idx != chan->pattern_hi_idx) {
> + } else if (chan->has_lut) {
> lpg_calc_freq(chan, NSEC_PER_MSEC);
> lpg_sdam_configure_triggers(chan, 1);
>
> @@ -1043,7 +1049,7 @@ static int lpg_pattern_set(struct lpg_led *led, struct led_pattern *led_pattern,
> */
>
> /* Detect palindromes and use "ping pong" to reduce LUT usage */
> - if (lpg->lut_base) {
> + if (lpg->lut_base && len > 2) {
A concern from AI:
Wait, if we are only detecting palindromes when 'len > 2', then 'ping_pong'
remains initialized to 'true' for 2-step patterns when 'lpg->lut_base' is set,
which still compresses 'actual_len' to 1 right below. Also, once 2-step
patterns are never compressed to a single LUT entry, is it ever possible for a
valid pattern to have 'pattern_lo_idx == pattern_hi_idx', or is the whole
'has_lut' addition in this patch unnecessary?
> for (i = 0; i < len / 2; i++) {
> brightness_a = pattern[i].brightness;
> brightness_b = pattern[len - i - 1].brightness;
> @@ -1118,6 +1124,7 @@ static int lpg_pattern_set(struct lpg_led *led, struct led_pattern *led_pattern,
>
> chan->pattern_lo_idx = lo_idx;
> chan->pattern_hi_idx = hi_idx;
> + chan->has_lut = true;
> }
>
> out_unlock:
> @@ -1176,7 +1183,7 @@ static int lpg_pattern_clear(struct lpg_led *led)
> mutex_lock(&lpg->lock);
>
> chan = led->channels[0];
> - lpg_lut_free(lpg, chan->pattern_lo_idx, chan->pattern_hi_idx);
> + lpg_lut_free(chan);
>
> for (i = 0; i < led->num_channels; i++) {
> chan = led->channels[i];
> @@ -1184,6 +1191,7 @@ static int lpg_pattern_clear(struct lpg_led *led)
> lpg_clear_pbs_trigger(chan->lpg, chan->lut_mask);
> chan->pattern_lo_idx = 0;
> chan->pattern_hi_idx = 0;
> + chan->has_lut = false;
> }
>
> mutex_unlock(&lpg->lock);
>
> --
> 2.43.0
>
>
--
Lee Jones
next prev parent reply other threads:[~2026-12-01 14:23 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 7:02 [PATCH v3 0/4] leds: leds-qcom-lpg: Add several fixes and the PWM frequency mode support Fenglin Wu
2026-11-23 7:02 ` [PATCH v3 1/4] leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak Fenglin Wu
2026-12-01 14:23 ` Lee Jones [this message]
2026-11-23 7:02 ` [PATCH v3 2/4] leds: rgb: leds-qcom-lpg: Fix LED color balancing in HW pattern mode Fenglin Wu
2026-12-01 14:24 ` Lee Jones
2026-11-23 7:02 ` [PATCH v3 3/4] leds: rgb: leds-qcom-lpg: Add mutex lock in lpg_pwm_get_state() Fenglin Wu
2026-11-23 7:02 ` [PATCH v3 4/4] leds: rgb: leds-qcom-lpg: Add PWM frequency mode support Fenglin Wu
2026-12-01 14:30 ` Lee Jones
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=179568565555.32537.11528409319338694015@kernel.org \
--to=lee@kernel.org \
--cc=andersson@kernel.org \
--cc=corbet@lwn.net \
--cc=david.collins@oss.qualcomm.com \
--cc=fenglin.wu@oss.qualcomm.com \
--cc=griffin@kroah.com \
--cc=kamal.wadhwa@oss.qualcomm.com \
--cc=linux-arm-msm@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-leds@vger.kernel.org \
--cc=linux-pwm@vger.kernel.org \
--cc=linux@gurudas.dev \
--cc=marijn.suijten@somainline.org \
--cc=pavel@kernel.org \
--cc=pavel@ucw.cz \
--cc=quic_amelende@quicinc.com \
--cc=rdunlap@infradead.org \
--cc=skhan@linuxfoundation.org \
--cc=subbaraman.narayanamurthy@oss.qualcomm.com \
--cc=ukleinek@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