Skip to contentSkip to conversation

crypto: sun4i-ss: prevent always fallback for ciphers

PATCH Superseded Corentin Labbe 4 participants 14 messages Sep 20, 2020 – Sep 25, 2020

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover crypto: sun4i-ss: prevent always fallback for ciphers

Hello

For help testing on "crypto: sun4i-ss - Fix sparse endianness markers", I have added "stats" support like other allwinner's crypto drivers. Seeing stats showed a clear problem, the ciphers function were not used at all.
This is due to the not-inialized need_fallback which is "init" as true everytime.
So basicly, since the patch introduced it, this probem hidden some bugs.

This serie fixes all hidden problems, then fix the initialization of "need_fallback" and then add the stats like other allwinner drivers.

Regards

Changes since v1:
- patch #4 is sufficient to fix BE problem (removed todo)

Corentin Labbe (7):
crypto: sun4i-ss: linearize buffers content must be kept
crypto: sun4i-ss: checking sg length is not sufficient
crypto: sun4i-ss: IV register does not work on A10 and A13
crypto: sun4i-ss: handle BigEndian for cipher
crypto: sun4i-ss: initialize need_fallback
crypto: sun4i-ss: enabled stats via debugfs
crypto: sun4i-ss: add SPDX header and remove blank lines

 drivers/crypto/allwinner/Kconfig              |  9 ++
 .../allwinner/sun4i-ss/sun4i-ss-cipher.c      | 82 ++++++++++++++-----
 .../crypto/allwinner/sun4i-ss/sun4i-ss-core.c | 54 ++++++++++++
 .../crypto/allwinner/sun4i-ss/sun4i-ss-hash.c |  8 ++
 .../crypto/allwinner/sun4i-ss/sun4i-ss-prng.c |  6 ++
 drivers/crypto/allwinner/sun4i-ss/sun4i-ss.h  | 11 +++
 6 files changed, 148 insertions(+), 22 deletions(-)
Signature · 1 line
2.26.2
#3 lore raw

PATCH v2 2/7 crypto: sun4i-ss: checking sg length is not sufficient

The optimized cipher function need length multiple of 4 bytes. But it get sometimes odd length.
This is due to SG data could be stored with an offset.

So the fix is to check also if the offset is aligned with 4 bytes.

Fixes 6298e948215f2 ("crypto: sunxi-ss - Add Allwinner Security System crypto accelerator")
---
 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c +2 -2
index b92d175b5d2a..2614640231dc 100644
@@ -188,12 +188,12 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
 	 * we can use the SS optimized function
 	 */
 	while (in_sg && no_chunk == 1) {
-		if (in_sg->length % 4)
+		if (in_sg->length % 4 || !IS_ALIGNED(in_sg->offset, sizeof(u32)))
 			no_chunk = 0;
 		in_sg = sg_next(in_sg);
 	}
 	while (out_sg && no_chunk == 1) {
-		if (out_sg->length % 4)
+		if (out_sg->length % 4 || !IS_ALIGNED(out_sg->offset, sizeof(u32)))
 			no_chunk = 0;
 		out_sg = sg_next(out_sg);
 	}
Signature · 1 line
2.26.2
#11 lore raw

PATCH v2 6/7 crypto: sun4i-ss: enabled stats via debugfs

In reply to Corentin Labbe

This patch enable to access usage stats for each algorithm.

---
 drivers/crypto/allwinner/Kconfig              |  9 ++++
 .../allwinner/sun4i-ss/sun4i-ss-cipher.c      | 21 ++++++++
 .../crypto/allwinner/sun4i-ss/sun4i-ss-core.c | 54 +++++++++++++++++++
 .../crypto/allwinner/sun4i-ss/sun4i-ss-hash.c |  8 +++
 .../crypto/allwinner/sun4i-ss/sun4i-ss-prng.c |  5 ++
 drivers/crypto/allwinner/sun4i-ss/sun4i-ss.h  | 11 ++++
 6 files changed, 108 insertions(+)
drivers/crypto/allwinner/Kconfig +9 -0
index 0e72543ad1f1..e9b7f7e3d307 100644
@@ -51,6 +51,15 @@ config CRYPTO_DEV_SUN4I_SS_PRNG
 	  Select this option if you want to provide kernel-side support for
 	  the Pseudo-Random Number Generator found in the Security System.
 
