[PATCH] spi: atcspi200: fix mutex initialization order

Pei Xiao posted 1 patch 4 weeks, 1 day ago
There is a newer version of this series
drivers/spi/spi-atcspi200.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
[PATCH] spi: atcspi200: fix mutex initialization order
Posted by Pei Xiao 4 weeks, 1 day ago
The atcspi_exec_mem_op() function may call mutex_lock() on the
driver's mutex before it is properly initialized if a SPI memory
operation is initiated immediately after devm_spi_register_controller()
is called. The mutex initialization currently occurs after the
controller registration, which leaves a window where the mutex could
be used uninitialized.

Move the mutex initialization to the beginning of the probe function,
before any registration or resource allocation. Also replace the
plain mutex_init() with devm_mutex_init() to benefit from automatic
cleanup on driver unbind, preventing potential resource leaks.

Fixes: 34e3815ea459 ("spi: atcspi200: Add ATCSPI200 SPI controller driver")
Signed-off-by: Pei Xiao <xiaopei01@kylinos.cn>
---
 drivers/spi/spi-atcspi200.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/spi-atcspi200.c b/drivers/spi/spi-atcspi200.c
index 6d4f3ff3d602..b0cc49c93740 100644
--- a/drivers/spi/spi-atcspi200.c
+++ b/drivers/spi/spi-atcspi200.c
@@ -567,6 +567,10 @@ static int atcspi_probe(struct platform_device *pdev)
 	spi->dev = &pdev->dev;
 	dev_set_drvdata(&pdev->dev, host);
 
+	ret = devm_mutex_init(spi->dev, &spi->mutex_lock);
+	if (ret)
+		return ret;
+
 	ret = atcspi_init_resources(pdev, spi, &mem_res);
 	if (ret)
 		goto free_controller;
@@ -592,7 +596,6 @@ static int atcspi_probe(struct platform_device *pdev)
 		else
 			spi->use_dma = true;
 	}
-	mutex_init(&spi->mutex_lock);
 
 	return 0;
 
-- 
2.25.1
Re: [PATCH] spi: atcspi200: fix mutex initialization order
Posted by Mark Brown 4 weeks, 1 day ago
On Tue, Mar 10, 2026 at 05:55:47PM +0800, Pei Xiao wrote:

> Move the mutex initialization to the beginning of the probe function,
> before any registration or resource allocation. Also replace the
> plain mutex_init() with devm_mutex_init() to benefit from automatic
> cleanup on driver unbind, preventing potential resource leaks.

> +++ b/drivers/spi/spi-atcspi200.c
> @@ -567,6 +567,10 @@ static int atcspi_probe(struct platform_device *pdev)
>  	spi->dev = &pdev->dev;
>  	dev_set_drvdata(&pdev->dev, host);
>  
> +	ret = devm_mutex_init(spi->dev, &spi->mutex_lock);
> +	if (ret)
> +		return ret;
> +
>  	ret = atcspi_init_resources(pdev, spi, &mem_res);
>  	if (ret)
>  		goto free_controller;

Shouldn't we be using free_controller if the mutex allocation fails too?
We'll leak the controller struct if that happens.