Skip to contentSkip to conversation

x86: clear RDRAND CPUID bit on AMD family 15h/16h

PATCH Superseded Jan Beulich 3 participants 6 messages Mar 9, 2020 – May 18, 2020

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 x86: clear RDRAND CPUID bit on AMD family 15h/16h

Inspired by Linux commit c49a0a80137c7ca7d6ced4c812c9e07a949f6f24:

There have been reports of RDRAND issues after resuming from suspend on some AMD family 15h and family 16h systems. This issue stems from a BIOS not performing the proper steps during resume to ensure RDRAND continues to function properly.

Update the CPU initialization to clear the RDRAND CPUID bit for any family 15h and 16h processor that supports RDRAND. If it is known that the family 15h or family 16h system does not have an RDRAND resume issue or that the system will not be placed in suspend, the "cpuid=rdrand" kernel parameter can be used to stop the clearing of the RDRAND CPUID bit.

Note, that clearing the RDRAND CPUID bit does not prevent a processor that normally supports the RDRAND instruction from executing it. So any code that determined the support based on family and model won't #UD.

Warn if no explicit choice was given on affected hardware.

Check RDRAND functions at boot as well as after S3 resume (the retry limit chosen is entirely arbitrary).

---
Still slightly RFC, and still in particular because of the change to parse_xen_cpuid(): Alternative approach suggestions are welcome. But now also because with many CPUs there may now be a lot of warnings in case of issues.
---
v4: Check always, including during boot. Slightly better sanity check,
inspired by Linux commit 7879fc4bdc7.
v3: Add call to warning_add(). If force-enabled, check RDRAND still
functioning after S3 resume.
v2: Re-base.

docs/misc/xen-command-line.pandoc +4 -0
@@ -488,6 +488,10 @@ The Speculation Control hardware feature
 be ignored, e.g. `no-ibrsb`, at which point Xen won't use them itself, and
 won't offer them to guests.
 
+`rdrand` can be used to override the default disabling of the feature on certain
+AMD systems.  Its negative form can of course also be used to suppress use and
+exposure of the feature.
+
 ### cpuid_mask_cpu
 > `= fam_0f_rev_[cdefg] | fam_10_rev_[bc] | fam_11_rev_b`
 
xen/arch/x86/cpu/amd.c +20 -0
@@ -4,6 +4,7 @@
 #include <xen/param.h>
 #include <xen/smp.h>
 #include <xen/pci.h>
+#include <xen/warning.h>
 #include <asm/io.h>
 #include <asm/msr.h>
 #include <asm/processor.h>
@@ -646,6 +647,25 @@ static void init_amd(struct cpuinfo_x86
 		if (acpi_smi_cmd && (acpi_enable_value | acpi_disable_value))
 			amd_acpi_c1e_quirk = true;
 		break;
+
+	case 0x15: case 0x16:
+		/*
+		 * There are too many Fam15/Fam16 systems where upon resume
+		 * from S3 firmware fails to re-setup properly functioning
+		 * RDRAND.  Clear the feature unless force-enabled on the
+		 * command line.
+		 */
+		if (c == &boot_cpu_data &&
+		    cpu_has(c, X86_FEATURE_RDRAND) &&
+		    !is_forced_cpu_cap(X86_FEATURE_RDRAND)) {
+			static const char __initconst text[] =
+				"RDRAND may cease to work on this hardware upon resume from S3.\n"
+				"Please choose an explicit cpuid={no-}rdrand setting.\n";
+
+			setup_clear_cpu_cap(X86_FEATURE_RDRAND);
+			warning_add(text);
+		}
+		break;
 	}
 
 	display_cacheinfo(c);
xen/arch/x86/cpu/common.c +28 -0
@@ -11,6 +11,7 @@
 #include <asm/io.h>
 #include <asm/mpspec.h>
 #include <asm/apic.h>
+#include <asm/random.h>
 #include <asm/setup.h>
 #include <mach_apic.h>
 #include <public/sysctl.h> /* for XEN_INVALID_{SOCKET,CORE}_ID */
@@ -98,6 +99,11 @@ void __init setup_force_cpu_cap(unsigned
 	__set_bit(cap, boot_cpu_data.x86_capability);
 }
 