+config CRYPTO_DEV_SUN4I_SS_DEBUG
+	bool "Enable sun4i-ss stats"
+	depends on CRYPTO_DEV_SUN4I_SS
+	depends on DEBUG_FS
+	help
+	  Say y to enable sun4i-ss debug stats.
+	  This will create /sys/kernel/debug/sun4i-ss/stats for displaying
+	  the number of requests per algorithm.
+
 config CRYPTO_DEV_SUN8I_CE
 	tristate "Support for Allwinner Crypto Engine cryptographic offloader"
 	select CRYPTO_SKCIPHER
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c +21 -0
index f3bdf465b02e..2ec359eaa4cf 100644
@@ -34,6 +34,10 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
 	struct sg_mapping_iter mi, mo;
 	unsigned int oi, oo; /* offset for in and out */
 	unsigned long flags;
+#ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG
+	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
+	struct sun4i_ss_alg_template *algt;
+#endif
 
 	if (!areq->cryptlen)
 		return 0;
@@ -50,6 +54,12 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
 		scatterwalk_map_and_copy(backup_iv, areq->src, areq->cryptlen - ivsize, ivsize, 0);
 	}
 
+#ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG
+	algt = container_of(alg, struct sun4i_ss_alg_template, alg.crypto);
+	algt->stat_opti++;
+	algt->stat_bytes += areq->cryptlen;
+#endif
+
 	spin_lock_irqsave(&ss->slock, flags);
 
 	for (i = 0; i < op->keylen / 4; i++)
@@ -134,7 +144,13 @@ static int noinline_for_stack sun4i_ss_cipher_poll_fallback(struct skcipher_requ
 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
 	struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
 	int err;
+#ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG
+	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
+	struct sun4i_ss_alg_template *algt;
 
+	algt = container_of(alg, struct sun4i_ss_alg_template, alg.crypto);
+	algt->stat_fb++;
+#endif
 	skcipher_request_set_tfm(&ctx->fallback_req, op->fallback_tfm);
 	skcipher_request_set_callback(&ctx->fallback_req, areq->base.flags,
 				      areq->base.complete, areq->base.data);
@@ -223,6 +239,11 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
 		scatterwalk_map_and_copy(backup_iv, areq->src, areq->cryptlen - ivsize, ivsize, 0);
 	}
 
+#ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG
+	algt->stat_req++;
+	algt->stat_bytes += areq->cryptlen;
+#endif
+
 	spin_lock_irqsave(&ss->slock, flags);
 
 	for (i = 0; i < op->keylen / 4; i++)
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-core.c +54 -0

66 lines not on this page · raw message

drivers/crypto/allwinner/sun4i-ss/sun4i-ss-hash.c +8 -0
index dc35edd90034..3da60256f498 100644
@@ -197,6 +197,10 @@ static int sun4i_hash(struct ahash_request *areq)
 	int in_r, err = 0;
 	size_t copied = 0;
 	__le32 wb = 0;
