x86/mce: Synchronize sysfs attribute updates with CPU hotplug
Subsystems (2)
syzbot
syzbotDuring CPU hotplug teardown, when an AP reaches CPUHP_AP_ONLINE_DYN, mce_cpu_pre_down() executes on the dying CPU, calling mce_disable_cpu() and timer_delete_sync() to disarm its per-CPU pinned MCE timer. However, the dying CPU remains in cpu_online_mask until the later CPUHP_TEARDOWN_CPU state. Sysfs write handlers modifying MCE attributes (such as check_interval, bank*, ignore_ce, or cmci_disabled) serialize among themselves via mce_sysfs_mutex and reinitialize MCE state across all online CPUs using on_each_cpu(mce_cpu_restart, ...) or on_each_cpu(mce_enable_ce, ...). Because these sysfs handlers have no synchronization with CPU hotplug, an IPI can execute on the dying CPU between mce_cpu_pre_down() and CPUHP_TEARDOWN_CPU, re-arming its per-CPU timer via __mcheck_cpu_init_timer().
When the dying CPU later reaches CPUHP_TIMERS_PREPARE, timers_dead_cpu() migrates any remaining pending timers from the dead CPU to an online CPU (such as CPU 0). When the migrated timer fires on CPU 0, mce_timer_fn() triggers a warning because the passed timer does not match CPU 0's per-CPU timer pointer. Furthermore, mce_timer_fn() re-arms the timer on the current CPU, permanently anchoring the offlined CPU's timer onto CPU 0's timer wheel where it repeatedly fires:
WARNING: CPU: 0 PID: 0 at arch/x86/kernel/cpu/mce/core.c:1770 mce_timer_fn+0x23f/0x280 RIP: 0010:mce_timer_fn+0x23f/0x280 arch/x86/kernel/cpu/mce/core.c:1770 Call Trace: <IRQ> call_timer_fn+0x18d/0x5f0 kernel/time/timer.c:1748 expire_timers kernel/time/timer.c:1799 [inline] __run_timers kernel/time/timer.c:2374 [inline] __run_timer_base+0x652/0x8b0 kernel/time/timer.c:2386 run_timer_softirq+0x67/0x170 kernel/time/timer.c:2403 handle_softirqs+0x226/0x860 kernel/softirq.c:645 __do_softirq kernel/softirq.c:679 [inline] invoke_softirq kernel/softirq.c:519 [inline] __irq_exit_rcu+0xcb/0x220 kernel/softirq.c:758 irq_exit_rcu+0x9/0x30 kernel/softirq.c:775 sysvec_apic_timer_interrupt+0xa6/0xc0 arch/x86/kernel/apic/apic.c:1062 </IRQ>
Acquiring cpus_read_lock() inside the sysfs write handlers is not viable because it causes an ABBA deadlock against CPU hotplug teardown: CPU hotplug holds cpu_hotplug_lock and waits for active kernfs references to drain in device_remove_file(), while sysfs write callbacks are invoked with an active kernfs reference held. Taking cpu_hotplug_lock inside the sysfs callback would invert this locking order.
Fix this by synchronizing MCE CPU hotplug callbacks with mce_sysfs_mutex and restricting sysfs broadcasts to the mce_device_initialized cpumask. In mce_cpu_online() and mce_cpu_pre_down(), acquire mce_sysfs_mutex around the MCE CPU state changes, timer setup/teardown, and mce_device_initialized updates, while keeping sysfs device creation and removal outside the mutex to preserve lock ordering. In mce_cpu_pre_down(), clearing the CPU from mce_device_initialized and disarming its timer under mce_sysfs_mutex ensures that concurrent sysfs handlers cannot re-arm the timer on the dying CPU. In mce_restart(), set_ignore_ce(), and set_cmci_disabled(), replace on_each_cpu() with on_each_cpu_mask(mce_device_initialized, ...), and update mce_timer_delete_all() to iterate over mce_device_initialized instead of cpu_online_mask. Finally, decouple mce_device_remove() from mce_device_initialized by checking whether the per-CPU device exists, and remove the redundant timer_setup() in __mcheck_cpu_init_timer().
To: "Borislav Petkov" <[email protected]>
To: "Dave Hansen" <[email protected]>
To: <[email protected]>
To: "Ingo Molnar" <[email protected]>
To: "Thomas Gleixner" <[email protected]>
To: "Tony Luck" <[email protected]>
To: <[email protected]>
To: "Seunghun Han" <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>
Cc: <[email protected]>
---
arch/x86/kernel/cpu/mce/core.c +15 -12
@@ -63,6 +63,8 @@/* sysfs synchronization */ static DEFINE_MUTEX (mce_sysfs_mutex); +static cpumask_var_t mce_device_initialized; +#define CREATE_TRACE_POINTS #include <trace/events/mce.h>
@@ -1813,7 +1815,7 @@ static void mce_timer_delete_all(void) {int cpu; -for_each_online_cpu (cpu) +for_each_cpu (cpu, mce_device_initialized)timer_delete_sync (&per_cpu (mce_timer, cpu)); }
@@ -2091,7 +2093,6 @@ static void __mcheck_cpu_init_timer(void) {struct timer_list *t =this_cpu_ptr (&mce_timer); -timer_setup (t, mce_timer_fn,TIMER_PINNED );mce_start_timer (t); }
@@ -2462,7 +2463,7 @@ static void mce_cpu_restart(void *data)static void mce_restart (void ) {mce_timer_delete_all (); -on_each_cpu (mce_cpu_restart,NULL ,1 ); +on_each_cpu_mask (mce_device_initialized, mce_cpu_restart,NULL ,1 );mce_schedule_work (); }
@@ -2553,12 +2554,12 @@ static ssize_t set_ignore_ce(struct device *s,if (new) {/* disable ce features */ mce_timer_delete_all (); -on_each_cpu (mce_disable_cmci,NULL ,1 ); +on_each_cpu_mask (mce_device_initialized, mce_disable_cmci,NULL ,1 ); mca_cfg.ignore_ce = true; }else {/* enable ce features */ mca_cfg.ignore_ce = false; -on_each_cpu (mce_enable_ce, (void *)1 ,1 ); +on_each_cpu_mask (mce_device_initialized, mce_enable_ce, (void *)1 ,1 ); } }mutex_unlock (&mce_sysfs_mutex);
@@ -2579,12 +2580,12 @@ static ssize_t set_cmci_disabled(struct device *s,if (mca_cfg.cmci_disabled ^ !!new) {if (new) {/* disable cmci */ -on_each_cpu (mce_disable_cmci,NULL ,1 ); +on_each_cpu_mask (mce_device_initialized, mce_disable_cmci,NULL ,1 ); mca_cfg.cmci_disabled = true; }else {/* enable cmci */ mca_cfg.cmci_disabled = false; -on_each_cpu (mce_enable_ce,NULL ,1 ); +on_each_cpu_mask (mce_device_initialized, mce_enable_ce,NULL ,1 ); } }mutex_unlock (&mce_sysfs_mutex);
@@ -2641,8 +2642,6 @@ static struct device_attribute *mce_device_attrs[] = { NULL }; -static cpumask_var_t mce_device_initialized; -static void mce_device_release (struct device *dev) {kfree (dev);
@@ -2682,7 +2681,6 @@ static int mce_device_create(unsigned int cpu)if (err) goto error2; } -cpumask_set_cpu (cpu, mce_device_initialized);per_cpu (mce_device, cpu) = dev;return 0 ;
@@ -2703,7 +2701,7 @@ static void mce_device_remove(unsigned int cpu)struct device *dev =per_cpu (mce_device, cpu);int i; -if (!cpumask_test_cpu (cpu, mce_device_initialized)) +if (!dev)return ;for (i =0 ; mce_device_attrs[i]; i++)
@@ -2713,7 +2711,6 @@ static void mce_device_remove(unsigned int cpu)device_remove_file (dev, &mce_bank_devs[i].attr);device_unregister (dev); -cpumask_clear_cpu (cpu, mce_device_initialized);per_cpu (mce_device, cpu) =NULL ; }
@@ -2761,8 +2758,11 @@ static int mce_cpu_online(unsigned int cpu)mce_device_create (cpu);mce_threshold_create_device (cpu); +mutex_lock (&mce_sysfs_mutex); +cpumask_set_cpu (cpu, mce_device_initialized);mce_reenable_cpu ();mce_start_timer (t); +mutex_unlock (&mce_sysfs_mutex);return 0 ; }
@@ -2770,8 +2770,11 @@ static int mce_cpu_pre_down(unsigned int cpu) {struct timer_list *t =this_cpu_ptr (&mce_timer); +mutex_lock (&mce_sysfs_mutex); +cpumask_clear_cpu (cpu, mce_device_initialized);mce_disable_cpu ();timer_delete_sync (t); +mutex_unlock (&mce_sysfs_mutex);mce_threshold_remove_device (cpu);mce_device_remove (cpu);return 0 ;
base-commit: c03c1a14507dfaf8b856c2608f9ac216021fc222
Signature · 9 lines
This is an AI-generated patch subject to moderation.
Reply with '#syz upstream' to Sign-off the patch as a human author
and send it to the upstream kernel mailing lists.
Reply with '#syz reject' to reject it ('#syz unreject' to undo).
See https://goo.gle/syzbot-ai-patches for information about AI-generated patches.
You can comment on the patch as usual, syzbot will try to address
the comments and send a new version of the patch if necessary.
syzbot engineers can be reached at [email protected].