Re: [RFC PATCH 28/56] stop_machine: Add stop_machine_nmi()

Chang S. Bae
2026-01-09T22:16:06Z

On 10/13/2025 7:34 AM, David Kaplan wrote:
>
> +/**
> + * stop_machine_nmi: freeze the machine and run this function in NMI context
> + * @fn: the function to run
> + * @data: the data ptr for the @fn()
> + * @cpus: the cpus to run the @fn() on (NULL = any online cpu)
> + *
> + * Like stop_machine() but runs the function in NMI context to avoid any risk of
> + * interruption due to NMIs.
> + *
> + * Protects against CPU hotplug.
> + */
> +int stop_machine_nmi(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus);
> +
> +/**
> + * stop_machine_cpuslocked_nmi: freeze and run this function in NMI context
> + * @fn: the function to run
> + * @data: the data ptr for the @fn()
> + * @cpus: the cpus to run the @fn() on (NULL = any online cpu)
> + *
> + * Same as above. Must be called from within a cpus_read_lock() protected
> + * region. Avoids nested calls to cpus_read_lock().
> + */
> +int stop_machine_cpuslocked_nmi(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus);

<snip>

> +int stop_machine_cpuslocked_nmi(cpu_stop_fn_t fn, void *data,
> + const struct cpumask *cpus)
> +{
> + return __stop_machine_cpuslocked(fn, data, cpus, true);
> +}
> +

It looks like this is readily missing the static key switching which is
handled below. I think the body could be something like:
...
static_branch_enable_cpuslocked(&stop_machine_nmi_handler_enable);
ret = __stop_machine_cpuslocked(fn, data, cpus, true);
static_branch_disable_cpuslocked(&stop_machine_nmi_handler_enable);
...

> +int stop_machine_nmi(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus)
> +{
> + int ret;
> +
> + cpus_read_lock();
> + static_branch_enable_cpuslocked(&stop_machine_nmi_handler_enable);
> + ret = stop_machine_cpuslocked_nmi(fn, data, cpus);
> + static_branch_disable_cpuslocked(&stop_machine_nmi_handler_enable);
> + cpus_read_unlock();
> + return ret;
> +}

With that, here __stop_machine_cpuslocked() can be invoked instead.

Thanks,
Chang