+#ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG
+	struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
+	struct sun4i_ss_alg_template *algt;
+#endif
 
 	dev_dbg(ss->dev, "%s %s bc=%llu len=%u mode=%x wl=%u h0=%0x",
 		__func__, crypto_tfm_alg_name(areq->base.tfm),
@@ -397,6 +401,10 @@ static int sun4i_hash(struct ahash_request *areq)
  */
 
 hash_final:
+#ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG
+	algt = container_of(alg, struct sun4i_ss_alg_template, alg.hash);
+	algt->stat_req++;
+#endif
 
 	/* write the remaining words of the wait buffer */
 	if (op->len) {
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-prng.c +5 -0
index 729aafdbea84..102f8a90ce0f 100644
@@ -32,6 +32,11 @@ int sun4i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
 	if (err < 0)
 		return err;
 
+#ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG
+	algt->stat_req++;
+	algt->stat_bytes += todo;
+#endif
+
 	spin_lock_bh(&ss->slock);
 
 	writel(mode, ss->base + SS_CTL);
drivers/crypto/allwinner/sun4i-ss/sun4i-ss.h +11 -0
index 163962f9e284..a98a2c05089b 100644
@@ -13,6 +13,7 @@
 
 #include <linux/clk.h>
 #include <linux/crypto.h>
+#include <linux/debugfs.h>
 #include <linux/io.h>
 #include <linux/module.h>
 #include <linux/of.h>
@@ -152,6 +153,10 @@ struct sun4i_ss_ctx {
 #ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_PRNG
 	u32 seed[SS_SEED_LEN / BITS_PER_LONG];
 #endif
+#ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG
+	struct dentry *dbgfs_dir;
+	struct dentry *dbgfs_stats;
+#endif
 };
 
 struct sun4i_ss_alg_template {
@@ -163,6 +168,12 @@ struct sun4i_ss_alg_template {
 		struct rng_alg rng;
 	} alg;
 	struct sun4i_ss_ctx *ss;
+#ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG
+	unsigned long stat_req;
+	unsigned long stat_fb;
+	unsigned long stat_bytes;
+	unsigned long stat_opti;
+#endif
 };
 
 struct sun4i_tfm_ctx {
Signature · 1 line
2.26.2
#1 lore raw

PATCH v2 1/7 crypto: sun4i-ss: linearize buffers content must be kept

In reply to Corentin Labbe

When running the non-optimized cipher function, SS produce partial random output.
This is due to linearize buffers being reseted after each loop.

Fixes 8d3bcb9900ca ("crypto: sun4i-ss - reduce stack usage")
---
 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c +2 -4
index b72de8939497..b92d175b5d2a 100644
@@ -163,6 +163,8 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
 	unsigned int todo;
 	struct sg_mapping_iter mi, mo;
 	unsigned int oi, oo;	/* offset for in and out */
+	char buf[4 * SS_RX_MAX];/* buffer for linearize SG src */
+	char bufo[4 * SS_TX_MAX]; /* buffer for linearize SG dst */
 	unsigned int ob = 0;	/* offset in buf */
 	unsigned int obo = 0;	/* offset in bufo*/
 	unsigned int obl = 0;	/* length of data in bufo */
@@ -233,8 +235,6 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
 
 	while (oleft) {
 		if (ileft) {
-			char buf[4 * SS_RX_MAX];/* buffer for linearize SG src */
-
 			/*
 			 * todo is the number of consecutive 4byte word that we
 			 * can read from current SG
@@ -295,8 +295,6 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
 				oo = 0;
 			}
 		} else {
-			char bufo[4 * SS_TX_MAX]; /* buffer for linearize SG dst */
-
 			/*
 			 * read obl bytes in bufo, we read at maximum for
 			 * emptying the device
Signature · 1 line
2.26.2
#10 lore raw

PATCH v2 5/7 crypto: sun4i-ss: initialize need_fallback

In reply to Corentin Labbe

The need_fallback is never initialized and seem to be always true at runtime. So all hardware operations are always bypassed.

Fixes 0ae1f46c55f87 ("crypto: sun4i-ss - fallback when length is not multiple of blocksize")
---
 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c +1 -1
index a05889745097..f3bdf465b02e 100644
@@ -181,7 +181,7 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
 	unsigned int obo = 0;	/* offset in bufo*/
 	unsigned int obl = 0;	/* length of data in bufo */
 	unsigned long flags;
-	bool need_fallback;
+	bool need_fallback = false;
 
 	if (!areq->cryptlen)
 		return 0;
Signature · 1 line
2.26.2
#13 lore raw

PATCH v2 7/7 crypto: sun4i-ss: add SPDX header and remove blank lines

In reply to Corentin Labbe

This patchs fixes some remaining style issue.

---
 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c | 3 ---
 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-prng.c   | 1 +
 2 files changed, 1 insertion(+), 3 deletions(-)
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c +0 -3
index 2ec359eaa4cf..3f19c00f1b0b 100644
@@ -137,7 +137,6 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
 	return err;
 }
 
-
 static int noinline_for_stack sun4i_ss_cipher_poll_fallback(struct skcipher_request *areq)
 {
 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
@@ -544,7 +543,6 @@ int sun4i_ss_cipher_init(struct crypto_tfm *tfm)
 				    sizeof(struct sun4i_cipher_req_ctx) +
 				    crypto_skcipher_reqsize(op->fallback_tfm));
 
-
 	err = pm_runtime_get_sync(op->ss->dev);
 	if (err < 0)
 		goto error_pm;
@@ -631,5 +629,4 @@ int sun4i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
 	crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
 
 	return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);
-
 }
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-prng.c +1 -0
index 102f8a90ce0f..4d1610952e90 100644
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 #include "sun4i-ss.h"
 
 int sun4i_ss_prng_seed(struct crypto_rng *tfm, const u8 *seed,
Signature · 1 line
2.26.2
#4 lore raw

PATCH v2 3/7 crypto: sun4i-ss: IV register does not work on A10 and A13

In reply to Corentin Labbe

Allwinner A10 and A13 SoC have a version of the SS which produce invalid IV in IVx register.

Instead of adding a variant for those, let's convert SS to produce IV directly from data.

Fixes 6298e948215f2 ("crypto: sunxi-ss - Add Allwinner Security System crypto accelerator")
---
 .../allwinner/sun4i-ss/sun4i-ss-cipher.c      | 34 +++++++++++++++----
 1 file changed, 28 insertions(+), 6 deletions(-)
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c +28 -6
index 2614640231dc..c6c25204780d 100644
@@ -20,6 +20,7 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
 	unsigned int ivsize = crypto_skcipher_ivsize(tfm);
 	struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
 	u32 mode = ctx->mode;
+	void *backup_iv = NULL;
 	/* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */
 	u32 rx_cnt = SS_RX_DEFAULT;
 	u32 tx_cnt = 0;
@@ -42,6 +43,13 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
 		return -EINVAL;
 	}
 
+	if (areq->iv && ivsize > 0 && mode & SS_DECRYPTION) {
+		backup_iv = kzalloc(ivsize, GFP_KERNEL);
+		if (!backup_iv)
+			return -ENOMEM;
+		scatterwalk_map_and_copy(backup_iv, areq->src, areq->cryptlen - ivsize, ivsize, 0);
+	}
+
 	spin_lock_irqsave(&ss->slock, flags);
 
 	for (i = 0; i < op->keylen; i += 4)
@@ -102,9 +110,12 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
 	} while (oleft);
 
 	if (areq->iv) {
-		for (i = 0; i < 4 && i < ivsize / 4; i++) {
-			v = readl(ss->base + SS_IV0 + i * 4);
-			*(u32 *)(areq->iv + i * 4) = v;
+		if (mode & SS_DECRYPTION) {
+			memcpy(areq->iv, backup_iv, ivsize);
+			kfree_sensitive(backup_iv);
+		} else {
+			scatterwalk_map_and_copy(areq->iv, areq->dst, areq->cryptlen - ivsize,
+						 ivsize, 0);
 		}
 	}
 
@@ -161,6 +172,7 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
 	unsigned int ileft = areq->cryptlen;
 	unsigned int oleft = areq->cryptlen;
 	unsigned int todo;
+	void *backup_iv = NULL;
 	struct sg_mapping_iter mi, mo;
 	unsigned int oi, oo;	/* offset for in and out */
 	char buf[4 * SS_RX_MAX];/* buffer for linearize SG src */
@@ -204,6 +216,13 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
 	if (need_fallback)
 		return sun4i_ss_cipher_poll_fallback(areq);
 
+	if (areq->iv && ivsize > 0 && mode & SS_DECRYPTION) {
+		backup_iv = kzalloc(ivsize, GFP_KERNEL);
+		if (!backup_iv)
+			return -ENOMEM;
+		scatterwalk_map_and_copy(backup_iv, areq->src, areq->cryptlen - ivsize, ivsize, 0);
+	}
+
 	spin_lock_irqsave(&ss->slock, flags);
 
 	for (i = 0; i < op->keylen; i += 4)
@@ -324,9 +343,12 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
 		}
 	}
 	if (areq->iv) {
-		for (i = 0; i < 4 && i < ivsize / 4; i++) {
-			v = readl(ss->base + SS_IV0 + i * 4);
-			*(u32 *)(areq->iv + i * 4) = v;
+		if (mode & SS_DECRYPTION) {
+			memcpy(areq->iv, backup_iv, ivsize);
+			kfree_sensitive(backup_iv);
+		} else {
+			scatterwalk_map_and_copy(areq->iv, areq->dst, areq->cryptlen - ivsize,
+						 ivsize, 0);
 		}
 	}
 
Signature · 1 line
2.26.2
#5 lore raw

PATCH v2 4/7 crypto: sun4i-ss: handle BigEndian for cipher

In reply to Corentin Labbe

Ciphers produce invalid results on BE.
Key and IV need to be written in LE.

Fixes 6298e948215f2 ("crypto: sunxi-ss - Add Allwinner Security System crypto accelerator")
---
 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c +6 -6
index c6c25204780d..a05889745097 100644
@@ -52,13 +52,13 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
 
 	spin_lock_irqsave(&ss->slock, flags);
 
-	for (i = 0; i < op->keylen; i += 4)
-		writel(*(op->key + i / 4), ss->base + SS_KEY0 + i);
+	for (i = 0; i < op->keylen / 4; i++)
+		writel(cpu_to_le32(op->key[i]), ss->base + SS_KEY0 + i * 4);
 
 	if (areq->iv) {
 		for (i = 0; i < 4 && i < ivsize / 4; i++) {
 			v = *(u32 *)(areq->iv + i * 4);
-			writel(v, ss->base + SS_IV0 + i * 4);
+			writel(cpu_to_le32(v), ss->base + SS_IV0 + i * 4);
 		}
 	}
 	writel(mode, ss->base + SS_CTL);
@@ -225,13 +225,13 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
 
 	spin_lock_irqsave(&ss->slock, flags);
 
-	for (i = 0; i < op->keylen; i += 4)
-		writel(*(op->key + i / 4), ss->base + SS_KEY0 + i);
+	for (i = 0; i < op->keylen / 4; i++)
+		writel(cpu_to_le32(op->key[i]), ss->base + SS_KEY0 + i * 4);
 
 	if (areq->iv) {
 		for (i = 0; i < 4 && i < ivsize / 4; i++) {
 			v = *(u32 *)(areq->iv + i * 4);
-			writel(v, ss->base + SS_IV0 + i * 4);
+			writel(cpu_to_le32(v), ss->base + SS_IV0 + i * 4);
 		}
 	}
 	writel(mode, ss->base + SS_CTL);
Signature · 1 line
2.26.2
#6 lore raw

Re: [PATCH v2 4/7] crypto: sun4i-ss: handle BigEndian for cipher

I love your patch! Perhaps something to improve:

Hi Corentin,

I love your patch! Perhaps something to improve:

[auto build test WARNING on sunxi/sunxi/for-next]
[also build test WARNING on cryptodev/master crypto/master soc/for-next v5.9-rc6 next-20200923] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Corentin-Labbe/crypto-sun4i-ss-prevent-always-fallback-for-ciphers/20200921-023818
base:   https://git.kernel.org/pub/scm/linux/kernel/git/sunxi/linux.git sunxi/for-next
config: arm64-randconfig-s031-20200923 (attached as .config)
compiler: aarch64-linux-gcc (GCC) 9.3.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.2-201-g24bdaac6-dirty
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=arm64

If you fix the issue, kindly add following tag as appropriate

sparse warnings: (new ones prefixed by >>)

>> drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:56:17: sparse: sparse: cast from restricted __le32
>> drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:56:17: sparse: sparse: incorrect type in argument 1 (different base types) @@     expected unsigned int [usertype] val @@     got restricted __le32 [usertype] @@
>> drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:56:17: sparse:     expected unsigned int [usertype] val
>> drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:56:17: sparse:     got restricted __le32 [usertype]
>> drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:56:17: sparse: sparse: cast from restricted __le32
>> drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:56:17: sparse: sparse: cast from restricted __le32
>> drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:56:17: sparse: sparse: cast from restricted __le32
>> drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:56:17: sparse: sparse: cast from restricted __le32

drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:61:25: sparse: sparse: cast from restricted __le32
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:61:25: sparse: sparse: incorrect type in argument 1 (different base types) @@ expected unsigned int [usertype] val @@ got restricted __le32 [usertype] @@
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:61:25: sparse: expected unsigned int [usertype] val
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:61:25: sparse: got restricted __le32 [usertype]
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:61:25: sparse: sparse: cast from restricted __le32 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:61:25: sparse: sparse: cast from restricted __le32 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:61:25: sparse: sparse: cast from restricted __le32 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:61:25: sparse: sparse: cast from restricted __le32 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:229:17: sparse: sparse: cast from restricted __le32
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:229:17: sparse: sparse: incorrect type in argument 1 (different base types) @@ expected unsigned int [usertype] val @@ got restricted __le32 [usertype] @@
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:229:17: sparse: expected unsigned int [usertype] val
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:229:17: sparse: got restricted __le32 [usertype]
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:229:17: sparse: sparse: cast from restricted __le32 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:229:17: sparse: sparse: cast from restricted __le32 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:229:17: sparse: sparse: cast from restricted __le32 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:229:17: sparse: sparse: cast from restricted __le32 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:234:25: sparse: sparse: cast from restricted __le32
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:234:25: sparse: sparse: incorrect type in argument 1 (different base types) @@ expected unsigned int [usertype] val @@ got restricted __le32 [usertype] @@
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:234:25: sparse: expected unsigned int [usertype] val
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:234:25: sparse: got restricted __le32 [usertype]
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:234:25: sparse: sparse: cast from restricted __le32 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:234:25: sparse: sparse: cast from restricted __le32 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:234:25: sparse: sparse: cast from restricted __le32 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c:234:25: sparse: sparse: cast from restricted __le32

# https://github.com/0day-ci/linux/commit/e7d9839f5afd2cd58a23dd28ee38538dbfef7609
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Corentin-Labbe/crypto-sun4i-ss-prevent-always-fallback-for-ciphers/20200921-023818 git checkout e7d9839f5afd2cd58a23dd28ee38538dbfef7609
vim +56 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c

    14	
    15	static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
    16	{
    17		struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
    18		struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
    19		struct sun4i_ss_ctx *ss = op->ss;
    20		unsigned int ivsize = crypto_skcipher_ivsize(tfm);
    21		struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
    22		u32 mode = ctx->mode;
    23		void *backup_iv = NULL;
    24		/* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */
    25		u32 rx_cnt = SS_RX_DEFAULT;
    26		u32 tx_cnt = 0;
    27		u32 spaces;
    28		u32 v;
    29		int err = 0;
    30		unsigned int i;
    31		unsigned int ileft = areq->cryptlen;
    32		unsigned int oleft = areq->cryptlen;
    33		unsigned int todo;
    34		struct sg_mapping_iter mi, mo;
    35		unsigned int oi, oo; /* offset for in and out */
    36		unsigned long flags;
    37	
    38		if (!areq->cryptlen)
    39			return 0;
    40	
    41		if (!areq->src || !areq->dst) {
    42			dev_err_ratelimited(ss->dev, "ERROR: Some SGs are NULL\n");
    43			return -EINVAL;
    44		}
    45	
    46		if (areq->iv && ivsize > 0 && mode & SS_DECRYPTION) {
    47			backup_iv = kzalloc(ivsize, GFP_KERNEL);
    48			if (!backup_iv)
    49				return -ENOMEM;
    50			scatterwalk_map_and_copy(backup_iv, areq->src, areq->cryptlen - ivsize, ivsize, 0);
    51		}
    52	
    53		spin_lock_irqsave(&ss->slock, flags);
    54	
    55		for (i = 0; i < op->keylen / 4; i++)
  > 56			writel(cpu_to_le32(op->key[i]), ss->base + SS_KEY0 + i * 4);
    57	
    58		if (areq->iv) {
    59			for (i = 0; i < 4 && i < ivsize / 4; i++) {
    60				v = *(u32 *)(areq->iv + i * 4);
    61				writel(cpu_to_le32(v), ss->base + SS_IV0 + i * 4);
    62			}
    63		}
    64		writel(mode, ss->base + SS_CTL);
    65	
    66		sg_miter_start(&mi, areq->src, sg_nents(areq->src),
    67			       SG_MITER_FROM_SG | SG_MITER_ATOMIC);
    68		sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),
    69			       SG_MITER_TO_SG | SG_MITER_ATOMIC);
    70		sg_miter_next(&mi);
    71		sg_miter_next(&mo);
    72		if (!mi.addr || !mo.addr) {
    73			dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
    74			err = -EINVAL;
    75			goto release_ss;
    76		}
    77	
    78		ileft = areq->cryptlen / 4;
    79		oleft = areq->cryptlen / 4;
    80		oi = 0;
    81		oo = 0;
    82		do {
    83			todo = min(rx_cnt, ileft);
    84			todo = min_t(size_t, todo, (mi.length - oi) / 4);
    85			if (todo) {
    86				ileft -= todo;
    87				writesl(ss->base + SS_RXFIFO, mi.addr + oi, todo);
    88				oi += todo * 4;
    89			}
    90			if (oi == mi.length) {
    91				sg_miter_next(&mi);
    92				oi = 0;
    93			}
    94	
    95			spaces = readl(ss->base + SS_FCSR);
    96			rx_cnt = SS_RXFIFO_SPACES(spaces);
    97			tx_cnt = SS_TXFIFO_SPACES(spaces);
    98	
    99			todo = min(tx_cnt, oleft);

100 todo = min_t(size_t, todo, (mo.length - oo) / 4);
101 if (todo) {
102 oleft -= todo;
103 readsl(ss->base + SS_TXFIFO, mo.addr + oo, todo);
104 oo += todo * 4;
105 }
106 if (oo == mo.length) {
107 sg_miter_next(&mo);
108 oo = 0;
109 }
110 } while (oleft);
111
112 if (areq->iv) {
113 if (mode & SS_DECRYPTION) {
114 memcpy(areq->iv, backup_iv, ivsize);
115 kfree_sensitive(backup_iv);
116 } else {
117 scatterwalk_map_and_copy(areq->iv, areq->dst, areq->cryptlen - ivsize,
118 ivsize, 0);
119 }
120 }
121
122 release_ss:
123 sg_miter_stop(&mi);
124 sg_miter_stop(&mo);
125 writel(0, ss->base + SS_CTL);
126 spin_unlock_irqrestore(&ss->slock, flags);
127 return err;
128 }
129

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#7 lore raw

