linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] spi: atmel-quadspi: Convert to platform remove callback returning void
@ 2023-03-17  8:42 Uwe Kleine-König
  2023-03-17  8:42 ` [PATCH 1/3] spi: atmel-quadspi: Don't leak clk enable count in pm resume Uwe Kleine-König
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Uwe Kleine-König @ 2023-03-17  8:42 UTC (permalink / raw)
  To: Mark Brown, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea
  Cc: linux-spi, linux-arm-kernel, kernel

Hello,

this series converts the atmel-quadspi driver to use the .remove_new()
callback that doesn't return an int but void. The motivation is to not
give driver authors a reason to (wrongly) believe that returning an
error code was sensible error handling. In fact the spi core only emits
a warning message in this case and otherwise continues as if the return
value was zero. This usually yields resource leaks that sometimes can
lead to exceptions later on.

The atmel-quadspi driver is one of these drivers that got error handling
wrong, this is fixed here and in the last patch the driver is converted
to .remove_new() with the eventual goal to change .remove() to return
void once all drivers are converted this way.

Best regards
Uwe

Uwe Kleine-König (3):
  spi: atmel-quadspi: Don't leak clk enable count in pm resume
  spi: atmel-quadspi: Free resources even if runtime resume failed in
    .remove()
  spi: atmel-quadspi: Convert to platform remove callback returning void

 drivers/spi/atmel-quadspi.c | 36 ++++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)


base-commit: fe15c26ee26efa11741a7b632e9f23b01aca4cc6
-- 
2.39.2


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/3] spi: atmel-quadspi: Don't leak clk enable count in pm resume
  2023-03-17  8:42 [PATCH 0/3] spi: atmel-quadspi: Convert to platform remove callback returning void Uwe Kleine-König
@ 2023-03-17  8:42 ` Uwe Kleine-König
  2023-03-17 10:01   ` Tudor Ambarus
  2023-03-17  8:42 ` [PATCH 2/3] spi: atmel-quadspi: Free resources even if runtime resume failed in .remove() Uwe Kleine-König
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 8+ messages in thread
From: Uwe Kleine-König @ 2023-03-17  8:42 UTC (permalink / raw)
  To: Mark Brown, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea
  Cc: linux-spi, linux-arm-kernel, kernel

The pm resume call is supposed to enable two clocks. If the second enable
fails the callback reports failure but doesn't undo the first enable.

So call clk_disable() for the first clock when clk_enable() for the second
one fails.

Fixes: 4a2f83b7f780 ("spi: atmel-quadspi: add runtime pm support")
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/spi/atmel-quadspi.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/atmel-quadspi.c b/drivers/spi/atmel-quadspi.c
index f4632cb07495..0c6f80ddea57 100644
--- a/drivers/spi/atmel-quadspi.c
+++ b/drivers/spi/atmel-quadspi.c
@@ -786,7 +786,11 @@ static int __maybe_unused atmel_qspi_runtime_resume(struct device *dev)
 	if (ret)
 		return ret;
 
