2008-07-28 11:16:28 -06:00
|
|
|
/* Copyright 2008, 2005 Rusty Russell rusty@rustcorp.com.au IBM Corporation.
|
2006-09-29 03:01:35 -06:00
|
|
|
* GPL v2 and any later version.
|
|
|
|
*/
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <linux/cpu.h>
|
|
|
|
#include <linux/err.h>
|
2007-05-08 01:25:08 -06:00
|
|
|
#include <linux/kthread.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/stop_machine.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <linux/syscalls.h>
|
2007-05-10 23:22:47 -06:00
|
|
|
#include <linux/interrupt.h>
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <asm/atomic.h>
|
|
|
|
#include <asm/uaccess.h>
|
|
|
|
|
2008-07-28 11:16:28 -06:00
|
|
|
/* This controls the threads on each CPU. */
|
2005-04-16 16:20:36 -06:00
|
|
|
enum stopmachine_state {
|
2008-07-28 11:16:28 -06:00
|
|
|
/* Dummy starting state for thread. */
|
|
|
|
STOPMACHINE_NONE,
|
|
|
|
/* Awaiting everyone to be scheduled. */
|
2005-04-16 16:20:36 -06:00
|
|
|
STOPMACHINE_PREPARE,
|
2008-07-28 11:16:28 -06:00
|
|
|
/* Disable interrupts. */
|
2005-04-16 16:20:36 -06:00
|
|
|
STOPMACHINE_DISABLE_IRQ,
|
2008-07-28 11:16:28 -06:00
|
|
|
/* Run the function */
|
2008-02-28 09:33:03 -07:00
|
|
|
STOPMACHINE_RUN,
|
2008-07-28 11:16:28 -06:00
|
|
|
/* Exit */
|
2005-04-16 16:20:36 -06:00
|
|
|
STOPMACHINE_EXIT,
|
|
|
|
};
|
2008-07-28 11:16:28 -06:00
|
|
|
static enum stopmachine_state state;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-02-28 09:33:03 -07:00
|
|
|
struct stop_machine_data {
|
|
|
|
int (*fn)(void *);
|
|
|
|
void *data;
|
2008-07-28 11:16:28 -06:00
|
|
|
int fnret;
|
|
|
|
};
|
2008-02-28 09:33:03 -07:00
|
|
|
|
2008-07-28 11:16:28 -06:00
|
|
|
/* Like num_online_cpus(), but hotplug cpu uses us, so we need this. */
|
|
|
|
static unsigned int num_threads;
|
|
|
|
static atomic_t thread_ack;
|
|
|
|
static DEFINE_MUTEX(lock);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-10-13 15:50:10 -06:00
|
|
|
static struct workqueue_struct *stop_machine_wq;
|
|
|
|
static struct stop_machine_data active, idle;
|
|
|
|
static const cpumask_t *active_cpus;
|
|
|
|
static void *stop_machine_work;
|
|
|
|
|
2008-07-28 11:16:28 -06:00
|
|
|
static void set_state(enum stopmachine_state newstate)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-07-28 11:16:28 -06:00
|
|
|
/* Reset ack counter. */
|
|
|
|
atomic_set(&thread_ack, num_threads);
|
|
|
|
smp_wmb();
|
|
|
|
state = newstate;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2008-07-28 11:16:28 -06:00
|
|
|
/* Last one to ack a state moves to the next state. */
|
|
|
|
static void ack_state(void)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-10-13 15:50:10 -06:00
|
|
|
if (atomic_dec_and_test(&thread_ack))
|
|
|
|
set_state(state + 1);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2008-10-13 15:50:10 -06:00
|
|
|
/* This is the actual function which stops the CPU. It runs
|
|
|
|
* in the context of a dedicated stopmachine workqueue. */
|
|
|
|
static void stop_cpu(struct work_struct *unused)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-07-28 11:16:28 -06:00
|
|
|
enum stopmachine_state curstate = STOPMACHINE_NONE;
|
2008-10-13 15:50:10 -06:00
|
|
|
struct stop_machine_data *smdata = &idle;
|
|
|
|
int cpu = smp_processor_id();
|
2008-10-22 09:00:26 -06:00
|
|
|
int err;
|
2008-10-13 15:50:10 -06:00
|
|
|
|
|
|
|
if (!active_cpus) {
|
|
|
|
if (cpu == first_cpu(cpu_online_map))
|
|
|
|
smdata = &active;
|
|
|
|
} else {
|
|
|
|
if (cpu_isset(cpu, *active_cpus))
|
|
|
|
smdata = &active;
|
|
|
|
}
|
2008-07-28 11:16:28 -06:00
|
|
|
/* Simple state machine */
|
|
|
|
do {
|
|
|
|
/* Chill out and ensure we re-read stopmachine_state. */
|
2008-05-08 07:20:38 -06:00
|
|
|
cpu_relax();
|
2008-07-28 11:16:28 -06:00
|
|
|
if (state != curstate) {
|
|
|
|
curstate = state;
|
|
|
|
switch (curstate) {
|
|
|
|
case STOPMACHINE_DISABLE_IRQ:
|
|
|
|
local_irq_disable();
|
|
|
|
hard_irq_disable();
|
|
|
|
break;
|
|
|
|
case STOPMACHINE_RUN:
|
2008-10-22 09:00:26 -06:00
|
|
|
/* On multiple CPUs only a single error code
|
|
|
|
* is needed to tell that something failed. */
|
|
|
|
err = smdata->fn(smdata->data);
|
|
|
|
if (err)
|
|
|
|
smdata->fnret = err;
|
2008-07-28 11:16:28 -06:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ack_state();
|
|
|
|
}
|
|
|
|
} while (curstate != STOPMACHINE_EXIT);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
local_irq_enable();
|
|
|
|
}
|
|
|
|
|
2008-07-28 11:16:28 -06:00
|
|
|
/* Callback for CPUs which aren't supposed to do anything. */
|
|
|
|
static int chill(void *unused)
|
2008-02-28 09:33:03 -07:00
|
|
|
{
|
2008-07-28 11:16:28 -06:00
|
|
|
return 0;
|
2008-02-28 09:33:03 -07:00
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-07-28 11:16:30 -06:00
|
|
|
int __stop_machine(int (*fn)(void *), void *data, const cpumask_t *cpus)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-10-13 15:50:10 -06:00
|
|
|
struct work_struct *sm_work;
|
2008-11-16 14:52:18 -07:00
|
|
|
int i, ret;
|
2008-07-28 11:16:28 -06:00
|
|
|
|
2008-10-13 15:50:10 -06:00
|
|
|
/* Set up initial state. */
|
|
|
|
mutex_lock(&lock);
|
|
|
|
num_threads = num_online_cpus();
|
|
|
|
active_cpus = cpus;
|
2008-07-28 11:16:28 -06:00
|
|
|
active.fn = fn;
|
|
|
|
active.data = data;
|
|
|
|
active.fnret = 0;
|
|
|
|
idle.fn = chill;
|
|
|
|
idle.data = NULL;
|
|
|
|
|
|
|
|
set_state(STOPMACHINE_PREPARE);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-10-13 15:50:10 -06:00
|
|
|
/* Schedule the stop_cpu work on all cpus: hold this CPU so one
|
2008-07-28 11:16:28 -06:00
|
|
|
* doesn't hit this CPU until we're ready. */
|
2008-07-28 11:16:30 -06:00
|
|
|
get_cpu();
|
2008-10-13 15:50:10 -06:00
|
|
|
for_each_online_cpu(i) {
|
|
|
|
sm_work = percpu_ptr(stop_machine_work, i);
|
|
|
|
INIT_WORK(sm_work, stop_cpu);
|
|
|
|
queue_work_on(i, stop_machine_wq, sm_work);
|
|
|
|
}
|
2008-07-28 11:16:28 -06:00
|
|
|
/* This will release the thread on our CPU. */
|
|
|
|
put_cpu();
|
2008-10-13 15:50:10 -06:00
|
|
|
flush_workqueue(stop_machine_wq);
|
2008-11-16 14:52:18 -07:00
|
|
|
ret = active.fnret;
|
2008-07-28 11:16:28 -06:00
|
|
|
mutex_unlock(&lock);
|
2008-11-16 14:52:18 -07:00
|
|
|
return ret;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2008-07-28 11:16:30 -06:00
|
|
|
int stop_machine(int (*fn)(void *), void *data, const cpumask_t *cpus)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* No CPUs can come up or down during this. */
|
2008-01-25 13:08:02 -07:00
|
|
|
get_online_cpus();
|
2008-07-28 11:16:30 -06:00
|
|
|
ret = __stop_machine(fn, data, cpus);
|
2008-01-25 13:08:02 -07:00
|
|
|
put_online_cpus();
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2008-07-28 11:16:30 -06:00
|
|
|
EXPORT_SYMBOL_GPL(stop_machine);
|
2008-10-13 15:50:10 -06:00
|
|
|
|
|
|
|
static int __init stop_machine_init(void)
|
|
|
|
{
|
|
|
|
stop_machine_wq = create_rt_workqueue("kstop");
|
|
|
|
stop_machine_work = alloc_percpu(struct work_struct);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-10-25 20:53:38 -06:00
|
|
|
core_initcall(stop_machine_init);
|