Re: [PATCH v2 4/7] crypto: sun4i-ss: handle BigEndian for cipher

In reply to Corentin Labbe
Quoted 16 lines · Corentin Labbe

Ciphers produce invalid results on BE.
Key and IV need to be written in LE.

Fixes 6298e948215f2 ("crypto: sunxi-ss - Add Allwinner Security System crypto accelerator")
---
 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c +0 -0
index c6c25204780d..a05889745097 100644

drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq) · line 56

Quoted 7 lines
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c +2 -2
@@ -52,13 +52,13 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
 
        spin_lock_irqsave(&ss->slock, flags);
 
-       for (i = 0; i < op->keylen; i += 4)
-               writel(*(op->key + i / 4), ss->base + SS_KEY0 + i);
+       for (i = 0; i < op->keylen / 4; i++)
+               writel(cpu_to_le32(op->key[i]), ss->base + SS_KEY0 + i * 4);

I suspect what you actually want here is writesl() in place of the loop. This skips the byteswap on big-endian, rather than swapping each word twice.

The point is that this register seems to act as a FIFO for a byte-stream rather than a 32-bit fixed-endian register.

Arnd

#8 lore raw

Re: [PATCH v2 4/7] crypto: sun4i-ss: handle BigEndian for cipher

Quoted 29 lines · Arnd Bergmann
Quoted 16 lines · Corentin Labbe

