Skip to contentSkip to conversation

Improvements for random.h/archrandom.h

PATCH Superseded Richard Henderson 4 participants 13 messages Oct 28, 2019 – Oct 30, 2019

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Improvements for random.h/archrandom.h

During patch review for an addition of archrandom.h for arm64, it was suggeted that the arch_random_get_* functions should be marked __must_check. Which does sound like a good idea, since the by-reference integer output may be uninitialized when the boolean result is false.

In addition, I noticed a few other minor inconsistencies between the different architectures: x86 defines some functional macros outside CONFIG_ARCH_RANDOM, and powerpc isn't using bool.

r~

Richard Henderson (6):
random: Mark CONFIG_ARCH_RANDOM functions __must_check
x86: Move arch_has_random* inside CONFIG_ARCH_RANDOM
x86: Mark archrandom.h functions __must_check
powerpc: Use bool in archrandom.h
powerpc: Mark archrandom.h functions __must_check
s390x: Mark archrandom.h functions __must_check

 arch/powerpc/include/asm/archrandom.h | 24 +++++++++++++-----------
 arch/s390/include/asm/archrandom.h    |  8 ++++----
 arch/x86/include/asm/archrandom.h     | 24 ++++++++++++------------
 include/linux/random.h                |  8 ++++----
 4 files changed, 33 insertions(+), 31 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/6 random: Mark CONFIG_ARCH_RANDOM functions __must_check

We cannot use the pointer output without validating the success of the random read.

---
Cc: Kees Cook <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>
Cc: [email protected]

---
 include/linux/random.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
include/linux/random.h +4 -4
index f189c927fdea..84947b489649 100644
@@ -167,11 +167,11 @@ static inline void prandom_seed_state(struct rnd_state *state, u64 seed)
 #ifdef CONFIG_ARCH_RANDOM
 # include <asm/archrandom.h>
 #else
-static inline bool arch_get_random_long(unsigned long *v)
+static inline bool __must_check arch_get_random_long(unsigned long *v)
 {
 	return 0;
 }
-static inline bool arch_get_random_int(unsigned int *v)
+static inline bool __must_check arch_get_random_int(unsigned int *v)
 {
 	return 0;
 }
@@ -179,11 +179,11 @@ static inline bool arch_has_random(void)
 {
 	return 0;
 }
-static inline bool arch_get_random_seed_long(unsigned long *v)
+static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
 {
 	return 0;
 }
-static inline bool arch_get_random_seed_int(unsigned int *v)
+static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
 {
 	return 0;
 }
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH 1/6] random: Mark CONFIG_ARCH_RANDOM functions __must_check

Quoted 18 lines · Richard Henderson

We cannot use the pointer output without validating the success of the random read.

---
Cc: Kees Cook <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>
Cc: [email protected]

---
 include/linux/random.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
include/linux/random.h +0 -0
index f189c927fdea..84947b489649 100644

include/linux/random.h static inline void prandom_seed_state(struct rnd_state *state, u64 seed) · line 172

Quoted 7 lines
include/linux/random.h +1 -1
@@ -167,11 +167,11 @@ static inline void prandom_seed_state(struct rnd_state *state, u64 seed)
 #ifdef CONFIG_ARCH_RANDOM
 # include <asm/archrandom.h>
 #else
-static inline bool arch_get_random_long(unsigned long *v)
+static inline bool __must_check arch_get_random_long(unsigned long *v)
 {
        return 0;

For symmetry with the other cleanups, you should probably change these into 'return false' as well

Quoted 28 lines
include/linux/random.h +3 -4
 }
-static inline bool arch_get_random_int(unsigned int *v)
+static inline bool __must_check arch_get_random_int(unsigned int *v)
 {
        return 0;
 }
@@ -179,11 +179,11 @@ static inline bool arch_has_random(void)
 {
        return 0;
 }
-static inline bool arch_get_random_seed_long(unsigned long *v)
+static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
 {
        return 0;
 }
