Skip to content

Commit fe5595c

Browse files
Sebastian Andrzej SiewiorKAGA-KOKO
authored andcommitted
stop_machine: Provide stop_machine_cpuslocked()
Some call sites of stop_machine() are within a get_online_cpus() protected region. stop_machine() calls get_online_cpus() as well, which is possible in the current implementation but prevents converting the hotplug locking to a percpu rwsem. Provide stop_machine_cpuslocked() to avoid nested calls to get_online_cpus(). Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Tested-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Acked-by: Ingo Molnar <mingo@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Steven Rostedt <rostedt@goodmis.org> Link: http://lkml.kernel.org/r/20170524081547.400700852@linutronix.de
1 parent 9805c67 commit fe5595c

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

‎include/linux/stop_machine.h‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,29 @@ static inline int try_stop_cpus(const struct cpumask *cpumask,
116116
* @fn() runs.
117117
*
118118
* This can be thought of as a very heavy write lock, equivalent to
119-
* grabbing every spinlock in the kernel. */
119+
* grabbing every spinlock in the kernel.
120+
*
121+
* Protects against CPU hotplug.
122+
*/
120123
int stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus);
121124

125+
/**
126+
* stop_machine_cpuslocked: freeze the machine on all CPUs and run this function
127+
* @fn: the function to run
128+
* @data: the data ptr for the @fn()
129+
* @cpus: the cpus to run the @fn() on (NULL = any online cpu)
130+
*
131+
* Same as above. Must be called from with in a cpus_read_lock() protected
132+
* region. Avoids nested calls to cpus_read_lock().
133+
*/
134+
int stop_machine_cpuslocked(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus);
135+
122136
int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
123137
const struct cpumask *cpus);
124138
#else /* CONFIG_SMP || CONFIG_HOTPLUG_CPU */
125139

126-
static inline int stop_machine(cpu_stop_fn_t fn, void *data,
127-
const struct cpumask *cpus)
140+
static inline int stop_machine_cpuslocked(cpu_stop_fn_t fn, void *data,
141+
const struct cpumask *cpus)
128142
{
129143
unsigned long flags;
130144
int ret;
@@ -134,6 +148,12 @@ static inline int stop_machine(cpu_stop_fn_t fn, void *data,
134148
return ret;
135149
}
136150

151+
static inline int stop_machine(cpu_stop_fn_t fn, void *data,
152+
const struct cpumask *cpus)
153+
{
154+
return stop_machine_cpuslocked(fn, data, cpus);
155+
}
156+
137157
static inline int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
138158
const struct cpumask *cpus)
139159
{

‎kernel/stop_machine.c‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,8 @@ static int __init cpu_stop_init(void)
552552
}
553553
early_initcall(cpu_stop_init);
554554

555-
static int __stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus)
555+
int stop_machine_cpuslocked(cpu_stop_fn_t fn, void *data,
556+
const struct cpumask *cpus)
556557
{
557558
struct multi_stop_data msdata = {
558559
.fn = fn,
@@ -561,6 +562,8 @@ static int __stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cp
561562
.active_cpus = cpus,
562563
};
563564

565+
lockdep_assert_cpus_held();
566+
564567
if (!stop_machine_initialized) {
565568
/*
566569
* Handle the case where stop_machine() is called
@@ -590,9 +593,9 @@ int stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus)
590593
int ret;
591594

592595
/* No CPUs can come up or down during this. */
593-
get_online_cpus();
594-
ret = __stop_machine(fn, data, cpus);
595-
put_online_cpus();
596+
cpus_read_lock();
597+
ret = stop_machine_cpuslocked(fn, data, cpus);
598+
cpus_read_unlock();
596599
return ret;
597600
}
598601
EXPORT_SYMBOL_GPL(stop_machine);

0 commit comments

Comments
 (0)