* [PATCH] leds: flash: leds-qcom-flash: Fix registry access after re-bind
@ 2025-05-28 19:44 Krzysztof Kozlowski
2025-05-29 3:11 ` Fenglin Wu
0 siblings, 1 reply; 3+ messages in thread
From: Krzysztof Kozlowski @ 2025-05-28 19:44 UTC (permalink / raw)
To: Lee Jones, Pavel Machek, Fenglin Wu, linux-leds, linux-kernel
Cc: Krzysztof Kozlowski, stable
Driver in probe() updates each of 'reg_field' with 'reg_base':
for (i = 0; i < REG_MAX_COUNT; i++)
regs[i].reg += reg_base;
'reg_field' array (under variable 'regs' above) is statically allocated,
this each re-bind would add another 'reg_base' leading to bogus
register addresses. Constify the local 'reg_field' array and duplicate
it in probe to solve this.
Fixes: 96a2e242a5dc ("leds: flash: Add driver to support flash LED module in QCOM PMICs")
Cc: <stable@vger.kernel.org>
Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
---
This is a nice example why constifying static memory is useful.
---
drivers/leds/flash/leds-qcom-flash.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/drivers/leds/flash/leds-qcom-flash.c b/drivers/leds/flash/leds-qcom-flash.c
index b4c19be51c4d..b8a48c15d797 100644
--- a/drivers/leds/flash/leds-qcom-flash.c
+++ b/drivers/leds/flash/leds-qcom-flash.c
@@ -117,7 +117,7 @@ enum {
REG_MAX_COUNT,
};
-static struct reg_field mvflash_3ch_regs[REG_MAX_COUNT] = {
+static const struct reg_field mvflash_3ch_regs[REG_MAX_COUNT] = {
REG_FIELD(0x08, 0, 7), /* status1 */
REG_FIELD(0x09, 0, 7), /* status2 */
REG_FIELD(0x0a, 0, 7), /* status3 */
@@ -132,7 +132,7 @@ static struct reg_field mvflash_3ch_regs[REG_MAX_COUNT] = {
REG_FIELD(0x58, 0, 2), /* therm_thrsh3 */
};
-static struct reg_field mvflash_4ch_regs[REG_MAX_COUNT] = {
+static const struct reg_field mvflash_4ch_regs[REG_MAX_COUNT] = {
REG_FIELD(0x06, 0, 7), /* status1 */
REG_FIELD(0x07, 0, 6), /* status2 */
REG_FIELD(0x09, 0, 7), /* status3 */
@@ -854,11 +854,17 @@ static int qcom_flash_led_probe(struct platform_device *pdev)
if (val == FLASH_SUBTYPE_3CH_PM8150_VAL || val == FLASH_SUBTYPE_3CH_PMI8998_VAL) {
flash_data->hw_type = QCOM_MVFLASH_3CH;
flash_data->max_channels = 3;
- regs = mvflash_3ch_regs;
+ regs = devm_kmemdup(dev, mvflash_3ch_regs, sizeof(mvflash_3ch_regs),
+ GFP_KERNEL);
+ if (!regs)
+ return -ENOMEM;
} else if (val == FLASH_SUBTYPE_4CH_VAL) {
flash_data->hw_type = QCOM_MVFLASH_4CH;
flash_data->max_channels = 4;
- regs = mvflash_4ch_regs;
+ regs = devm_kmemdup(dev, mvflash_4ch_regs, sizeof(mvflash_3ch_regs),
+ GFP_KERNEL);
+ if (!regs)
+ return -ENOMEM;
rc = regmap_read(regmap, reg_base + FLASH_REVISION_REG, &val);
if (rc < 0) {
@@ -880,6 +886,7 @@ static int qcom_flash_led_probe(struct platform_device *pdev)
dev_err(dev, "Failed to allocate regmap field, rc=%d\n", rc);
return rc;
}
+ devm_kfree(dev, regs); /* devm_regmap_field_bulk_alloc() makes copies */
platform_set_drvdata(pdev, flash_data);
mutex_init(&flash_data->lock);
--
2.45.2
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] leds: flash: leds-qcom-flash: Fix registry access after re-bind
2025-05-28 19:44 [PATCH] leds: flash: leds-qcom-flash: Fix registry access after re-bind Krzysztof Kozlowski
@ 2025-05-29 3:11 ` Fenglin Wu
2025-05-29 6:31 ` Krzysztof Kozlowski
0 siblings, 1 reply; 3+ messages in thread
From: Fenglin Wu @ 2025-05-29 3:11 UTC (permalink / raw)
To: Krzysztof Kozlowski, Lee Jones, Pavel Machek, linux-leds, linux-kernel
Cc: stable
On 5/29/2025 3:44 AM, Krzysztof Kozlowski wrote:
> Driver in probe() updates each of 'reg_field' with 'reg_base':
>
> for (i = 0; i < REG_MAX_COUNT; i++)
> regs[i].reg += reg_base;
>
> 'reg_field' array (under variable 'regs' above) is statically allocated,
> this each re-bind would add another 'reg_base' leading to bogus
> register addresses. Constify the local 'reg_field' array and duplicate
> it in probe to solve this.
>
> Fixes: 96a2e242a5dc ("leds: flash: Add driver to support flash LED module in QCOM PMICs")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
>
> ---
>
> This is a nice example why constifying static memory is useful.
Thanks for fixing it!
> ---
> drivers/leds/flash/leds-qcom-flash.c | 15 +++++++++++----
> 1 file changed, 11 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/leds/flash/leds-qcom-flash.c b/drivers/leds/flash/leds-qcom-flash.c
> index b4c19be51c4d..b8a48c15d797 100644
> --- a/drivers/leds/flash/leds-qcom-flash.c
> +++ b/drivers/leds/flash/leds-qcom-flash.c
> @@ -117,7 +117,7 @@ enum {
> REG_MAX_COUNT,
> };
>
> -static struct reg_field mvflash_3ch_regs[REG_MAX_COUNT] = {
> +static const struct reg_field mvflash_3ch_regs[REG_MAX_COUNT] = {
> REG_FIELD(0x08, 0, 7), /* status1 */
> REG_FIELD(0x09, 0, 7), /* status2 */
> REG_FIELD(0x0a, 0, 7), /* status3 */
> @@ -132,7 +132,7 @@ static struct reg_field mvflash_3ch_regs[REG_MAX_COUNT] = {
> REG_FIELD(0x58, 0, 2), /* therm_thrsh3 */
> };
>
> -static struct reg_field mvflash_4ch_regs[REG_MAX_COUNT] = {
> +static const struct reg_field mvflash_4ch_regs[REG_MAX_COUNT] = {
> REG_FIELD(0x06, 0, 7), /* status1 */
> REG_FIELD(0x07, 0, 6), /* status2 */
> REG_FIELD(0x09, 0, 7), /* status3 */
> @@ -854,11 +854,17 @@ static int qcom_flash_led_probe(struct platform_device *pdev)
> if (val == FLASH_SUBTYPE_3CH_PM8150_VAL || val == FLASH_SUBTYPE_3CH_PMI8998_VAL) {
> flash_data->hw_type = QCOM_MVFLASH_3CH;
> flash_data->max_channels = 3;
> - regs = mvflash_3ch_regs;
> + regs = devm_kmemdup(dev, mvflash_3ch_regs, sizeof(mvflash_3ch_regs),
> + GFP_KERNEL);
> + if (!regs)
> + return -ENOMEM;
> } else if (val == FLASH_SUBTYPE_4CH_VAL) {
> flash_data->hw_type = QCOM_MVFLASH_4CH;
> flash_data->max_channels = 4;
> - regs = mvflash_4ch_regs;
> + regs = devm_kmemdup(dev, mvflash_4ch_regs, sizeof(mvflash_3ch_regs),
Minor: sizeof(mvflash_4ch_regs)
> + GFP_KERNEL);
> + if (!regs)
> + return -ENOMEM;
>
> rc = regmap_read(regmap, reg_base + FLASH_REVISION_REG, &val);
> if (rc < 0) {
> @@ -880,6 +886,7 @@ static int qcom_flash_led_probe(struct platform_device *pdev)
> dev_err(dev, "Failed to allocate regmap field, rc=%d\n", rc);
> return rc;
> }
> + devm_kfree(dev, regs); /* devm_regmap_field_bulk_alloc() makes copies */
>
> platform_set_drvdata(pdev, flash_data);
> mutex_init(&flash_data->lock);
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] leds: flash: leds-qcom-flash: Fix registry access after re-bind
2025-05-29 3:11 ` Fenglin Wu
@ 2025-05-29 6:31 ` Krzysztof Kozlowski
0 siblings, 0 replies; 3+ messages in thread
From: Krzysztof Kozlowski @ 2025-05-29 6:31 UTC (permalink / raw)
To: Fenglin Wu, Lee Jones, Pavel Machek, linux-leds, linux-kernel; +Cc: stable
On 29/05/2025 05:11, Fenglin Wu wrote:
>> -static struct reg_field mvflash_4ch_regs[REG_MAX_COUNT] = {
>> +static const struct reg_field mvflash_4ch_regs[REG_MAX_COUNT] = {
>> REG_FIELD(0x06, 0, 7), /* status1 */
>> REG_FIELD(0x07, 0, 6), /* status2 */
>> REG_FIELD(0x09, 0, 7), /* status3 */
>> @@ -854,11 +854,17 @@ static int qcom_flash_led_probe(struct platform_device *pdev)
>> if (val == FLASH_SUBTYPE_3CH_PM8150_VAL || val == FLASH_SUBTYPE_3CH_PMI8998_VAL) {
>> flash_data->hw_type = QCOM_MVFLASH_3CH;
>> flash_data->max_channels = 3;
>> - regs = mvflash_3ch_regs;
>> + regs = devm_kmemdup(dev, mvflash_3ch_regs, sizeof(mvflash_3ch_regs),
>> + GFP_KERNEL);
>> + if (!regs)
>> + return -ENOMEM;
>> } else if (val == FLASH_SUBTYPE_4CH_VAL) {
>> flash_data->hw_type = QCOM_MVFLASH_4CH;
>> flash_data->max_channels = 4;
>> - regs = mvflash_4ch_regs;
>> + regs = devm_kmemdup(dev, mvflash_4ch_regs, sizeof(mvflash_3ch_regs),
>
> Minor: sizeof(mvflash_4ch_regs)
Copy paste, thanks.
>
Best regards,
Krzysztof
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-05-29 6:31 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-05-28 19:44 [PATCH] leds: flash: leds-qcom-flash: Fix registry access after re-bind Krzysztof Kozlowski
2025-05-29 3:11 ` Fenglin Wu
2025-05-29 6:31 ` Krzysztof Kozlowski
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®