1
2#ifndef _LINUX_STOP_MACHINE
3#define _LINUX_STOP_MACHINE
4
5#include <linux/cpu.h>
6#include <linux/cpumask.h>
7#include <linux/smp.h>
8#include <linux/list.h>
9
10
11
12
13
14
15
16
17
18
19
20typedef int (*cpu_stop_fn_t)(void *arg);
21
22#ifdef CONFIG_SMP
23
24struct cpu_stop_work {
25 struct list_head list;
26 cpu_stop_fn_t fn;
27 void *arg;
28 struct cpu_stop_done *done;
29};
30
31int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg);
32int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void *arg);
33bool stop_one_cpu_nowait(unsigned int cpu, cpu_stop_fn_t fn, void *arg,
34 struct cpu_stop_work *work_buf);
35int stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg);
36int try_stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg);
37void stop_machine_park(int cpu);
38void stop_machine_unpark(int cpu);
39void stop_machine_yield(const struct cpumask *cpumask);
40
41#else
42
43#include <linux/workqueue.h>
44
45struct cpu_stop_work {
46 struct work_struct work;
47 cpu_stop_fn_t fn;
48 void *arg;
49};
50
51static inline int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
52{
53 int ret = -ENOENT;
54 preempt_disable();
55 if (cpu == smp_processor_id())
56 ret = fn(arg);
57 preempt_enable();
58 return ret;
59}
60
61static void stop_one_cpu_nowait_workfn(struct work_struct *work)
62{
63 struct cpu_stop_work *stwork =
64 container_of(work, struct cpu_stop_work, work);
65 preempt_disable();
66 stwork->fn(stwork->arg);
67 preempt_enable();
68}
69
70static inline bool stop_one_cpu_nowait(unsigned int cpu,
71 cpu_stop_fn_t fn, void *arg,
72 struct cpu_stop_work *work_buf)
73{
74 if (cpu == smp_processor_id()) {
75 INIT_WORK(&work_buf->work, stop_one_cpu_nowait_workfn);
76 work_buf->fn = fn;
77 work_buf->arg = arg;
78 schedule_work(&work_buf->work);
79 return true;
80 }
81
82 return false;
83}
84
85static inline int stop_cpus(const struct cpumask *cpumask,
86 cpu_stop_fn_t fn, void *arg)
87{
88 if (cpumask_test_cpu(raw_smp_processor_id(), cpumask))
89 return stop_one_cpu(raw_smp_processor_id(), fn, arg);
90 return -ENOENT;
91}
92
93static inline int try_stop_cpus(const struct cpumask *cpumask,
94 cpu_stop_fn_t fn, void *arg)
95{
96 return stop_cpus(cpumask, fn, arg);
97}
98
99#endif
100
101
102
103
104
105
106
107#if defined(CONFIG_SMP) || defined(CONFIG_HOTPLUG_CPU)
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125int stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus);
126
127
128
129
130
131
132
133
134
135
136int stop_machine_cpuslocked(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus);
137
138int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
139 const struct cpumask *cpus);
140#else
141
142static inline int stop_machine_cpuslocked(cpu_stop_fn_t fn, void *data,
143 const struct cpumask *cpus)
144{
145 unsigned long flags;
146 int ret;
147 local_irq_save(flags);
148 ret = fn(data);
149 local_irq_restore(flags);
150 return ret;
151}
152
153static inline int stop_machine(cpu_stop_fn_t fn, void *data,
154 const struct cpumask *cpus)
155{
156 return stop_machine_cpuslocked(fn, data, cpus);
157}
158
159static inline int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
160 const struct cpumask *cpus)
161{
162 return stop_machine(fn, data, cpus);
163}
164
165#endif
166#endif
167