[PATCH 2/2] mtd: rawnand: arasan: fix GPIO CS leak on probe error paths
Zhao Feng
zhaofeng2 at cmss.chinamobile.com
Sun Nov 8 21:27:24 PST 2026
The CS GPIO descriptors acquired by anfc_parse_cs() via
rawnand_dt_parse_gpio_cs() are only released in anfc_remove(). Two
probe error paths leak them:
- anfc_parse_cs() itself returns -EINVAL on an invalid DT GPIO CS
layout without releasing the freshly acquired descriptors;
- anfc_probe() bails out when anfc_chips_init() fails, leaving the
successfully parsed CS array behind.
Factor the gpiod_put() loop of anfc_remove() into anfc_put_cs() and
call it from both error paths as well. gpiod_put() accepts NULL, so
this is also safe when the default all-NULL CS array is in place.
Fixes: acbd3d0945f9 ("mtd: rawnand: arasan: Leverage additional GPIO CS")
Cc: stable at vger.kernel.org
Signed-off-by: Zhao Feng <zhaofeng2 at cmss.chinamobile.com>
---
Verified against current upstream master. Not runtime-tested (no
Arasan hardware).
drivers/mtd/nand/raw/arasan-nand-controller.c | 26 ++++++++++++++----
1 file changed, 18 insertions(+), 8 deletions(-)
diff --git a/drivers/mtd/nand/raw/arasan-nand-controller.c b/drivers/mtd/nand/raw/arasan-nand-controller.c
--- a/drivers/mtd/nand/raw/arasan-nand-controller.c
+++ b/drivers/mtd/nand/raw/arasan-nand-controller.c
@@ -1391,6 +1391,14 @@ static void anfc_reset(struct arasan_nfc *nfc)
nfc->cur_cs = -1;
}
+static void anfc_put_cs(struct arasan_nfc *nfc)
+{
+ int i;
+
+ for (i = 0; i < nfc->ncs; i++)
+ gpiod_put(nfc->cs_array[i]);
+}
+
static int anfc_parse_cs(struct arasan_nfc *nfc)
{
@@ -1413,7 +1421,9 @@ static int anfc_parse_cs(struct arasan_nfc *nfc)
if (nfc->ncs > 2 && !nfc->cs_array[0] && !nfc->cs_array[1]) {
dev_err(nfc->dev,
"Assign a single native CS when using GPIOs\n");
+ anfc_put_cs(nfc);
+
return -EINVAL;
}
if (nfc->cs_array[0])
@@ -1469,23 +1479,23 @@ static int anfc_probe(struct platform_device *pdev)
ret = anfc_chips_init(nfc);
if (ret)
- return ret;
+ goto err_put_cs;
platform_set_drvdata(pdev, nfc);
return 0;
+
+err_put_cs:
+ anfc_put_cs(nfc);
+
+ return ret;
}
static void anfc_remove(struct platform_device *pdev)
{
- int i;
struct arasan_nfc *nfc = platform_get_drvdata(pdev);
- for (i = 0; i < nfc->ncs; i++) {
- if (nfc->cs_array[i]) {
- gpiod_put(nfc->cs_array[i]);
- }
- }
-
+ anfc_put_cs(nfc);
+
anfc_chips_cleanup(nfc);
}
--
2.39.2
More information about the linux-mtd
mailing list