-	return clk_enable(aq->qspick);
+	ret = clk_enable(aq->qspick);
+	if (ret)
+		clk_disable(aq->pclk);
+
+	return ret;
 }
 
 static const struct dev_pm_ops __maybe_unused atmel_qspi_pm_ops = {
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/3] spi: atmel-quadspi: Free resources even if runtime resume failed in .remove()
  2023-03-17  8:42 [PATCH 0/3] spi: atmel-quadspi: Convert to platform remove callback returning void Uwe Kleine-König
  2023-03-17  8:42 ` [PATCH 1/3] spi: atmel-quadspi: Don't leak clk enable count in pm resume Uwe Kleine-König
@ 2023-03-17  8:42 ` Uwe Kleine-König
  2023-03-17 10:15   ` Tudor Ambarus
  2023-03-17  8:42 ` [PATCH 3/3] spi: atmel-quadspi: Convert to platform remove callback returning void Uwe Kleine-König
  2023-03-17 17:44 ` [PATCH 0/3] " Mark Brown
  3 siblings, 1 reply; 8+ messages in thread
From: Uwe Kleine-König @ 2023-03-17  8:42 UTC (permalink / raw)
  To: Mark Brown, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea
  Cc: linux-spi, linux-arm-kernel, kernel

An early error exit in atmel_qspi_remove() doesn't prevent the device
unbind. So this results in an spi controller with an unbound parent
and unmapped register space (because devm_ioremap_resource() is undone).
So using the remaining spi controller probably results in an oops.

Instead unregister the controller unconditionally and only skip hardware
access and clk disable.

Also add a warning about resume failing and return zero unconditionally.
The latter has the only effect to suppress a less helpful error message by
the spi core.

Fixes: 4a2f83b7f780 ("spi: atmel-quadspi: add runtime pm support")
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/spi/atmel-quadspi.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/drivers/spi/atmel-quadspi.c b/drivers/spi/atmel-quadspi.c
index 0c6f80ddea57..713a4d6700fd 100644
--- a/drivers/spi/atmel-quadspi.c
+++ b/drivers/spi/atmel-quadspi.c
@@ -706,18 +706,28 @@ static int atmel_qspi_remove(struct platform_device *pdev)
 	struct atmel_qspi *aq = spi_controller_get_devdata(ctrl);
 	int ret;
 
-	ret = pm_runtime_resume_and_get(&pdev->dev);
-	if (ret < 0)
-		return ret;
-
 	spi_unregister_controller(ctrl);
-	atmel_qspi_write(QSPI_CR_QSPIDIS, aq, QSPI_CR);
+
+	ret = pm_runtime_get_sync(&pdev->dev);
+	if (ret >= 0) {
+		atmel_qspi_write(QSPI_CR_QSPIDIS, aq, QSPI_CR);
+		clk_disable(aq->qspick);
+		clk_disable(aq->pclk);
+	} else {
+		/*
+		 * atmel_qspi_runtime_{suspend,resume} just disable and enable
+		 * the two clks respectively. So after resume failed these are
+		 * off, and we skip hardware access and disabling these clks again.
+		 */
+		dev_warn(&pdev->dev, "Failed to resume device on remove\n");
+	}
+
+	clk_unprepare(aq->qspick);
+	clk_unprepare(aq->pclk);
 
 	pm_runtime_disable(&pdev->dev);
 	pm_runtime_put_noidle(&pdev->dev);
 
-	clk_disable_unprepare(aq->qspick);
-	clk_disable_unprepare(aq->pclk);
 	return 0;
 }
 
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 3/3] spi: atmel-quadspi: Convert to platform remove callback returning void
  2023-03-17  8:42 [PATCH 0/3] spi: atmel-quadspi: Convert to platform remove callback returning void Uwe Kleine-König
  2023-03-17  8:42 ` [PATCH 1/3] spi: atmel-quadspi: Don't leak clk enable count in pm resume Uwe Kleine-König
  2023-03-17  8:42 ` [PATCH 2/3] spi: atmel-quadspi: Free resources even if runtime resume failed in .remove() Uwe Kleine-König
@ 2023-03-17  8:42 ` Uwe Kleine-König
  2023-03-17 10:16   ` Tudor Ambarus
  2023-03-17 17:44 ` [PATCH 0/3] " Mark Brown
  3 siblings, 1 reply; 8+ messages in thread
From: Uwe Kleine-König @ 2023-03-17  8:42 UTC (permalink / raw)
  To: Mark Brown, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea
  Cc: linux-spi, linux-arm-kernel, kernel

The .remove() callback for a platform driver returns an int which makes
many driver authors wrongly assume it's possible to do error handling by
returning an error code. However the value returned is (mostly) ignored
and this typically results in resource leaks. To improve here there is a
quest to make the remove callback return void. In the first step of this
quest all drivers are converted to .remove_new() which already returns
void.

