[RFC] Generic crypto counters

2012-06-15 Thread Jan Glauber
Hi Herbert,

I've noticed that the powerpc folks were able to sneak counters for
their hardware crypto implementation into upstream [1].

Simple counters for the number of processed bytes per algorithm is
something which I wanted to have for some time now. The reason is that
its not obvious to tell if the hardware or software implementation
was used. The only way to determine this currently is to look at the
speed of the operation or add debug output, both methods kind of suck.

So I could also brew up some debugfs files for s390 CPACF but I
wonder if we shouldn't come up with a generic counter implementation
for all crypto algorithms. The output could be added to the
existing per algorithm values under /proc/crypto.

Below is a quick hack to implement these counters for the shash
algorithms, just to show what I'm thinking about...

Is this approach feasible? Can we have something generic? Or should I also go
the debugfs way?

thanks,
Jan

[1] drivers/crypto/nx/nx_debugfs.c

diff --git a/crypto/algapi.c b/crypto/algapi.c
index 056571b..001842b 100644
--- a/crypto/algapi.c
+++ b/crypto/algapi.c
@@ -190,6 +190,10 @@ static struct crypto_larval *__crypto_register_alg(struct 
crypto_alg *alg)
/* No cheating! */
alg-cra_flags = ~CRYPTO_ALG_TESTED;
 
+   /* Init statistics */
+   atomic_set(alg-cra_stats.ops, 0);
+   atomic_set(alg-cra_stats.bytes, 0);
+
ret = -EEXIST;
 
atomic_set(alg-cra_refcnt, 1);
diff --git a/crypto/proc.c b/crypto/proc.c
index 4a0a7aa..c167e79 100644
--- a/crypto/proc.c
+++ b/crypto/proc.c
@@ -96,6 +96,14 @@ static int c_show(struct seq_file *m, void *p)
goto out;
}
 
+   /* only show for supported types */
+   if (alg-cra_flags  CRYPTO_ALG_TYPE_SHASH) {
+   seq_printf(m, operations   : %u\n,
+  atomic_read(alg-cra_stats.ops));
+   seq_printf(m, bytes: %Lu\n,
+  atomic_read(alg-cra_stats.bytes));
+   }
+
if (alg-cra_type  alg-cra_type-show) {
alg-cra_type-show(m, alg);
goto out;
diff --git a/crypto/shash.c b/crypto/shash.c
index 21fc12e..e842899 100644
--- a/crypto/shash.c
+++ b/crypto/shash.c
@@ -100,11 +100,15 @@ int crypto_shash_update(struct shash_desc *desc, const u8 
*data,
struct crypto_shash *tfm = desc-tfm;
struct shash_alg *shash = crypto_shash_alg(tfm);
unsigned long alignmask = crypto_shash_alignmask(tfm);
+   int err;
 
if ((unsigned long)data  alignmask)
-   return shash_update_unaligned(desc, data, len);
-
-   return shash-update(desc, data, len);
+   err = shash_update_unaligned(desc, data, len);
+   else
+   err = shash-update(desc, data, len);
+   if (!err)
+   crypto_stat_inc(desc-tfm-base, len);
+   return err;
 }
 EXPORT_SYMBOL_GPL(crypto_shash_update);
 
@@ -135,11 +139,15 @@ int crypto_shash_final(struct shash_desc *desc, u8 *out)
struct crypto_shash *tfm = desc-tfm;
struct shash_alg *shash = crypto_shash_alg(tfm);
unsigned long alignmask = crypto_shash_alignmask(tfm);
+   int err;
 
if ((unsigned long)out  alignmask)
-   return shash_final_unaligned(desc, out);
-
-   return shash-final(desc, out);
+   err = shash_final_unaligned(desc, out);
+   else
+   err = shash-final(desc, out);
+   if (!err)
+   crypto_stat_inc(desc-tfm-base, 
crypto_shash_digestsize(desc-tfm));
+   return err;
 }
 EXPORT_SYMBOL_GPL(crypto_shash_final);
 
