linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] crypto: stm32/crc32 - Avoid lock if hardware is already used
@ 2020-09-04 11:25 Nicolas Toromanoff
  2020-09-11  4:28 ` Herbert Xu
  0 siblings, 1 reply; 3+ messages in thread
From: Nicolas Toromanoff @ 2020-09-04 11:25 UTC (permalink / raw)
  To: Herbert Xu, David S . Miller, Maxime Coquelin, Alexandre Torgue
  Cc: Nicolas Toromanoff, linux-kernel, Ard Biesheuvel, linux-crypto,
	linux-stm32, linux-arm-kernel

If STM32 CRC device is already in use, calculate CRC by software.

This will release CPU constraint for a concurrent access to the
hardware, and avoid masking irqs during the whole block processing.

Fixes: 7795c0baf5ac ("crypto: stm32/crc32 - protect from concurrent accesses")

Signed-off-by: Nicolas Toromanoff <nicolas.toromanoff@st.com>
---
 drivers/crypto/stm32/Kconfig       |  2 ++
 drivers/crypto/stm32/stm32-crc32.c | 15 ++++++++++++---
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/drivers/crypto/stm32/Kconfig b/drivers/crypto/stm32/Kconfig
index 4ef3eb11361c..8d605b07571f 100644
--- a/drivers/crypto/stm32/Kconfig
+++ b/drivers/crypto/stm32/Kconfig
@@ -3,6 +3,8 @@ config CRYPTO_DEV_STM32_CRC
 	tristate "Support for STM32 crc accelerators"
 	depends on ARCH_STM32
 	select CRYPTO_HASH
+	select CRYPTO_CRC32
+	select CRYPTO_CRC32C
 	help
 	  This enables support for the CRC32 hw accelerator which can be found
 	  on STMicroelectronics STM32 SOC.
diff --git a/drivers/crypto/stm32/stm32-crc32.c b/drivers/crypto/stm32/stm32-crc32.c
index 783a64f3f635..75867c0b0017 100644
--- a/drivers/crypto/stm32/stm32-crc32.c
+++ b/drivers/crypto/stm32/stm32-crc32.c
@@ -6,6 +6,7 @@
 
 #include <linux/bitrev.h>
 #include <linux/clk.h>
+#include <linux/crc32.h>
 #include <linux/crc32poly.h>
 #include <linux/io.h>
 #include <linux/kernel.h>
@@ -149,7 +150,6 @@ static int burst_update(struct shash_desc *desc, const u8 *d8,
 	struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
 	struct stm32_crc_ctx *mctx = crypto_shash_ctx(desc->tfm);
 	struct stm32_crc *crc;
-	unsigned long flags;
 
 	crc = stm32_crc_get_next_crc();
 	if (!crc)
@@ -157,7 +157,15 @@ static int burst_update(struct shash_desc *desc, const u8 *d8,
 
 	pm_runtime_get_sync(crc->dev);
 
-	spin_lock_irqsave(&crc->lock, flags);
+	if (!spin_trylock(&crc->lock)) {
+		/* Hardware is busy, calculate crc32 by software */
+		if (mctx->poly == CRC32_POLY_LE)
+			ctx->partial = crc32_le(ctx->partial, d8, length);
+		else
+			ctx->partial = __crc32c_le(ctx->partial, d8, length);
+
+		goto pm_out;
+	}
 
 	/*
 	 * Restore previously calculated CRC for this context as init value
@@ -197,8 +205,9 @@ static int burst_update(struct shash_desc *desc, const u8 *d8,
 	/* Store partial result */
 	ctx->partial = readl_relaxed(crc->regs + CRC_DR);
 
-	spin_unlock_irqrestore(&crc->lock, flags);
+	spin_unlock(&crc->lock);
 
+pm_out:
 	pm_runtime_mark_last_busy(crc->dev);
 	pm_runtime_put_autosuspend(crc->dev);
 
-- 
2.17.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

* Re: [PATCH] crypto: stm32/crc32 - Avoid lock if hardware is already used
  2020-09-04 11:25 [PATCH] crypto: stm32/crc32 - Avoid lock if hardware is already used Nicolas Toromanoff
@ 2020-09-11  4:28 ` Herbert Xu
  2020-09-15 15:54   ` Nicolas TOROMANOFF
  0 siblings, 1 reply; 3+ messages in thread
From: Herbert Xu @ 2020-09-11  4:28 UTC (permalink / raw)
  To: Nicolas Toromanoff
  Cc: Alexandre Torgue, David S . Miller, linux-kernel, linux-stm32,
	linux-crypto, Maxime Coquelin, Ard Biesheuvel, linux-arm-kernel

On Fri, Sep 04, 2020 at 01:25:27PM +0200, Nicolas Toromanoff wrote:
> If STM32 CRC device is already in use, calculate CRC by software.
> 
> This will release CPU constraint for a concurrent access to the
> hardware, and avoid masking irqs during the whole block processing.
> 
> Fixes: 7795c0baf5ac ("crypto: stm32/crc32 - protect from concurrent accesses")
> 
> Signed-off-by: Nicolas Toromanoff <nicolas.toromanoff@st.com>
> ---
>  drivers/crypto/stm32/Kconfig       |  2 ++
>  drivers/crypto/stm32/stm32-crc32.c | 15 ++++++++++++---
>  2 files changed, 14 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/crypto/stm32/Kconfig b/drivers/crypto/stm32/Kconfig
> index 4ef3eb11361c..8d605b07571f 100644
> --- a/drivers/crypto/stm32/Kconfig
> +++ b/drivers/crypto/stm32/Kconfig
> @@ -3,6 +3,8 @@ config CRYPTO_DEV_STM32_CRC
>  	tristate "Support for STM32 crc accelerators"
>  	depends on ARCH_STM32
>  	select CRYPTO_HASH
> +	select CRYPTO_CRC32
> +	select CRYPTO_CRC32C

Shouldn't this be "select CRC32"?

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

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

* Re: [PATCH] crypto: stm32/crc32 - Avoid lock if hardware is already used
  2020-09-11  4:28 ` Herbert Xu
