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

From: Jason A. Donenfeld
Date: Wed Feb 14 2024 - 14:58:47 EST


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@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/
[2] https://lore.kernel.org/all/20240209214530.GHZcac-vOym8k3IuJm@fat_crate.local/

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

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)
--
2.43.0