@@ -156,11 +164,15 @@ int crypto_shash_finup(struct shash_desc *desc, const u8 
*data,
struct crypto_shash *tfm = desc-tfm;
struct shash_alg *shash = crypto_shash_alg(tfm);
unsigned long alignmask = crypto_shash_alignmask(tfm);
+   int err;
 
if (((unsigned long)data | (unsigned long)out)  alignmask)
-   return shash_finup_unaligned(desc, data, len, out);
-
-   return shash-finup(desc, data, len, out);
+   err = shash_finup_unaligned(desc, data, len, out);
+   else
+   err = shash-finup(desc, data, len, out);
+   if (!err)
+   crypto_stat_inc(desc-tfm-base, len);
+   return err;
 }
 EXPORT_SYMBOL_GPL(crypto_shash_finup);
 
@@ -177,11 +189,15 @@ int crypto_shash_digest(struct shash_desc *desc, const u8 
*data,
struct crypto_shash *tfm = desc-tfm;
struct shash_alg *shash = crypto_shash_alg(tfm);
unsigned long alignmask = crypto_shash_alignmask(tfm);
+   int err;
 
if (((unsigned long)data | (unsigned long)out)  alignmask)
-   return shash_digest_unaligned(desc, data, len, out);
-
-   return shash-digest(desc, data, len, out);
+   err = shash_digest_unaligned(desc, data, len, out);
+   else
+   err = shash-digest(desc, data, len, out);
+   if (!err)
+   

Re: RFC: support for MV_CESA with IDMA or TDMA

2012-06-15 Thread Phil Sutter
Hi,

On Fri, Jun 15, 2012 at 09:40:28AM +0800, cloudy.linux wrote:
 I would like to have a try on those patches. But what version of kernel 
 should I apply those patches on?

Sorry for the caused confusion. I have applied those patches to linus'
git, preceded by the three accepted ones of the earlier four. Yay.

Long story short, please just fetch git://nwl.cc/~n0-1/linux.git and
checkout the 'cesa-dma' branch. It's exactly what I formatted the
patches from.

Greetings, Phil


Phil Sutter
Software Engineer

-- 


Viprinet GmbH
Mainzer Str. 43
55411 Bingen am Rhein
Germany

Phone/Zentrale: +49-6721-49030-0
Direct line/Durchwahl:  +49-6721-49030-134
Fax:+49-6721-49030-209

phil.sut...@viprinet.com
http://www.viprinet.com

Registered office/Sitz der Gesellschaft: Bingen am Rhein
Commercial register/Handelsregister: Amtsgericht Mainz HRB40380
CEO/Geschäftsführer: Simon Kissel
--
To unsubscribe from this list: send the line unsubscribe linux-crypto in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 0/2] Fixes for MV_CESA with IDMA or TDMA

2012-06-15 Thread Simon Baatz
Hi Phil,

thanks for providing these patches; it's great to finally see DMA
support for CESA in the kernel. Additionally, the implementation seems
to be fine regarding cache incoherencies (at least my test in [*]
works).

I have two patches for your patchset...

- Fix for mv_init_engine error handling

- My system locked up hard when mv_dma and mv_cesa were built as
  modules. mv_cesa has code to enable the crypto clock in 3.5, but
  mv_dma already accesses the CESA engine before. Thus, we need to
  enable this clock here, too.

[*] http://www.spinics.net/lists/arm-kernel/msg176913.html

Simon Baatz (2):
  mv_dma: fix mv_init_engine() error case
  ARM: Orion: mv_dma: Add support for clk

 arch/arm/mach-kirkwood/common.c |1 +
 drivers/crypto/mv_dma.c |   18 +++---
 2 files changed, 16 insertions(+), 3 deletions(-)

-- 
1.7.9.5

--
To unsubscribe from this list: send the line unsubscribe linux-crypto in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 1/2] mv_dma: fix mv_init_engine() error case

2012-06-15 Thread Simon Baatz
Fix wrongly placed free_irq in mv_init_engine() error recovery. In fact, we
can remove the respective label, since request_irq() is the last thing the
function does anyway.

Signed-off-by: Simon Baatz gmbno...@gmail.com
---
 drivers/crypto/mv_dma.c |4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/crypto/mv_dma.c b/drivers/crypto/mv_dma.c
