All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] hwrng: xgene - Some improvements
@ 2023-02-14 16:28 Uwe Kleine-König
  2023-02-14 16:28 ` [PATCH 1/3] hwrng: xgene - Simplify using dev_err_probe() Uwe Kleine-König
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Uwe Kleine-König @ 2023-02-14 16:28 UTC (permalink / raw)
  To: Olivia Mackall, Herbert Xu; +Cc: linux-crypto, kernel

Hello,

while working on the quest to make struct platform_driver::remove() return void
I stumbled over the xgene-rng driver because it didn't return 0 in .remove().

Looking at it I found two other patch opportunities, here is the result.

I think the driver has some more problems:

 - device_init_wakeup() is only called after devm_hwrng_register(). After the
   latter returns the respective callbacks can be called. Is the device already
   in the right state before device_init_wakeup(..., 1)?

 - Similar problem on .remove(): device_init_wakeup(..., 0) is called before
   hwrng_unregister() happens.

 - If there are two (or more) devices of that type, .probe() for the 2nd overwrites
   xgene_rng_func.priv of the first one.

Best regards
Uwe

Uwe Kleine-König (3):
  hwrng: xgene - Simplify using dev_err_probe()
  hwrng: xgene - Simplify using devm_clk_get_optional_enabled()
  hwrng: xgene - Improve error reporting for problems during .remove()

 drivers/char/hw_random/xgene-rng.c | 44 ++++++++----------------------
 1 file changed, 11 insertions(+), 33 deletions(-)

base-commit: e05dec85e78317f251eddd27e0357b2253d9dfc4
-- 
2.39.1


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

* [PATCH 1/3] hwrng: xgene - Simplify using dev_err_probe()
  2023-02-14 16:28 [PATCH 0/3] hwrng: xgene - Some improvements Uwe Kleine-König
@ 2023-02-14 16:28 ` Uwe Kleine-König
  2023-02-14 16:28 ` [PATCH 2/3] hwrng: xgene - Simplify using devm_clk_get_optional_enabled() Uwe Kleine-König
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Uwe Kleine-König @ 2023-02-14 16:28 UTC (permalink / raw)
  To: Olivia Mackall, Herbert Xu; +Cc: linux-crypto, kernel

dev_err_probe simplifies the idiom:

	if (ret != -EPROBE_DEFER)
		dev_err(...)
	return ret;

, emits the error code in a human readable way and even yields a useful
entry in /sys/kernel/debug/devices_deferred in the EPROBE_DEFER case.

So simplify and at the same time improve the driver by using
dev_err_probe().

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/char/hw_random/xgene-rng.c | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)

diff --git a/drivers/char/hw_random/xgene-rng.c b/drivers/char/hw_random/xgene-rng.c
index 008e6db9ce01..a6a30686a58b 100644
--- a/drivers/char/hw_random/xgene-rng.c
+++ b/drivers/char/hw_random/xgene-rng.c
@@ -337,10 +337,8 @@ static int xgene_rng_probe(struct platform_device *pdev)
 
 	rc = devm_request_irq(&pdev->dev, ctx->irq, xgene_rng_irq_handler, 0,
 				dev_name(&pdev->dev), ctx);
-	if (rc) {
-		dev_err(&pdev->dev, "Could not request RNG alarm IRQ\n");
-		return rc;
-	}
+	if (rc)
+		return dev_err_probe(&pdev->dev, rc, "Could not request RNG alarm IRQ\n");
 
 	/* Enable IP clock */
 	ctx->clk = devm_clk_get(&pdev->dev, NULL);
@@ -348,30 +346,25 @@ static int xgene_rng_probe(struct platform_device *pdev)
 		dev_warn(&pdev->dev, "Couldn't get the clock for RNG\n");
 	} else {
 		rc = clk_prepare_enable(ctx->clk);
-		if (rc) {
-			dev_warn(&pdev->dev,
-				 "clock prepare enable failed for RNG");
-			return rc;
-		}
+		if (rc)
+			return dev_err_probe(&pdev->dev, rc,
+					     "clock prepare enable failed for RNG");
 	}
 
 	xgene_rng_func.priv = (unsigned long) ctx;
 
 	rc = devm_hwrng_register(&pdev->dev, &xgene_rng_func);
 	if (rc) {
-		dev_err(&pdev->dev, "RNG registering failed error %d\n", rc);
 		if (!IS_ERR(ctx->clk))
 			clk_disable_unprepare(ctx->clk);
-		return rc;
+		return dev_err_probe(&pdev->dev, rc, "RNG registering failed\n");
 	}
 
 	rc = device_init_wakeup(&pdev->dev, 1);
 	if (rc) {
-		dev_err(&pdev->dev, "RNG device_init_wakeup failed error %d\n",
-			rc);
 		if (!IS_ERR(ctx->clk))
 			clk_disable_unprepare(ctx->clk);
-		return rc;
+		return dev_err_probe(&pdev->dev, rc, "RNG device_init_wakeup failed\n");
 	}
 
 	return 0;
-- 
2.39.1


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

* [PATCH 2/3] hwrng: xgene - Simplify using devm_clk_get_optional_enabled()
  2023-02-14 16:28 [PATCH 0/3] hwrng: xgene - Some improvements Uwe Kleine-König
  2023-02-14 16:28 ` [PATCH 1/3] hwrng: xgene - Simplify using dev_err_probe() Uwe Kleine-König
