[PATCH 11/13] leds: sc27xx: Fix devm vs. non-devm ordering

From: Wang Yufen
Date: Wed Nov 09 2022 - 03:28:46 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: e081c49e30ec ("leds: Add Spreadtrum SC27xx breathing light controller driver")
Signed-off-by: Wang Yufen <wangyufen@xxxxxxxxxx>
Cc: Baolin Wang <baolin.wang@xxxxxxxxxx>
---
drivers/leds/leds-sc27xx-bltc.c | 27 +++++++++++----------------
1 file changed, 11 insertions(+), 16 deletions(-)

diff --git a/drivers/leds/leds-sc27xx-bltc.c b/drivers/leds/leds-sc27xx-bltc.c
index e199ea1..43a821c 100644
--- a/drivers/leds/leds-sc27xx-bltc.c
+++ b/drivers/leds/leds-sc27xx-bltc.c
@@ -273,6 +273,11 @@ static int sc27xx_led_register(struct device *dev, struct sc27xx_led_priv *priv)
return 0;
}

+static void sc27xx_led_mutex_destroy(void *lock)
+{
+ mutex_destroy(lock);
+}
+
static int sc27xx_led_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
@@ -297,6 +302,11 @@ static int sc27xx_led_probe(struct platform_device *pdev)

platform_set_drvdata(pdev, priv);
mutex_init(&priv->lock);
+ err = devm_add_action_or_reset(dev, sc27xx_led_mutex_destroy,
+ &priv->lock);
+ if (err)
+ return err;
+
priv->base = base;
priv->regmap = dev_get_regmap(dev->parent, NULL);
if (!priv->regmap) {
@@ -309,13 +319,11 @@ static int sc27xx_led_probe(struct platform_device *pdev)
err = of_property_read_u32(child, "reg", &reg);
if (err) {
of_node_put(child);
- mutex_destroy(&priv->lock);
return err;
}

if (reg >= SC27XX_LEDS_MAX || priv->leds[reg].active) {
of_node_put(child);
- mutex_destroy(&priv->lock);
return -EINVAL;
}

@@ -323,19 +331,7 @@ static int sc27xx_led_probe(struct platform_device *pdev)
priv->leds[reg].active = true;
}

- err = sc27xx_led_register(dev, priv);
- if (err)
- mutex_destroy(&priv->lock);
-
- return err;
-}
-
-static int sc27xx_led_remove(struct platform_device *pdev)
-{
- struct sc27xx_led_priv *priv = platform_get_drvdata(pdev);
-
- mutex_destroy(&priv->lock);
- return 0;
+ return sc27xx_led_register(dev, priv);
}

static const struct of_device_id sc27xx_led_of_match[] = {
@@ -350,7 +346,6 @@ static int sc27xx_led_remove(struct platform_device *pdev)
.of_match_table = sc27xx_led_of_match,
},
.probe = sc27xx_led_probe,
- .remove = sc27xx_led_remove,
};

module_platform_driver(sc27xx_led_driver);
--
1.8.3.1