[PATCH 4/7] x86/microcode: Distinguish NMI control path on stop-machine callback

Chang S. Bae posted 7 patches 2 weeks, 1 day ago
[PATCH 4/7] x86/microcode: Distinguish NMI control path on stop-machine callback
Posted by Chang S. Bae 2 weeks, 1 day ago
load_cpus_stopped() currently centralizes the stop_machine() callback for
both NMI and NMI-less rendezvous. microcode_update_handler() alone is
enough for the latter.

While the NMI-based rendezvous finally reaches the same update handler,
it requires additional logic to trigger and process NMIs. That machinery
will be replaced by stop_machine_nmi().

As preparation for that conversion, split the callback path to make
NMI-specific steps explicit and clear.

Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
---
 arch/x86/kernel/cpu/microcode/core.c | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)

diff --git a/arch/x86/kernel/cpu/microcode/core.c b/arch/x86/kernel/cpu/microcode/core.c
index 68049f171860..28317176ae29 100644
--- a/arch/x86/kernel/cpu/microcode/core.c
+++ b/arch/x86/kernel/cpu/microcode/core.c
@@ -524,7 +524,7 @@ void noinstr microcode_offline_nmi_handler(void)
 	wait_for_ctrl();
 }
 
-static noinstr bool microcode_update_handler(void)
+static noinstr int microcode_update_handler(void *unused)
 {
 	unsigned int cpu = raw_smp_processor_id();
 
@@ -540,7 +540,7 @@ static noinstr bool microcode_update_handler(void)
 	touch_nmi_watchdog();
 	instrumentation_end();
 
-	return true;
+	return 0;
 }
 
 /*
@@ -561,19 +561,15 @@ bool noinstr microcode_nmi_handler(void)
 		return false;
 
 	raw_cpu_write(ucode_ctrl.nmi_enabled, false);
-	return microcode_update_handler();
+	return microcode_update_handler(NULL) == 0;
 }
 
 static int load_cpus_stopped(void *unused)
 {
-	if (microcode_ops->use_nmi) {
-		/* Enable the NMI handler and raise NMI */
-		this_cpu_write(ucode_ctrl.nmi_enabled, true);
-		apic->send_IPI(smp_processor_id(), NMI_VECTOR);
-	} else {
-		/* Just invoke the handler directly */
-		microcode_update_handler();
-	}
+	/* Enable the NMI handler and raise NMI */
+	this_cpu_write(ucode_ctrl.nmi_enabled, true);
+	apic->send_IPI(smp_processor_id(), NMI_VECTOR);
+
 	return 0;
 }
 
@@ -610,13 +606,13 @@ static int load_late_stop_cpus(bool is_safe)
 	 */
 	store_cpu_caps(&prev_info);
 
-	if (microcode_ops->use_nmi)
+	if (microcode_ops->use_nmi) {
 		static_branch_enable_cpuslocked(&microcode_nmi_handler_enable);
-
-	stop_machine_cpuslocked(load_cpus_stopped, NULL, cpu_online_mask);
-
-	if (microcode_ops->use_nmi)
+		stop_machine_cpuslocked(load_cpus_stopped, NULL, cpu_online_mask);
 		static_branch_disable_cpuslocked(&microcode_nmi_handler_enable);
+	} else {
+		stop_machine_cpuslocked(microcode_update_handler, NULL, cpu_online_mask);
+	}
 
 	/* Analyze the results */
 	for_each_cpu_and(cpu, cpu_present_mask, &cpus_booted_once_mask) {
-- 
2.51.0
Re: [PATCH 4/7] x86/microcode: Distinguish NMI control path on stop-machine callback
Posted by Thomas Gleixner 1 week, 5 days ago
On Sun, Jan 25 2026 at 01:42, Chang S. Bae wrote:
>  static int load_cpus_stopped(void *unused)
>  {
> -	if (microcode_ops->use_nmi) {
> -		/* Enable the NMI handler and raise NMI */
> -		this_cpu_write(ucode_ctrl.nmi_enabled, true);
> -		apic->send_IPI(smp_processor_id(), NMI_VECTOR);
> -	} else {
> -		/* Just invoke the handler directly */
> -		microcode_update_handler();
> -	}
> +	/* Enable the NMI handler and raise NMI */
> +	this_cpu_write(ucode_ctrl.nmi_enabled, true);
> +	apic->send_IPI(smp_processor_id(), NMI_VECTOR);
> +

With this change the function name is completely bogus.
Re: [PATCH 4/7] x86/microcode: Distinguish NMI control path on stop-machine callback
Posted by Chang S. Bae 1 week, 3 days ago
On 1/28/2026 12:11 AM, Thomas Gleixner wrote:
> On Sun, Jan 25 2026 at 01:42, Chang S. Bae wrote:
>>   static int load_cpus_stopped(void *unused)
>>   {
>> -	if (microcode_ops->use_nmi) {
>> -		/* Enable the NMI handler and raise NMI */
>> -		this_cpu_write(ucode_ctrl.nmi_enabled, true);
>> -		apic->send_IPI(smp_processor_id(), NMI_VECTOR);
>> -	} else {
>> -		/* Just invoke the handler directly */
>> -		microcode_update_handler();
>> -	}
>> +	/* Enable the NMI handler and raise NMI */
>> +	this_cpu_write(ucode_ctrl.nmi_enabled, true);
>> +	apic->send_IPI(smp_processor_id(), NMI_VECTOR);
>> +
> 
> With this change the function name is completely bogus.

Yes, you're right. The change should stand in a sensible shape if the 
series were to stop here. I think stop_this_cpu_nmi() that you mentioned 
on patch1 sounds aligned, or maybe stop_cpu_in_nmi() like that.