[v2,1/2] x86/archrandom: WARN if RDRAND fails and don't retry

Message ID 20240214195744.8332-2-Jason@zx2c4.com
State New
Headers
Series CoCo/RDRAND brokenness fixes |

Commit Message

Jason A. Donenfeld Feb. 14, 2024, 7:56 p.m. UTC
  According to both Intel [1] and AMD [2], RDRAND will never fail unless
the hardware is defective. The old advice to try 10 times is not
considered correct.

RDSEED is a different story. But with RDRAND never failing, WARN()ing
when it does seems like a reasonable thing to do, as it's probably
indicative of some deeper problem.

[1] https://lore.kernel.org/all/DM8PR11MB57503A2BB6F74618D64CC44AE74E2@DM8PR11MB5750.namprd11.prod.outlook.com/
[2] https://lore.kernel.org/all/20240209214530.GHZcac-vOym8k3IuJm@fat_crate.local/

Cc: Borislav Petkov <bp@alien8.de>
Cc: Daniel P. Berrangé <berrange@redhat.com>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Elena Reshetova <elena.reshetova@intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: Theodore Ts'o <tytso@mit.edu>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 arch/x86/include/asm/archrandom.h | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
  

Patch

diff --git a/arch/x86/include/asm/archrandom.h b/arch/x86/include/asm/archrandom.h
index 02bae8e0758b..1cc01ecd5e75 100644
--- a/arch/x86/include/asm/archrandom.h
+++ b/arch/x86/include/asm/archrandom.h
@@ -13,22 +13,18 @@ 
 #include <asm/processor.h>
 #include <asm/cpufeature.h>
 
-#define RDRAND_RETRY_LOOPS	10
-
 /* Unconditional execution of RDRAND and RDSEED */
 
 static inline bool __must_check rdrand_long(unsigned long *v)
 {
 	bool ok;
-	unsigned int retry = RDRAND_RETRY_LOOPS;
-	do {
-		asm volatile("rdrand %[out]"
-			     CC_SET(c)
-			     : CC_OUT(c) (ok), [out] "=r" (*v));
-		if (ok)
-			return true;
-	} while (--retry);
-	return false;
+	asm volatile("rdrand %[out]"
+		     CC_SET(c)
+		     : CC_OUT(c) (ok), [out] "=r" (*v));
+#ifndef KASLR_COMPRESSED_BOOT
+	WARN_ON(!ok);
+#endif
+	return ok;
 }
 
 static inline bool __must_check rdseed_long(unsigned long *v)