summaryrefslogtreecommitdiff
path: root/xen/common/stop_machine.c
blob: 3adbe380de965d7e11ff0692fe707a1803c1d22a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/******************************************************************************
 * common/stop_machine.c
 *
 * Facilities to put whole machine in a safe 'stop' state
 *
 * Copyright 2005 Rusty Russell rusty@rustcorp.com.au IBM Corporation
 * Copyright 2008 Kevin Tian <kevin.tian@intel.com>, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; If not, see <http://www.gnu.org/licenses/>.
 */

#include <xen/init.h>
#include <xen/sched.h>
#include <xen/spinlock.h>
#include <xen/tasklet.h>
#include <xen/stop_machine.h>
#include <xen/errno.h>
#include <xen/smp.h>
#include <xen/cpu.h>
#include <asm/current.h>
#include <asm/processor.h>

enum stopmachine_state {
    STOPMACHINE_START,
    STOPMACHINE_PREPARE,
    STOPMACHINE_DISABLE_IRQ,
    STOPMACHINE_INVOKE,
    STOPMACHINE_EXIT
};

struct stopmachine_data {
    unsigned int nr_cpus;

    enum stopmachine_state state;
    atomic_t done;

    unsigned int fn_cpu;
    int fn_result;
    int (*fn)(void *);
    void *fn_data;
};

static DEFINE_PER_CPU(struct tasklet, stopmachine_tasklet);
static struct stopmachine_data stopmachine_data;
static DEFINE_SPINLOCK(stopmachine_lock);

static void stopmachine_set_state(enum stopmachine_state state)
{
    atomic_set(&stopmachine_data.done, 0);
    smp_wmb();
    stopmachine_data.state = state;
}

static void stopmachine_wait_state(void)
{
    while ( atomic_read(&stopmachine_data.done) != stopmachine_data.nr_cpus )
        cpu_relax();
}

/*
 * Sync all processors and call a function on one or all of them.
 * As stop_machine_run() is using a tasklet for syncing the processors it is
 * mandatory to be called only on an idle vcpu, as otherwise active core
 * scheduling might hang.
 */
int stop_machine_run(int (*fn)(void *), void *data, unsigned int cpu)
{
    unsigned int i, nr_cpus;
    unsigned int this = smp_processor_id();
    int ret;

    BUG_ON(!local_irq_is_enabled());
    BUG_ON(!is_idle_vcpu(current));

    /* cpu_online_map must not change. */
    if ( !get_cpu_maps() )
        return -EBUSY;

    nr_cpus = num_online_cpus();
    if ( cpu_online(this) )
        nr_cpus--;

    /* Must not spin here as the holder will expect us to be descheduled. */
    if ( !spin_trylock(&stopmachine_lock) )
    {
        put_cpu_maps();
        return -EBUSY;
    }

    stopmachine_data.fn = fn;
    stopmachine_data.fn_data = data;
    stopmachine_data.nr_cpus = nr_cpus;
    stopmachine_data.fn_cpu = cpu;
    stopmachine_data.fn_result = 0;
    atomic_set(&stopmachine_data.done, 0);
    stopmachine_data.state = STOPMACHINE_START;

    smp_wmb();

    for_each_online_cpu ( i )
        if ( i != this )
            tasklet_schedule_on_cpu(&per_cpu(stopmachine_tasklet, i), i);

    stopmachine_set_state(STOPMACHINE_PREPARE);
    stopmachine_wait_state();

    local_irq_disable();
    stopmachine_set_state(STOPMACHINE_DISABLE_IRQ);
    stopmachine_wait_state();
    spin_debug_disable();

    stopmachine_set_state(STOPMACHINE_INVOKE);
    if ( (cpu == this) || (cpu == NR_CPUS) )
    {
        ret = (*fn)(data);
        if ( ret )
            write_atomic(&stopmachine_data.fn_result, ret);
    }
    stopmachine_wait_state();
    ret = stopmachine_data.fn_result;

    spin_debug_enable();
    stopmachine_set_state(STOPMACHINE_EXIT);
    stopmachine_wait_state();
    local_irq_enable();

    spin_unlock(&stopmachine_lock);

    put_cpu_maps();

    return ret;
}

static void cf_check stopmachine_action(void *data)
{
    unsigned int cpu = (unsigned long)data;
    enum stopmachine_state state = STOPMACHINE_START;

    BUG_ON(cpu != smp_processor_id());

    smp_mb();

    while ( state != STOPMACHINE_EXIT )
    {
        while ( stopmachine_data.state == state )
            cpu_relax();

        state = stopmachine_data.state;
        switch ( state )
        {
        case STOPMACHINE_DISABLE_IRQ:
            local_irq_disable();
            break;
        case STOPMACHINE_INVOKE:
            if ( (stopmachine_data.fn_cpu == smp_processor_id()) ||
                 (stopmachine_data.fn_cpu == NR_CPUS) )
            {
                int ret = stopmachine_data.fn(stopmachine_data.fn_data);

                if ( ret )
                    write_atomic(&stopmachine_data.fn_result, ret);
            }
            break;
        default:
            break;
        }

        smp_mb();
        atomic_inc(&stopmachine_data.done);
    }

    local_irq_enable();
}

static int cf_check cpu_callback(
    struct notifier_block *nfb, unsigned long action, void *hcpu)
{
    unsigned int cpu = (unsigned long)hcpu;

    if ( action == CPU_UP_PREPARE )
        tasklet_init(&per_cpu(stopmachine_tasklet, cpu),
                     stopmachine_action, hcpu);

    return NOTIFY_DONE;
}

static struct notifier_block cpu_nfb = {
    .notifier_call = cpu_callback
};

static int __init cf_check cpu_stopmachine_init(void)
{
    unsigned int cpu;
    for_each_online_cpu ( cpu )
    {
        void *hcpu = (void *)(long)cpu;
        cpu_callback(&cpu_nfb, CPU_UP_PREPARE, hcpu);
    }
    register_cpu_notifier(&cpu_nfb);
    return 0;
}
__initcall(cpu_stopmachine_init);