[PATCH 05/13] leds: lm3697: Fix devm vs. non-devm ordering

From: Wang Yufen
Date: Wed Nov 09 2022 - 03:28:24 EST


When non-devm resources are allocated they mustn't be followed by devm
allocations, otherwise it will break the tear down ordering and might
lead to crashes or other bugs during ->remove() stage. Fix this by
wrapping mutex_destroy() call with devm_add_action_or_reset().

Fixes: 5c1d824cda9f ("leds: lm3697: Introduce the lm3697 driver")
Signed-off-by: Wang Yufen <wangyufen@xxxxxxxxxx>
Cc: Dan Murphy <dmurphy@xxxxxx>
---
drivers/leds/leds-lm3697.c | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/drivers/leds/leds-lm3697.c b/drivers/leds/leds-lm3697.c
index 71231a6..a345333 100644
--- a/drivers/leds/leds-lm3697.c
+++ b/drivers/leds/leds-lm3697.c
@@ -299,6 +299,11 @@ static int lm3697_probe_dt(struct lm3697 *priv)
return ret;
}

+static void lm3697_mutex_destroy(void *lock)
+{
+ mutex_destroy(lock);
+}
+
static int lm3697_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
@@ -318,8 +323,12 @@ static int lm3697_probe(struct i2c_client *client,
return -ENOMEM;

mutex_init(&led->lock);
- i2c_set_clientdata(client, led);
+ ret = devm_add_action_or_reset(&client->dev, lm3697_mutex_destroy,
+ &led->lock);
+ if (ret)
+ return ret;

+ i2c_set_clientdata(client, led);
led->client = client;
led->dev = dev;
led->num_banks = count;
@@ -356,8 +365,6 @@ static void lm3697_remove(struct i2c_client *client)
if (ret)
dev_err(dev, "Failed to disable regulator\n");
}
-
- mutex_destroy(&led->lock);
}

static const struct i2c_device_id lm3697_id[] = {
--
1.8.3.1