Trivially convert this driver from always returning zero in the remove
callback to the void returning variant.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/spi/atmel-quadspi.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/spi/atmel-quadspi.c b/drivers/spi/atmel-quadspi.c
index 713a4d6700fd..3d1252566134 100644
--- a/drivers/spi/atmel-quadspi.c
+++ b/drivers/spi/atmel-quadspi.c
@@ -700,7 +700,7 @@ static int atmel_qspi_probe(struct platform_device *pdev)
 	return err;
 }
 
-static int atmel_qspi_remove(struct platform_device *pdev)
+static void atmel_qspi_remove(struct platform_device *pdev)
 {
 	struct spi_controller *ctrl = platform_get_drvdata(pdev);
 	struct atmel_qspi *aq = spi_controller_get_devdata(ctrl);
@@ -727,8 +727,6 @@ static int atmel_qspi_remove(struct platform_device *pdev)
 
 	pm_runtime_disable(&pdev->dev);
 	pm_runtime_put_noidle(&pdev->dev);
-
-	return 0;
 }
 
 static int __maybe_unused atmel_qspi_suspend(struct device *dev)
@@ -837,7 +835,7 @@ static struct platform_driver atmel_qspi_driver = {
 		.pm	= pm_ptr(&atmel_qspi_pm_ops),
 	},
 	.probe		= atmel_qspi_probe,
-	.remove		= atmel_qspi_remove,
+	.remove_new	= atmel_qspi_remove,
 };
 module_platform_driver(atmel_qspi_driver);
 
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/3] spi: atmel-quadspi: Don't leak clk enable count in pm resume
  2023-03-17  8:42 ` [PATCH 1/3] spi: atmel-quadspi: Don't leak clk enable count in pm resume Uwe Kleine-König
@ 2023-03-17 10:01   ` Tudor Ambarus
  0 siblings, 0 replies; 8+ messages in thread
From: Tudor Ambarus @ 2023-03-17 10:01 UTC (permalink / raw)
  To: Uwe Kleine-König, Mark Brown, Nicolas Ferre,
	Alexandre Belloni, Claudiu Beznea
  Cc: linux-spi, linux-arm-kernel, kernel



On 3/17/23 08:42, Uwe Kleine-König wrote:
> The pm resume call is supposed to enable two clocks. If the second enable
> fails the callback reports failure but doesn't undo the first enable.
> 
> So call clk_disable() for the first clock when clk_enable() for the second
> one fails.
> 
> Fixes: 4a2f83b7f780 ("spi: atmel-quadspi: add runtime pm support")
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

Reviewed-by: Tudor Ambarus <tudor.ambarus@linaro.org>

> ---
>  drivers/spi/atmel-quadspi.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/spi/atmel-quadspi.c b/drivers/spi/atmel-quadspi.c
> index f4632cb07495..0c6f80ddea57 100644
> --- a/drivers/spi/atmel-quadspi.c
> +++ b/drivers/spi/atmel-quadspi.c
> @@ -786,7 +786,11 @@ static int __maybe_unused atmel_qspi_runtime_resume(struct device *dev)
>  	if (ret)
>  		return ret;
>  
> -	return clk_enable(aq->qspick);
> +	ret = clk_enable(aq->qspick);
> +	if (ret)
> +		clk_disable(aq->pclk);
> +
> +	return ret;
>  }
>  
>  static const struct dev_pm_ops __maybe_unused atmel_qspi_pm_ops = {

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/3] spi: atmel-quadspi: Free resources even if runtime resume failed in .remove()
  2023-03-17  8:42 ` [PATCH 2/3] spi: atmel-quadspi: Free resources even if runtime resume failed in .remove() Uwe Kleine-König
@ 2023-03-17 10:15   ` Tudor Ambarus
  0 siblings, 0 replies; 8+ messages in thread
From: Tudor Ambarus @ 2023-03-17 10:15 UTC (permalink / raw)
  To: Uwe Kleine-König, Mark Brown, Nicolas Ferre,
	Alexandre Belloni, Claudiu Beznea
  Cc: linux-spi, linux-arm-kernel, kernel



