Re: [PATCH v3 1/4] leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak

From: Lee Jones

Date: Tue Dec 01 2026 - 09:23:20 EST


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@xxxxxxxxxxxxxxxx/
> Signed-off-by: Fenglin Wu <fenglin.wu@xxxxxxxxxxxxxxxx>
> ---
> 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