2024-05-16 01:22:44

by Marek Vasut

[permalink] [raw]
Subject: [PATCH 1/2] hwrng: stm32 - use pm_runtime_resume_and_get()

include/linux/pm_runtime.h pm_runtime_get_sync() description suggests to
... consider using pm_runtime_resume_and_get() instead of it, especially
if its return value is checked by the caller, as this is likely to result
in cleaner code.

This is indeed better, switch to pm_runtime_resume_and_get() which
correctly suspends the device again in case of failure. Also add error
checking into the RNG driver in case pm_runtime_resume_and_get() does
fail, which is currently not done, and it does detect sporadic -EACCES
error return after resume, which would otherwise lead to a hang due to
register access on un-resumed hardware. Now the read simply errors out
and the system does not hang.

Signed-off-by: Marek Vasut <[email protected]>
---
Cc: "Uwe Kleine-König" <[email protected]>
Cc: Alexandre Torgue <[email protected]>
Cc: Gatien Chevallier <[email protected]>
Cc: Herbert Xu <[email protected]>
Cc: Marek Vasut <[email protected]>
Cc: Maxime Coquelin <[email protected]>
Cc: Olivia Mackall <[email protected]>
Cc: Rob Herring <[email protected]>
Cc: Yang Yingliang <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
drivers/char/hw_random/stm32-rng.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/char/hw_random/stm32-rng.c b/drivers/char/hw_random/stm32-rng.c
index 0e903d6e22e30..6dec4adc49853 100644
--- a/drivers/char/hw_random/stm32-rng.c
+++ b/drivers/char/hw_random/stm32-rng.c
@@ -187,7 +187,9 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
int retval = 0, err = 0;
u32 sr;

- pm_runtime_get_sync((struct device *) priv->rng.priv);
+ retval = pm_runtime_resume_and_get((struct device *)priv->rng.priv);
+ if (retval)
+ return retval;

if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS)
stm32_rng_conceal_seed_error(rng);
--
2.43.0



2024-05-16 01:23:36

by Marek Vasut

[permalink] [raw]
Subject: [PATCH 2/2] hwrng: stm32 - cache device pointer in struct stm32_rng_private

Place device pointer in struct stm32_rng_private and use it all over the
place to get rid of the horrible type casts throughout the driver.

No functional change.

Signed-off-by: Marek Vasut <[email protected]>
---
Cc: "Uwe Kleine-König" <[email protected]>
Cc: Alexandre Torgue <[email protected]>
Cc: Gatien Chevallier <[email protected]>
Cc: Herbert Xu <[email protected]>
Cc: Marek Vasut <[email protected]>
Cc: Maxime Coquelin <[email protected]>
Cc: Olivia Mackall <[email protected]>
Cc: Rob Herring <[email protected]>
Cc: Yang Yingliang <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
drivers/char/hw_random/stm32-rng.c | 25 +++++++++++++------------
1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/drivers/char/hw_random/stm32-rng.c b/drivers/char/hw_random/stm32-rng.c
index 6dec4adc49853..00012e6e4ccc8 100644
--- a/drivers/char/hw_random/stm32-rng.c
+++ b/drivers/char/hw_random/stm32-rng.c
@@ -70,6 +70,7 @@ struct stm32_rng_config {

struct stm32_rng_private {
struct hwrng rng;
+ struct device *dev;
void __iomem *base;
struct clk *clk;
struct reset_control *rst;
@@ -99,7 +100,7 @@ struct stm32_rng_private {
*/
static int stm32_rng_conceal_seed_error_cond_reset(struct stm32_rng_private *priv)
{
- struct device *dev = (struct device *)priv->rng.priv;
+ struct device *dev = priv->dev;
u32 sr = readl_relaxed(priv->base + RNG_SR);
u32 cr = readl_relaxed(priv->base + RNG_CR);
int err;
@@ -171,7 +172,7 @@ static int stm32_rng_conceal_seed_error(struct hwrng *rng)
{
struct stm32_rng_private *priv = container_of(rng, struct stm32_rng_private, rng);

- dev_dbg((struct device *)priv->rng.priv, "Concealing seed error\n");
+ dev_dbg(priv->dev, "Concealing seed error\n");

if (priv->data->has_cond_reset)
return stm32_rng_conceal_seed_error_cond_reset(priv);
@@ -187,7 +188,7 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
int retval = 0, err = 0;
u32 sr;

- retval = pm_runtime_resume_and_get((struct device *)priv->rng.priv);
+ retval = pm_runtime_resume_and_get(priv->dev);
if (retval)
return retval;

@@ -206,7 +207,7 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
sr, sr,
10, 50000);
if (err) {
- dev_err((struct device *)priv->rng.priv,
+ dev_err(priv->dev,
"%s: timeout %x!\n", __func__, sr);
break;
}
@@ -220,7 +221,7 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
err = stm32_rng_conceal_seed_error(rng);
i++;
if (err && i > RNG_NB_RECOVER_TRIES) {
- dev_err((struct device *)priv->rng.priv,
+ dev_err(priv->dev,
"Couldn't recover from seed error\n");
retval = -ENOTRECOVERABLE;
goto exit_rpm;
@@ -239,7 +240,7 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
err = stm32_rng_conceal_seed_error(rng);
i++;
if (err && i > RNG_NB_RECOVER_TRIES) {
- dev_err((struct device *)priv->rng.priv,
+ dev_err(priv->dev,
"Couldn't recover from seed error");
retval = -ENOTRECOVERABLE;
goto exit_rpm;
@@ -255,8 +256,8 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
}

exit_rpm:
- pm_runtime_mark_last_busy((struct device *) priv->rng.priv);
- pm_runtime_put_sync_autosuspend((struct device *) priv->rng.priv);
+ pm_runtime_mark_last_busy(priv->dev);
+ pm_runtime_put_sync_autosuspend(priv->dev);

return retval || !wait ? retval : -EIO;
}
@@ -331,8 +332,7 @@ static int stm32_rng_init(struct hwrng *rng)
10, 50000);
if (err) {
clk_disable_unprepare(priv->clk);
- dev_err((struct device *)priv->rng.priv,
- "%s: timeout %x!\n", __func__, reg);
+ dev_err(priv->dev, "%s: timeout %x!\n", __func__, reg);
return -EINVAL;
}
} else {
@@ -360,7 +360,7 @@ static int stm32_rng_init(struct hwrng *rng)
10, 100000);
if (err || (reg & ~RNG_SR_DRDY)) {
clk_disable_unprepare(priv->clk);
- dev_err((struct device *)priv->rng.priv,
+ dev_err(priv->dev,
"%s: timeout:%x SR: %x!\n", __func__, err, reg);
return -EINVAL;
}
@@ -467,7 +467,7 @@ static int __maybe_unused stm32_rng_resume(struct device *dev)

if (err) {
clk_disable_unprepare(priv->clk);
- dev_err((struct device *)priv->rng.priv,
+ dev_err(priv->dev,
"%s: timeout:%x CR: %x!\n", __func__, err, reg);
return -EINVAL;
}
@@ -543,6 +543,7 @@ static int stm32_rng_probe(struct platform_device *ofdev)

priv->ced = of_property_read_bool(np, "clock-error-detect");
priv->lock_conf = of_property_read_bool(np, "st,rng-lock-conf");
+ priv->dev = dev;

priv->data = of_device_get_match_data(dev);
if (!priv->data)
--
2.43.0


2024-05-16 08:14:43

by Gatien CHEVALLIER

[permalink] [raw]
Subject: Re: [PATCH 1/2] hwrng: stm32 - use pm_runtime_resume_and_get()

On 5/16/24 03:20, Marek Vasut wrote:
> include/linux/pm_runtime.h pm_runtime_get_sync() description suggests to
> ... consider using pm_runtime_resume_and_get() instead of it, especially
> if its return value is checked by the caller, as this is likely to result
> in cleaner code.
>
> This is indeed better, switch to pm_runtime_resume_and_get() which
> correctly suspends the device again in case of failure. Also add error
> checking into the RNG driver in case pm_runtime_resume_and_get() does
> fail, which is currently not done, and it does detect sporadic -EACCES
> error return after resume, which would otherwise lead to a hang due to
> register access on un-resumed hardware. Now the read simply errors out
> and the system does not hang.
>
> Signed-off-by: Marek Vasut <[email protected]>
> ---
> Cc: "Uwe Kleine-König" <[email protected]>
> Cc: Alexandre Torgue <[email protected]>
> Cc: Gatien Chevallier <[email protected]>
> Cc: Herbert Xu <[email protected]>
> Cc: Marek Vasut <[email protected]>
> Cc: Maxime Coquelin <[email protected]>
> Cc: Olivia Mackall <[email protected]>
> Cc: Rob Herring <[email protected]>
> Cc: Yang Yingliang <[email protected]>
> Cc: [email protected]
> Cc: [email protected]
> Cc: [email protected]
> Cc: [email protected]
> ---
> drivers/char/hw_random/stm32-rng.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/char/hw_random/stm32-rng.c b/drivers/char/hw_random/stm32-rng.c
> index 0e903d6e22e30..6dec4adc49853 100644
> --- a/drivers/char/hw_random/stm32-rng.c
> +++ b/drivers/char/hw_random/stm32-rng.c
> @@ -187,7 +187,9 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
> int retval = 0, err = 0;
> u32 sr;
>
> - pm_runtime_get_sync((struct device *) priv->rng.priv);
> + retval = pm_runtime_resume_and_get((struct device *)priv->rng.priv);
> + if (retval)
> + return retval;
>
> if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS)
> stm32_rng_conceal_seed_error(rng);

Hi Marek,

I'll check in other stm32 drivers as well.

Acked-by: Gatien Chevallier <[email protected]>

Thanks,
Gatien

2024-05-16 09:38:58

by Gatien CHEVALLIER

[permalink] [raw]
Subject: Re: [PATCH 2/2] hwrng: stm32 - cache device pointer in struct stm32_rng_private



On 5/16/24 03:20, Marek Vasut wrote:
> Place device pointer in struct stm32_rng_private and use it all over the
> place to get rid of the horrible type casts throughout the driver.
>
> No functional change.
>
> Signed-off-by: Marek Vasut <[email protected]>
> ---
> Cc: "Uwe Kleine-König" <[email protected]>
> Cc: Alexandre Torgue <[email protected]>
> Cc: Gatien Chevallier <[email protected]>
> Cc: Herbert Xu <[email protected]>
> Cc: Marek Vasut <[email protected]>
> Cc: Maxime Coquelin <[email protected]>
> Cc: Olivia Mackall <[email protected]>
> Cc: Rob Herring <[email protected]>
> Cc: Yang Yingliang <[email protected]>
> Cc: [email protected]
> Cc: [email protected]
> Cc: [email protected]
> Cc: [email protected]
> ---
> drivers/char/hw_random/stm32-rng.c | 25 +++++++++++++------------
> 1 file changed, 13 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/char/hw_random/stm32-rng.c b/drivers/char/hw_random/stm32-rng.c
> index 6dec4adc49853..00012e6e4ccc8 100644
> --- a/drivers/char/hw_random/stm32-rng.c
> +++ b/drivers/char/hw_random/stm32-rng.c
> @@ -70,6 +70,7 @@ struct stm32_rng_config {
>
> struct stm32_rng_private {
> struct hwrng rng;
> + struct device *dev;
> void __iomem *base;
> struct clk *clk;
> struct reset_control *rst;
> @@ -99,7 +100,7 @@ struct stm32_rng_private {
> */
> static int stm32_rng_conceal_seed_error_cond_reset(struct stm32_rng_private *priv)
> {
> - struct device *dev = (struct device *)priv->rng.priv;
> + struct device *dev = priv->dev;
> u32 sr = readl_relaxed(priv->base + RNG_SR);
> u32 cr = readl_relaxed(priv->base + RNG_CR);
> int err;
> @@ -171,7 +172,7 @@ static int stm32_rng_conceal_seed_error(struct hwrng *rng)
> {
> struct stm32_rng_private *priv = container_of(rng, struct stm32_rng_private, rng);
>
> - dev_dbg((struct device *)priv->rng.priv, "Concealing seed error\n");
> + dev_dbg(priv->dev, "Concealing seed error\n");
>
> if (priv->data->has_cond_reset)
> return stm32_rng_conceal_seed_error_cond_reset(priv);
> @@ -187,7 +188,7 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
> int retval = 0, err = 0;
> u32 sr;
>
> - retval = pm_runtime_resume_and_get((struct device *)priv->rng.priv);
> + retval = pm_runtime_resume_and_get(priv->dev);
> if (retval)
> return retval;
>
> @@ -206,7 +207,7 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
> sr, sr,
> 10, 50000);
> if (err) {
> - dev_err((struct device *)priv->rng.priv,
> + dev_err(priv->dev,
> "%s: timeout %x!\n", __func__, sr);

Nit: Fits in one line

> break;
> }
> @@ -220,7 +221,7 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
> err = stm32_rng_conceal_seed_error(rng);
> i++;
> if (err && i > RNG_NB_RECOVER_TRIES) {
> - dev_err((struct device *)priv->rng.priv,
> + dev_err(priv->dev,
> "Couldn't recover from seed error\n");

Nit: Fits in one line

> retval = -ENOTRECOVERABLE;
> goto exit_rpm;
> @@ -239,7 +240,7 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
> err = stm32_rng_conceal_seed_error(rng);
> i++;
> if (err && i > RNG_NB_RECOVER_TRIES) {
> - dev_err((struct device *)priv->rng.priv,
> + dev_err(priv->dev,
> "Couldn't recover from seed error");

Nit: Fits in one line

> retval = -ENOTRECOVERABLE;
> goto exit_rpm;
> @@ -255,8 +256,8 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
> }
>
> exit_rpm:
> - pm_runtime_mark_last_busy((struct device *) priv->rng.priv);
> - pm_runtime_put_sync_autosuspend((struct device *) priv->rng.priv);
> + pm_runtime_mark_last_busy(priv->dev);
> + pm_runtime_put_sync_autosuspend(priv->dev);
>
> return retval || !wait ? retval : -EIO;
> }
> @@ -331,8 +332,7 @@ static int stm32_rng_init(struct hwrng *rng)
> 10, 50000);
> if (err) {
> clk_disable_unprepare(priv->clk);
> - dev_err((struct device *)priv->rng.priv,
> - "%s: timeout %x!\n", __func__, reg);
> + dev_err(priv->dev, "%s: timeout %x!\n", __func__, reg);
> return -EINVAL;
> }
> } else {
> @@ -360,7 +360,7 @@ static int stm32_rng_init(struct hwrng *rng)
> 10, 100000);
> if (err || (reg & ~RNG_SR_DRDY)) {
> clk_disable_unprepare(priv->clk);
> - dev_err((struct device *)priv->rng.priv,
> + dev_err(priv->dev,
> "%s: timeout:%x SR: %x!\n", __func__, err, reg);

Nit: Fits in one line

> return -EINVAL;
> }
> @@ -467,7 +467,7 @@ static int __maybe_unused stm32_rng_resume(struct device *dev)
>
> if (err) {
> clk_disable_unprepare(priv->clk);
> - dev_err((struct device *)priv->rng.priv,
> + dev_err(priv->dev,
> "%s: timeout:%x CR: %x!\n", __func__, err, reg);
> return -EINVAL;
> }
> @@ -543,6 +543,7 @@ static int stm32_rng_probe(struct platform_device *ofdev)
>
> priv->ced = of_property_read_bool(np, "clock-error-detect");
> priv->lock_conf = of_property_read_bool(np, "st,rng-lock-conf");
> + priv->dev = dev;
>
> priv->data = of_device_get_match_data(dev);
> if (!priv->data)

With nits applied,

Acked-by: Gatien Chevallier <[email protected]>

2024-05-16 10:58:54

by Marek Vasut

[permalink] [raw]
Subject: Re: [PATCH 2/2] hwrng: stm32 - cache device pointer in struct stm32_rng_private

On 5/16/24 11:37 AM, Gatien CHEVALLIER wrote:

Hi,

>> diff --git a/drivers/char/hw_random/stm32-rng.c
>> b/drivers/char/hw_random/stm32-rng.c
>> index 6dec4adc49853..00012e6e4ccc8 100644
>> --- a/drivers/char/hw_random/stm32-rng.c
>> +++ b/drivers/char/hw_random/stm32-rng.c
>> @@ -70,6 +70,7 @@ struct stm32_rng_config {
>>   struct stm32_rng_private {
>>       struct hwrng rng;
>> +    struct device *dev;
>>       void __iomem *base;
>>       struct clk *clk;
>>       struct reset_control *rst;
>> @@ -99,7 +100,7 @@ struct stm32_rng_private {
>>    */
>>   static int stm32_rng_conceal_seed_error_cond_reset(struct
>> stm32_rng_private *priv)
>>   {
>> -    struct device *dev = (struct device *)priv->rng.priv;
>> +    struct device *dev = priv->dev;
>>       u32 sr = readl_relaxed(priv->base + RNG_SR);
>>       u32 cr = readl_relaxed(priv->base + RNG_CR);
>>       int err;
>> @@ -171,7 +172,7 @@ static int stm32_rng_conceal_seed_error(struct
>> hwrng *rng)
>>   {
>>       struct stm32_rng_private *priv = container_of(rng, struct
>> stm32_rng_private, rng);
>> -    dev_dbg((struct device *)priv->rng.priv, "Concealing seed error\n");
>> +    dev_dbg(priv->dev, "Concealing seed error\n");
>>       if (priv->data->has_cond_reset)
>>           return stm32_rng_conceal_seed_error_cond_reset(priv);
>> @@ -187,7 +188,7 @@ static int stm32_rng_read(struct hwrng *rng, void
>> *data, size_t max, bool wait)
>>       int retval = 0, err = 0;
>>       u32 sr;
>> -    retval = pm_runtime_resume_and_get((struct device *)priv->rng.priv);
>> +    retval = pm_runtime_resume_and_get(priv->dev);
>>       if (retval)
>>           return retval;
>> @@ -206,7 +207,7 @@ static int stm32_rng_read(struct hwrng *rng, void
>> *data, size_t max, bool wait)
>>                                      sr, sr,
>>                                      10, 50000);
>>               if (err) {
>> -                dev_err((struct device *)priv->rng.priv,
>> +                dev_err(priv->dev,
>>                       "%s: timeout %x!\n", __func__, sr);
>
> Nit: Fits in one line

The limit is now 100 instead of 80 chars, right ?

btw I found one more and fixed it.

2024-05-16 11:48:57

by Gatien CHEVALLIER

[permalink] [raw]
Subject: Re: [PATCH 2/2] hwrng: stm32 - cache device pointer in struct stm32_rng_private



On 5/16/24 12:52, Marek Vasut wrote:
> On 5/16/24 11:37 AM, Gatien CHEVALLIER wrote:
>
> Hi,
>
>>> diff --git a/drivers/char/hw_random/stm32-rng.c
>>> b/drivers/char/hw_random/stm32-rng.c
>>> index 6dec4adc49853..00012e6e4ccc8 100644
>>> --- a/drivers/char/hw_random/stm32-rng.c
>>> +++ b/drivers/char/hw_random/stm32-rng.c
>>> @@ -70,6 +70,7 @@ struct stm32_rng_config {
>>>   struct stm32_rng_private {
>>>       struct hwrng rng;
>>> +    struct device *dev;
>>>       void __iomem *base;
>>>       struct clk *clk;
>>>       struct reset_control *rst;
>>> @@ -99,7 +100,7 @@ struct stm32_rng_private {
>>>    */
>>>   static int stm32_rng_conceal_seed_error_cond_reset(struct
>>> stm32_rng_private *priv)
>>>   {
>>> -    struct device *dev = (struct device *)priv->rng.priv;
>>> +    struct device *dev = priv->dev;
>>>       u32 sr = readl_relaxed(priv->base + RNG_SR);
>>>       u32 cr = readl_relaxed(priv->base + RNG_CR);
>>>       int err;
>>> @@ -171,7 +172,7 @@ static int stm32_rng_conceal_seed_error(struct
>>> hwrng *rng)
>>>   {
>>>       struct stm32_rng_private *priv = container_of(rng, struct
>>> stm32_rng_private, rng);
>>> -    dev_dbg((struct device *)priv->rng.priv, "Concealing seed
>>> error\n");
>>> +    dev_dbg(priv->dev, "Concealing seed error\n");
>>>       if (priv->data->has_cond_reset)
>>>           return stm32_rng_conceal_seed_error_cond_reset(priv);
>>> @@ -187,7 +188,7 @@ static int stm32_rng_read(struct hwrng *rng, void
>>> *data, size_t max, bool wait)
>>>       int retval = 0, err = 0;
>>>       u32 sr;
>>> -    retval = pm_runtime_resume_and_get((struct device
>>> *)priv->rng.priv);
>>> +    retval = pm_runtime_resume_and_get(priv->dev);
>>>       if (retval)
>>>           return retval;
>>> @@ -206,7 +207,7 @@ static int stm32_rng_read(struct hwrng *rng, void
>>> *data, size_t max, bool wait)
>>>                                      sr, sr,
>>>                                      10, 50000);
>>>               if (err) {
>>> -                dev_err((struct device *)priv->rng.priv,
>>> +                dev_err(priv->dev,
>>>                       "%s: timeout %x!\n", __func__, sr);
>>
>> Nit: Fits in one line
>
> The limit is now 100 instead of 80 chars, right ?
>
> btw I found one more and fixed it.

Yes it is,

thanks