@ 2023-02-14 16:28 ` Uwe Kleine-König
  2023-02-14 16:28 ` [PATCH 3/3] hwrng: xgene - Improve error reporting for problems during .remove() Uwe Kleine-König
  2023-03-10 11:25 ` [PATCH 0/3] hwrng: xgene - Some improvements Herbert Xu
  3 siblings, 0 replies; 7+ messages in thread
From: Uwe Kleine-König @ 2023-02-14 16:28 UTC (permalink / raw)
  To: Olivia Mackall, Herbert Xu; +Cc: linux-crypto, kernel

Instead of ignoring errors returned by devm_clk_get() and manually
enabling the clk for the whole lifetime of the bound device, use
devm_clk_get_optional_enabled(). This is simpler and also more correct
as it doesn't ignore errors. This is also more correct because now the
call to clk_disable_unprepare() can be dropped from xgene_rng_remove()
which happened while the hwrn device was still registered. With the devm
callback disabling the clk happens correctly only after
devm_hwrng_register() is undone.

As a result struct xgene_rng_dev::clk is only used in xgene_rng_probe, and
so the struct member can be replaced by a local variable.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/char/hw_random/xgene-rng.c | 27 ++++++---------------------
 1 file changed, 6 insertions(+), 21 deletions(-)

diff --git a/drivers/char/hw_random/xgene-rng.c b/drivers/char/hw_random/xgene-rng.c
index a6a30686a58b..31662614a48f 100644
--- a/drivers/char/hw_random/xgene-rng.c
+++ b/drivers/char/hw_random/xgene-rng.c
@@ -84,7 +84,6 @@ struct xgene_rng_dev {
 	unsigned long failure_ts;/* First failure timestamp */
 	struct timer_list failure_timer;
 	struct device *dev;
-	struct clk *clk;
 };
 
 static void xgene_rng_expired_timer(struct timer_list *t)
@@ -314,6 +313,7 @@ static struct hwrng xgene_rng_func = {
 static int xgene_rng_probe(struct platform_device *pdev)
 {
 	struct xgene_rng_dev *ctx;
+	struct clk *clk;
 	int rc = 0;
 
 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
@@ -341,45 +341,30 @@ static int xgene_rng_probe(struct platform_device *pdev)
 		return dev_err_probe(&pdev->dev, rc, "Could not request RNG alarm IRQ\n");
 
 	/* Enable IP clock */
-	ctx->clk = devm_clk_get(&pdev->dev, NULL);
-	if (IS_ERR(ctx->clk)) {
-		dev_warn(&pdev->dev, "Couldn't get the clock for RNG\n");
-	} else {
-		rc = clk_prepare_enable(ctx->clk);
-		if (rc)
-			return dev_err_probe(&pdev->dev, rc,
-					     "clock prepare enable failed for RNG");
-	}
+	clk = devm_clk_get_optional_enabled(&pdev->dev, NULL);
+	if (IS_ERR(clk))
+		return dev_err_probe(&pdev->dev, PTR_ERR(clk), "Couldn't get the clock for RNG\n");
 
 	xgene_rng_func.priv = (unsigned long) ctx;
 
 	rc = devm_hwrng_register(&pdev->dev, &xgene_rng_func);
-	if (rc) {
-		if (!IS_ERR(ctx->clk))
-			clk_disable_unprepare(ctx->clk);
+	if (rc)
 		return dev_err_probe(&pdev->dev, rc, "RNG registering failed\n");
-	}
 
 	rc = device_init_wakeup(&pdev->dev, 1);
-	if (rc) {
-		if (!IS_ERR(ctx->clk))
-			clk_disable_unprepare(ctx->clk);
+	if (rc)
 		return dev_err_probe(&pdev->dev, rc, "RNG device_init_wakeup failed\n");
-	}
 
 	return 0;
 }
 
 static int xgene_rng_remove(struct platform_device *pdev)
 {
-	struct xgene_rng_dev *ctx = platform_get_drvdata(pdev);
 	int rc;
 
 	rc = device_init_wakeup(&pdev->dev, 0);
 	if (rc)
 		dev_err(&pdev->dev, "RNG init wakeup failed error %d\n", rc);
-	if (!IS_ERR(ctx->clk))
-		clk_disable_unprepare(ctx->clk);
 
 	return rc;
 }
-- 
2.39.1


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

* [PATCH 3/3] hwrng: xgene - Improve error reporting for problems during .remove()
  2023-02-14 16:28 [PATCH 0/3] hwrng: xgene - Some improvements Uwe Kleine-König
  2023-02-14 16:28 ` [PATCH 1/3] hwrng: xgene - Simplify using dev_err_probe() Uwe Kleine-König
  2023-02-14 16:28 ` [PATCH 2/3] hwrng: xgene - Simplify using devm_clk_get_optional_enabled() Uwe Kleine-König
@ 2023-02-14 16:28 ` Uwe Kleine-König
  2023-03-14  9:04   ` Uwe Kleine-König
  2023-03-10 11:25 ` [PATCH 0/3] hwrng: xgene - Some improvements Herbert Xu
  3 siblings, 1 reply; 7+ messages in thread
