mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mikhail Paulyshka <me@mixaill.net>
To: Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, linux-kernel@vger.kernel.org
Cc: Mikhail Paulyshka <me@mixaill.net>
Subject: [PATCH 1/2] x86/rdrand: implement sanity check for RDSEED
Date: Wed, 12 Mar 2025 15:31:29 +0300	[thread overview]
Message-ID: <20250312123130.8290-2-me@mixaill.net> (raw)
In-Reply-To: <20250312123130.8290-1-me@mixaill.net>

On AMD Cyan Skillfish (Family 0x17 Model 0x47 Stepping 0x0) there is
a situation where RDRAND works perfectly but RDSEED generates FF's

Performs a separate check for RDRAND and RDSEED as their behavior
may be different.

Signed-off-by: Mikhail Paulyshka <me@mixaill.net>
---
 arch/x86/include/asm/archrandom.h |  1 +
 arch/x86/kernel/cpu/common.c      |  1 +
 arch/x86/kernel/cpu/rdrand.c      | 43 ++++++++++++++++++++++++++++---
 3 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/arch/x86/include/asm/archrandom.h b/arch/x86/include/asm/archrandom.h
index 02bae8e0758b..62ffc8983700 100644
--- a/arch/x86/include/asm/archrandom.h
+++ b/arch/x86/include/asm/archrandom.h
@@ -57,6 +57,7 @@ static inline size_t __must_check arch_get_random_seed_longs(unsigned long *v, s
 
 #ifndef CONFIG_UML
 void x86_init_rdrand(struct cpuinfo_x86 *c);
+void x86_init_rdseed(struct cpuinfo_x86 *c);
 #endif
 
 #endif /* ASM_X86_ARCHRANDOM_H */
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 7cce91b19fb2..277781863210 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -1883,6 +1883,7 @@ static void identify_cpu(struct cpuinfo_x86 *c)
 	}
 
 	x86_init_rdrand(c);
+	x86_init_rdseed(c);
 	setup_pku(c);
 	setup_cet(c);
 
diff --git a/arch/x86/kernel/cpu/rdrand.c b/arch/x86/kernel/cpu/rdrand.c
index eeac00d20926..e9f7ef5dfe25 100644
--- a/arch/x86/kernel/cpu/rdrand.c
+++ b/arch/x86/kernel/cpu/rdrand.c
@@ -12,18 +12,20 @@
 #include <asm/archrandom.h>
 #include <asm/sections.h>
 
+
+enum { SAMPLES = 8, MIN_CHANGE = 5 };
+
 /*
  * RDRAND has Built-In-Self-Test (BIST) that runs on every invocation.
  * Run the instruction a few times as a sanity check. Also make sure
  * it's not outputting the same value over and over, which has happened
  * as a result of past CPU bugs.
  *
- * If it fails, it is simple to disable RDRAND and RDSEED here.
+ * If it fails, it is simple to disable RDRAND here.
  */
 
 void x86_init_rdrand(struct cpuinfo_x86 *c)
 {
-	enum { SAMPLES = 8, MIN_CHANGE = 5 };
 	unsigned long sample, prev;
 	bool failure = false;
 	size_t i, changed;
@@ -44,7 +46,42 @@ void x86_init_rdrand(struct cpuinfo_x86 *c)
 
 	if (failure) {
 		clear_cpu_cap(c, X86_FEATURE_RDRAND);
-		clear_cpu_cap(c, X86_FEATURE_RDSEED);
 		pr_emerg("RDRAND is not reliable on this platform; disabling.\n");
 	}
 }
+
+
+/*
+ * RDSEED has Built-In-Self-Test (BIST) that runs on every invocation.
+ * Run the instruction a few times as a sanity check. Also make sure
+ * it's not outputting the same value over and over, which has happened
+ * as a result of past CPU bugs.
+ *
+ * If it fails, it is simple to disable RDSEED here.
+ */
+
+void x86_init_rdseed(struct cpuinfo_x86 *c)
+{
+	unsigned long sample, prev;
+	bool failure = false;
+	size_t i, changed;
+
+	if (!cpu_has(c, X86_FEATURE_RDSEED))
+		return;
+
+	for (changed = 0, i = 0; i < SAMPLES; ++i) {
+		if (!rdseed_long(&sample)) {
+			failure = true;
+			break;
+		}
+		changed += i && sample != prev;
+		prev = sample;
+	}
+	if (changed < MIN_CHANGE)
+		failure = true;
+
+	if (failure) {
+		clear_cpu_cap(c, X86_FEATURE_RDSEED);
+		pr_emerg("RDSEED is not reliable on this platform; disabling.\n");
+	}
+}
-- 
2.48.1


  reply	other threads:[~2025-03-12 12:40 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-12 12:31 [PATCH 0/2] " Mikhail Paulyshka
2025-03-12 12:31 ` Mikhail Paulyshka [this message]
2025-03-12 16:32   ` [PATCH 1/2] " Borislav Petkov
2025-03-21  6:51   ` kernel test robot
2025-03-12 12:31 ` [PATCH 2/2] x86/rdrand: hide RDRAND and RDSEED from CPUID in case of a malfunction Mikhail Paulyshka
2025-03-12 12:45   ` Borislav Petkov
2025-03-12 13:36     ` Mikhail Paulyshka
2025-03-12 12:40 ` [PATCH 0/2] x86/rdrand: implement sanity check for RDSEED Borislav Petkov
2025-03-12 13:17   ` Mikhail Paulyshka
2025-03-14  8:40     ` Mikhail Paulyshka
2025-03-13 17:32 ` Borislav Petkov
2025-03-14  8:16   ` Mikhail Paulyshka
2025-03-18 20:50 ` Mario Limonciello
2025-03-18 21:40   ` Mikhail Paulyshka
2025-04-14 11:52 ` Borislav Petkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250312123130.8290-2-me@mixaill.net \
    --to=me@mixaill.net \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®