[07/13] leds: lp8860: Fix devm vs. non-devm ordering

Message ID 1667983694-15040-8-git-send-email-wangyufen@huawei.com
State New
Headers
Series leds: Fix devm vs. non-devm ordering |

Commit Message

wangyufen Nov. 9, 2022, 8:48 a.m. UTC
  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: a2169c9b762a ("leds: lp8860: Various fixes to align with LED framework")
Signed-off-by: Wang Yufen <wangyufen@huawei.com>
Cc: Dan Murphy <dmurphy@ti.com>
---
 drivers/leds/leds-lp8860.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
  

Patch

diff --git a/drivers/leds/leds-lp8860.c b/drivers/leds/leds-lp8860.c
index e2b36d3..ca73d91 100644
--- a/drivers/leds/leds-lp8860.c
+++ b/drivers/leds/leds-lp8860.c
@@ -375,6 +375,11 @@  static int lp8860_init(struct lp8860_led *led)
 	.cache_type = REGCACHE_NONE,
 };
 
+static void lp8860_mutex_destroy(void *lock)
+{
+	mutex_destroy(lock);
+}
+
 static int lp8860_probe(struct i2c_client *client,
 			const struct i2c_device_id *id)
 {
@@ -408,6 +413,10 @@  static int lp8860_probe(struct i2c_client *client,
 	led->led_dev.brightness_set_blocking = lp8860_brightness_set;
 
 	mutex_init(&led->lock);
+	ret = devm_add_action_or_reset(&client->dev, lp8860_mutex_destroy,
+				       &led->lock);
+	if (ret)
+		return ret;
 
 	i2c_set_clientdata(client, led);
 
@@ -459,8 +468,6 @@  static void lp8860_remove(struct i2c_client *client)
 			dev_err(&led->client->dev,
 				"Failed to disable regulator\n");
 	}
-
-	mutex_destroy(&led->lock);
 }
 
 static const struct i2c_device_id lp8860_id[] = {