Merge tag 'mm-hotfixes-stable-2023-05-03-16-27' of git://git.kernel.org/pub/scm/linux...
[linux-block.git] / include / linux / smp.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef __LINUX_SMP_H
3#define __LINUX_SMP_H
4
5/*
6 * Generic SMP support
7 * Alan Cox. <alan@redhat.com>
8 */
9
79974a0e 10#include <linux/errno.h>
54514a70 11#include <linux/types.h>
3d442233 12#include <linux/list.h>
3d442233 13#include <linux/cpumask.h>
04948c7f 14#include <linux/init.h>
8c4890d1 15#include <linux/smp_types.h>
1da177e4 16
3a5f65df 17typedef void (*smp_call_func_t)(void *info);
5671d814 18typedef bool (*smp_cond_func_t)(int cpu, void *info);
4b44a21d 19
4b44a21d
PZ
20/*
21 * structure shares (partial) layout with struct irq_work
22 */
966a9671 23struct __call_single_data {
545b8c8d 24 struct __call_single_node node;
3a5f65df 25 smp_call_func_t func;
3d442233 26 void *info;
3d442233
JA
27};
28
545b8c8d
PZ
29#define CSD_INIT(_func, _info) \
30 (struct __call_single_data){ .func = (_func), .info = (_info), }
31
966a9671
YH
32/* Use __aligned() to avoid to use 2 cache lines for 1 csd */
33typedef struct __call_single_data call_single_data_t
34 __aligned(sizeof(struct __call_single_data));
35
545b8c8d
PZ
36#define INIT_CSD(_csd, _func, _info) \
37do { \
38 *(_csd) = CSD_INIT((_func), (_info)); \
39} while (0)
40
4b44a21d
PZ
41/*
42 * Enqueue a llist_node on the call_single_queue; be very careful, read
43 * flush_smp_call_function_queue() in detail.
44 */
45extern void __smp_call_single_queue(int cpu, struct llist_node *node);
46
e057d7ae
MT
47/* total number of cpus in this system (may exceed NR_CPUS) */
48extern unsigned int total_cpus;
49
3a5f65df
DH
50int smp_call_function_single(int cpuid, smp_call_func_t func, void *info,
51 int wait);
53ce3d95 52
a5aa5ce3
NA
53void on_each_cpu_cond_mask(smp_cond_func_t cond_func, smp_call_func_t func,
54 void *info, bool wait, const struct cpumask *mask);
55
1139aeb1 56int smp_call_function_single_async(int cpu, struct __call_single_data *csd);
a5aa5ce3 57
6f1f942c
HY
58/*
59 * Cpus stopping functions in panic. All have default weak definitions.
60 * Architecture-dependent code may override them.
61 */
7412a60d 62void __noreturn panic_smp_self_stop(void);
27dea14c 63void __noreturn nmi_panic_self_stop(struct pt_regs *regs);
6f1f942c
HY
64void crash_smp_send_stop(void);
65
bff2dc42
DD
66/*
67 * Call a function on all processors
68 */
a5aa5ce3
NA
69static inline void on_each_cpu(smp_call_func_t func, void *info, int wait)
70{
71 on_each_cpu_cond_mask(NULL, func, info, wait, cpu_online_mask);
72}
73
74/**
75 * on_each_cpu_mask(): Run a function on processors specified by
76 * cpumask, which may include the local processor.
77 * @mask: The set of cpus to run on (only runs on online subset).
78 * @func: The function to run. This must be fast and non-blocking.
79 * @info: An arbitrary pointer to pass to the function.
80 * @wait: If true, wait (atomically) until function has completed
81 * on other CPUs.
82 *
83 * If @wait is true, then returns once @func has returned.
84 *
85 * You must not call this function with disabled interrupts or from a
86 * hardware interrupt handler or from a bottom half handler. The
87 * exception is that it may be used during early boot while
88 * early_boot_irqs_disabled is set.
fa688207 89 */
a5aa5ce3
NA
90static inline void on_each_cpu_mask(const struct cpumask *mask,
91 smp_call_func_t func, void *info, bool wait)
92{
93 on_each_cpu_cond_mask(NULL, func, info, wait, mask);
94}
fa688207
DD
95
96/*
97 * Call a function on each processor for which the supplied function
98 * cond_func returns a positive value. This may include the local
a5aa5ce3
NA
99 * processor. May be used during early boot while early_boot_irqs_disabled is
100 * set. Use local_irq_save/restore() instead of local_irq_disable/enable().
fa688207 101 */
a5aa5ce3
NA
102static inline void on_each_cpu_cond(smp_cond_func_t cond_func,
103 smp_call_func_t func, void *info, bool wait)
104{
105 on_each_cpu_cond_mask(cond_func, func, info, wait, cpu_online_mask);
106}
7cf64f86 107
1da177e4
LT
108#ifdef CONFIG_SMP
109
110#include <linux/preempt.h>
1da177e4
LT
111#include <linux/compiler.h>
112#include <linux/thread_info.h>
113#include <asm/smp.h>
1da177e4
LT
114
115/*
116 * main cross-CPU interfaces, handles INIT, TLB flush, STOP, etc.
117 * (defined in asm header):
d1dedb52 118 */
1da177e4
LT
119
120/*
121 * stops all CPUs but the current one:
122 */
123extern void smp_send_stop(void);
124
125/*
126 * sends a 'reschedule' event to another CPU:
127 */
4c8c3c7f
VS
128extern void arch_smp_send_reschedule(int cpu);
129/*
130 * scheduler_ipi() is inline so can't be passed as callback reason, but the
131 * callsite IP should be sufficient for root-causing IPIs sent from here.
132 */
68e2d17c
PZ
133#define smp_send_reschedule(cpu) ({ \
134 trace_ipi_send_cpu(cpu, _RET_IP_, NULL); \
135 arch_smp_send_reschedule(cpu); \
4c8c3c7f 136})
1da177e4
LT
137
138/*
139 * Prepare machine for booting other CPUs.
140 */
141extern void smp_prepare_cpus(unsigned int max_cpus);
142
143/*
144 * Bring a CPU up
145 */
8239c25f 146extern int __cpu_up(unsigned int cpunum, struct task_struct *tidle);
1da177e4
LT
147
148/*
149 * Final polishing of CPUs
150 */
151extern void smp_cpus_done(unsigned int max_cpus);
152
153/*
154 * Call a function on all other processors
155 */
caa75932 156void smp_call_function(smp_call_func_t func, void *info, int wait);
54b11e6d 157void smp_call_function_many(const struct cpumask *mask,
3a5f65df 158 smp_call_func_t func, void *info, bool wait);
2d3854a3 159
2ea6dec4 160int smp_call_function_any(const struct cpumask *mask,
3a5f65df 161 smp_call_func_t func, void *info, int wait);
2ea6dec4 162
f37f435f 163void kick_all_cpus_sync(void);
c6f4459f 164void wake_up_all_idle_cpus(void);
f37f435f 165
3d442233
JA
166/*
167 * Generic and arch helpers
168 */
d8ad7d11 169void __init call_function_init(void);
3d442233 170void generic_smp_call_function_single_interrupt(void);
9a46ad6d
SL
171#define generic_smp_call_function_interrupt \
172 generic_smp_call_function_single_interrupt
a3bc0dbc 173
1da177e4
LT
174/*
175 * Mark the boot cpu "online" so that it can call console drivers in
176 * printk() and can access its per-cpu storage.
177 */
178void smp_prepare_boot_cpu(void);
179
ca74a6f8 180extern unsigned int setup_max_cpus;
34db18a0
AW
181extern void __init setup_nr_cpu_ids(void);
182extern void __init smp_init(void);
ca74a6f8 183
8ce371f9
PZ
184extern int __boot_cpu_id;
185
186static inline int get_boot_cpu_id(void)
187{
188 return __boot_cpu_id;
189}
190
1da177e4
LT
191#else /* !SMP */
192
d1dedb52
IM
193static inline void smp_send_stop(void) { }
194
1da177e4
LT
195/*
196 * These macros fold the SMP functionality into a single CPU system
197 */
39c715b7 198#define raw_smp_processor_id() 0
caa75932 199static inline void up_smp_call_function(smp_call_func_t func, void *info)
3c30b06d 200{
3c30b06d 201}
8691e5a8 202#define smp_call_function(func, info, wait) \
a5fbb6d1 203 (up_smp_call_function(func, info))
3b8967d7 204
79a88102 205static inline void smp_send_reschedule(int cpu) { }
2ac6608c 206#define smp_prepare_boot_cpu() do {} while (0)
d2ff9118
RR
207#define smp_call_function_many(mask, func, info, wait) \
208 (up_smp_call_function(func, info))
d8ad7d11 209static inline void call_function_init(void) { }
2ea6dec4
RR
210
211static inline int
3a5f65df 212smp_call_function_any(const struct cpumask *mask, smp_call_func_t func,
2ea6dec4 213 void *info, int wait)
3d442233 214{
2ea6dec4 215 return smp_call_function_single(0, func, info, wait);
3d442233 216}
2ea6dec4 217
f37f435f 218static inline void kick_all_cpus_sync(void) { }
c6f4459f 219static inline void wake_up_all_idle_cpus(void) { }
f37f435f 220
30b8b006
TG
221#ifdef CONFIG_UP_LATE_INIT
222extern void __init up_late_init(void);
223static inline void smp_init(void) { up_late_init(); }
224#else
225static inline void smp_init(void) { }
226#endif
227
8ce371f9
PZ
228static inline int get_boot_cpu_id(void)
229{
230 return 0;
231}
232
1da177e4
LT
233#endif /* !SMP */
234
9ed7d75b
PZ
235/**
236 * raw_processor_id() - get the current (unstable) CPU id
237 *
238 * For then you know what you are doing and need an unstable
239 * CPU id.
240 */
241
242/**
243 * smp_processor_id() - get the current (stable) CPU id
244 *
245 * This is the normal accessor to the CPU id and should be used
246 * whenever possible.
247 *
248 * The CPU id is stable when:
1da177e4 249 *
9ed7d75b
PZ
250 * - IRQs are disabled;
251 * - preemption is disabled;
252 * - the task is CPU affine.
1da177e4 253 *
9ed7d75b
PZ
254 * When CONFIG_DEBUG_PREEMPT; we verify these assumption and WARN
255 * when smp_processor_id() is used when the CPU id is not stable.
1da177e4 256 */
9ed7d75b
PZ
257
258/*
259 * Allow the architecture to differentiate between a stable and unstable read.
260 * For example, x86 uses an IRQ-safe asm-volatile read for the unstable but a
261 * regular asm read for the stable.
262 */
263#ifndef __smp_processor_id
264#define __smp_processor_id(x) raw_smp_processor_id(x)
265#endif
266
39c715b7
IM
267#ifdef CONFIG_DEBUG_PREEMPT
268 extern unsigned int debug_smp_processor_id(void);
269# define smp_processor_id() debug_smp_processor_id()
1da177e4 270#else
9ed7d75b 271# define smp_processor_id() __smp_processor_id()
1da177e4
LT
272#endif
273
9ed7d75b 274#define get_cpu() ({ preempt_disable(); __smp_processor_id(); })
1da177e4 275#define put_cpu() preempt_enable()
1da177e4 276
a146649b
IM
277/*
278 * Callback to arch code if there's nosmp or maxcpus=0 on the
279 * boot command line:
280 */
281extern void arch_disable_smp_support(void);
282
56555855
QY
283extern void arch_thaw_secondary_cpus_begin(void);
284extern void arch_thaw_secondary_cpus_end(void);
fb37bb04 285
033ab7f8
AM
286void smp_setup_processor_id(void);
287
df8ce9d7
JG
288int smp_call_on_cpu(unsigned int cpu, int (*func)(void *), void *par,
289 bool phys);
290
31487f83
RW
291/* SMP core functions */
292int smpcfd_prepare_cpu(unsigned int cpu);
293int smpcfd_dead_cpu(unsigned int cpu);
294int smpcfd_dying_cpu(unsigned int cpu);
295
1da177e4 296#endif /* __LINUX_SMP_H */