Skip to contentSkip to conversation

crypto: inside-secure - fix type of buffer in eip197_write_firmware

PATCH In review Ben Dooks (Codethink) 2 participants 4 messages Oct 16, 2019 – Oct 21, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: inside-secure - fix type of buffer in eip197_write_firmware

In eip197_write_firmware() the firmware buffer is sent using writel(be32_to_cpu(),,,) this produces a number of warnings.

Note, should this really be cpu_to_be32() ?

drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32

---
Cc: Antoine Tenart <[email protected]>
Cc: Herbert Xu <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 drivers/crypto/inside-secure/safexcel.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/crypto/inside-secure/safexcel.c +3 -3
index 223d1bfdc7e6..dd33f6dda295 100644
@@ -298,13 +298,13 @@ static void eip197_init_firmware(struct safexcel_crypto_priv *priv)
 static int eip197_write_firmware(struct safexcel_crypto_priv *priv,
 				  const struct firmware *fw)
 {
-	const u32 *data = (const u32 *)fw->data;
+	const __be32 *data = (const __be32 *)fw->data;
 	int i;
 
 	/* Write the firmware */
-	for (i = 0; i < fw->size / sizeof(u32); i++)
+	for (i = 0; i < fw->size / sizeof(__be32); i++)
 		writel(be32_to_cpu(data[i]),
-		       priv->base + EIP197_CLASSIFICATION_RAMS + i * sizeof(u32));
+		       priv->base + EIP197_CLASSIFICATION_RAMS + i * sizeof(__be32));
 
 	/* Exclude final 2 NOPs from size */
 	return i - EIP197_FW_TERMINAL_NOPS;
Signature · 1 line
2.23.0
#1 lore raw
Quoted 10 lines

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Ben Dooks (Codethink)
Sent: Wednesday, October 16, 2019 1:50 PM
To: [email protected]
Cc: Ben Dooks (Codethink) <[email protected]>; Antoine Tenart <[email protected]>; Herbert Xu <[email protected]>; David S. Miller <[email protected]>; [email protected]; [email protected]
Subject: [PATCH] crypto: inside-secure - fix type of buffer in eip197_write_firmware

Quoted 5 lines

In eip197_write_firmware() the firmware buffer is sent using writel(be32_to_cpu(),,,) this produces a number of warnings.

Note, should this really be cpu_to_be32() ?

No, it should certainly not be cpu_to_be32() since the HW itself is most definitely little endian, so that would not make sense to me.

Actually, I don't think either solution would be correct on a big-endian CPU. But I don't have any big-endian CPU available to test that theory.

What I believe must happen is that the bytes must *always* be swapped here, regardless of the endianness of the CPU. And with a little-endian CPU, be32_to_cpu() coincidentally always does that.

Basically, what we need here is: read a dword (32 bits) from the memory subsystem and write it back to the memory subsystem with bytes reversed.

Does the kernel have any dedicated function for just always swapping?

Anyway: NACK on this patch for now due to this.

Quoted 40 lines

drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32

---
Cc: Antoine Tenart <[email protected]>
Cc: Herbert Xu <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 drivers/crypto/inside-secure/safexcel.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/crypto/inside-secure/safexcel.c +3 -4
index 223d1bfdc7e6..dd33f6dda295 100644
@@ -298,13 +298,13 @@ static void eip197_init_firmware(struct safexcel_crypto_priv *priv)
 static int eip197_write_firmware(struct safexcel_crypto_priv *priv,
 				  const struct firmware *fw)
 {
-	const u32 *data = (const u32 *)fw->data;
+	const __be32 *data = (const __be32 *)fw->data;
 	int i;
 
 	/* Write the firmware */
-	for (i = 0; i < fw->size / sizeof(u32); i++)
+	for (i = 0; i < fw->size / sizeof(__be32); i++)
 		writel(be32_to_cpu(data[i]),
-		       priv->base + EIP197_CLASSIFICATION_RAMS + i * sizeof(u32));
+		       priv->base + EIP197_CLASSIFICATION_RAMS + i * sizeof(__be32));
 
 	/* Exclude final 2 NOPs from size */
 	return i - EIP197_FW_TERMINAL_NOPS;
--
Quoted 1 line

2.23.0

Regards,
Pascal van Leeuwen
Silicon IP Architect, Multi-Protocol Engines @ Verimatrix www.insidesecure.com

#2 lore raw
In reply to Ben Dooks (Codethink)
Quoted 32 lines

-----Original Message-----
From: Pascal Van Leeuwen
Sent: Thursday, October 17, 2019 7:14 PM
To: 'Ben Dooks (Codethink)' <[email protected]>; linux- [email protected]
Cc: Antoine Tenart <[email protected]>; Herbert Xu <[email protected]>; David S. Miller <[email protected]>; linux- [email protected]; [email protected]
Subject: RE: [PATCH] crypto: inside-secure - fix type of buffer in eip197_write_firmware