+bool is_forced_cpu_cap(unsigned int cap)
+{
+	return test_bit(cap, forced_caps);
+}
+
 static void default_init(struct cpuinfo_x86 * c)
 {
 	/* Not much we can do here... */
@@ -498,6 +504,28 @@ void identify_cpu(struct cpuinfo_x86 *c)
 	printk("\n");
 #endif
 
+	/*
+	 * If RDRAND is available, make an attempt to check that it actually
+	 * (still) works.
+	 */
+	if (cpu_has(c, X86_FEATURE_RDRAND)) {
+		unsigned int prev = 0;
+
+		for (i = 0; i < 5; ++i)
+		{
+			unsigned int cur = arch_get_random();
+
+			if (prev && cur != prev)
+				break;
+			prev = cur;
+			cpu_relax();
+		}
+
+		if (i >= 5)
+			printk(XENLOG_WARNING "CPU%u: RDRAND appears to not work\n",
+			       smp_processor_id());
+	}
+
 	if (system_state == SYS_STATE_resume)
 		return;
 
xen/arch/x86/cpuid.c +3 -0
@@ -71,6 +71,9 @@ static int __init parse_xen_cpuid(const
             {
                 if ( !val )
                     setup_clear_cpu_cap(mid->bit);
+                else if ( mid->bit == X86_FEATURE_RDRAND &&
+                          (cpuid_ecx(1) & cpufeat_mask(X86_FEATURE_RDRAND)) )
+                    setup_force_cpu_cap(X86_FEATURE_RDRAND);
                 mid = NULL;
             }
 
xen/include/asm-x86/processor.h +1 -0
@@ -166,6 +166,7 @@ extern const struct x86_cpu_id *x86_matc
 extern void identify_cpu(struct cpuinfo_x86 *);
 extern void setup_clear_cpu_cap(unsigned int);
 extern void setup_force_cpu_cap(unsigned int);
+extern bool is_forced_cpu_cap(unsigned int);
 extern void print_cpu_info(unsigned int cpu);
 extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
 

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#1 lore raw

Ping: [PATCH v4] x86: clear RDRAND CPUID bit on AMD family 15h/16h

Quoted 22 lines · Jan Beulich

Inspired by Linux commit c49a0a80137c7ca7d6ced4c812c9e07a949f6f24:

There have been reports of RDRAND issues after resuming from suspend on some AMD family 15h and family 16h systems. This issue stems from a BIOS not performing the proper steps during resume to ensure RDRAND continues to function properly.

Update the CPU initialization to clear the RDRAND CPUID bit for any family 15h and 16h processor that supports RDRAND. If it is known that the family 15h or family 16h system does not have an RDRAND resume issue or that the system will not be placed in suspend, the "cpuid=rdrand" kernel parameter can be used to stop the clearing of the RDRAND CPUID bit.

Note, that clearing the RDRAND CPUID bit does not prevent a processor that normally supports the RDRAND instruction from executing it. So any code that determined the support based on family and model won't #UD.

Warn if no explicit choice was given on affected hardware.

Check RDRAND functions at boot as well as after S3 resume (the retry limit chosen is entirely arbitrary).

Quoted 6 lines

---
Still slightly RFC, and still in particular because of the change to parse_xen_cpuid(): Alternative approach suggestions are welcome. But now also because with many CPUs there may now be a lot of warnings in case of issues.

Ping?

Quoted 129 lines

---
v4: Check always, including during boot. Slightly better sanity check,
inspired by Linux commit 7879fc4bdc7.
v3: Add call to warning_add(). If force-enabled, check RDRAND still
functioning after S3 resume.
v2: Re-base.

docs/misc/xen-command-line.pandoc +4 -0
@@ -488,6 +488,10 @@ The Speculation Control hardware feature
 be ignored, e.g. `no-ibrsb`, at which point Xen won't use them itself, and
 won't offer them to guests.
 
+`rdrand` can be used to override the default disabling of the feature on certain
+AMD systems.  Its negative form can of course also be used to suppress use and
+exposure of the feature.
+
 ### cpuid_mask_cpu
 > `= fam_0f_rev_[cdefg] | fam_10_rev_[bc] | fam_11_rev_b`
 
xen/arch/x86/cpu/amd.c +20 -0
@@ -4,6 +4,7 @@
 #include <xen/param.h>
 #include <xen/smp.h>
 #include <xen/pci.h>
+#include <xen/warning.h>
 #include <asm/io.h>
 #include <asm/msr.h>
 #include <asm/processor.h>
@@ -646,6 +647,25 @@ static void init_amd(struct cpuinfo_x86
 		if (acpi_smi_cmd && (acpi_enable_value | acpi_disable_value))
 			amd_acpi_c1e_quirk = true;
 		break;
+
+	case 0x15: case 0x16:
+		/*
+		 * There are too many Fam15/Fam16 systems where upon resume
+		 * from S3 firmware fails to re-setup properly functioning
+		 * RDRAND.  Clear the feature unless force-enabled on the
+		 * command line.
+		 */
+		if (c == &boot_cpu_data &&
+		    cpu_has(c, X86_FEATURE_RDRAND) &&
+		    !is_forced_cpu_cap(X86_FEATURE_RDRAND)) {
+			static const char __initconst text[] =
+				"RDRAND may cease to work on this hardware upon resume from S3.\n"
+				"Please choose an explicit cpuid={no-}rdrand setting.\n";
+
+			setup_clear_cpu_cap(X86_FEATURE_RDRAND);
+			warning_add(text);
+		}
+		break;
 	}
 
 	display_cacheinfo(c);
xen/arch/x86/cpu/common.c +28 -0
@@ -11,6 +11,7 @@
 #include <asm/io.h>
 #include <asm/mpspec.h>
 #include <asm/apic.h>
+#include <asm/random.h>
 #include <asm/setup.h>
 #include <mach_apic.h>
 #include <public/sysctl.h> /* for XEN_INVALID_{SOCKET,CORE}_ID */
@@ -98,6 +99,11 @@ void __init setup_force_cpu_cap(unsigned
 	__set_bit(cap, boot_cpu_data.x86_capability);
 }
 
+bool is_forced_cpu_cap(unsigned int cap)
+{
+	return test_bit(cap, forced_caps);
+}
+
 static void default_init(struct cpuinfo_x86 * c)
 {
 	/* Not much we can do here... */
@@ -498,6 +504,28 @@ void identify_cpu(struct cpuinfo_x86 *c)
 	printk("\n");
 #endif
 
+	/*
+	 * If RDRAND is available, make an attempt to check that it actually
+	 * (still) works.
+	 */
+	if (cpu_has(c, X86_FEATURE_RDRAND)) {
+		unsigned int prev = 0;
+
+		for (i = 0; i < 5; ++i)
+		{
+			unsigned int cur = arch_get_random();
+
+			if (prev && cur != prev)
+				break;
+			prev = cur;
+			cpu_relax();
+		}
+
+		if (i >= 5)
+			printk(XENLOG_WARNING "CPU%u: RDRAND appears to not work\n",
+			       smp_processor_id());
+	}
+
 	if (system_state == SYS_STATE_resume)
 		return;
 
xen/arch/x86/cpuid.c +3 -0
@@ -71,6 +71,9 @@ static int __init parse_xen_cpuid(const
             {
                 if ( !val )
                     setup_clear_cpu_cap(mid->bit);
+                else if ( mid->bit == X86_FEATURE_RDRAND &&
+                          (cpuid_ecx(1) & cpufeat_mask(X86_FEATURE_RDRAND)) )
+                    setup_force_cpu_cap(X86_FEATURE_RDRAND);
                 mid = NULL;
             }
 
xen/include/asm-x86/processor.h +1 -0
@@ -166,6 +166,7 @@ extern const struct x86_cpu_id *x86_matc
 extern void identify_cpu(struct cpuinfo_x86 *);
 extern void setup_clear_cpu_cap(unsigned int);
 extern void setup_force_cpu_cap(unsigned int);
+extern bool is_forced_cpu_cap(unsigned int);
 extern void print_cpu_info(unsigned int cpu);
 extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
 
#2 lore raw
In reply to Jan Beulich
Quoted 11 lines · Jan Beulich

Inspired by Linux commit c49a0a80137c7ca7d6ced4c812c9e07a949f6f24:

There have been reports of RDRAND issues after resuming from suspend on some AMD family 15h and family 16h systems. This issue stems from a BIOS not performing the proper steps during resume to ensure RDRAND continues to function properly.

Update the CPU initialization to clear the RDRAND CPUID bit for any family 15h and 16h processor that supports RDRAND. If it is known that the family 15h or family 16h system does not have an RDRAND resume issue or that the system will not be placed in suspend, the "cpuid=rdrand" kernel parameter

I'm not sure what is best to do here.  The type suggests that this is a verbatim copy of the Linux commit message, but this tiny detail is Xen specific.

Quoted 6 lines

can be used to stop the clearing of the RDRAND CPUID bit.

Note, that clearing the RDRAND CPUID bit does not prevent a processor that normally supports the RDRAND instruction from executing it. So any code that determined the support based on family and model won't #UD.

Quoted 6 lines

Warn if no explicit choice was given on affected hardware.

Check RDRAND functions at boot as well as after S3 resume (the retry limit chosen is entirely arbitrary).

CC'ing Paul & George for 4.14 things:

tl;dr Some AMD system firmware doesn't reinitialise the RNG configuration on S3, resulting in the RDRAND instruction returning -1 and "data good".

AMD's recommended mitigation is to disable RDRAND entirely.  We can't identify the problem before an S3 resume and finding the instruction broken, and can't repair the damage at that point (The MSR needing fixing has already been locked), and AMD don't have a list of DMI quirks/equivalent to use to identify the buggy systems on boot.

Also, there is no public statement from AMD on the matter, but one obvious fallout on Linux systems is systemd looping forever trying to create a UUID which doesn't already alias in its database.

The impact of this would be substantially less bad if Xen could identify (a lack of) platform support for S3 at boot, as AMD servers have never supported S3.  This would reduce the user-nagging to only client systems, but there isn't an obvious way to do this.

It's a complete mess, but I don't think we should put this out without some form of release note.

Quoted 3 lines

---
Still slightly RFC, and still in particular because of the change to parse_xen_cpuid():

FWIW, that is very similar to XenServer's AVX512 off-by-default bodge until the default vs max policy work is ready.

I don't have a better suggestion right now, but hopefully something better might become obvious when we've got more users.  Either way, I'm expecting it to turn into a "switch ( mid->bit )" expression in due course.

Quoted 11 lines
xen/arch/x86/cpu/amd.c +1 -0
@@ -4,6 +4,7 @@
 #include <xen/param.h>
 #include <xen/smp.h>
 #include <xen/pci.h>
+#include <xen/warning.h>
 #include <asm/io.h>
 #include <asm/msr.h>
 #include <asm/processor.h>

xen/arch/x86/cpu/amd.c static void init_amd(struct cpuinfo_x86 · line 653

Quoted 7 lines
xen/arch/x86/cpu/amd.c +4 -0
@@ -646,6 +647,25 @@ static void init_amd(struct cpuinfo_x86
 		if (acpi_smi_cmd && (acpi_enable_value | acpi_disable_value))
 			amd_acpi_c1e_quirk = true;
 		break;
+
+	case 0x15: case 0x16:
+		/*
+		 * There are too many Fam15/Fam16 systems where upon resume

"some" systems.

xen/arch/x86/cpu/amd.c static void init_amd(struct cpuinfo_x86 · line 655

Quoted 2 lines
xen/arch/x86/cpu/amd.c +2 -0
+		 * from S3 firmware fails to re-setup properly functioning
+		 * RDRAND.

I think this needs another sentence of explanation.

By the time we can spot the problem, it is too late to take evasive action, and there is nothing Xen can do to repair the problem.

xen/arch/x86/cpu/amd.c static void init_amd(struct cpuinfo_x86 · line 666

Quoted 12 lines
xen/arch/x86/cpu/amd.c +11 -0
  Clear the feature unless force-enabled on the
+		 * command line.
+		 */
+		if (c == &boot_cpu_data &&
+		    cpu_has(c, X86_FEATURE_RDRAND) &&
+		    !is_forced_cpu_cap(X86_FEATURE_RDRAND)) {
+			static const char __initconst text[] =
+				"RDRAND may cease to work on this hardware upon resume from S3.\n"
+				"Please choose an explicit cpuid={no-}rdrand setting.\n";
+
+			setup_clear_cpu_cap(X86_FEATURE_RDRAND);
+			warning_add(text);

What do you think to clobbering RDRAND via the CPUMASK registers in this case?  We've got full control there, and it would stop PV userspace as well.

Quoted 28 lines
xen/arch/x86/cpu/amd.c +2 -0
+		}
+		break;
 	}
 
xen/arch/x86/cpu/amd.c +0 -0
 	display_cacheinfo(c);
xen/arch/x86/cpu/common.c +6 -0
@@ -11,6 +11,7 @@
 #include <asm/io.h>
 #include <asm/mpspec.h>
 #include <asm/apic.h>
+#include <asm/random.h>
 #include <asm/setup.h>
 #include <mach_apic.h>
 #include <public/sysctl.h> /* for XEN_INVALID_{SOCKET,CORE}_ID */
@@ -98,6 +99,11 @@ void __init setup_force_cpu_cap(unsigned
 	__set_bit(cap, boot_cpu_data.x86_capability);
 }
 
+bool is_forced_cpu_cap(unsigned int cap)
+{
+	return test_bit(cap, forced_caps);
+}
+
 static void default_init(struct cpuinfo_x86 * c)
 {
 	/* Not much we can do here... */

xen/arch/x86/cpu/common.c void identify_cpu(struct cpuinfo_x86 *c) · line 510

Quoted 7 lines
xen/arch/x86/cpu/common.c +4 -0
@@ -498,6 +504,28 @@ void identify_cpu(struct cpuinfo_x86 *c)
 	printk("\n");
 #endif
 
+	/*
+	 * If RDRAND is available, make an attempt to check that it actually
+	 * (still) works.
+	 */

Do you think it would be helpful to test in the opposite case as well. If we come back from S3 and find that RDRAND does actually work, then it is safe to tell the user that they can re-enable.

xen/arch/x86/cpu/common.c void identify_cpu(struct cpuinfo_x86 *c) · line 521

Quoted 11 lines
xen/arch/x86/cpu/common.c +11 -0
+	if (cpu_has(c, X86_FEATURE_RDRAND)) {
+		unsigned int prev = 0;
+
+		for (i = 0; i < 5; ++i)
+		{
+			unsigned int cur = arch_get_random();
+
+			if (prev && cur != prev)
+				break;
+			prev = cur;
+			cpu_relax();

Why the relax?  We're not polling hammering the memory bus waiting for an unknown period of time until something changes.

We simply need up to 5 random numbers as fast as the RNG can produce them (which is actually quite slow.  RDRAND has ~350 cycle latency minimum.)

~Andrew

Quoted 10 lines
xen/arch/x86/cpu/common.c +7 -0
+		}
+
+		if (i >= 5)
+			printk(XENLOG_WARNING "CPU%u: RDRAND appears to not work\n",
+			       smp_processor_id());
+	}
+
 	if (system_state == SYS_STATE_resume)
 		return;
 
#3 lore raw
Quoted 8 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich

Inspired by Linux commit c49a0a80137c7ca7d6ced4c812c9e07a949f6f24:

There have been reports of RDRAND issues after resuming from suspend on some AMD family 15h and family 16h systems. This issue stems from a BIOS not performing the proper steps during resume to ensure RDRAND continues to function properly.

Quoted 8 lines
Quoted 4 lines

Update the CPU initialization to clear the RDRAND CPUID bit for any family 15h and 16h processor that supports RDRAND. If it is known that the family 15h or family 16h system does not have an RDRAND resume issue or that the system will not be placed in suspend, the "cpuid=rdrand" kernel parameter

I'm not sure what is best to do here.  The type suggests that this is a verbatim copy of the Linux commit message, but this tiny detail is Xen specific.

It simply didn't seem to make sense to leave the Linux way of specifying this in here, just to then say further down what the correct (Xen) way is.

Quoted 10 lines
Quoted 3 lines

---
Still slightly RFC, and still in particular because of the change to parse_xen_cpuid():

FWIW, that is very similar to XenServer's AVX512 off-by-default bodge until the default vs max policy work is ready.

I don't have a better suggestion right now, but hopefully something better might become obvious when we've got more users.  Either way, I'm expecting it to turn into a "switch ( mid->bit )" expression in due course.

IOW do you want me to use switch() right away?

Quoted 11 lines

xen/arch/x86/cpu/amd.c static void init_amd(struct cpuinfo_x86 · line 653

Quoted 8 lines
xen/arch/x86/cpu/amd.c +4 -0
@@ -646,6 +647,25 @@ static void init_amd(struct cpuinfo_x86
 		if (acpi_smi_cmd && (acpi_enable_value | acpi_disable_value))
 			amd_acpi_c1e_quirk = true;
 		break;
+
+	case 0x15: case 0x16:
+		/*
+		 * There are too many Fam15/Fam16 systems where upon resume

"some" systems.

Quoted 7 lines

Review static void init_amd(struct cpuinfo_x86 · line 655

Quoted 2 lines
(quoted diff) +2 -0
+		 * from S3 firmware fails to re-setup properly functioning
+		 * RDRAND.

I think this needs another sentence of explanation.

By the time we can spot the problem, it is too late to take evasive action, and there is nothing Xen can do to repair the problem.

Sure, added.

Quoted 7 lines
Quoted 7 lines
xen/arch/x86/cpu/amd.c +6 -0
  Clear the feature unless force-enabled on the
+		 * command line.
+		 */
+		if (c == &boot_cpu_data &&
+		    cpu_has(c, X86_FEATURE_RDRAND) &&
+		    !is_forced_cpu_cap(X86_FEATURE_RDRAND)) {
+			static const char __initconst text[] =
Quoted 8 lines

xen/arch/x86/cpu/amd.c static void init_amd(struct cpuinfo_x86 · line 666

Quoted 5 lines
xen/arch/x86/cpu/amd.c +5 -0
+				"RDRAND may cease to work on this hardware upon resume from S3.\n"
+				"Please choose an explicit cpuid={no-}rdrand setting.\n";
+
+			setup_clear_cpu_cap(X86_FEATURE_RDRAND);
+			warning_add(text);

What do you think to clobbering RDRAND via the CPUMASK registers in this case?  We've got full control there, and it would stop PV userspace as well.

Why would such be needed? The host_policy -> pv_max_cpuid_policy -> recalculate_cpuid_policy() propagation already causes the flag to get zapped from guest policies once it got cleared here. And it's the guest policy that controls what gets put in the masking MSRs for PV guests, isn't it?

Quoted 4 lines
Quoted 4 lines
xen/arch/x86/cpu/common.c +0 -0
@@ -498,6 +504,28 @@ void identify_cpu(struct cpuinfo_x86 *c)
 	printk("\n");
 #endif
 
Quoted 8 lines

xen/arch/x86/cpu/common.c void identify_cpu(struct cpuinfo_x86 *c) · line 510

Quoted 4 lines
xen/arch/x86/cpu/common.c +4 -0
+	/*
+	 * If RDRAND is available, make an attempt to check that it actually
+	 * (still) works.
+	 */

Do you think it would be helpful to test in the opposite case as well. If we come back from S3 and find that RDRAND does actually work, then it is safe to tell the user that they can re-enable.

I'd view this as a nice-to-have that isn't all that obvious how to actually implement in a sufficiently clean way. For example, we can't use arch_get_random() in that case. Therefore I'd prefer if this extra courtesy could be left out of here for now, and - if indeed deemed useful - be added later.

Quoted 15 lines

xen/arch/x86/cpu/common.c void identify_cpu(struct cpuinfo_x86 *c) · line 521

Quoted 11 lines
xen/arch/x86/cpu/common.c +11 -0
+	if (cpu_has(c, X86_FEATURE_RDRAND)) {
+		unsigned int prev = 0;
+
+		for (i = 0; i < 5; ++i)
+		{
+			unsigned int cur = arch_get_random();
+
+			if (prev && cur != prev)
+				break;
+			prev = cur;
+			cpu_relax();

Why the relax?  We're not polling hammering the memory bus waiting for an unknown period of time until something changes.

Quoted 2 lines

We simply need up to 5 random numbers as fast as the RNG can produce them (which is actually quite slow.  RDRAND has ~350 cycle latency minimum.)

Dropped; I put it there simply to give the hardware some breathing room between adjacent requests.

Jan

Reviewed-by on v4 1/1
#4 lore raw
In reply to Jan Beulich
Quoted 17 lines · Jan Beulich

Inspired by Linux commit c49a0a80137c7ca7d6ced4c812c9e07a949f6f24:

There have been reports of RDRAND issues after resuming from suspend on some AMD family 15h and family 16h systems. This issue stems from a BIOS not performing the proper steps during resume to ensure RDRAND continues to function properly.

Update the CPU initialization to clear the RDRAND CPUID bit for any family 15h and 16h processor that supports RDRAND. If it is known that the family 15h or family 16h system does not have an RDRAND resume issue or that the system will not be placed in suspend, the "cpuid=rdrand" kernel parameter can be used to stop the clearing of the RDRAND CPUID bit.

Note, that clearing the RDRAND CPUID bit does not prevent a processor that normally supports the RDRAND instruction from executing it. So any code that determined the support based on family and model won't #UD.

Quoted 6 lines

Warn if no explicit choice was given on affected hardware.

Check RDRAND functions at boot as well as after S3 resume (the retry limit chosen is entirely arbitrary).

Quoted 40 lines

---
Still slightly RFC, and still in particular because of the change to parse_xen_cpuid(): Alternative approach suggestions are welcome. But now also because with many CPUs there may now be a lot of warnings in case of issues.
---
v4: Check always, including during boot. Slightly better sanity check,
inspired by Linux commit 7879fc4bdc7.
v3: Add call to warning_add(). If force-enabled, check RDRAND still
functioning after S3 resume.
v2: Re-base.

docs/misc/xen-command-line.pandoc +4 -0
@@ -488,6 +488,10 @@ The Speculation Control hardware feature
 be ignored, e.g. `no-ibrsb`, at which point Xen won't use them itself, and
 won't offer them to guests.
 
+`rdrand` can be used to override the default disabling of the feature on certain
+AMD systems.  Its negative form can of course also be used to suppress use and
+exposure of the feature.
+
 ### cpuid_mask_cpu
 > `= fam_0f_rev_[cdefg] | fam_10_rev_[bc] | fam_11_rev_b`
 
xen/arch/x86/cpu/amd.c +2 -0
@@ -4,6 +4,7 @@
 #include <xen/param.h>
 #include <xen/smp.h>
 #include <xen/pci.h>
+#include <xen/warning.h>
 #include <asm/io.h>
 #include <asm/msr.h>
 #include <asm/processor.h>
@@ -646,6 +647,25 @@ static void init_amd(struct cpuinfo_x86
 		if (acpi_smi_cmd && (acpi_enable_value | acpi_disable_value))
 			amd_acpi_c1e_quirk = true;
 		break;
+

xen/arch/x86/cpu/amd.c static void init_amd(struct cpuinfo_x86 · line 660

Quoted 10 lines
xen/arch/x86/cpu/amd.c +10 -0
+	case 0x15: case 0x16:
+		/*
+		 * There are too many Fam15/Fam16 systems where upon resume
+		 * from S3 firmware fails to re-setup properly functioning
+		 * RDRAND.  Clear the feature unless force-enabled on the
+		 * command line.
+		 */
+		if (c == &boot_cpu_data &&
+		    cpu_has(c, X86_FEATURE_RDRAND) &&
+		    !is_forced_cpu_cap(X86_FEATURE_RDRAND)) {

Given this is the only user of is_forced_cpu_cap...

Quoted 22 lines
xen/arch/x86/cpu/amd.c +8 -0
+			static const char __initconst text[] =
+				"RDRAND may cease to work on this hardware upon resume from S3.\n"
+				"Please choose an explicit cpuid={no-}rdrand setting.\n";
+
+			setup_clear_cpu_cap(X86_FEATURE_RDRAND);
+			warning_add(text);
+		}
+		break;
 	}
 
 	display_cacheinfo(c);
xen/arch/x86/cpu/common.c +1 -0
@@ -11,6 +11,7 @@
 #include <asm/io.h>
 #include <asm/mpspec.h>
 #include <asm/apic.h>
+#include <asm/random.h>
 #include <asm/setup.h>
 #include <mach_apic.h>
 #include <public/sysctl.h> /* for XEN_INVALID_{SOCKET,CORE}_ID */

xen/arch/x86/cpu/common.c void __init setup_force_cpu_cap(unsigned · line 102

Quoted 4 lines
xen/arch/x86/cpu/common.c +1 -0
@@ -98,6 +99,11 @@ void __init setup_force_cpu_cap(unsigned
 	__set_bit(cap, boot_cpu_data.x86_capability);
 }
 
+bool is_forced_cpu_cap(unsigned int cap)

... I think this could be made __init?

Thanks, Roger.

#5 lore raw
Quoted 18 lines · Roger Pau Monné
Quoted 17 lines · Jan Beulich

Inspired by Linux commit c49a0a80137c7ca7d6ced4c812c9e07a949f6f24:

There have been reports of RDRAND issues after resuming from suspend on some AMD family 15h and family 16h systems. This issue stems from a BIOS not performing the proper steps during resume to ensure RDRAND continues to function properly.

Update the CPU initialization to clear the RDRAND CPUID bit for any family 15h and 16h processor that supports RDRAND. If it is known that the family 15h or family 16h system does not have an RDRAND resume issue or that the system will not be placed in suspend, the "cpuid=rdrand" kernel parameter can be used to stop the clearing of the RDRAND CPUID bit.

Note, that clearing the RDRAND CPUID bit does not prevent a processor that normally supports the RDRAND instruction from executing it. So any code that determined the support based on family and model won't #UD.

Quoted 8 lines
Quoted 6 lines

Warn if no explicit choice was given on affected hardware.

Check RDRAND functions at boot as well as after S3 resume (the retry limit chosen is entirely arbitrary).

Thanks much.

Quoted 43 lines
Quoted 5 lines
xen/arch/x86/cpu/amd.c +1 -0
@@ -646,6 +647,25 @@ static void init_amd(struct cpuinfo_x86
 		if (acpi_smi_cmd && (acpi_enable_value | acpi_disable_value))
 			amd_acpi_c1e_quirk = true;
 		break;
+

xen/arch/x86/cpu/amd.c static void init_amd(struct cpuinfo_x86 · line 660

Quoted 10 lines
xen/arch/x86/cpu/amd.c +10 -0
+	case 0x15: case 0x16:
+		/*
+		 * There are too many Fam15/Fam16 systems where upon resume
+		 * from S3 firmware fails to re-setup properly functioning
+		 * RDRAND.  Clear the feature unless force-enabled on the
+		 * command line.
+		 */
+		if (c == &boot_cpu_data &&
+		    cpu_has(c, X86_FEATURE_RDRAND) &&
+		    !is_forced_cpu_cap(X86_FEATURE_RDRAND)) {

Given this is the only user of is_forced_cpu_cap...

Quoted 25 lines
xen/arch/x86/cpu/amd.c +8 -0
+			static const char __initconst text[] =
+				"RDRAND may cease to work on this hardware upon resume from S3.\n"
+				"Please choose an explicit cpuid={no-}rdrand setting.\n";
+
+			setup_clear_cpu_cap(X86_FEATURE_RDRAND);
+			warning_add(text);
+		}
+		break;
 	}
 
 	display_cacheinfo(c);
xen/arch/x86/cpu/common.c +1 -0
@@ -11,6 +11,7 @@
 #include <asm/io.h>
 #include <asm/mpspec.h>
 #include <asm/apic.h>
+#include <asm/random.h>
 #include <asm/setup.h>
 #include <mach_apic.h>
 #include <public/sysctl.h> /* for XEN_INVALID_{SOCKET,CORE}_ID */
@@ -98,6 +99,11 @@ void __init setup_force_cpu_cap(unsigned
 	__set_bit(cap, boot_cpu_data.x86_capability);
 }
 
Quoted 3 lines

xen/arch/x86/cpu/common.c void __init setup_force_cpu_cap(unsigned · line 102

Quoted 1 line
xen/arch/x86/cpu/common.c +1 -0
+bool is_forced_cpu_cap(unsigned int cap)

... I think this could be made __init?

Ah, now it can be again, yes. It was an endless back and forth between the various versions (some not even posted).

Jan