[PATCH 03/13] leds: lm3532: Fix devm vs. non-devm ordering

From: Wang Yufen
Date: Wed Nov 09 2022 - 03:28:12 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: bc1b8492c764 ("leds: lm3532: Introduce the lm3532 LED driver")
Signed-off-by: Wang Yufen <wangyufen@xxxxxxxxxx>
Cc: Dan Murphy <dmurphy@xxxxxx>
---
drivers/leds/leds-lm3532.c | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/drivers/leds/leds-lm3532.c b/drivers/leds/leds-lm3532.c
index db64d44..a052966 100644
--- a/drivers/leds/leds-lm3532.c
+++ b/drivers/leds/leds-lm3532.c
@@ -663,6 +663,11 @@ static int lm3532_parse_node(struct lm3532_data *priv)
return ret;
}

+static void lm3532_mutex_destroy(void *lock)
+{
+ mutex_destroy(lock);
+}
+
static int lm3532_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
@@ -693,6 +698,11 @@ static int lm3532_probe(struct i2c_client *client,
}

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

ret = lm3532_parse_node(drvdata);
--
1.8.3.1