Skip to content

Commit 79cfbdf

Browse files
Srivatsa S. Bhatrjwysocki
authored andcommitted
PM / Sleep: Fix race between CPU hotplug and freezer
The CPU hotplug notifications sent out by the _cpu_up() and _cpu_down() functions depend on the value of the 'tasks_frozen' argument passed to them (which indicates whether tasks have been frozen or not). (Examples for such CPU hotplug notifications: CPU_ONLINE, CPU_ONLINE_FROZEN, CPU_DEAD, CPU_DEAD_FROZEN). Thus, it is essential that while the callbacks for those notifications are running, the state of the system with respect to the tasks being frozen or not remains unchanged, *throughout that duration*. Hence there is a need for synchronizing the CPU hotplug code with the freezer subsystem. Since the freezer is involved only in the Suspend/Hibernate call paths, this patch hooks the CPU hotplug code to the suspend/hibernate notifiers PM_[SUSPEND|HIBERNATE]_PREPARE and PM_POST_[SUSPEND|HIBERNATE] to prevent the race between CPU hotplug and freezer, thus ensuring that CPU hotplug notifications will always be run with the state of the system really being what the notifications indicate, _throughout_ their execution time. Signed-off-by: Srivatsa S. Bhat <srivatsa.bhat@linux.vnet.ibm.com> Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
1 parent ddf6ce4 commit 79cfbdf

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

‎kernel/cpu.c‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/stop_machine.h>
1616
#include <linux/mutex.h>
1717
#include <linux/gfp.h>
18+
#include <linux/suspend.h>
1819

1920
#ifdef CONFIG_SMP
2021
/* Serializes the updates to cpu_online_mask, cpu_present_mask */
@@ -476,6 +477,79 @@ static int alloc_frozen_cpus(void)
476477
return 0;
477478
}
478479
core_initcall(alloc_frozen_cpus);
480+
481+
/*
482+
* Prevent regular CPU hotplug from racing with the freezer, by disabling CPU
483+
* hotplug when tasks are about to be frozen. Also, don't allow the freezer
484+
* to continue until any currently running CPU hotplug operation gets
485+
* completed.
486+
* To modify the 'cpu_hotplug_disabled' flag, we need to acquire the
487+
* 'cpu_add_remove_lock'. And this same lock is also taken by the regular
488+
* CPU hotplug path and released only after it is complete. Thus, we
489+
* (and hence the freezer) will block here until any currently running CPU
490+
* hotplug operation gets completed.
491+
*/
492+
void cpu_hotplug_disable_before_freeze(void)
493+
{
494+
cpu_maps_update_begin();
495+
cpu_hotplug_disabled = 1;
496+
cpu_maps_update_done();
497+
}
498+
499+
500+
/*
501+
* When tasks have been thawed, re-enable regular CPU hotplug (which had been
502+
* disabled while beginning to freeze tasks).
503+
*/
504+
void cpu_hotplug_enable_after_thaw(void)
505+
{
506+
cpu_maps_update_begin();
507+
cpu_hotplug_disabled = 0;
508+
cpu_maps_update_done();
509+
}
510+
511+
/*
512+
* When callbacks for CPU hotplug notifications are being executed, we must
513+
* ensure that the state of the system with respect to the tasks being frozen
514+
* or not, as reported by the notification, remains unchanged *throughout the
515+
* duration* of the execution of the callbacks.
516+
* Hence we need to prevent the freezer from racing with regular CPU hotplug.
517+
*
518+
* This synchronization is implemented by mutually excluding regular CPU
519+
* hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
520+
* Hibernate notifications.
521+
*/
522+
static int
523+
cpu_hotplug_pm_callback(struct notifier_block *nb,
524+
unsigned long action, void *ptr)
525+
{
526+
switch (action) {
527+
528+
case PM_SUSPEND_PREPARE:
529+
case PM_HIBERNATION_PREPARE:
530+
cpu_hotplug_disable_before_freeze();
531+
break;
532+
533+
case PM_POST_SUSPEND:
534+
case PM_POST_HIBERNATION:
535+
cpu_hotplug_enable_after_thaw();
536+
break;
537+
538+
default:
539+
return NOTIFY_DONE;
540+
}
541+
542+
return NOTIFY_OK;
543+
}
544+
545+
546+
int cpu_hotplug_pm_sync_init(void)
547+
{
548+
pm_notifier(cpu_hotplug_pm_callback, 0);
549+
return 0;
550+
}
551+
core_initcall(cpu_hotplug_pm_sync_init);
552+
479553
#endif /* CONFIG_PM_SLEEP_SMP */
480554

481555
/**

0 commit comments

Comments
 (0)