index b84ff80..125dfee 100644
--- a/drivers/crypto/mv_dma.c
+++ b/drivers/crypto/mv_dma.c
@@ -296,7 +296,7 @@ static int mv_init_engine(struct platform_device *pdev,
if (init_dma_desclist(tpg.desclist, tpg.dev,
sizeof(struct mv_dma_desc), MV_DMA_ALIGN, 0)) {
rc = -ENOMEM;
-   goto out_free_irq;
+   goto out_unmap_reg;
}
if (set_dma_desclist_size(tpg.desclist, MV_DMA_INIT_POOLSIZE)) {
rc = -ENOMEM;
@@ -330,8 +330,6 @@ out_free_all:
platform_set_drvdata(pdev, NULL);
 out_free_desclist:
fini_dma_desclist(tpg.desclist);
-out_free_irq:
-   free_irq(tpg.irq, tpg);
 out_unmap_reg:
iounmap(tpg.reg);
tpg.dev = NULL;
-- 
1.7.9.5

--
To unsubscribe from this list: send the line unsubscribe linux-crypto in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 2/2] ARM: Orion: mv_dma: Add support for clk

2012-06-15 Thread Simon Baatz
mv_dma needs the crypto clock. Some orion platforms support gating of the
clock.  If the clock exists enable/disable it as appropriate.

Signed-off-by: Simon Baatz gmbno...@gmail.com
---
 arch/arm/mach-kirkwood/common.c |1 +
 drivers/crypto/mv_dma.c |   14 ++
 2 files changed, 15 insertions(+)

diff --git a/arch/arm/mach-kirkwood/common.c b/arch/arm/mach-kirkwood/common.c
index 560b920..e7bbc60 100644
--- a/arch/arm/mach-kirkwood/common.c
+++ b/arch/arm/mach-kirkwood/common.c
@@ -234,6 +234,7 @@ void __init kirkwood_clk_init(void)
orion_clkdev_add(NULL, orion-ehci.0, usb0);
orion_clkdev_add(NULL, orion_nand, runit);
orion_clkdev_add(NULL, mvsdio, sdio);
+   orion_clkdev_add(NULL, mv_tdma, crypto);
orion_clkdev_add(NULL, mv_crypto, crypto);
orion_clkdev_add(NULL, MV_XOR_SHARED_NAME .0, xor0);
orion_clkdev_add(NULL, MV_XOR_SHARED_NAME .1, xor1);
diff --git a/drivers/crypto/mv_dma.c b/drivers/crypto/mv_dma.c
index 125dfee..9fdb7be 100644
--- a/drivers/crypto/mv_dma.c
+++ b/drivers/crypto/mv_dma.c
@@ -13,6 +13,7 @@
 #include linux/dmapool.h
 #include linux/interrupt.h
 #include linux/module.h
+#include linux/clk.h
 #include linux/slab.h
 #include linux/platform_device.h
 
@@ -36,6 +37,7 @@ struct mv_dma_priv {
struct device *dev;
void __iomem *reg;
int irq;
+   struct clk *clk;
/* protecting the dma descriptors and stuff */
spinlock_t lock;
struct dma_desclist desclist;
@@ -292,6 +294,12 @@ static int mv_init_engine(struct platform_device *pdev,
goto out_unmap_reg;
}
 
+   /* Not all platforms can gate the clock, so it is not
+  an error if the clock does not exists. */
+   tpg.clk = clk_get(pdev-dev, NULL);
+   if (!IS_ERR(tpg.clk))
+   clk_prepare_enable(tpg.clk);
+
/* initialise DMA descriptor list */
if (init_dma_desclist(tpg.desclist, tpg.dev,
sizeof(struct mv_dma_desc), MV_DMA_ALIGN, 0)) {
@@ -343,6 +351,12 @@ static int mv_remove(struct platform_device *pdev)
fini_dma_desclist(tpg.desclist);
free_irq(tpg.irq, tpg);
iounmap(tpg.reg);
+
+   if (!IS_ERR(tpg.clk)) {
+   clk_disable_unprepare(tpg.clk);
+   clk_put(tpg.clk);
+   }
+
tpg.dev = NULL;
return 0;
 }
-- 
1.7.9.5

--
To unsubscribe from this list: send the line unsubscribe linux-crypto in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html