Skip to content

Commit 16e53db

Browse files
Srivatsa S. Bhattorvalds
authored andcommitted
CPU hotplug: provide a generic helper to disable/enable CPU hotplug
There are instances in the kernel where we would like to disable CPU hotplug (from sysfs) during some important operation. Today the freezer code depends on this and the code to do it was kinda tailor-made for that. Restructure the code and make it generic enough to be useful for other usecases too. Signed-off-by: Srivatsa S. Bhat <[email protected]> Signed-off-by: Robin Holt <[email protected]> Cc: H. Peter Anvin <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Russ Anderson <[email protected]> Cc: Robin Holt <[email protected]> Cc: Russell King <[email protected]> Cc: Guan Xuetao <[email protected]> Cc: Shawn Guo <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 77293e2 commit 16e53db

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

include/linux/cpu.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ extern struct bus_type cpu_subsys;
175175

176176
extern void get_online_cpus(void);
177177
extern void put_online_cpus(void);
178+
extern void cpu_hotplug_disable(void);
179+
extern void cpu_hotplug_enable(void);
178180
#define hotcpu_notifier(fn, pri) cpu_notifier(fn, pri)
179181
#define register_hotcpu_notifier(nb) register_cpu_notifier(nb)
180182
#define unregister_hotcpu_notifier(nb) unregister_cpu_notifier(nb)
@@ -198,6 +200,8 @@ static inline void cpu_hotplug_driver_unlock(void)
198200

199201
#define get_online_cpus() do { } while (0)
200202
#define put_online_cpus() do { } while (0)
203+
#define cpu_hotplug_disable() do { } while (0)
204+
#define cpu_hotplug_enable() do { } while (0)
201205
#define hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
202206
/* These aren't inline functions due to a GCC bug. */
203207
#define register_hotcpu_notifier(nb) ({ (void)(nb); 0; })

kernel/cpu.c

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,27 @@ static void cpu_hotplug_done(void)
133133
mutex_unlock(&cpu_hotplug.lock);
134134
}
135135

136+
/*
137+
* Wait for currently running CPU hotplug operations to complete (if any) and
138+
* disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
139+
* the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
140+
* hotplug path before performing hotplug operations. So acquiring that lock
141+
* guarantees mutual exclusion from any currently running hotplug operations.
142+
*/
143+
void cpu_hotplug_disable(void)
144+
{
145+
cpu_maps_update_begin();
146+
cpu_hotplug_disabled = 1;
147+
cpu_maps_update_done();
148+
}
149+
150+
void cpu_hotplug_enable(void)
151+
{
152+
cpu_maps_update_begin();
153+
cpu_hotplug_disabled = 0;
154+
cpu_maps_update_done();
155+
}
156+
136157
#else /* #if CONFIG_HOTPLUG_CPU */
137158
static void cpu_hotplug_begin(void) {}
138159
static void cpu_hotplug_done(void) {}
@@ -540,36 +561,6 @@ static int __init alloc_frozen_cpus(void)
540561
}
541562
core_initcall(alloc_frozen_cpus);
542563

543-
/*
544-
* Prevent regular CPU hotplug from racing with the freezer, by disabling CPU
545-
* hotplug when tasks are about to be frozen. Also, don't allow the freezer
546-
* to continue until any currently running CPU hotplug operation gets
547-
* completed.
548-
* To modify the 'cpu_hotplug_disabled' flag, we need to acquire the
549-
* 'cpu_add_remove_lock'. And this same lock is also taken by the regular
550-
* CPU hotplug path and released only after it is complete. Thus, we
551-
* (and hence the freezer) will block here until any currently running CPU
552-
* hotplug operation gets completed.
553-
*/
554-
void cpu_hotplug_disable_before_freeze(void)
555-
{
556-
cpu_maps_update_begin();
557-
cpu_hotplug_disabled = 1;
558-
cpu_maps_update_done();
559-
}
560-
561-
562-
/*
563-
* When tasks have been thawed, re-enable regular CPU hotplug (which had been
564-
* disabled while beginning to freeze tasks).
565-
*/
566-
void cpu_hotplug_enable_after_thaw(void)
567-
{
568-
cpu_maps_update_begin();
569-
cpu_hotplug_disabled = 0;
570-
cpu_maps_update_done();
571-
}
572-
573564
/*
574565
* When callbacks for CPU hotplug notifications are being executed, we must
575566
* ensure that the state of the system with respect to the tasks being frozen
@@ -589,12 +580,12 @@ cpu_hotplug_pm_callback(struct notifier_block *nb,
589580

590581
case PM_SUSPEND_PREPARE:
591582
case PM_HIBERNATION_PREPARE:
592-
cpu_hotplug_disable_before_freeze();
583+
cpu_hotplug_disable();
593584
break;
594585

595586
case PM_POST_SUSPEND:
596587
case PM_POST_HIBERNATION:
597-
cpu_hotplug_enable_after_thaw();
588+
cpu_hotplug_enable();
598589
break;
599590

600591
default:

0 commit comments

Comments
 (0)