x86/ucode: Parallel loading fixes
cover x86/ucode: Parallel loading fixes
At least patch 1 wants backporting. Commit e0bb712a28a9 ("x86/ucode: Abort parallel load early on any control thread error") made the problem easier to hit, but the bug has been present since the introduction of parallel loading.
Another potential fix would be to have stop_machine_run()'s error value filled in using cmpxchg.
Andrew Cooper (3):
x86/ucode: Fix error handling during parallel ucode load x86/ucode: Drop structurally unreachable ASSERT()s x86/ucode: Create a real type for loading_state
xen/arch/x86/cpu/microcode/core.c | 37 +++++++++++++++---------------- 1 file changed, 18 insertions(+), 19 deletions(-)
Signature · 1 line
2.39.5
PATCH 1/3 x86/ucode: Fix error handling during parallel ucode load
wait_for_state() returns false on encountering LOADING_EXIT. control_thread_fn() can move directly to this state in the case of an early error. It is not an error condition for APs, but right now the latest write into stopmachine_data.fn_result wins, causing the real error, -EIO, to get clobbered with -EBUSY. e.g.:
# xen-ucode /lib/firmware/amd-ucode/microcode_amd_fam17h.bin --force Failed to update microcode. (err: Device or resource busy)
(XEN) 256 cores are to update their microcode
(XEN) microcode: CPU0 update rev 0x830107d to 0x830107c failed, result 0x830107d (XEN) Late loading aborted: CPU0 failed to update ucode: -5
Drop all the -EBUSY's, and treat hitting LOADING_EXIT as a success case. This causes only a single error to be returned through stop_machine_run(). e.g.:
# xen-ucode /lib/firmware/amd-ucode/microcode_amd_fam17h.bin --force Failed to update microcode. (err: Input/output error)
(XEN) 256 cores are to update their microcode
(XEN) microcode: CPU0 update rev 0x830107d to 0x830107c failed, result 0x830107d (XEN) Late loading aborted: CPU0 failed to update ucode: -5
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
Xen 4.19 and earlier hit this case naturally, without the need of --force.
--- xen/arch/x86/cpu/microcode/core.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
xen/arch/x86/cpu/microcode/core.c +6 -4
@@ -260,7 +260,9 @@ static int secondary_nmi_work(void) {cpumask_set_cpu (smp_processor_id (), &cpu_callin_map); -return wait_for_state (LOADING_EXIT ) ?0 : -EBUSY ; +wait_for_state (LOADING_EXIT ); + +return 0 ; } static int primary_thread_work(const struct microcode_patch *patch,
@@ -271,7 +273,7 @@ static int primary_thread_work(const struct microcode_patch *patch,cpumask_set_cpu (smp_processor_id (), &cpu_callin_map);if ( !wait_for_state (LOADING_ENTER ) ) -return -EBUSY ; +return 0 ; ret =alternative_call (ucode_ops.apply_microcode, patch, flags);if ( !ret )
@@ -313,7 +315,7 @@ static int cf_check microcode_nmi_callback(static int secondary_thread_fn (void ) {if ( !wait_for_state (LOADING_CALLIN ) ) -return -EBUSY ; +return 0 ;self_nmi ();
@@ -336,7 +338,7 @@ static int primary_thread_fn(const struct microcode_patch *patch, unsigned int flags) {if ( !wait_for_state (LOADING_CALLIN ) ) -return -EBUSY ; +return 0 ;if ( ucode_in_nmi ) {
Signature · 1 line
2.39.5
Re: [PATCH 1/3] x86/ucode: Fix error handling during parallel ucode load
Quoted 9 lines · Andrew Cooper
wait_for_state()returns false on encounteringLOADING_EXIT.control_thread_fn()can move directly to this state in the case of an early error. It is not an error condition for APs, but right now the latest write into stopmachine_data.fn_result wins, causing the real error,-EIO, to get clobbered with-EBUSY. e.g.:# xen-ucode /lib/firmware/amd-ucode/microcode_amd_fam17h.bin --force Failed to update microcode. (err: Device or resource busy)
Quoted 6 lines
(XEN) 256 cores are to update their microcode
(XEN) microcode: CPU0 update rev0x830107dto0x830107cfailed, result0x830107d(XEN) Late loading aborted: CPU0 failed to update ucode: -5Drop all the -EBUSY's, and treat hitting
LOADING_EXITas a success case. This causes only a single error to be returned throughstop_machine_run(). e.g.:
Why "single"? stop_machine_run() can't return multiple ones, having only a scalar return type? Or do you mean "a single, consistent" or some such?
Quoted 6 lines
# xen-ucode /lib/firmware/amd-ucode/microcode_amd_fam17h.bin --force Failed to update microcode. (err: Input/output error)(XEN) 256 cores are to update their microcode
(XEN) microcode: CPU0 update rev0x830107dto0x830107cfailed, result0x830107d(XEN) Late loading aborted: CPU0 failed to update ucode: -5
The sole difference being which specific error is observed, which looks to support the above interpretation. What I don't quite understand is ...
Quoted 1 line
Fixes 5ed12565aa32 ("microcode: rendezvous CPUs in NMI handler and load ucode")
... this and the specific indication that this needs backporting: Why is the particular error code this important here?
xen/arch/x86/cpu/microcode/core.c static int secondary_nmi_work(void) · line 266
Quoted 11 lines
xen/arch/x86/cpu/microcode/core.c +3
-1@@ -260,7 +260,9 @@ static int secondary_nmi_work(void) {cpumask_set_cpu (smp_processor_id (), &cpu_callin_map); -return wait_for_state (LOADING_EXIT ) ?0 : -EBUSY ; +wait_for_state (LOADING_EXIT ); + +return 0 ; }
At which point the function could as well return void? Preferably with this adjustment (and the knock-on one at the call site) and with the slight clarification to the description
Quoted 19 lines
xen/arch/x86/cpu/microcode/core.c +2
-2@@ -271,7 +273,7 @@ static int primary_thread_work(const struct microcode_patch *patch,cpumask_set_cpu (smp_processor_id (), &cpu_callin_map);if ( !wait_for_state (LOADING_ENTER ) ) -return -EBUSY ; +return 0 ; ret =alternative_call (ucode_ops.apply_microcode, patch, flags);if ( !ret )@@ -313,7 +315,7 @@ static int cf_check microcode_nmi_callback(static int secondary_thread_fn (void ) {if ( !wait_for_state (LOADING_CALLIN ) ) -return -EBUSY ; +return 0 ;self_nmi ();
xen/arch/x86/cpu/microcode/core.c static int primary_thread_fn(const struct microcode_patch *patch, · line 344
Quoted 8 lines
xen/arch/x86/cpu/microcode/core.c +1
-1@@ -336,7 +338,7 @@ static int primary_thread_fn(const struct microcode_patch *patch, unsigned int flags) {if ( !wait_for_state (LOADING_CALLIN ) ) -return -EBUSY ; +return 0 ;if ( ucode_in_nmi ) {
Vaguely recalling the original intentions, these changes looked wrong to me at the first glance. But yes, an exit indication from the control thread isn't really a separate error condition.
Jan
Re: [PATCH 1/3] x86/ucode: Fix error handling during parallel ucode load
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Andrew Cooper
wait_for_state()returns false on encounteringLOADING_EXIT.control_thread_fn()can move directly to this state in the case of an early error. It is not an error condition for APs, but right now the latest write into stopmachine_data.fn_result wins, causing the real error,-EIO, to get clobbered with-EBUSY. e.g.:# xen-ucode /lib/firmware/amd-ucode/microcode_amd_fam17h.bin --force Failed to update microcode. (err: Device or resource busy)
Quoted 8 lines
Quoted 6 lines
(XEN) 256 cores are to update their microcode
(XEN) microcode: CPU0 update rev0x830107dto0x830107cfailed, result0x830107d(XEN) Late loading aborted: CPU0 failed to update ucode: -5Drop all the -EBUSY's, and treat hitting
LOADING_EXITas a success case. This causes only a single error to be returned throughstop_machine_run(). e.g.:Why "single"?
stop_machine_run()can't return multiple ones, having only a scalar return type? Or do you mean "a single, consistent" or some such?
stop_machine_run() has a data race on stopmachine_data.fn_result.
Any CPU returning any nonzero value back into the stop_machine machinery will update the singleton result, and latest wins.
This causes the BSP to return -EIO, and all APs to return 0 and not interfere with the -EIO.
Quoted 7 lines
Quoted 7 lines
# xen-ucode /lib/firmware/amd-ucode/microcode_amd_fam17h.bin --force Failed to update microcode. (err: Input/output error)(XEN) 256 cores are to update their microcode
(XEN) microcode: CPU0 update rev0x830107dto0x830107cfailed, result0x830107d(XEN) Late loading aborted: CPU0 failed to update ucode: -5
Quoted 6 lines
The sole difference being which specific error is observed, which looks to support the above interpretation. What I don't quite understand is ...
Quoted 1 line
Fixes 5ed12565aa32 ("microcode: rendezvous CPUs in NMI handler and load ucode")... this and the specific indication that this needs backporting: Why is the particular error code this important here?
Because userspace cares about -EEXIST as a special case for success.
Having -EEIXST clobbered with -EBUSY causes a false negative failure in XenServer's testing.
As said in the cover letter, 4.19 and earlier now suffer this as a side effect of e0bb712a28a9 ("x86/ucode: Abort parallel load early on any control thread error") because out-of-date ucodes used to be passed into stop_machine and cause every CPU to fail with -EEXIST.
Quoted 14 lines
xen/arch/x86/cpu/microcode/core.c static int secondary_nmi_work(void) · line 266
Quoted 11 lines
xen/arch/x86/cpu/microcode/core.c +3
-1@@ -260,7 +260,9 @@ static int secondary_nmi_work(void) {cpumask_set_cpu (smp_processor_id (), &cpu_callin_map); -return wait_for_state (LOADING_EXIT ) ?0 : -EBUSY ; +wait_for_state (LOADING_EXIT ); + +return 0 ; }At which point the function could as well return void? Preferably with this adjustment (and the knock-on one at the call site) and with the slight clarification to the description
Quoted 1 line
Reviewed-by Jan Beulich <[email protected]>
I have a different series, but ucode_in_nmi needs untangling first.
Even changing this function to be void causes this patch to be dominated by cleanup, which isn't appropriate for a bugfix.
~Andrew
PATCH 2/3 x86/ucode: Drop structurally unreachable ASSERT()s
In reply to Andrew CooperIt's impossible for wait_for_state() to return false when looking for LOADING_EXIT, so much so that the optimiser can drop the ASSERT()s.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/cpu/microcode/core.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-)
xen/arch/x86/cpu/microcode/core.c +6 -10
@@ -320,12 +320,10 @@ static int secondary_thread_fn(void)self_nmi ();/* -* Wait for ucode loading is done in case that the NMI does not arrive -* synchronously, which may lead to a not-yet-updated CPU signature is -* copied below. +* NMIs may not be delivered synchronously. Wait for the primary threads +* to be done. */ -if (unlikely (!wait_for_state (LOADING_EXIT )) ) -ASSERT_UNREACHABLE (); +wait_for_state (LOADING_EXIT );/* Copy update revision from the primary thread. */ this_cpu(cpu_sig).rev =
@@ -345,12 +343,10 @@ static int primary_thread_fn(const struct microcode_patch *patch,self_nmi ();/* -* Wait for ucode loading is done in case that the NMI does not arrive -* synchronously, which may lead to a not-yet-updated error is returned -* below. +* NMIs may not be delivered synchronously. Wait for the primary +* threads to be done. */ -if (unlikely (!wait_for_state (LOADING_EXIT )) ) -ASSERT_UNREACHABLE (); +wait_for_state (LOADING_EXIT );return this_cpu (loading_err); }
Signature · 1 line
2.39.5
Re: [PATCH 2/3] x86/ucode: Drop structurally unreachable ASSERT()s
Quoted 7 lines · Andrew Cooper
It's impossible for
wait_for_state()to return false when looking forLOADING_EXIT, so much so that the optimiser can drop the ASSERT()s.No functional change.
Fixes 5ed12565aa32 ("microcode: rendezvous CPUs in NMI handler and load ucode")Signed-off-by Andrew Cooper <[email protected]>
Besides helping code clarity, mainly a Misra fix as it looks (unreachable code).
Jan
PATCH 3/3 x86/ucode: Create a real type for loading_state
In reply to Andrew CooperUsing typeof() in wait_for_state()/set_state() unnecesserily cryptic, and more verbose than using a proper type.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/cpu/microcode/core.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
xen/arch/x86/cpu/microcode/core.c +6 -5
@@ -70,12 +70,13 @@ static unsigned int nr_cores;* - LOADING_ENTER: all CPUs have called in. Initiate ucode loading. * - LOADING_EXIT: ucode loading is done or aborted. */ -static enum { +typedef enum {LOADING_PREPARE ,LOADING_CALLIN ,LOADING_ENTER ,LOADING_EXIT , -} loading_state; +}loading_state_t ; +static loading_state_t loading_state;struct patch_with_flags {unsigned int flags;
@@ -237,9 +238,9 @@ static bool cf_check wait_cpu_callout(unsigned int nr)return atomic_read (&cpu_out) >= nr; } -static bool wait_for_state (typeof (loading_state) state) +static bool wait_for_state (loading_state_t state) { -typeof (loading_state) cur_state; +loading_state_t cur_state;while ( (cur_state =ACCESS_ONCE (loading_state)) != state ) {
@@ -251,7 +252,7 @@ static bool wait_for_state(typeof(loading_state) state)return true; } -static void set_state (typeof (loading_state) state) +static void set_state (loading_state_t state) {ACCESS_ONCE (loading_state) = state; }
Signature · 1 line
2.39.5
Re: [PATCH 3/3] x86/ucode: Create a real type for loading_state
Quoted 6 lines · Andrew Cooper
Using
typeof()in wait_for_state()/set_state() unnecesserily cryptic, and more verbose than using a proper type.No functional change.
Signed-off-by Andrew Cooper <[email protected]>
I don't strictly mind the change, so
but I think the use of typeof() also has its benefits:
xen/arch/x86/cpu/microcode/core.c static bool cf_check wait_cpu_callout(unsigned int nr) · line 246
Quoted 12 lines
xen/arch/x86/cpu/microcode/core.c +2
-2@@ -237,9 +238,9 @@ static bool cf_check wait_cpu_callout(unsigned int nr)return atomic_read (&cpu_out) >= nr; } -static bool wait_for_state (typeof (loading_state) state) +static bool wait_for_state (loading_state_t state) { -typeof (loading_state) cur_state; +loading_state_t cur_state;while ( (cur_state =ACCESS_ONCE (loading_state)) != state ) {
Even if the type of loading_state changed, no type mismatches would result here. Or in other words, a type adjustment there would not entail code changes here and ...
xen/arch/x86/cpu/microcode/core.c static bool wait_for_state(typeof(loading_state) state) · line 258
Quoted 9 lines
xen/arch/x86/cpu/microcode/core.c +1
-1@@ -251,7 +252,7 @@ static bool wait_for_state(typeof(loading_state) state)return true; } -static void set_state (typeof (loading_state) state) +static void set_state (loading_state_t state) {ACCESS_ONCE (loading_state) = state; }
... here (which could easily be forgotten as the compiler might not flag such mismatches).
Thing of course is that the type of loading_state is pretty unlikely to change.
Jan