Quoted 2 lines

-----Original Message-----
From: [email protected] <[email protected]> On Behalf

Of Ben

Quoted 8 lines

Dooks (Codethink)
Sent: Wednesday, October 16, 2019 1:50 PM
To: [email protected]
Cc: Ben Dooks (Codethink) <[email protected]>; Antoine Tenart <[email protected]>; Herbert Xu <[email protected]>; David S. Miller <[email protected]>; [email protected]; [email protected]
Subject: [PATCH] crypto: inside-secure - fix type of buffer in eip197_write_firmware

Quoted 5 lines

In eip197_write_firmware() the firmware buffer is sent using writel(be32_to_cpu(),,,) this produces a number of warnings.

Note, should this really be cpu_to_be32() ?

No, it should certainly not be cpu_to_be32() since the HW itself is most definitely little endian, so that would not make sense to me.

Actually, I don't think either solution would be correct on a big-endian CPU. But I don't have any big-endian CPU available to test that theory.

Quoted 9 lines

What I believe must happen is that the bytes must *always* be swapped here, regardless of the endianness of the CPU. And with a little-endian CPU, be32_to_cpu() coincidentally always does that.

Basically, what we need here is: read a dword (32 bits) from the memory subsystem and write it back to the memory subsystem with bytes reversed.

Does the kernel have any dedicated function for just always swapping?

After some more thought on the train home:

I think the correct construct would be cpu_to_le32(be32_to_cpu(data[i])) This would correctly reflect that the data is read from big-endian memory and subsequently written to little-endian "memory" (aka the EIP). It also fits in nicely with your other changes. Could you work that into a patch v2? Then I would ack it (after testing).

Quoted 41 lines

Anyway: NACK on this patch for now due to this.

drivers/crypto/inside-

Quoted 19 lines

drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32

---
Cc: Antoine Tenart <[email protected]>
Cc: Herbert Xu <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 drivers/crypto/inside-secure/safexcel.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/crypto/inside- renamed from drivers/crypto/inside-secure/safexcel.c +0 -0

secure/safexcel.c