On 3/17/23 08:42, Uwe Kleine-König wrote:
> An early error exit in atmel_qspi_remove() doesn't prevent the device
> unbind. So this results in an spi controller with an unbound parent
> and unmapped register space (because devm_ioremap_resource() is undone).
> So using the remaining spi controller probably results in an oops.
> 
> Instead unregister the controller unconditionally and only skip hardware
> access and clk disable.
> 
> Also add a warning about resume failing and return zero unconditionally.
> The latter has the only effect to suppress a less helpful error message by
> the spi core.
> 
> Fixes: 4a2f83b7f780 ("spi: atmel-quadspi: add runtime pm support")
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

Reviewed-by: Tudor Ambarus <tudor.ambarus@linaro.org>

> ---
>  drivers/spi/atmel-quadspi.c | 24 +++++++++++++++++-------
>  1 file changed, 17 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/spi/atmel-quadspi.c b/drivers/spi/atmel-quadspi.c
> index 0c6f80ddea57..713a4d6700fd 100644
> --- a/drivers/spi/atmel-quadspi.c
> +++ b/drivers/spi/atmel-quadspi.c
> @@ -706,18 +706,28 @@ static int atmel_qspi_remove(struct platform_device *pdev)
>  	struct atmel_qspi *aq = spi_controller_get_devdata(ctrl);
>  	int ret;
>  
> -	ret = pm_runtime_resume_and_get(&pdev->dev);
> -	if (ret < 0)
> -		return ret;
> -
>  	spi_unregister_controller(ctrl);
> -	atmel_qspi_write(QSPI_CR_QSPIDIS, aq, QSPI_CR);
> +
> +	ret = pm_runtime_get_sync(&pdev->dev);
> +	if (ret >= 0) {
> +		atmel_qspi_write(QSPI_CR_QSPIDIS, aq, QSPI_CR);
> +		clk_disable(aq->qspick);
> +		clk_disable(aq->pclk);
> +	} else {
> +		/*
> +		 * atmel_qspi_runtime_{suspend,resume} just disable and enable
> +		 * the two clks respectively. So after resume failed these are
> +		 * off, and we skip hardware access and disabling these clks again.
> +		 */
> +		dev_warn(&pdev->dev, "Failed to resume device on remove\n");
> +	}
> +
> +	clk_unprepare(aq->qspick);
> +	clk_unprepare(aq->pclk);
>  
>  	pm_runtime_disable(&pdev->dev);
>  	pm_runtime_put_noidle(&pdev->dev);
>  
> -	clk_disable_unprepare(aq->qspick);
> -	clk_disable_unprepare(aq->pclk);
>  	return 0;
>  }
>  

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 3/3] spi: atmel-quadspi: Convert to platform remove callback returning void
  2023-03-17  8:42 ` [PATCH 3/3] spi: atmel-quadspi: Convert to platform remove callback returning void Uwe Kleine-König
@ 2023-03-17 10:16   ` Tudor Ambarus
  0 siblings, 0 replies; 8+ messages in thread
From: Tudor Ambarus @ 2023-03-17 10:16 UTC (permalink / raw)
  To: Uwe Kleine-König, Mark Brown, Nicolas Ferre,
	Alexandre Belloni, Claudiu Beznea
  Cc: linux-spi, linux-arm-kernel, kernel



On 3/17/23 08:42, Uwe Kleine-König wrote:
> The .remove() callback for a platform driver returns an int which makes
> many driver authors wrongly assume it's possible to do error handling by
> returning an error code. However the value returned is (mostly) ignored
> and this typically results in resource leaks. To improve here there is a
> quest to make the remove callback return void. In the first step of this
> quest all drivers are converted to .remove_new() which already returns
> void.
> 
> Trivially convert this driver from always returning zero in the remove
> callback to the void returning variant.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

Reviewed-by: Tudor Ambarus <tudor.ambarus@linaro.org>