Ciphers produce invalid results on BE.
Key and IV need to be written in LE.

Fixes 6298e948215f2 ("crypto: sunxi-ss - Add Allwinner Security System crypto accelerator")
---
 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c +0 -0
index c6c25204780d..a05889745097 100644

drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq) · line 56

Quoted 7 lines
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c +2 -2
@@ -52,13 +52,13 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
 
        spin_lock_irqsave(&ss->slock, flags);
 
-       for (i = 0; i < op->keylen; i += 4)
-               writel(*(op->key + i / 4), ss->base + SS_KEY0 + i);
+       for (i = 0; i < op->keylen / 4; i++)
+               writel(cpu_to_le32(op->key[i]), ss->base + SS_KEY0 + i * 4);

I suspect what you actually want here is writesl() in place of the loop. This skips the byteswap on big-endian, rather than swapping each word twice.

Quoted 4 lines

The point is that this register seems to act as a FIFO for a byte-stream rather than a 32-bit fixed-endian register.

Arnd

Thanks, using writesl() fixes the warning, but I need to keep the loop since the register is different each time. Or does it is better to use directly __raw_writel() ?

#9 lore raw

Re: [PATCH v2 4/7] crypto: sun4i-ss: handle BigEndian for cipher

Quoted 19 lines · LABBE Corentin
Quoted 18 lines · Arnd Bergmann

drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq) · line 56

Quoted 12 lines · Corentin Labbe
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c +2 -2
index c6c25204780d..a05889745097 100644
@@ -52,13 +52,13 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
 
        spin_lock_irqsave(&ss->slock, flags);
 
-       for (i = 0; i < op->keylen; i += 4)
-               writel(*(op->key + i / 4), ss->base + SS_KEY0 + i);
+       for (i = 0; i < op->keylen / 4; i++)
+               writel(cpu_to_le32(op->key[i]), ss->base + SS_KEY0 + i * 4);

I suspect what you actually want here is writesl() in place of the loop. This skips the byteswap on big-endian, rather than swapping each word twice.

Quoted 5 lines
Quoted 2 lines

The point is that this register seems to act as a FIFO for a byte-stream rather than a 32-bit fixed-endian register.

Thanks, using writesl() fixes the warning, but I need to keep the loop since the register is different each time.

Ah, I see. I thought we had an interface for that as well, but I can't find it now. I see memcpy_toio32() in one driver, but that implementation appears to be wrong here (and probably also wrong for the machine it was meant for)

There is the regular memcpy_toio(), but on big-endian Arm that turns into a per-byte copy, which might either not work on your hardware or be too slow.

There is also __iowrite32_copy(), which is not what I had remembered but does seem to do what you want here.

