Skip to content

Commit 9ea09af

Browse files
heicarstrustyrussell
authored andcommitted
stop_machine: introduce stop_machine_create/destroy.
Introduce stop_machine_create/destroy. With this interface subsystems that need a non-failing stop_machine environment can create the stop_machine machine threads before actually calling stop_machine. When the threads aren't needed anymore they can be killed with stop_machine_destroy again. When stop_machine gets called and the threads aren't present they will be created and destroyed automatically. This restores the old behaviour of stop_machine. This patch also converts cpu hotplug to the new interface since it is special: cpu_down calls __stop_machine instead of stop_machine. However the kstop threads will only be created when stop_machine gets called. Changing the code so that the threads would be created automatically on __stop_machine is currently not possible: when __stop_machine gets called we hold cpu_add_remove_lock, which is the same lock that create_rt_workqueue would take. So the workqueue needs to be created before the cpu hotplug code locks cpu_add_remove_lock. Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent c298be7 commit 9ea09af

3 files changed

Lines changed: 72 additions & 11 deletions

File tree

‎include/linux/stop_machine.h‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ int stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus);
3535
* won't come or go while it's being called. Used by hotplug cpu.
3636
*/
3737
int __stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus);
38+
39+
/**
40+
* stop_machine_create: create all stop_machine threads
41+
*
42+
* Description: This causes all stop_machine threads to be created before
43+
* stop_machine actually gets called. This can be used by subsystems that
44+
* need a non failing stop_machine infrastructure.
45+
*/
46+
int stop_machine_create(void);
47+
48+
/**
49+
* stop_machine_destroy: destroy all stop_machine threads
50+
*
51+
* Description: This causes all stop_machine threads which were created with
52+
* stop_machine_create to be destroyed again.
53+
*/
54+
void stop_machine_destroy(void);
55+
3856
#else
3957

4058
static inline int stop_machine(int (*fn)(void *), void *data,
@@ -46,5 +64,9 @@ static inline int stop_machine(int (*fn)(void *), void *data,
4664
local_irq_enable();
4765
return ret;
4866
}
67+
68+
static inline int stop_machine_create(void) { return 0; }
69+
static inline void stop_machine_destroy(void) { }
70+
4971
#endif /* CONFIG_SMP */
5072
#endif /* _LINUX_STOP_MACHINE */

‎kernel/cpu.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,11 @@ static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
269269

270270
int __ref cpu_down(unsigned int cpu)
271271
{
272-
int err = 0;
272+
int err;
273273

274+
err = stop_machine_create();
275+
if (err)
276+
return err;
274277
cpu_maps_update_begin();
275278

276279
if (cpu_hotplug_disabled) {
@@ -297,6 +300,7 @@ int __ref cpu_down(unsigned int cpu)
297300

298301
out:
299302
cpu_maps_update_done();
303+
stop_machine_destroy();
300304
return err;
301305
}
302306
EXPORT_SYMBOL(cpu_down);

‎kernel/stop_machine.c‎

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ struct stop_machine_data {
3838
static unsigned int num_threads;
3939
static atomic_t thread_ack;
4040
static DEFINE_MUTEX(lock);
41-
41+
/* setup_lock protects refcount, stop_machine_wq and stop_machine_work. */
42+
static DEFINE_MUTEX(setup_lock);
43+
/* Users of stop_machine. */
44+
static int refcount;
4245
static struct workqueue_struct *stop_machine_wq;
4346
static struct stop_machine_data active, idle;
4447
static const cpumask_t *active_cpus;
@@ -109,6 +112,43 @@ static int chill(void *unused)
109112
return 0;
110113
}
111114

115+
int stop_machine_create(void)
116+
{
117+
mutex_lock(&setup_lock);
118+
if (refcount)
119+
goto done;
120+
stop_machine_wq = create_rt_workqueue("kstop");
121+
if (!stop_machine_wq)
122+
goto err_out;
123+
stop_machine_work = alloc_percpu(struct work_struct);
124+
if (!stop_machine_work)
125+
goto err_out;
126+
done:
127+
refcount++;
128+
mutex_unlock(&setup_lock);
129+
return 0;
130+
131+
err_out:
132+
if (stop_machine_wq)
133+
destroy_workqueue(stop_machine_wq);
134+
mutex_unlock(&setup_lock);
135+
return -ENOMEM;
136+
}
137+
EXPORT_SYMBOL_GPL(stop_machine_create);
138+
139+
void stop_machine_destroy(void)
140+
{
141+
mutex_lock(&setup_lock);
142+
refcount--;
143+
if (refcount)
144+
goto done;
145+
destroy_workqueue(stop_machine_wq);
146+
free_percpu(stop_machine_work);
147+
done:
148+
mutex_unlock(&setup_lock);
149+
}
150+
EXPORT_SYMBOL_GPL(stop_machine_destroy);
151+
112152
int __stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus)
113153
{
114154
struct work_struct *sm_work;
@@ -146,19 +186,14 @@ int stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus)
146186
{
147187
int ret;
148188

189+
ret = stop_machine_create();
190+
if (ret)
191+
return ret;
149192
/* No CPUs can come up or down during this. */
150193
get_online_cpus();
151194
ret = __stop_machine(fn, data, cpus);
152195
put_online_cpus();
153-
196+
stop_machine_destroy();
154197
return ret;
155198
}
156199
EXPORT_SYMBOL_GPL(stop_machine);
157-
158-
static int __init stop_machine_init(void)
159-
{
160-
stop_machine_wq = create_rt_workqueue("kstop");
161-
stop_machine_work = alloc_percpu(struct work_struct);
162-
return 0;
163-
}
164-
core_initcall(stop_machine_init);

0 commit comments

Comments
 (0)