From: Uwe Kleine-König @ 2023-02-14 16:28 UTC (permalink / raw)
  To: Olivia Mackall, Herbert Xu; +Cc: linux-crypto, kernel

Returning an error value in a platform driver's remove callback results in
a generic error message being emitted by the driver core, but otherwise it
doesn't make a difference. The device goes away anyhow.

As the driver already emits a better error message than the core, suppress
the generic error message by returning zero unconditionally.
---
 drivers/char/hw_random/xgene-rng.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/char/hw_random/xgene-rng.c b/drivers/char/hw_random/xgene-rng.c
index 31662614a48f..c67d3185b5b6 100644
--- a/drivers/char/hw_random/xgene-rng.c
+++ b/drivers/char/hw_random/xgene-rng.c
@@ -366,7 +366,7 @@ static int xgene_rng_remove(struct platform_device *pdev)
 	if (rc)
 		dev_err(&pdev->dev, "RNG init wakeup failed error %d\n", rc);
 
-	return rc;
+	return 0;
 }
 
 static const struct of_device_id xgene_rng_of_match[] = {
-- 
2.39.1


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

* Re: [PATCH 0/3] hwrng: xgene - Some improvements
  2023-02-14 16:28 [PATCH 0/3] hwrng: xgene - Some improvements Uwe Kleine-König
                   ` (2 preceding siblings ...)
  2023-02-14 16:28 ` [PATCH 3/3] hwrng: xgene - Improve error reporting for problems during .remove() Uwe Kleine-König
@ 2023-03-10 11:25 ` Herbert Xu
  3 siblings, 0 replies; 7+ messages in thread
From: Herbert Xu @ 2023-03-10 11:25 UTC (permalink / raw)
  To: Uwe Kleine-König; +Cc: Olivia Mackall, linux-crypto, kernel

On Tue, Feb 14, 2023 at 05:28:26PM +0100, Uwe Kleine-König wrote:
> Hello,
> 
> while working on the quest to make struct platform_driver::remove() return void
> I stumbled over the xgene-rng driver because it didn't return 0 in .remove().
> 
> Looking at it I found two other patch opportunities, here is the result.
> 
> I think the driver has some more problems:
> 
>  - device_init_wakeup() is only called after devm_hwrng_register(). After the
>    latter returns the respective callbacks can be called. Is the device already
>    in the right state before device_init_wakeup(..., 1)?
> 
>  - Similar problem on .remove(): device_init_wakeup(..., 0) is called before
>    hwrng_unregister() happens.
> 
>  - If there are two (or more) devices of that type, .probe() for the 2nd overwrites
>    xgene_rng_func.priv of the first one.
> 
> Best regards
> Uwe
> 
> Uwe Kleine-König (3):
>   hwrng: xgene - Simplify using dev_err_probe()
>   hwrng: xgene - Simplify using devm_clk_get_optional_enabled()
>   hwrng: xgene - Improve error reporting for problems during .remove()
> 
>  drivers/char/hw_random/xgene-rng.c | 44 ++++++++----------------------
>  1 file changed, 11 insertions(+), 33 deletions(-)
> 
> base-commit: e05dec85e78317f251eddd27e0357b2253d9dfc4
> -- 
> 2.39.1

All applied.  Thanks.
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

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

* Re: [PATCH 3/3] hwrng: xgene - Improve error reporting for problems during .remove()
  2023-02-14 16:28 ` [PATCH 3/3] hwrng: xgene - Improve error reporting for problems during .remove() Uwe Kleine-König
@ 2023-03-14  9:04   ` Uwe Kleine-König
  2023-03-14  9:07     ` Herbert Xu
  0 siblings, 1 reply; 7+ messages in thread
From: Uwe Kleine-König @ 2023-03-14  9:04 UTC (permalink / raw)
  To: Olivia Mackall, Herbert Xu; +Cc: linux-crypto, kernel

[-- Attachment #1: Type: text/plain, Size: 856 bytes --]

Hello,

On Tue, Feb 14, 2023 at 05:28:29PM +0100, Uwe Kleine-König wrote:
> Returning an error value in a platform driver's remove callback results in
> a generic error message being emitted by the driver core, but otherwise it
> doesn't make a difference. The device goes away anyhow.
> 
> As the driver already emits a better error message than the core, suppress
> the generic error message by returning zero unconditionally.

I forgot to add a S-o-b line here. Stephen Rothwell pointed that out for
the applied patch in next.

I don't know how/if Herbert will fix this, but for the Record:

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH 3/3] hwrng: xgene - Improve error reporting for problems during .remove()
  2023-03-14  9:04   ` Uwe Kleine-König
@ 2023-03-14  9:07     ` Herbert Xu
  0 siblings, 0 replies; 7+ messages in thread
From: Herbert Xu @ 2023-03-14  9:07 UTC (permalink / raw)
  To: Uwe Kleine-König; +Cc: Olivia Mackall, linux-crypto, kernel

On Tue, Mar 14, 2023 at 10:04:09AM +0100, Uwe Kleine-König wrote:
>
> I don't know how/if Herbert will fix this, but for the Record:
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

I've fixed it in my tree.  Thanks!
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

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

end of thread, other threads:[~2023-03-14  9:07 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-14 16:28 [PATCH 0/3] hwrng: xgene - Some improvements Uwe Kleine-König
2023-02-14 16:28 ` [PATCH 1/3] hwrng: xgene - Simplify using dev_err_probe() Uwe Kleine-König
2023-02-14 16:28 ` [PATCH 2/3] hwrng: xgene - Simplify using devm_clk_get_optional_enabled() Uwe Kleine-König
2023-02-14 16:28 ` [PATCH 3/3] hwrng: xgene - Improve error reporting for problems during .remove() Uwe Kleine-König
2023-03-14  9:04   ` Uwe Kleine-König
2023-03-14  9:07     ` Herbert Xu
2023-03-10 11:25 ` [PATCH 0/3] hwrng: xgene - Some improvements Herbert Xu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.