Quoted 1 line

Or does it is better to use directly __raw_writel() ?

__raw_writel() is not very portable, so I would avoid that in normal device drivers even when you only run them on specific hardware.

Arnd

maintainer
#2 lore raw

Re: [PATCH v2 1/7] crypto: sun4i-ss: linearize buffers content must be kept

In reply to Corentin Labbe
Quoted 24 lines · Corentin Labbe

When running the non-optimized cipher function, SS produce partial random output.
This is due to linearize buffers being reseted after each loop.

Fixes 8d3bcb9900ca ("crypto: sun4i-ss - reduce stack usage")
---
 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c +2 -0
index b72de8939497..b92d175b5d2a 100644
@@ -163,6 +163,8 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
 	unsigned int todo;
 	struct sg_mapping_iter mi, mo;
 	unsigned int oi, oo;	/* offset for in and out */
+	char buf[4 * SS_RX_MAX];/* buffer for linearize SG src */
+	char bufo[4 * SS_TX_MAX]; /* buffer for linearize SG dst */
 	unsigned int ob = 0;	/* offset in buf */
 	unsigned int obo = 0;	/* offset in bufo*/
 	unsigned int obl = 0;	/* length of data in bufo */

drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c static int sun4i_ss_cipher_poll(struct skcipher_request *areq) · line 240