drivers/crypto/inside-secure/safexcel.c static void eip197_init_firmware(struct safexcel_crypto_priv

Quoted 4 lines

index 223d1bfdc7e6..dd33f6dda295 100644

drivers/crypto/inside-secure/safexcel.c +0 -0
@@ -298,13 +298,13 @@ static void eip197_init_firmware(struct safexcel_crypto_priv

*priv)

Quoted 14 lines
drivers/crypto/inside-secure/safexcel.c +3 -3
 static int eip197_write_firmware(struct safexcel_crypto_priv *priv,
 				  const struct firmware *fw)
 {
-	const u32 *data = (const u32 *)fw->data;
+	const __be32 *data = (const __be32 *)fw->data;
 	int i;
 
 	/* Write the firmware */
-	for (i = 0; i < fw->size / sizeof(u32); i++)
+	for (i = 0; i < fw->size / sizeof(__be32); i++)
 		writel(be32_to_cpu(data[i]),
-		       priv->base + EIP197_CLASSIFICATION_RAMS + i * sizeof(u32));
+		       priv->base + EIP197_CLASSIFICATION_RAMS + i * sizeof(__be32));
 
Quoted 9 lines
Quoted 4 lines
drivers/crypto/inside-secure/safexcel.c +0 -1
 	/* Exclude final 2 NOPs from size */
 	return i - EIP197_FW_TERMINAL_NOPS;
--

2.23.0

Regards,
Pascal van Leeuwen
Silicon IP Architect, Multi-Protocol Engines @ Verimatrix www.insidesecure.com

Regards,
Pascal van Leeuwen
Silicon IP Architect, Multi-Protocol Engines @ Verimatrix www.insidesecure.com

Acked-by on v1 1/1
#3 lore raw
In reply to Ben Dooks (Codethink)
Quoted 39 lines

-----Original Message-----
From: Pascal Van Leeuwen
Sent: Thursday, October 17, 2019 9:46 PM
To: 'Ben Dooks (Codethink)' <[email protected]>; '[email protected]' <[email protected]>
Cc: 'Antoine Tenart' <[email protected]>; 'Herbert Xu' <[email protected]>; 'David S. Miller' <[email protected]>; '[email protected]' <linux- [email protected]>; '[email protected]' <[email protected]>
Subject: RE: [PATCH] crypto: inside-secure - fix type of buffer in eip197_write_firmware

Quoted 28 lines

-----Original Message-----
From: Pascal Van Leeuwen
Sent: Thursday, October 17, 2019 7:14 PM
To: 'Ben Dooks (Codethink)' <[email protected]>; linux- [email protected]
Cc: Antoine Tenart <[email protected]>; Herbert Xu <[email protected]>; David S. Miller <[email protected]>; linux- [email protected]; [email protected]
Subject: RE: [PATCH] crypto: inside-secure - fix type of buffer in eip197_write_firmware

Quoted 2 lines

-----Original Message-----
From: [email protected] <[email protected]> On Behalf

Of Ben

Quoted 8 lines

Dooks (Codethink)
Sent: Wednesday, October 16, 2019 1:50 PM
To: [email protected]
Cc: Ben Dooks (Codethink) <[email protected]>; Antoine Tenart <[email protected]>; Herbert Xu <[email protected]>; David S. Miller <[email protected]>; [email protected]; [email protected]
Subject: [PATCH] crypto: inside-secure - fix type of buffer in eip197_write_firmware

Quoted 5 lines

In eip197_write_firmware() the firmware buffer is sent using writel(be32_to_cpu(),,,) this produces a number of warnings.

Note, should this really be cpu_to_be32() ?

No, it should certainly not be cpu_to_be32() since the HW itself is most definitely little endian, so that would not make sense to me.

Never mind that. While looking into more endianness related sparse issues, I realised that the HW register access is actually configured to match the CPU endianness. So the CPU will not need to swap bytes when accessing the hardware registers.
(Technically, the hardware _is_ little endian but our slave interface contains byte swapping logic that is enabled for big-endian CPU's ...)

Quoted 14 lines
Quoted 4 lines

Actually, I don't think either solution would be correct on a big-endian CPU. But I don't have any big-endian CPU available to test that theory.

Quoted 9 lines

What I believe must happen is that the bytes must *always* be swapped here, regardless of the endianness of the CPU. And with a little-endian CPU, be32_to_cpu() coincidentally always does that.

Basically, what we need here is: read a dword (32 bits) from the memory subsystem and write it back to the memory subsystem with bytes reversed.

Does the kernel have any dedicated function for just always swapping?

Quoted 8 lines

After some more thought on the train home:

I think the correct construct would be cpu_to_le32(be32_to_cpu(data[i])) This would correctly reflect that the data is read from big-endian memory and subsequently written to little-endian "memory" (aka the EIP). It also fits in nicely with your other changes. Could you work that into a patch v2? Then I would ack it (after testing).

Since the HW slave ifc is configured to match the CPU endianness, the cpu_to_le32() I suggested should NOT be performed and the code is fine as it was.

Quoted 2 lines
Quoted 2 lines

Anyway: NACK on this patch for now due to this.

Apologies for the mistake and inconvenience. Correction:

Quoted 49 lines
Quoted 39 lines

drivers/crypto/inside-

Quoted 19 lines

drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32 drivers/crypto/inside-secure/safexcel.c:306:17: warning: cast to restricted __be32

---
Cc: Antoine Tenart <[email protected]>
Cc: Herbert Xu <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 drivers/crypto/inside-secure/safexcel.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/crypto/inside- renamed from drivers/crypto/inside-secure/safexcel.c +0 -0

secure/safexcel.c

drivers/crypto/inside-secure/safexcel.c static void eip197_init_firmware(struct safexcel_crypto_priv

Quoted 4 lines

index 223d1bfdc7e6..dd33f6dda295 100644

drivers/crypto/inside-secure/safexcel.c +0 -0
@@ -298,13 +298,13 @@ static void eip197_init_firmware(struct safexcel_crypto_priv

*priv)

Quoted 14 lines
drivers/crypto/inside-secure/safexcel.c +3 -3
 static int eip197_write_firmware(struct safexcel_crypto_priv *priv,
 				  const struct firmware *fw)
 {
-	const u32 *data = (const u32 *)fw->data;
+	const __be32 *data = (const __be32 *)fw->data;
 	int i;
 
 	/* Write the firmware */
-	for (i = 0; i < fw->size / sizeof(u32); i++)
+	for (i = 0; i < fw->size / sizeof(__be32); i++)
 		writel(be32_to_cpu(data[i]),
-		       priv->base + EIP197_CLASSIFICATION_RAMS + i * sizeof(u32));
+		       priv->base + EIP197_CLASSIFICATION_RAMS + i * sizeof(__be32));
 
Quoted 9 lines
Quoted 4 lines
drivers/crypto/inside-secure/safexcel.c +0 -1
 	/* Exclude final 2 NOPs from size */
 	return i - EIP197_FW_TERMINAL_NOPS;
--

2.23.0

Regards,
Pascal van Leeuwen
Silicon IP Architect, Multi-Protocol Engines @ Verimatrix www.insidesecure.com

Quoted 4 lines

Regards,
Pascal van Leeuwen
Silicon IP Architect, Multi-Protocol Engines @ Verimatrix www.insidesecure.com

Regards,
Pascal van Leeuwen
Silicon IP Architect, Multi-Protocol Engines @ Verimatrix www.insidesecure.com