Re: [PATCH 2/2] x86/random: Issue a warning if RDRAND or RDSEED fails

Kuppuswamy Sathyanarayanan
2024-01-30T15:50:43Z

On 1/30/24 12:30 AM, Kirill A. Shutemov wrote:
> RDRAND and RDSEED instructions rarely fail. Ten retries should be
> sufficient to account for occasional failures.
>
> If the instruction fails more than ten times, it is likely that the
> hardware is broken or someone is attempting to exceed the rate at which
> the random number generator hardware can provide random numbers.
>
> Issue a warning if ten retries were not enough.

Did you come across a case where it fails? Wondering why add this
warning now?

>
> Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
> ---
> arch/x86/include/asm/archrandom.h | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/arch/x86/include/asm/archrandom.h b/arch/x86/include/asm/archrandom.h
> index 918c5880de9e..fc8d837fb3b9 100644
> --- a/arch/x86/include/asm/archrandom.h
> +++ b/arch/x86/include/asm/archrandom.h
> @@ -13,6 +13,12 @@
> #include <asm/processor.h>
> #include <asm/cpufeature.h>
>
> +#ifdef KASLR_COMPRESSED_BOOT
> +#define rd_warn(msg) warn(msg)

Why not use warn_once in both cases?

> +#else
> +#define rd_warn(msg) WARN_ONCE(1, msg)
> +#endif
> +
> #define RDRAND_RETRY_LOOPS 10
>
> /* Unconditional execution of RDRAND and RDSEED */
> @@ -28,6 +34,9 @@ static inline bool __must_check rdrand_long(unsigned long *v)
> if (ok)
> return true;
> } while (--retry);
> +
> + rd_warn("RDRAND failed\n");
> +
> return false;
> }
>
> @@ -45,6 +54,8 @@ static inline bool __must_check rdseed_long(unsigned long *v)
> return true;
> } while (--retry);
>
> + rd_warn("RDSEED failed\n");
> +
> return false;
> }
>

--
Sathyanarayanan Kuppuswamy
Linux Kernel Developer