linux-crypto.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] hwrng: stm32 - improve performance
@ 2022-10-12 16:09 Tomas Marek
  2022-10-12 16:09 ` [PATCH 1/2] hwrng: stm32 - fix number of returned bytes on read Tomas Marek
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Tomas Marek @ 2022-10-12 16:09 UTC (permalink / raw)
  To: mpm, herbert
  Cc: mcoquelin.stm32, linux-arm-kernel, linux-crypto, linux-kernel,
	linux-stm32, alexandre.torgue, oleg.karfich, Tomas Marek

Fix issues in the STM32 TRNG driver to improve performance.

Tomas Marek (2):
  hwrng: stm32 - fix number of returned bytes on read
  hwrng: stm32 - fix read of the last word

 drivers/char/hw_random/stm32-rng.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

-- 
2.17.1


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

* [PATCH 1/2] hwrng: stm32 - fix number of returned bytes on read
  2022-10-12 16:09 [PATCH 0/2] hwrng: stm32 - improve performance Tomas Marek
@ 2022-10-12 16:09 ` Tomas Marek
  2022-10-21 10:41   ` Herbert Xu
  2022-10-12 16:09 ` [PATCH 2/2] hwrng: stm32 - fix read of the last word Tomas Marek
  2022-10-21 11:39 ` [PATCH 0/2] hwrng: stm32 - improve performance Herbert Xu
  2 siblings, 1 reply; 6+ messages in thread
From: Tomas Marek @ 2022-10-12 16:09 UTC (permalink / raw)
  To: mpm, herbert
  Cc: mcoquelin.stm32, linux-arm-kernel, linux-crypto, linux-kernel,
	linux-stm32, alexandre.torgue, oleg.karfich, Tomas Marek

The stm32_rng_read() function uses `retval` variable as a counter of
generated random bytes. However, the same variable is used to store
a result of the polling function in case the driver is waiting until
the TRNG is ready. The TRNG generates random numbers by 16B. One
loop read 4B. So, the function calls the polling every 16B, i.e.
every 4th loop. The `retval` counter is reset on poll call and only
number of bytes read after the last poll call is returned to the
caller. The remaining sampled random bytes (for example 48 out of
64 in case 64 bytes are read) are not used.

Use different variable to store the polling function result and
do not overwrite `retval` counter.

Cc: Oleg Karfich <oleg.karfich@wago.com>
Signed-off-by: Tomas Marek <tomas.marek@elrest.cz>
---
 drivers/char/hw_random/stm32-rng.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/char/hw_random/stm32-rng.c b/drivers/char/hw_random/stm32-rng.c
index bc22178f83e8..8eaacefd498b 100644
--- a/drivers/char/hw_random/stm32-rng.c
+++ b/drivers/char/hw_random/stm32-rng.c
@@ -49,11 +49,13 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
 		/* Manage timeout which is based on timer and take */
 		/* care of initial delay time when enabling rng	*/
 		if (!sr && wait) {
-			retval = readl_relaxed_poll_timeout_atomic(priv->base
+			int ret;
+
+			ret = readl_relaxed_poll_timeout_atomic(priv->base
 								   + RNG_SR,
 								   sr, sr,
 								   10, 50000);
-			if (retval)
+			if (ret)
 				dev_err((struct device *)priv->rng.priv,
 					"%s: timeout %x!\n", __func__, sr);
 		}
-- 
2.17.1


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

* [PATCH 2/2] hwrng: stm32 - fix read of the last word
  2022-10-12 16:09 [PATCH 0/2] hwrng: stm32 - improve performance Tomas Marek
  2022-10-12 16:09 ` [PATCH 1/2] hwrng: stm32 - fix number of returned bytes on read Tomas Marek
@ 2022-10-12 16:09 ` Tomas Marek
  2022-10-21 11:39 ` [PATCH 0/2] hwrng: stm32 - improve performance Herbert Xu
  2 siblings, 0 replies; 6+ messages in thread
From: Tomas Marek @ 2022-10-12 16:09 UTC (permalink / raw)
  To: mpm, herbert
  Cc: mcoquelin.stm32, linux-arm-kernel, linux-crypto, linux-kernel,
	linux-stm32, alexandre.torgue, oleg.karfich, Tomas Marek

The stm32_rng_read() function samples TRNG by 4 bytes until at
least 5 bytes are free in the input buffer. The last four bytes
are never read. For example, 60 bytes are returned in case the
input buffer size is 64 bytes.

Read until at least 4 bytes are free in the input buffer. Fill
the buffer entirely in case the buffer size is divisible by 4.

Cc: Oleg Karfich <oleg.karfich@wago.com>
Signed-off-by: Tomas Marek <tomas.marek@elrest.cz>
---
 drivers/char/hw_random/stm32-rng.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/char/hw_random/stm32-rng.c b/drivers/char/hw_random/stm32-rng.c
index 8eaacefd498b..366edda4848b 100644
--- a/drivers/char/hw_random/stm32-rng.c
+++ b/drivers/char/hw_random/stm32-rng.c
@@ -44,7 +44,7 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
 
 	pm_runtime_get_sync((struct device *) priv->rng.priv);
 