@ 2020-09-15 15:54   ` Nicolas TOROMANOFF
  0 siblings, 0 replies; 3+ messages in thread
From: Nicolas TOROMANOFF @ 2020-09-15 15:54 UTC (permalink / raw)
  To: Herbert Xu
  Cc: Alexandre TORGUE, David S . Miller, linux-kernel, linux-stm32,
	linux-crypto, Maxime Coquelin, Ard Biesheuvel, linux-arm-kernel

Hello Herbert,

On 9/11/20 6:28 AM, Herbert Xu wrote:
> On Fri, Sep 04, 2020 at 01:25:27PM +0200, Nicolas Toromanoff wrote:
>> If STM32 CRC device is already in use, calculate CRC by software.
>>
>> This will release CPU constraint for a concurrent access to the
>> hardware, and avoid masking irqs during the whole block processing.
>>
>> Fixes: 7795c0baf5ac ("crypto: stm32/crc32 - protect from concurrent accesses")
>>
>> Signed-off-by: Nicolas Toromanoff <nicolas.toromanoff@st.com>
>> ---
>>   drivers/crypto/stm32/Kconfig       |  2 ++
>>   drivers/crypto/stm32/stm32-crc32.c | 15 ++++++++++++---
>>   2 files changed, 14 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/crypto/stm32/Kconfig b/drivers/crypto/stm32/Kconfig
>> index 4ef3eb11361c..8d605b07571f 100644
>> --- a/drivers/crypto/stm32/Kconfig
>> +++ b/drivers/crypto/stm32/Kconfig
>> @@ -3,6 +3,8 @@ config CRYPTO_DEV_STM32_CRC
>>   	tristate "Support for STM32 crc accelerators"
>>   	depends on ARCH_STM32
>>   	select CRYPTO_HASH
>> +	select CRYPTO_CRC32
>> +	select CRYPTO_CRC32C
> 
> Shouldn't this be "select CRC32"?

Correct "select CRC32" is enough.
I'll send a version 2 with only "select CRC32".


Nicolas.
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

end of thread, other threads:[~2020-09-15 15:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-04 11:25 [PATCH] crypto: stm32/crc32 - Avoid lock if hardware is already used Nicolas Toromanoff
2020-09-11  4:28 ` Herbert Xu
2020-09-15 15:54   ` Nicolas TOROMANOFF

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