> ---
>  drivers/spi/atmel-quadspi.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/spi/atmel-quadspi.c b/drivers/spi/atmel-quadspi.c
> index 713a4d6700fd..3d1252566134 100644
> --- a/drivers/spi/atmel-quadspi.c
> +++ b/drivers/spi/atmel-quadspi.c
> @@ -700,7 +700,7 @@ static int atmel_qspi_probe(struct platform_device *pdev)
>  	return err;
>  }
>  
> -static int atmel_qspi_remove(struct platform_device *pdev)
> +static void atmel_qspi_remove(struct platform_device *pdev)
>  {
>  	struct spi_controller *ctrl = platform_get_drvdata(pdev);
>  	struct atmel_qspi *aq = spi_controller_get_devdata(ctrl);
> @@ -727,8 +727,6 @@ static int atmel_qspi_remove(struct platform_device *pdev)
>  
>  	pm_runtime_disable(&pdev->dev);
>  	pm_runtime_put_noidle(&pdev->dev);
> -
> -	return 0;
>  }
>  
>  static int __maybe_unused atmel_qspi_suspend(struct device *dev)
> @@ -837,7 +835,7 @@ static struct platform_driver atmel_qspi_driver = {
>  		.pm	= pm_ptr(&atmel_qspi_pm_ops),
>  	},
>  	.probe		= atmel_qspi_probe,
> -	.remove		= atmel_qspi_remove,
> +	.remove_new	= atmel_qspi_remove,
>  };
>  module_platform_driver(atmel_qspi_driver);
>  

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 0/3] spi: atmel-quadspi: Convert to platform remove callback returning void
  2023-03-17  8:42 [PATCH 0/3] spi: atmel-quadspi: Convert to platform remove callback returning void Uwe Kleine-König
                   ` (2 preceding siblings ...)
  2023-03-17  8:42 ` [PATCH 3/3] spi: atmel-quadspi: Convert to platform remove callback returning void Uwe Kleine-König
@ 2023-03-17 17:44 ` Mark Brown
  3 siblings, 0 replies; 8+ messages in thread
From: Mark Brown @ 2023-03-17 17:44 UTC (permalink / raw)
  To: Nicolas Ferre, Alexandre Belloni, Claudiu Beznea, Uwe Kleine-König
  Cc: linux-spi, linux-arm-kernel, kernel

On Fri, 17 Mar 2023 09:42:29 +0100, Uwe Kleine-König wrote:
> this series converts the atmel-quadspi driver to use the .remove_new()
> callback that doesn't return an int but void. The motivation is to not
> give driver authors a reason to (wrongly) believe that returning an
> error code was sensible error handling. In fact the spi core only emits
> a warning message in this case and otherwise continues as if the return
> value was zero. This usually yields resource leaks that sometimes can
> lead to exceptions later on.
> 
> [...]

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-next

Thanks!

[1/3] spi: atmel-quadspi: Don't leak clk enable count in pm resume
      commit: c18bbac353ffed50be134b0a2a059a2bd540c503
[2/3] spi: atmel-quadspi: Free resources even if runtime resume failed in .remove()
      commit: 9448bc1dee65f86c0fe64d9dea8b410af0586886
[3/3] spi: atmel-quadspi: Convert to platform remove callback returning void
      commit: 4d70dd0a25081bc4e6378d0da4a7c367389df707

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-03-17 17:45 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-17  8:42 [PATCH 0/3] spi: atmel-quadspi: Convert to platform remove callback returning void Uwe Kleine-König
2023-03-17  8:42 ` [PATCH 1/3] spi: atmel-quadspi: Don't leak clk enable count in pm resume Uwe Kleine-König
2023-03-17 10:01   ` Tudor Ambarus
2023-03-17  8:42 ` [PATCH 2/3] spi: atmel-quadspi: Free resources even if runtime resume failed in .remove() Uwe Kleine-König
2023-03-17 10:15   ` Tudor Ambarus
2023-03-17  8:42 ` [PATCH 3/3] spi: atmel-quadspi: Convert to platform remove callback returning void Uwe Kleine-König
2023-03-17 10:16   ` Tudor Ambarus
2023-03-17 17:44 ` [PATCH 0/3] " Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).