-	while (max > sizeof(u32)) {
+	while (max >= sizeof(u32)) {
 		sr = readl_relaxed(priv->base + RNG_SR);
 		/* Manage timeout which is based on timer and take */
 		/* care of initial delay time when enabling rng	*/
-- 
2.17.1


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

* Re: [PATCH 1/2] hwrng: stm32 - fix number of returned bytes on read
  2022-10-12 16:09 ` [PATCH 1/2] hwrng: stm32 - fix number of returned bytes on read Tomas Marek
@ 2022-10-21 10:41   ` Herbert Xu
  2022-10-25 14:41     ` Tomas Marek
  0 siblings, 1 reply; 6+ messages in thread
From: Herbert Xu @ 2022-10-21 10:41 UTC (permalink / raw)
  To: Tomas Marek
  Cc: mpm, herbert, mcoquelin.stm32, linux-arm-kernel, linux-crypto,
	linux-kernel, linux-stm32, alexandre.torgue, oleg.karfich

On Wed, Oct 12, 2022 at 06:09:23PM +0200, Tomas Marek wrote:
>
> diff --git a/drivers/char/hw_random/stm32-rng.c b/drivers/char/hw_random/stm32-rng.c
> index bc22178f83e8..8eaacefd498b 100644
> --- a/drivers/char/hw_random/stm32-rng.c
> +++ b/drivers/char/hw_random/stm32-rng.c
> @@ -49,11 +49,13 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
>  		/* Manage timeout which is based on timer and take */
>  		/* care of initial delay time when enabling rng	*/
>  		if (!sr && wait) {
> -			retval = readl_relaxed_poll_timeout_atomic(priv->base
> +			int ret;
> +
> +			ret = readl_relaxed_poll_timeout_atomic(priv->base

This would make a lot more sense if you called it err instead of ret.

But as you're fixing a real bug I'm going to apply your patch as is
and you can post an incremental patch to improve it.

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] 6+ messages in thread

* Re: [PATCH 0/2] hwrng: stm32 - improve performance
  2022-10-12 16:09 [PATCH 0/2] hwrng: stm32 - improve performance Tomas Marek
  2022-10-12 16:09 ` [PATCH 1/2] hwrng: stm32 - fix number of returned bytes on read Tomas Marek
  2022-10-12 16:09 ` [PATCH 2/2] hwrng: stm32 - fix read of the last word Tomas Marek
@ 2022-10-21 11:39 ` Herbert Xu
  2 siblings, 0 replies; 6+ messages in thread
From: Herbert Xu @ 2022-10-21 11:39 UTC (permalink / raw)
  To: Tomas Marek
  Cc: mpm, mcoquelin.stm32, linux-arm-kernel, linux-crypto,
	linux-kernel, linux-stm32, alexandre.torgue, oleg.karfich

On Wed, Oct 12, 2022 at 06:09:22PM +0200, Tomas Marek wrote:
> Fix issues in the STM32 TRNG driver to improve performance.
> 
> Tomas Marek (2):
>   hwrng: stm32 - fix number of returned bytes on read
>   hwrng: stm32 - fix read of the last word
> 
>  drivers/char/hw_random/stm32-rng.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> -- 
> 2.17.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] 6+ messages in thread

* Re: [PATCH 1/2] hwrng: stm32 - fix number of returned bytes on read
  2022-10-21 10:41   ` Herbert Xu
@ 2022-10-25 14:41     ` Tomas Marek
  0 siblings, 0 replies; 6+ messages in thread
From: Tomas Marek @ 2022-10-25 14:41 UTC (permalink / raw)
  To: Herbert Xu
  Cc: mpm, mcoquelin.stm32, linux-arm-kernel, linux-crypto,
	linux-kernel, linux-stm32, alexandre.torgue, oleg.karfich

On Fri, Oct 21, 2022 at 06:41:07PM +0800, Herbert Xu wrote:
> On Wed, Oct 12, 2022 at 06:09:23PM +0200, Tomas Marek wrote:
> >
> > diff --git a/drivers/char/hw_random/stm32-rng.c b/drivers/char/hw_random/stm32-rng.c
> > index bc22178f83e8..8eaacefd498b 100644
> > --- a/drivers/char/hw_random/stm32-rng.c
> > +++ b/drivers/char/hw_random/stm32-rng.c
> > @@ -49,11 +49,13 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
> >  		/* Manage timeout which is based on timer and take */
> >  		/* care of initial delay time when enabling rng	*/
> >  		if (!sr && wait) {
> > -			retval = readl_relaxed_poll_timeout_atomic(priv->base
> > +			int ret;
> > +
> > +			ret = readl_relaxed_poll_timeout_atomic(priv->base
> 
> This would make a lot more sense if you called it err instead of ret.
> 
> But as you're fixing a real bug I'm going to apply your patch as is
> and you can post an incremental patch to improve it.

OK, sounds reasonable. I'll post new patch and rename ret to err.

Thanks for the hint and for the review.

Tomas

> 
> 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] 6+ messages in thread

end of thread, other threads:[~2022-10-25 14:42 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-12 16:09 [PATCH 0/2] hwrng: stm32 - improve performance Tomas Marek
2022-10-12 16:09 ` [PATCH 1/2] hwrng: stm32 - fix number of returned bytes on read Tomas Marek
2022-10-21 10:41   ` Herbert Xu
2022-10-25 14:41     ` Tomas Marek
2022-10-12 16:09 ` [PATCH 2/2] hwrng: stm32 - fix read of the last word Tomas Marek
2022-10-21 11:39 ` [PATCH 0/2] hwrng: stm32 - improve performance Herbert Xu

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).