Quoted 8 lines
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c +0 -2
@@ -233,8 +235,6 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
 
 	while (oleft) {
 		if (ileft) {
-			char buf[4 * SS_RX_MAX];/* buffer for linearize SG src */
-
 			/*
 			 * todo is the number of consecutive 4byte word that we
 			 * can read from current SG

Ouch. So this is obviously correct but I wonder if the stack usage would be an issue again?

How about moving this code into another function so that it sits at the same level as the fallback function, which would mean that the buffers do not double up with the one for the fallback?

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
maintainer
#12 lore raw

Re: [PATCH v2 6/7] crypto: sun4i-ss: enabled stats via debugfs

In reply to Corentin Labbe
Quoted 17 lines · Corentin Labbe
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c +4 -0
index f3bdf465b02e..2ec359eaa4cf 100644
@@ -34,6 +34,10 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
 	struct sg_mapping_iter mi, mo;
 	unsigned int oi, oo; /* offset for in and out */
 	unsigned long flags;
+#ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG
+	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
+	struct sun4i_ss_alg_template *algt;
+#endif
 
 	if (!areq->cryptlen)
 		return 0;

drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq) · line 61

Quoted 8 lines
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c +5 -0
@@ -50,6 +54,12 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
 		scatterwalk_map_and_copy(backup_iv, areq->src, areq->cryptlen - ivsize, ivsize, 0);
 	}
 
+#ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG
+	algt = container_of(alg, struct sun4i_ss_alg_template, alg.crypto);
+	algt->stat_opti++;
+	algt->stat_bytes += areq->cryptlen;
+#endif

Please do

	if (IS_ENABLED(CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG)) {
		...
	}

instead so this always gets compiled.

drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)

Quoted 6 lines
drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c +6 -0
+#ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG
+	/* Ignore error of debugfs */
+	ss->dbgfs_dir = debugfs_create_dir("sun4i-ss", NULL);
+	ss->dbgfs_stats = debugfs_create_file("stats", 0444, ss->dbgfs_dir, ss,
+					      &sun4i_ss_debugfs_fops);
+#endif

The calls too be done under an if statement. Only the assignments need to use the ifdef.

drivers/crypto/allwinner/sun4i-ss/sun4i-ss.h line 16

Quoted 9 lines
drivers/crypto/allwinner/sun4i-ss/sun4i-ss.h +1 -0
index 163962f9e284..a98a2c05089b 100644
@@ -13,6 +13,7 @@
 
 #include <linux/clk.h>
 #include <linux/crypto.h>
+#include <linux/debugfs.h>

Please add this into the c files that actually use debugfs, and not here.

drivers/crypto/allwinner/sun4i-ss/sun4i-ss.h struct sun4i_ss_ctx { · line 159

Quoted 11 lines
drivers/crypto/allwinner/sun4i-ss/sun4i-ss.h +4 -0
 #include <linux/io.h>
 #include <linux/module.h>
 #include <linux/of.h>
@@ -152,6 +153,10 @@ struct sun4i_ss_ctx {
 #ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_PRNG
 	u32 seed[SS_SEED_LEN / BITS_PER_LONG];
 #endif
+#ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG
+	struct dentry *dbgfs_dir;
+	struct dentry *dbgfs_stats;
+#endif

You just need to add forward declarations for struct dentry.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt