commit | author | age
|
a07526
|
1 |
/* SPDX-License-Identifier: GPL-2.0 */ |
H |
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> |
2f529f
|
9 |
#include <linux/interrupt.h> |
a07526
|
10 |
|
H |
11 |
/* |
|
12 |
* stop_cpu[s]() is simplistic per-cpu maximum priority cpu |
|
13 |
* monopolization mechanism. The caller can specify a non-sleeping |
|
14 |
* function to be executed on a single or multiple cpus preempting all |
|
15 |
* other processes and monopolizing those cpus until it finishes. |
|
16 |
* |
|
17 |
* Resources for this mechanism are preallocated when a cpu is brought |
|
18 |
* up and requests are guaranteed to be served as long as the target |
|
19 |
* cpus are online. |
|
20 |
*/ |
|
21 |
typedef int (*cpu_stop_fn_t)(void *arg); |
|
22 |
|
|
23 |
#ifdef CONFIG_SMP |
|
24 |
|
|
25 |
struct cpu_stop_work { |
|
26 |
struct list_head list; /* cpu_stopper->works */ |
|
27 |
cpu_stop_fn_t fn; |
|
28 |
void *arg; |
|
29 |
struct cpu_stop_done *done; |
|
30 |
}; |
|
31 |
|
|
32 |
/* |
|
33 |
* Structure to determine completion condition and record errors. May |
|
34 |
* be shared by works on different cpus. |
|
35 |
*/ |
|
36 |
struct cpu_stop_done { |
|
37 |
atomic_t nr_todo; /* nr left to execute */ |
|
38 |
int ret; /* collected return value */ |
|
39 |
struct completion completion; /* fired if nr_todo reaches 0 */ |
|
40 |
}; |
|
41 |
|
|
42 |
int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg); |
|
43 |
int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void *arg); |
|
44 |
bool stop_one_cpu_nowait(unsigned int cpu, cpu_stop_fn_t fn, void *arg, |
|
45 |
struct cpu_stop_work *work_buf); |
|
46 |
void stop_machine_park(int cpu); |
|
47 |
void stop_machine_unpark(int cpu); |
|
48 |
void stop_machine_yield(const struct cpumask *cpumask); |
|
49 |
int stop_one_cpu_async(unsigned int cpu, cpu_stop_fn_t fn, void *arg, |
|
50 |
struct cpu_stop_work *work_buf, |
|
51 |
struct cpu_stop_done *done); |
|
52 |
void cpu_stop_work_wait(struct cpu_stop_work *work_buf); |
|
53 |
|
|
54 |
#else /* CONFIG_SMP */ |
|
55 |
|
|
56 |
#include <linux/workqueue.h> |
|
57 |
|
|
58 |
struct cpu_stop_work { |
|
59 |
struct work_struct work; |
|
60 |
cpu_stop_fn_t fn; |
|
61 |
void *arg; |
|
62 |
}; |
|
63 |
|
|
64 |
static inline int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg) |
|
65 |
{ |
|
66 |
int ret = -ENOENT; |
|
67 |
preempt_disable(); |
|
68 |
if (cpu == smp_processor_id()) |
|
69 |
ret = fn(arg); |
|
70 |
preempt_enable(); |
|
71 |
return ret; |
|
72 |
} |
|
73 |
|
|
74 |
static void stop_one_cpu_nowait_workfn(struct work_struct *work) |
|
75 |
{ |
|
76 |
struct cpu_stop_work *stwork = |
|
77 |
container_of(work, struct cpu_stop_work, work); |
|
78 |
preempt_disable(); |
|
79 |
stwork->fn(stwork->arg); |
|
80 |
preempt_enable(); |
|
81 |
} |
|
82 |
|
|
83 |
static inline bool stop_one_cpu_nowait(unsigned int cpu, |
|
84 |
cpu_stop_fn_t fn, void *arg, |
|
85 |
struct cpu_stop_work *work_buf) |
|
86 |
{ |
|
87 |
if (cpu == smp_processor_id()) { |
|
88 |
INIT_WORK(&work_buf->work, stop_one_cpu_nowait_workfn); |
|
89 |
work_buf->fn = fn; |
|
90 |
work_buf->arg = arg; |
|
91 |
schedule_work(&work_buf->work); |
|
92 |
return true; |
|
93 |
} |
|
94 |
|
|
95 |
return false; |
|
96 |
} |
|
97 |
|
|
98 |
#endif /* CONFIG_SMP */ |
|
99 |
|
|
100 |
/* |
|
101 |
* stop_machine "Bogolock": stop the entire machine, disable |
|
102 |
* interrupts. This is a very heavy lock, which is equivalent to |
|
103 |
* grabbing every spinlock (and more). So the "read" side to such a |
|
104 |
* lock is anything which disables preemption. |
|
105 |
*/ |
|
106 |
#if defined(CONFIG_SMP) || defined(CONFIG_HOTPLUG_CPU) |
|
107 |
|
|
108 |
/** |
|
109 |
* stop_machine: freeze the machine on all CPUs and run this function |
|
110 |
* @fn: the function to run |
|
111 |
* @data: the data ptr for the @fn() |
|
112 |
* @cpus: the cpus to run the @fn() on (NULL = any online cpu) |
|
113 |
* |
|
114 |
* Description: This causes a thread to be scheduled on every cpu, |
|
115 |
* each of which disables interrupts. The result is that no one is |
|
116 |
* holding a spinlock or inside any other preempt-disabled region when |
|
117 |
* @fn() runs. |
|
118 |
* |
|
119 |
* This can be thought of as a very heavy write lock, equivalent to |
|
120 |
* grabbing every spinlock in the kernel. |
|
121 |
* |
|
122 |
* Protects against CPU hotplug. |
|
123 |
*/ |
|
124 |
int stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus); |
|
125 |
|
|
126 |
/** |
|
127 |
* stop_machine_cpuslocked: freeze the machine on all CPUs and run this function |
|
128 |
* @fn: the function to run |
|
129 |
* @data: the data ptr for the @fn() |
|
130 |
* @cpus: the cpus to run the @fn() on (NULL = any online cpu) |
|
131 |
* |
|
132 |
* Same as above. Must be called from with in a cpus_read_lock() protected |
|
133 |
* region. Avoids nested calls to cpus_read_lock(). |
|
134 |
*/ |
|
135 |
int stop_machine_cpuslocked(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus); |
|
136 |
|
|
137 |
int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data, |
|
138 |
const struct cpumask *cpus); |
|
139 |
#else /* CONFIG_SMP || CONFIG_HOTPLUG_CPU */ |
|
140 |
|
|
141 |
static __always_inline int stop_machine_cpuslocked(cpu_stop_fn_t fn, void *data, |
|
142 |
const struct cpumask *cpus) |
|
143 |
{ |
|
144 |
unsigned long flags; |
|
145 |
int ret; |
|
146 |
local_irq_save(flags); |
2f529f
|
147 |
hard_irq_disable(); |
a07526
|
148 |
ret = fn(data); |
2f529f
|
149 |
hard_irq_enable(); |
a07526
|
150 |
local_irq_restore(flags); |
H |
151 |
return ret; |
|
152 |
} |
|
153 |
|
|
154 |
static __always_inline int |
|
155 |
stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus) |
|
156 |
{ |
|
157 |
return stop_machine_cpuslocked(fn, data, cpus); |
|
158 |
} |
|
159 |
|
|
160 |
static __always_inline int |
|
161 |
stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data, |
|
162 |
const struct cpumask *cpus) |
|
163 |
{ |
|
164 |
return stop_machine(fn, data, cpus); |
|
165 |
} |
|
166 |
|
|
167 |
#endif /* CONFIG_SMP || CONFIG_HOTPLUG_CPU */ |
|
168 |
#endif /* _LINUX_STOP_MACHINE */ |