-static inline bool arch_get_random_seed_int(unsigned int *v)
+static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
 {
        return 0;
 }
--

2.17.1

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#3 lore raw

PATCH 2/6 x86: Move arch_has_random* inside CONFIG_ARCH_RANDOM

In reply to Richard Henderson

These functions are declared generically without CONFIG_ARCH_RANDOM. The only reason this compiles for x86 is that we currently have a mix of inline functions are preprocessor defines.

---
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>

---
 arch/x86/include/asm/archrandom.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/include/asm/archrandom.h +4 -4
index af45e1452f09..5904d7d9e703 100644
@@ -73,10 +73,6 @@ static inline bool rdseed_int(unsigned int *v)
 	return ok;
 }
 
-/* Conditional execution based on CPU type */
-#define arch_has_random()	static_cpu_has(X86_FEATURE_RDRAND)
-#define arch_has_random_seed()	static_cpu_has(X86_FEATURE_RDSEED)
-
 /*
  * These are the generic interfaces; they must not be declared if the
  * stubs in <linux/random.h> are to be invoked,
@@ -84,6 +80,10 @@ static inline bool rdseed_int(unsigned int *v)
  */
 #ifdef CONFIG_ARCH_RANDOM
 
+/* Conditional execution based on CPU type */
+#define arch_has_random()	static_cpu_has(X86_FEATURE_RDRAND)
+#define arch_has_random_seed()	static_cpu_has(X86_FEATURE_RDSEED)
+
 static inline bool arch_get_random_long(unsigned long *v)
 {
 	return arch_has_random() ? rdrand_long(v) : false;
Signature · 1 line
2.17.1
#4 lore raw

PATCH 3/6 x86: Mark archrandom.h functions __must_check

In reply to Richard Henderson

We cannot use the pointer output without validating the success of the random read.

---
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>

---
 arch/x86/include/asm/archrandom.h | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
arch/x86/include/asm/archrandom.h +8 -8
index 5904d7d9e703..9e4ea9e53dd0 100644
@@ -27,7 +27,7 @@
 
 /* Unconditional execution of RDRAND and RDSEED */
 
-static inline bool rdrand_long(unsigned long *v)
+static inline bool __must_check rdrand_long(unsigned long *v)
 {
 	bool ok;
 	unsigned int retry = RDRAND_RETRY_LOOPS;
@@ -41,7 +41,7 @@ static inline bool rdrand_long(unsigned long *v)
 	return false;
 }
 
-static inline bool rdrand_int(unsigned int *v)
+static inline bool __must_check rdrand_int(unsigned int *v)
 {
 	bool ok;
 	unsigned int retry = RDRAND_RETRY_LOOPS;
@@ -55,7 +55,7 @@ static inline bool rdrand_int(unsigned int *v)
 	return false;
 }
 
-static inline bool rdseed_long(unsigned long *v)
+static inline bool __must_check rdseed_long(unsigned long *v)
 {
 	bool ok;
 	asm volatile(RDSEED_LONG
@@ -64,7 +64,7 @@ static inline bool rdseed_long(unsigned long *v)
 	return ok;
 }
 
-static inline bool rdseed_int(unsigned int *v)
+static inline bool __must_check rdseed_int(unsigned int *v)
 {
 	bool ok;
 	asm volatile(RDSEED_INT
@@ -84,22 +84,22 @@ static inline bool rdseed_int(unsigned int *v)
 #define arch_has_random()	static_cpu_has(X86_FEATURE_RDRAND)
 #define arch_has_random_seed()	static_cpu_has(X86_FEATURE_RDSEED)
 
-static inline bool arch_get_random_long(unsigned long *v)
+static inline bool __must_check arch_get_random_long(unsigned long *v)
 {
 	return arch_has_random() ? rdrand_long(v) : false;
 }
 
-static inline bool arch_get_random_int(unsigned int *v)
+static inline bool __must_check arch_get_random_int(unsigned int *v)
 {
 	return arch_has_random() ? rdrand_int(v) : false;
 }
 
-static inline bool arch_get_random_seed_long(unsigned long *v)
+static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
 {
 	return arch_has_random_seed() ? rdseed_long(v) : false;
 }
 
-static inline bool arch_get_random_seed_int(unsigned int *v)
+static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
 {
 	return arch_has_random_seed() ? rdseed_int(v) : false;
 }
Signature · 1 line
2.17.1
#5 lore raw

PATCH 4/6 powerpc: Use bool in archrandom.h

In reply to Richard Henderson

The generic interface uses bool not int; match that.

---
Cc: Benjamin Herrenschmidt <[email protected]>
Cc: Paul Mackerras <[email protected]>
Cc: Michael Ellerman <[email protected]>

---
 arch/powerpc/include/asm/archrandom.h | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)
arch/powerpc/include/asm/archrandom.h +13 -11
index 9c63b596e6ce..f8a887c8b7f8 100644
@@ -6,27 +6,28 @@
 
 #include <asm/machdep.h>
 
-static inline int arch_get_random_long(unsigned long *v)
+static inline bool arch_get_random_long(unsigned long *v)
 {
-	return 0;
+	return false;
 }
 
-static inline int arch_get_random_int(unsigned int *v)
+static inline bool arch_get_random_int(unsigned int *v)
 {
-	return 0;
+	return false;
 }
 
-static inline int arch_get_random_seed_long(unsigned long *v)
+static inline bool arch_get_random_seed_long(unsigned long *v)
 {
 	if (ppc_md.get_random_seed)
 		return ppc_md.get_random_seed(v);
 
-	return 0;
+	return false;
 }
-static inline int arch_get_random_seed_int(unsigned int *v)
+
+static inline bool arch_get_random_seed_int(unsigned int *v)
 {
 	unsigned long val;
-	int rc;
+	bool rc;
 
 	rc = arch_get_random_long(&val);
 	if (rc)
@@ -35,15 +36,16 @@ static inline int arch_get_random_seed_int(unsigned int *v)
 	return rc;
 }
 
-static inline int arch_has_random(void)
+static inline bool arch_has_random(void)
 {
-	return 0;
+	return false;
 }
 
-static inline int arch_has_random_seed(void)
+static inline bool arch_has_random_seed(void)
 {
 	return !!ppc_md.get_random_seed;
 }
+
 #endif /* CONFIG_ARCH_RANDOM */
 
 #ifdef CONFIG_PPC_POWERNV
Signature · 1 line
2.17.1
#6 lore raw

PATCH 5/6 powerpc: Mark archrandom.h functions __must_check

In reply to Richard Henderson

We cannot use the pointer output without validating the success of the random read.

---
Cc: Benjamin Herrenschmidt <[email protected]>
Cc: Paul Mackerras <[email protected]>
Cc: Michael Ellerman <[email protected]>

---
 arch/powerpc/include/asm/archrandom.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/powerpc/include/asm/archrandom.h +4 -4
index f8a887c8b7f8..ee214b153a71 100644
@@ -6,17 +6,17 @@
 
 #include <asm/machdep.h>
 
-static inline bool arch_get_random_long(unsigned long *v)
+static inline bool __must_check arch_get_random_long(unsigned long *v)
 {
 	return false;
 }
 
-static inline bool arch_get_random_int(unsigned int *v)
+static inline bool __must_check arch_get_random_int(unsigned int *v)
 {
 	return false;
 }
 
-static inline bool arch_get_random_seed_long(unsigned long *v)
+static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
 {
 	if (ppc_md.get_random_seed)
 		return ppc_md.get_random_seed(v);
@@ -24,7 +24,7 @@ static inline bool arch_get_random_seed_long(unsigned long *v)
 	return false;
 }
 
-static inline bool arch_get_random_seed_int(unsigned int *v)
+static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
 {
 	unsigned long val;
 	bool rc;
Signature · 1 line
2.17.1
reviewer Acked-by on v1 5/6
#7 lore raw

Re: [PATCH 5/6] powerpc: Mark archrandom.h functions __must_check

Quoted 2 lines · Richard Henderson

We cannot use the pointer output without validating the success of the random read.

You _can_, but you must not. </pedant>

Quoted 8 lines

---
Cc: Benjamin Herrenschmidt <[email protected]>
Cc: Paul Mackerras <[email protected]>
Cc: Michael Ellerman <[email protected]>

---
 arch/powerpc/include/asm/archrandom.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

cheers

Quoted 36 lines
arch/powerpc/include/asm/archrandom.h +4 -4
index f8a887c8b7f8..ee214b153a71 100644
@@ -6,17 +6,17 @@
 
 #include <asm/machdep.h>
 
-static inline bool arch_get_random_long(unsigned long *v)
+static inline bool __must_check arch_get_random_long(unsigned long *v)
 {
 	return false;
 }
 
-static inline bool arch_get_random_int(unsigned int *v)
+static inline bool __must_check arch_get_random_int(unsigned int *v)
 {
 	return false;
 }
 
-static inline bool arch_get_random_seed_long(unsigned long *v)
+static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
 {
 	if (ppc_md.get_random_seed)
 		return ppc_md.get_random_seed(v);
@@ -24,7 +24,7 @@ static inline bool arch_get_random_seed_long(unsigned long *v)
 	return false;
 }
 
-static inline bool arch_get_random_seed_int(unsigned int *v)
+static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
 {
 	unsigned long val;
 	bool rc;
Signature · 1 line
2.17.1
#8 lore raw

PATCH 6/6 s390x: Mark archrandom.h functions __must_check

In reply to Richard Henderson

We cannot use the pointer output without validating the success of the random read.

---
Cc: Heiko Carstens <[email protected]>
Cc: Vasily Gorbik <[email protected]>
Cc: Christian Borntraeger <[email protected]>

---
 arch/s390/include/asm/archrandom.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/s390/include/asm/archrandom.h +4 -4
index c67b82dfa558..f3f1ee0a8c38 100644
@@ -33,17 +33,17 @@ static inline bool arch_has_random_seed(void)
 	return false;
 }
 
-static inline bool arch_get_random_long(unsigned long *v)
+static inline bool __must_check arch_get_random_long(unsigned long *v)
 {
 	return false;
 }
 
-static inline bool arch_get_random_int(unsigned int *v)
+static inline bool __must_check arch_get_random_int(unsigned int *v)
 {
 	return false;
 }
 
-static inline bool arch_get_random_seed_long(unsigned long *v)
+static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
 {
 	if (static_branch_likely(&s390_arch_random_available)) {
 		return s390_arch_random_generate((u8 *)v, sizeof(*v));
@@ -51,7 +51,7 @@ static inline bool arch_get_random_seed_long(unsigned long *v)
 	return false;
 }
 
-static inline bool arch_get_random_seed_int(unsigned int *v)
+static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
 {
 	if (static_branch_likely(&s390_arch_random_available)) {
 		return s390_arch_random_generate((u8 *)v, sizeof(*v));
Signature · 1 line
2.17.1
#9 lore raw

Re: [PATCH 6/6] s390x: Mark archrandom.h functions __must_check

Quoted 38 lines · Richard Henderson

We cannot use the pointer output without validating the success of the random read.

---
Cc: Heiko Carstens <[email protected]>
Cc: Vasily Gorbik <[email protected]>
Cc: Christian Borntraeger <[email protected]>

---
 arch/s390/include/asm/archrandom.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/s390/include/asm/archrandom.h +3 -3
index c67b82dfa558..f3f1ee0a8c38 100644
@@ -33,17 +33,17 @@ static inline bool arch_has_random_seed(void)
 	return false;
 }
 
-static inline bool arch_get_random_long(unsigned long *v)
+static inline bool __must_check arch_get_random_long(unsigned long *v)
 {
 	return false;
 }
 
-static inline bool arch_get_random_int(unsigned int *v)
+static inline bool __must_check arch_get_random_int(unsigned int *v)
 {
 	return false;
 }
 
-static inline bool arch_get_random_seed_long(unsigned long *v)
+static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
 {
 	if (static_branch_likely(&s390_arch_random_available)) {
 		return s390_arch_random_generate((u8 *)v, sizeof(*v));

arch/s390/include/asm/archrandom.h static inline bool arch_get_random_seed_long(unsigned long *v) · line 57

Quoted 8 lines
arch/s390/include/asm/archrandom.h +1 -1
@@ -51,7 +51,7 @@ static inline bool arch_get_random_seed_long(unsigned long *v)
 	return false;
 }
 
-static inline bool arch_get_random_seed_int(unsigned int *v)
+static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
 {
 	if (static_branch_likely(&s390_arch_random_available)) {
 		return s390_arch_random_generate((u8 *)v, sizeof(*v));

Fine with me, Thanks, reviewed, build and tested.
You may add my reviewed-by: Harald Freudenberger <[email protected]> However, will this go into the kernel tree via crypto or s390 subsystem ?

#10 lore raw

Re: [PATCH 6/6] s390x: Mark archrandom.h functions __must_check

Quoted 3 lines · Harald Freudenberger

Fine with me, Thanks, reviewed, build and tested.
You may add my reviewed-by: Harald Freudenberger <[email protected]> However, will this go into the kernel tree via crypto or s390 subsystem ?

That's an excellent question.

As an API decision, perhaps going via crypto makes more sense, but none of the patches are dependent on one another, so they could go through separate architecture trees.

It has been a long time since I have done much kernel work; I'm open to suggestions on the subject.

r~

#11 lore raw

Re: [PATCH 6/6] s390x: Mark archrandom.h functions __must_check

Quoted 10 lines · Richard Henderson
Quoted 3 lines · Harald Freudenberger

Fine with me, Thanks, reviewed, build and tested.
You may add my reviewed-by: Harald Freudenberger <[email protected]> However, will this go into the kernel tree via crypto or s390 subsystem ?

That's an excellent question.

As an API decision, perhaps going via crypto makes more sense, but none of the patches are dependent on one another, so they could go through separate architecture trees.

Quoted 5 lines

It has been a long time since I have done much kernel work; I'm open to suggestions on the subject.

r~

Since the change needs to be done in include/linux/random.h
and in parallel with all the arch files in arch/xxx/include/asm/archrandom.h it should go in one shot. I'd suggest to post the patch series to linux-crypto and let Herbert Xu handle this.

Reviewed-by on the v1 series
#12 lore raw
In reply to Richard Henderson
Quoted 15 lines · Richard Henderson

During patch review for an addition of archrandom.h for arm64, it was suggeted that the arch_random_get_* functions should be marked __must_check. Which does sound like a good idea, since the by-reference integer output may be uninitialized when the boolean result is false.

In addition, I noticed a few other minor inconsistencies between the different architectures: x86 defines some functional macros outside CONFIG_ARCH_RANDOM, and powerpc isn't using bool.

r~

Quoted 8 lines

Richard Henderson (6):
random: Mark CONFIG_ARCH_RANDOM functions __must_check
x86: Move arch_has_random* inside CONFIG_ARCH_RANDOM
x86: Mark archrandom.h functions __must_check
powerpc: Use bool in archrandom.h
powerpc: Mark archrandom.h functions __must_check
s390x: Mark archrandom.h functions __must_check

Modulo the nit in reply to 1/6:

Quoted 14 lines
 arch/powerpc/include/asm/archrandom.h | 24 +++++++++++++-----------
 arch/s390/include/asm/archrandom.h    |  8 ++++----
 arch/x86/include/asm/archrandom.h     | 24 ++++++++++++------------
 include/linux/random.h                |  8 ++++----
 4 files changed, 33 insertions(+), 31 deletions(-)

--
2.17.1

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel