blob: af0d51da84a2246fa1293cfffa92ba3f1840989b [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Jens Axboe3d442232008-06-26 11:21:34 +02002/*
3 * Generic helpers for smp ipi calls
4 *
5 * (C) Jens Axboe <jens.axboe@oracle.com> 2008
Jens Axboe3d442232008-06-26 11:21:34 +02006 */
Michael Ellermanca7dfdb2016-10-26 16:37:53 +11007
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Frederic Weisbecker47885012014-05-08 01:37:48 +020010#include <linux/irq_work.h>
Jens Axboe3d442232008-06-26 11:21:34 +020011#include <linux/rcupdate.h>
Linus Torvalds59190f4212008-07-15 14:02:33 -070012#include <linux/rculist.h>
Ingo Molnar641cd4c2009-03-13 10:47:34 +010013#include <linux/kernel.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040014#include <linux/export.h>
Ingo Molnar0b13fda2009-02-25 16:52:11 +010015#include <linux/percpu.h>
16#include <linux/init.h>
Sebastian Andrzej Siewiorf9d34592021-01-23 21:10:25 +010017#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/gfp.h>
Jens Axboe3d442232008-06-26 11:21:34 +020019#include <linux/smp.h>
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010020#include <linux/cpu.h>
Chuansheng Liuc6f44592014-09-04 15:17:54 +080021#include <linux/sched.h>
Ingo Molnar4c822692017-02-01 16:36:40 +010022#include <linux/sched/idle.h>
Juergen Gross47ae4b02016-08-29 08:48:43 +020023#include <linux/hypervisor.h>
Paul E. McKenney35feb602020-06-30 13:22:54 -070024#include <linux/sched/clock.h>
25#include <linux/nmi.h>
26#include <linux/sched/debug.h>
Jens Axboe3d442232008-06-26 11:21:34 +020027
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070028#include "smpboot.h"
Ingo Molnar1f8db412020-05-28 11:01:34 +020029#include "sched/smp.h"
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070030
Peter Zijlstra545b8c82020-06-15 11:29:31 +020031#define CSD_TYPE(_csd) ((_csd)->node.u_flags & CSD_FLAG_TYPE_MASK)
Jens Axboe3d442232008-06-26 11:21:34 +020032
33struct call_function_data {
Ying Huang966a9672017-08-08 12:30:00 +080034 call_single_data_t __percpu *csd;
Ingo Molnar0b13fda2009-02-25 16:52:11 +010035 cpumask_var_t cpumask;
Aaron Lu3fc5b3b2017-05-19 15:53:31 +080036 cpumask_var_t cpumask_ipi;
Jens Axboe3d442232008-06-26 11:21:34 +020037};
38
Nadav Amita22793c2019-06-12 23:48:11 -070039static DEFINE_PER_CPU_ALIGNED(struct call_function_data, cfd_data);
Milton Millere03bcb62010-01-18 13:00:51 +110040
Christoph Hellwig6897fc22014-01-30 15:45:47 -080041static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010042
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -070043static void flush_smp_call_function_queue(bool warn_cpu_offline);
44
Richard Weinberger31487f82016-07-13 17:17:01 +000045int smpcfd_prepare_cpu(unsigned int cpu)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010046{
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010047 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
48
Richard Weinberger31487f82016-07-13 17:17:01 +000049 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
50 cpu_to_node(cpu)))
51 return -ENOMEM;
Aaron Lu3fc5b3b2017-05-19 15:53:31 +080052 if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL,
53 cpu_to_node(cpu))) {
54 free_cpumask_var(cfd->cpumask);
55 return -ENOMEM;
56 }
Ying Huang966a9672017-08-08 12:30:00 +080057 cfd->csd = alloc_percpu(call_single_data_t);
Richard Weinberger31487f82016-07-13 17:17:01 +000058 if (!cfd->csd) {
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010059 free_cpumask_var(cfd->cpumask);
Aaron Lu3fc5b3b2017-05-19 15:53:31 +080060 free_cpumask_var(cfd->cpumask_ipi);
Richard Weinberger31487f82016-07-13 17:17:01 +000061 return -ENOMEM;
62 }
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -070063
Richard Weinberger31487f82016-07-13 17:17:01 +000064 return 0;
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010065}
66
Richard Weinberger31487f82016-07-13 17:17:01 +000067int smpcfd_dead_cpu(unsigned int cpu)
68{
69 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
70
71 free_cpumask_var(cfd->cpumask);
Aaron Lu3fc5b3b2017-05-19 15:53:31 +080072 free_cpumask_var(cfd->cpumask_ipi);
Richard Weinberger31487f82016-07-13 17:17:01 +000073 free_percpu(cfd->csd);
74 return 0;
75}
76
77int smpcfd_dying_cpu(unsigned int cpu)
78{
79 /*
80 * The IPIs for the smp-call-function callbacks queued by other
81 * CPUs might arrive late, either due to hardware latencies or
82 * because this CPU disabled interrupts (inside stop-machine)
83 * before the IPIs were sent. So flush out any pending callbacks
84 * explicitly (without waiting for the IPIs to arrive), to
85 * ensure that the outgoing CPU doesn't go offline with work
86 * still pending.
87 */
88 flush_smp_call_function_queue(false);
Peter Zijlstraafaa6532020-05-26 18:11:00 +020089 irq_work_run();
Richard Weinberger31487f82016-07-13 17:17:01 +000090 return 0;
91}
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010092
Takao Indohd8ad7d12011-03-29 12:35:04 -040093void __init call_function_init(void)
Jens Axboe3d442232008-06-26 11:21:34 +020094{
95 int i;
96
Christoph Hellwig6897fc22014-01-30 15:45:47 -080097 for_each_possible_cpu(i)
98 init_llist_head(&per_cpu(call_single_queue, i));
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010099
Richard Weinberger31487f82016-07-13 17:17:01 +0000100 smpcfd_prepare_cpu(smp_processor_id());
Jens Axboe3d442232008-06-26 11:21:34 +0200101}
102
Paul E. McKenney35feb602020-06-30 13:22:54 -0700103#ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
104
105static DEFINE_PER_CPU(call_single_data_t *, cur_csd);
106static DEFINE_PER_CPU(smp_call_func_t, cur_csd_func);
107static DEFINE_PER_CPU(void *, cur_csd_info);
108
109#define CSD_LOCK_TIMEOUT (5ULL * NSEC_PER_SEC)
Wei Yongjun2b722162020-07-06 21:49:41 +0800110static atomic_t csd_bug_count = ATOMIC_INIT(0);
Paul E. McKenney35feb602020-06-30 13:22:54 -0700111
112/* Record current CSD work for current CPU, NULL to erase. */
113static void csd_lock_record(call_single_data_t *csd)
114{
115 if (!csd) {
116 smp_mb(); /* NULL cur_csd after unlock. */
117 __this_cpu_write(cur_csd, NULL);
118 return;
119 }
120 __this_cpu_write(cur_csd_func, csd->func);
121 __this_cpu_write(cur_csd_info, csd->info);
122 smp_wmb(); /* func and info before csd. */
123 __this_cpu_write(cur_csd, csd);
124 smp_mb(); /* Update cur_csd before function call. */
125 /* Or before unlock, as the case may be. */
126}
127
128static __always_inline int csd_lock_wait_getcpu(call_single_data_t *csd)
129{
130 unsigned int csd_type;
131
132 csd_type = CSD_TYPE(csd);
133 if (csd_type == CSD_TYPE_ASYNC || csd_type == CSD_TYPE_SYNC)
Ingo Molnara787bda2020-11-27 11:09:57 +0100134 return csd->node.dst; /* Other CSD_TYPE_ values might not have ->dst. */
Paul E. McKenney35feb602020-06-30 13:22:54 -0700135 return -1;
136}
137
138/*
139 * Complain if too much time spent waiting. Note that only
140 * the CSD_TYPE_SYNC/ASYNC types provide the destination CPU,
141 * so waiting on other types gets much less information.
142 */
143static __always_inline bool csd_lock_wait_toolong(call_single_data_t *csd, u64 ts0, u64 *ts1, int *bug_id)
144{
145 int cpu = -1;
146 int cpux;
147 bool firsttime;
148 u64 ts2, ts_delta;
149 call_single_data_t *cpu_cur_csd;
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200150 unsigned int flags = READ_ONCE(csd->node.u_flags);
Paul E. McKenney35feb602020-06-30 13:22:54 -0700151
152 if (!(flags & CSD_FLAG_LOCK)) {
153 if (!unlikely(*bug_id))
154 return true;
155 cpu = csd_lock_wait_getcpu(csd);
156 pr_alert("csd: CSD lock (#%d) got unstuck on CPU#%02d, CPU#%02d released the lock.\n",
157 *bug_id, raw_smp_processor_id(), cpu);
158 return true;
159 }
160
161 ts2 = sched_clock();
162 ts_delta = ts2 - *ts1;
163 if (likely(ts_delta <= CSD_LOCK_TIMEOUT))
164 return false;
165
166 firsttime = !*bug_id;
167 if (firsttime)
168 *bug_id = atomic_inc_return(&csd_bug_count);
169 cpu = csd_lock_wait_getcpu(csd);
170 if (WARN_ONCE(cpu < 0 || cpu >= nr_cpu_ids, "%s: cpu = %d\n", __func__, cpu))
171 cpux = 0;
172 else
173 cpux = cpu;
174 cpu_cur_csd = smp_load_acquire(&per_cpu(cur_csd, cpux)); /* Before func and info. */
175 pr_alert("csd: %s non-responsive CSD lock (#%d) on CPU#%d, waiting %llu ns for CPU#%02d %pS(%ps).\n",
176 firsttime ? "Detected" : "Continued", *bug_id, raw_smp_processor_id(), ts2 - ts0,
177 cpu, csd->func, csd->info);
178 if (cpu_cur_csd && csd != cpu_cur_csd) {
179 pr_alert("\tcsd: CSD lock (#%d) handling prior %pS(%ps) request.\n",
180 *bug_id, READ_ONCE(per_cpu(cur_csd_func, cpux)),
181 READ_ONCE(per_cpu(cur_csd_info, cpux)));
182 } else {
183 pr_alert("\tcsd: CSD lock (#%d) %s.\n",
184 *bug_id, !cpu_cur_csd ? "unresponsive" : "handling this request");
185 }
186 if (cpu >= 0) {
187 if (!trigger_single_cpu_backtrace(cpu))
188 dump_cpu_task(cpu);
189 if (!cpu_cur_csd) {
190 pr_alert("csd: Re-sending CSD lock (#%d) IPI from CPU#%02d to CPU#%02d\n", *bug_id, raw_smp_processor_id(), cpu);
191 arch_send_call_function_single_ipi(cpu);
192 }
193 }
194 dump_stack();
195 *ts1 = ts2;
196
197 return false;
198}
199
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100200/*
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100201 * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
202 *
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100203 * For non-synchronous ipi calls the csd can still be in use by the
204 * previous function call. For multi-cpu calls its even more interesting
205 * as we'll have to ensure no other cpu is observing our csd.
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100206 */
Ying Huang966a9672017-08-08 12:30:00 +0800207static __always_inline void csd_lock_wait(call_single_data_t *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100208{
Paul E. McKenney35feb602020-06-30 13:22:54 -0700209 int bug_id = 0;
210 u64 ts0, ts1;
211
212 ts1 = ts0 = sched_clock();
213 for (;;) {
214 if (csd_lock_wait_toolong(csd, ts0, &ts1, &bug_id))
215 break;
216 cpu_relax();
217 }
218 smp_acquire__after_ctrl_dep();
219}
220
221#else
222static void csd_lock_record(call_single_data_t *csd)
223{
224}
225
226static __always_inline void csd_lock_wait(call_single_data_t *csd)
227{
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200228 smp_cond_load_acquire(&csd->node.u_flags, !(VAL & CSD_FLAG_LOCK));
Peter Zijlstra6e275632009-02-25 13:59:48 +0100229}
Paul E. McKenney35feb602020-06-30 13:22:54 -0700230#endif
Peter Zijlstra6e275632009-02-25 13:59:48 +0100231
Ying Huang966a9672017-08-08 12:30:00 +0800232static __always_inline void csd_lock(call_single_data_t *csd)
Peter Zijlstra6e275632009-02-25 13:59:48 +0100233{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700234 csd_lock_wait(csd);
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200235 csd->node.u_flags |= CSD_FLAG_LOCK;
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100236
237 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100238 * prevent CPU from reordering the above assignment
239 * to ->flags with any subsequent assignments to other
Ying Huang966a9672017-08-08 12:30:00 +0800240 * fields of the specified call_single_data_t structure:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100241 */
Linus Torvalds80538712015-02-11 12:42:10 -0800242 smp_wmb();
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100243}
244
Ying Huang966a9672017-08-08 12:30:00 +0800245static __always_inline void csd_unlock(call_single_data_t *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100246{
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200247 WARN_ON(!(csd->node.u_flags & CSD_FLAG_LOCK));
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100248
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100249 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100250 * ensure we're all done before releasing data:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100251 */
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200252 smp_store_release(&csd->node.u_flags, 0);
Jens Axboe3d442232008-06-26 11:21:34 +0200253}
254
Ying Huang966a9672017-08-08 12:30:00 +0800255static DEFINE_PER_CPU_SHARED_ALIGNED(call_single_data_t, csd_data);
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100256
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200257void __smp_call_single_queue(int cpu, struct llist_node *node)
258{
259 /*
260 * The list addition should be visible before sending the IPI
261 * handler locks the list to pull the entry off it because of
262 * normal cache coherency rules implied by spinlocks.
263 *
264 * If IPIs can go out of order to the cache coherency protocol
265 * in an architecture, sufficient synchronisation should be added
266 * to arch code to make it appear to obey cache coherency WRT
267 * locking and barrier primitives. Generic code isn't really
268 * equipped to do the right thing...
269 */
270 if (llist_add(node, &per_cpu(call_single_queue, cpu)))
271 send_call_function_single_ipi(cpu);
272}
273
Jens Axboe3d442232008-06-26 11:21:34 +0200274/*
Ying Huang966a9672017-08-08 12:30:00 +0800275 * Insert a previously allocated call_single_data_t element
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100276 * for execution on the given CPU. data must already have
277 * ->func, ->info, and ->flags set.
Jens Axboe3d442232008-06-26 11:21:34 +0200278 */
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200279static int generic_exec_single(int cpu, call_single_data_t *csd)
Jens Axboe3d442232008-06-26 11:21:34 +0200280{
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100281 if (cpu == smp_processor_id()) {
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200282 smp_call_func_t func = csd->func;
283 void *info = csd->info;
Linus Torvalds80538712015-02-11 12:42:10 -0800284 unsigned long flags;
285
286 /*
287 * We can unlock early even for the synchronous on-stack case,
288 * since we're doing this from the same CPU..
289 */
Paul E. McKenney35feb602020-06-30 13:22:54 -0700290 csd_lock_record(csd);
Linus Torvalds80538712015-02-11 12:42:10 -0800291 csd_unlock(csd);
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100292 local_irq_save(flags);
293 func(info);
Paul E. McKenney35feb602020-06-30 13:22:54 -0700294 csd_lock_record(NULL);
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100295 local_irq_restore(flags);
296 return 0;
297 }
298
Linus Torvalds5224b962015-04-19 04:56:03 -0400299 if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu)) {
300 csd_unlock(csd);
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100301 return -ENXIO;
Linus Torvalds5224b962015-04-19 04:56:03 -0400302 }
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100303
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200304 __smp_call_single_queue(cpu, &csd->node.llist);
Jens Axboe3d442232008-06-26 11:21:34 +0200305
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100306 return 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200307}
308
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700309/**
310 * generic_smp_call_function_single_interrupt - Execute SMP IPI callbacks
311 *
312 * Invoked by arch to handle an IPI for call function single.
313 * Must be called with interrupts disabled.
Jens Axboe3d442232008-06-26 11:21:34 +0200314 */
315void generic_smp_call_function_single_interrupt(void)
316{
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700317 flush_smp_call_function_queue(true);
318}
319
320/**
321 * flush_smp_call_function_queue - Flush pending smp-call-function callbacks
322 *
323 * @warn_cpu_offline: If set to 'true', warn if callbacks were queued on an
324 * offline CPU. Skip this check if set to 'false'.
325 *
326 * Flush any pending smp-call-function callbacks queued on this CPU. This is
327 * invoked by the generic IPI handler, as well as by a CPU about to go offline,
328 * to ensure that all pending IPI callbacks are run before it goes completely
329 * offline.
330 *
331 * Loop through the call_single_queue and run all the queued callbacks.
332 * Must be called with interrupts disabled.
333 */
334static void flush_smp_call_function_queue(bool warn_cpu_offline)
335{
Ying Huang966a9672017-08-08 12:30:00 +0800336 call_single_data_t *csd, *csd_next;
Peter Zijlstra52103be2020-05-26 18:10:59 +0200337 struct llist_node *entry, *prev;
338 struct llist_head *head;
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700339 static bool warned;
340
Frederic Weisbecker83efcbd2017-11-06 16:01:22 +0100341 lockdep_assert_irqs_disabled();
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700342
Christoph Lameterbb964a92014-08-17 12:30:24 -0500343 head = this_cpu_ptr(&call_single_queue);
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700344 entry = llist_del_all(head);
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700345 entry = llist_reverse_order(entry);
Jens Axboe3d442232008-06-26 11:21:34 +0200346
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700347 /* There shouldn't be any pending callbacks on an offline CPU. */
348 if (unlikely(warn_cpu_offline && !cpu_online(smp_processor_id()) &&
349 !warned && !llist_empty(head))) {
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700350 warned = true;
351 WARN(1, "IPI on offline CPU %d\n", smp_processor_id());
Suresh Siddha269c8612009-08-19 18:05:35 -0700352
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700353 /*
354 * We don't have to use the _safe() variant here
355 * because we are not invoking the IPI handlers yet.
356 */
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200357 llist_for_each_entry(csd, entry, node.llist) {
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200358 switch (CSD_TYPE(csd)) {
359 case CSD_TYPE_ASYNC:
360 case CSD_TYPE_SYNC:
361 case CSD_TYPE_IRQ_WORK:
362 pr_warn("IPI callback %pS sent to offline CPU\n",
363 csd->func);
364 break;
Jens Axboe3d442232008-06-26 11:21:34 +0200365
Peter Zijlstraa14886642020-05-26 18:11:04 +0200366 case CSD_TYPE_TTWU:
367 pr_warn("IPI task-wakeup sent to offline CPU\n");
368 break;
Linus Torvalds80538712015-02-11 12:42:10 -0800369
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200370 default:
371 pr_warn("IPI callback, unknown type %d, sent to offline CPU\n",
372 CSD_TYPE(csd));
373 break;
374 }
Linus Torvalds80538712015-02-11 12:42:10 -0800375 }
Jens Axboe3d442232008-06-26 11:21:34 +0200376 }
Frederic Weisbecker47885012014-05-08 01:37:48 +0200377
378 /*
Peter Zijlstra52103be2020-05-26 18:10:59 +0200379 * First; run all SYNC callbacks, people are waiting for us.
Frederic Weisbecker47885012014-05-08 01:37:48 +0200380 */
Peter Zijlstra52103be2020-05-26 18:10:59 +0200381 prev = NULL;
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200382 llist_for_each_entry_safe(csd, csd_next, entry, node.llist) {
Jens Axboe3d442232008-06-26 11:21:34 +0200383 /* Do we wait until *after* callback? */
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200384 if (CSD_TYPE(csd) == CSD_TYPE_SYNC) {
385 smp_call_func_t func = csd->func;
386 void *info = csd->info;
387
Peter Zijlstra52103be2020-05-26 18:10:59 +0200388 if (prev) {
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200389 prev->next = &csd_next->node.llist;
Peter Zijlstra52103be2020-05-26 18:10:59 +0200390 } else {
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200391 entry = &csd_next->node.llist;
Peter Zijlstra52103be2020-05-26 18:10:59 +0200392 }
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200393
Paul E. McKenney35feb602020-06-30 13:22:54 -0700394 csd_lock_record(csd);
Jens Axboe3d442232008-06-26 11:21:34 +0200395 func(info);
Jens Axboe3d442232008-06-26 11:21:34 +0200396 csd_unlock(csd);
Paul E. McKenney35feb602020-06-30 13:22:54 -0700397 csd_lock_record(NULL);
Jens Axboe3d442232008-06-26 11:21:34 +0200398 } else {
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200399 prev = &csd->node.llist;
Jens Axboe3d442232008-06-26 11:21:34 +0200400 }
401 }
Frederic Weisbecker47885012014-05-08 01:37:48 +0200402
Peter Zijlstraa14886642020-05-26 18:11:04 +0200403 if (!entry)
404 return;
405
Frederic Weisbecker47885012014-05-08 01:37:48 +0200406 /*
Peter Zijlstra52103be2020-05-26 18:10:59 +0200407 * Second; run all !SYNC callbacks.
408 */
Peter Zijlstraa14886642020-05-26 18:11:04 +0200409 prev = NULL;
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200410 llist_for_each_entry_safe(csd, csd_next, entry, node.llist) {
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200411 int type = CSD_TYPE(csd);
Peter Zijlstra52103be2020-05-26 18:10:59 +0200412
Peter Zijlstraa14886642020-05-26 18:11:04 +0200413 if (type != CSD_TYPE_TTWU) {
414 if (prev) {
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200415 prev->next = &csd_next->node.llist;
Peter Zijlstraa14886642020-05-26 18:11:04 +0200416 } else {
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200417 entry = &csd_next->node.llist;
Peter Zijlstraa14886642020-05-26 18:11:04 +0200418 }
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200419
Peter Zijlstraa14886642020-05-26 18:11:04 +0200420 if (type == CSD_TYPE_ASYNC) {
421 smp_call_func_t func = csd->func;
422 void *info = csd->info;
423
Paul E. McKenney35feb602020-06-30 13:22:54 -0700424 csd_lock_record(csd);
Peter Zijlstraa14886642020-05-26 18:11:04 +0200425 csd_unlock(csd);
426 func(info);
Paul E. McKenney35feb602020-06-30 13:22:54 -0700427 csd_lock_record(NULL);
Peter Zijlstraa14886642020-05-26 18:11:04 +0200428 } else if (type == CSD_TYPE_IRQ_WORK) {
429 irq_work_single(csd);
430 }
431
432 } else {
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200433 prev = &csd->node.llist;
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200434 }
Peter Zijlstra52103be2020-05-26 18:10:59 +0200435 }
Peter Zijlstraa14886642020-05-26 18:11:04 +0200436
437 /*
438 * Third; only CSD_TYPE_TTWU is left, issue those.
439 */
440 if (entry)
441 sched_ttwu_pending(entry);
Jens Axboe3d442232008-06-26 11:21:34 +0200442}
443
Peter Zijlstrab2a02fc2020-05-26 18:11:01 +0200444void flush_smp_call_function_from_idle(void)
445{
446 unsigned long flags;
447
448 if (llist_empty(this_cpu_ptr(&call_single_queue)))
449 return;
450
451 local_irq_save(flags);
452 flush_smp_call_function_queue(true);
Sebastian Andrzej Siewiorf9d34592021-01-23 21:10:25 +0100453 if (local_softirq_pending())
454 do_softirq();
455
Peter Zijlstrab2a02fc2020-05-26 18:11:01 +0200456 local_irq_restore(flags);
Jens Axboe3d442232008-06-26 11:21:34 +0200457}
458
459/*
460 * smp_call_function_single - Run a function on a specific CPU
461 * @func: The function to run. This must be fast and non-blocking.
462 * @info: An arbitrary pointer to pass to the function.
463 * @wait: If true, wait until function has completed on other CPUs.
464 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800465 * Returns 0 on success, else a negative status code.
Jens Axboe3d442232008-06-26 11:21:34 +0200466 */
David Howells3a5f65df2010-10-27 17:28:36 +0100467int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
Jens Axboe8691e5a2008-06-06 11:18:06 +0200468 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200469{
Ying Huang966a9672017-08-08 12:30:00 +0800470 call_single_data_t *csd;
471 call_single_data_t csd_stack = {
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200472 .node = { .u_flags = CSD_FLAG_LOCK | CSD_TYPE_SYNC, },
Ying Huang966a9672017-08-08 12:30:00 +0800473 };
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100474 int this_cpu;
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100475 int err;
Jens Axboe3d442232008-06-26 11:21:34 +0200476
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100477 /*
478 * prevent preemption and reschedule on another processor,
479 * as well as CPU removal
480 */
481 this_cpu = get_cpu();
482
Suresh Siddha269c8612009-08-19 18:05:35 -0700483 /*
484 * Can deadlock when called with interrupts disabled.
485 * We allow cpu's that are not yet online though, as no one else can
486 * send smp call function interrupt to this cpu and as such deadlocks
487 * can't happen.
488 */
489 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
490 && !oops_in_progress);
Jens Axboe3d442232008-06-26 11:21:34 +0200491
Peter Zijlstra19dbdcb2019-07-18 11:20:09 +0200492 /*
493 * When @wait we can deadlock when we interrupt between llist_add() and
494 * arch_send_call_function_ipi*(); when !@wait we can deadlock due to
495 * csd_lock() on because the interrupt context uses the same csd
496 * storage.
497 */
498 WARN_ON_ONCE(!in_task());
499
Linus Torvalds80538712015-02-11 12:42:10 -0800500 csd = &csd_stack;
501 if (!wait) {
502 csd = this_cpu_ptr(&csd_data);
503 csd_lock(csd);
504 }
505
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200506 csd->func = func;
507 csd->info = info;
Paul E. McKenney35feb602020-06-30 13:22:54 -0700508#ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200509 csd->node.src = smp_processor_id();
510 csd->node.dst = cpu;
Paul E. McKenneye48c15b2020-06-29 17:21:32 -0700511#endif
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200512
513 err = generic_exec_single(cpu, csd);
Linus Torvalds80538712015-02-11 12:42:10 -0800514
515 if (wait)
516 csd_lock_wait(csd);
Jens Axboe3d442232008-06-26 11:21:34 +0200517
518 put_cpu();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100519
H. Peter Anvinf73be6de2008-08-25 17:07:14 -0700520 return err;
Jens Axboe3d442232008-06-26 11:21:34 +0200521}
522EXPORT_SYMBOL(smp_call_function_single);
523
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100524/**
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100525 * smp_call_function_single_async(): Run an asynchronous function on a
526 * specific CPU.
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100527 * @cpu: The CPU to run on.
528 * @csd: Pre-allocated and setup data structure
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100529 *
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100530 * Like smp_call_function_single(), but the call is asynchonous and
531 * can thus be done from contexts with disabled interrupts.
532 *
533 * The caller passes his own pre-allocated data structure
534 * (ie: embedded in an object) and is responsible for synchronizing it
535 * such that the IPIs performed on the @csd are strictly serialized.
536 *
Peter Xu5a18cec2019-12-16 16:31:23 -0500537 * If the function is called with one csd which has not yet been
538 * processed by previous call to smp_call_function_single_async(), the
539 * function will return immediately with -EBUSY showing that the csd
540 * object is still in progress.
541 *
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100542 * NOTE: Be careful, there is unfortunately no current debugging facility to
543 * validate the correctness of this serialization.
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100544 */
Ying Huang966a9672017-08-08 12:30:00 +0800545int smp_call_function_single_async(int cpu, call_single_data_t *csd)
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100546{
547 int err = 0;
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100548
Frederic Weisbeckerfce8ad12014-02-24 16:40:01 +0100549 preempt_disable();
Linus Torvalds80538712015-02-11 12:42:10 -0800550
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200551 if (csd->node.u_flags & CSD_FLAG_LOCK) {
Peter Xu5a18cec2019-12-16 16:31:23 -0500552 err = -EBUSY;
553 goto out;
554 }
Linus Torvalds80538712015-02-11 12:42:10 -0800555
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200556 csd->node.u_flags = CSD_FLAG_LOCK;
Linus Torvalds80538712015-02-11 12:42:10 -0800557 smp_wmb();
558
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200559 err = generic_exec_single(cpu, csd);
Peter Xu5a18cec2019-12-16 16:31:23 -0500560
561out:
Frederic Weisbeckerfce8ad12014-02-24 16:40:01 +0100562 preempt_enable();
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100563
564 return err;
565}
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100566EXPORT_SYMBOL_GPL(smp_call_function_single_async);
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100567
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800568/*
569 * smp_call_function_any - Run a function on any of the given cpus
570 * @mask: The mask of cpus it can run on.
571 * @func: The function to run. This must be fast and non-blocking.
572 * @info: An arbitrary pointer to pass to the function.
573 * @wait: If true, wait until function has completed.
574 *
575 * Returns 0 on success, else a negative status code (if no cpus were online).
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800576 *
577 * Selection preference:
578 * 1) current cpu if in @mask
579 * 2) any cpu of current node if in @mask
580 * 3) any other online cpu in @mask
581 */
582int smp_call_function_any(const struct cpumask *mask,
David Howells3a5f65df2010-10-27 17:28:36 +0100583 smp_call_func_t func, void *info, int wait)
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800584{
585 unsigned int cpu;
586 const struct cpumask *nodemask;
587 int ret;
588
589 /* Try for same CPU (cheapest) */
590 cpu = get_cpu();
591 if (cpumask_test_cpu(cpu, mask))
592 goto call;
593
594 /* Try for same node. */
David Johnaf2422c2010-01-15 17:01:23 -0800595 nodemask = cpumask_of_node(cpu_to_node(cpu));
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800596 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
597 cpu = cpumask_next_and(cpu, nodemask, mask)) {
598 if (cpu_online(cpu))
599 goto call;
600 }
601
602 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
603 cpu = cpumask_any_and(mask, cpu_online_mask);
604call:
605 ret = smp_call_function_single(cpu, func, info, wait);
606 put_cpu();
607 return ret;
608}
609EXPORT_SYMBOL_GPL(smp_call_function_any);
610
Nadav Amita32a4d82021-02-20 15:17:04 -0800611/*
612 * Flags to be used as scf_flags argument of smp_call_function_many_cond().
613 *
614 * %SCF_WAIT: Wait until function execution is completed
615 * %SCF_RUN_LOCAL: Run also locally if local cpu is set in cpumask
616 */
617#define SCF_WAIT (1U << 0)
618#define SCF_RUN_LOCAL (1U << 1)
619
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100620static void smp_call_function_many_cond(const struct cpumask *mask,
621 smp_call_func_t func, void *info,
Nadav Amita32a4d82021-02-20 15:17:04 -0800622 unsigned int scf_flags,
623 smp_cond_func_t cond_func)
Jens Axboe3d442232008-06-26 11:21:34 +0200624{
Nadav Amita32a4d82021-02-20 15:17:04 -0800625 int cpu, last_cpu, this_cpu = smp_processor_id();
Andrew Mortone1d12f32013-04-30 15:27:28 -0700626 struct call_function_data *cfd;
Nadav Amita32a4d82021-02-20 15:17:04 -0800627 bool wait = scf_flags & SCF_WAIT;
628 bool run_remote = false;
629 bool run_local = false;
630 int nr_cpus = 0;
631
632 lockdep_assert_preemption_disabled();
Jens Axboe3d442232008-06-26 11:21:34 +0200633
Suresh Siddha269c8612009-08-19 18:05:35 -0700634 /*
635 * Can deadlock when called with interrupts disabled.
636 * We allow cpu's that are not yet online though, as no one else can
637 * send smp call function interrupt to this cpu and as such deadlocks
638 * can't happen.
639 */
Nadav Amita32a4d82021-02-20 15:17:04 -0800640 if (cpu_online(this_cpu) && !oops_in_progress &&
641 !early_boot_irqs_disabled)
642 lockdep_assert_irqs_enabled();
Jens Axboe3d442232008-06-26 11:21:34 +0200643
Peter Zijlstra19dbdcb2019-07-18 11:20:09 +0200644 /*
645 * When @wait we can deadlock when we interrupt between llist_add() and
646 * arch_send_call_function_ipi*(); when !@wait we can deadlock due to
647 * csd_lock() on because the interrupt context uses the same csd
648 * storage.
649 */
650 WARN_ON_ONCE(!in_task());
651
Nadav Amita32a4d82021-02-20 15:17:04 -0800652 /* Check if we need local execution. */
653 if ((scf_flags & SCF_RUN_LOCAL) && cpumask_test_cpu(this_cpu, mask))
654 run_local = true;
655
656 /* Check if we need remote execution, i.e., any CPU excluding this one. */
Rusty Russell54b11e62008-12-30 09:05:16 +1030657 cpu = cpumask_first_and(mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100658 if (cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030659 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Nadav Amita32a4d82021-02-20 15:17:04 -0800660 if (cpu < nr_cpu_ids)
661 run_remote = true;
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100662
Nadav Amita32a4d82021-02-20 15:17:04 -0800663 if (run_remote) {
664 cfd = this_cpu_ptr(&cfd_data);
665 cpumask_and(cfd->cpumask, mask, cpu_online_mask);
666 __cpumask_clear_cpu(this_cpu, cfd->cpumask);
Jens Axboe3d442232008-06-26 11:21:34 +0200667
Nadav Amita32a4d82021-02-20 15:17:04 -0800668 cpumask_clear(cfd->cpumask_ipi);
669 for_each_cpu(cpu, cfd->cpumask) {
670 call_single_data_t *csd = per_cpu_ptr(cfd->csd, cpu);
Rusty Russell54b11e62008-12-30 09:05:16 +1030671
Nadav Amita32a4d82021-02-20 15:17:04 -0800672 if (cond_func && !cond_func(cpu, info))
673 continue;
Jens Axboe3d442232008-06-26 11:21:34 +0200674
Nadav Amita32a4d82021-02-20 15:17:04 -0800675 csd_lock(csd);
676 if (wait)
677 csd->node.u_flags |= CSD_TYPE_SYNC;
678 csd->func = func;
679 csd->info = info;
Paul E. McKenney35feb602020-06-30 13:22:54 -0700680#ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
Nadav Amita32a4d82021-02-20 15:17:04 -0800681 csd->node.src = smp_processor_id();
682 csd->node.dst = cpu;
Paul E. McKenneye48c15b2020-06-29 17:21:32 -0700683#endif
Nadav Amita32a4d82021-02-20 15:17:04 -0800684 if (llist_add(&csd->node.llist, &per_cpu(call_single_queue, cpu))) {
685 __cpumask_set_cpu(cpu, cfd->cpumask_ipi);
686 nr_cpus++;
687 last_cpu = cpu;
688 }
689 }
690
691 /*
692 * Choose the most efficient way to send an IPI. Note that the
693 * number of CPUs might be zero due to concurrent changes to the
694 * provided mask.
695 */
696 if (nr_cpus == 1)
Peter Zijlstrad43f17a2021-03-02 08:02:43 +0100697 send_call_function_single_ipi(last_cpu);
Nadav Amita32a4d82021-02-20 15:17:04 -0800698 else if (likely(nr_cpus > 1))
699 arch_send_call_function_ipi_mask(cfd->cpumask_ipi);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800700 }
Suresh Siddha561920a02008-10-30 18:28:41 +0100701
Nadav Amita32a4d82021-02-20 15:17:04 -0800702 if (run_local && (!cond_func || cond_func(this_cpu, info))) {
703 unsigned long flags;
Jens Axboe3d442232008-06-26 11:21:34 +0200704
Nadav Amita32a4d82021-02-20 15:17:04 -0800705 local_irq_save(flags);
706 func(info);
707 local_irq_restore(flags);
708 }
709
710 if (run_remote && wait) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700711 for_each_cpu(cpu, cfd->cpumask) {
Ying Huang966a9672017-08-08 12:30:00 +0800712 call_single_data_t *csd;
Andrew Mortone1d12f32013-04-30 15:27:28 -0700713
714 csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800715 csd_lock_wait(csd);
716 }
717 }
Jens Axboe3d442232008-06-26 11:21:34 +0200718}
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100719
720/**
Nadav Amita32a4d82021-02-20 15:17:04 -0800721 * smp_call_function_many(): Run a function on a set of CPUs.
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100722 * @mask: The set of cpus to run on (only runs on online subset).
723 * @func: The function to run. This must be fast and non-blocking.
724 * @info: An arbitrary pointer to pass to the function.
Nadav Amita32a4d82021-02-20 15:17:04 -0800725 * @flags: Bitmask that controls the operation. If %SCF_WAIT is set, wait
726 * (atomically) until function has completed on other CPUs. If
727 * %SCF_RUN_LOCAL is set, the function will also be run locally
728 * if the local CPU is set in the @cpumask.
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100729 *
730 * If @wait is true, then returns once @func has returned.
731 *
732 * You must not call this function with disabled interrupts or from a
733 * hardware interrupt handler or from a bottom half handler. Preemption
734 * must be disabled when calling this function.
735 */
736void smp_call_function_many(const struct cpumask *mask,
737 smp_call_func_t func, void *info, bool wait)
738{
Nadav Amita32a4d82021-02-20 15:17:04 -0800739 smp_call_function_many_cond(mask, func, info, wait * SCF_WAIT, NULL);
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100740}
Rusty Russell54b11e62008-12-30 09:05:16 +1030741EXPORT_SYMBOL(smp_call_function_many);
Jens Axboe3d442232008-06-26 11:21:34 +0200742
743/**
744 * smp_call_function(): Run a function on all other CPUs.
745 * @func: The function to run. This must be fast and non-blocking.
746 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100747 * @wait: If true, wait (atomically) until function has completed
748 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200749 *
Rusty Russell54b11e62008-12-30 09:05:16 +1030750 * Returns 0.
Jens Axboe3d442232008-06-26 11:21:34 +0200751 *
752 * If @wait is true, then returns once @func has returned; otherwise
Sheng Yang72f279b2009-10-22 19:19:34 +0800753 * it returns just before the target cpu calls @func.
Jens Axboe3d442232008-06-26 11:21:34 +0200754 *
755 * You must not call this function with disabled interrupts or from a
756 * hardware interrupt handler or from a bottom half handler.
757 */
Nadav Amitcaa75932019-06-12 23:48:05 -0700758void smp_call_function(smp_call_func_t func, void *info, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200759{
Jens Axboe3d442232008-06-26 11:21:34 +0200760 preempt_disable();
Rusty Russell54b11e62008-12-30 09:05:16 +1030761 smp_call_function_many(cpu_online_mask, func, info, wait);
Jens Axboe3d442232008-06-26 11:21:34 +0200762 preempt_enable();
Jens Axboe3d442232008-06-26 11:21:34 +0200763}
764EXPORT_SYMBOL(smp_call_function);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800765
Amerigo Wang34db18a02011-03-22 16:34:06 -0700766/* Setup configured maximum number of CPUs to activate */
767unsigned int setup_max_cpus = NR_CPUS;
768EXPORT_SYMBOL(setup_max_cpus);
769
770
771/*
772 * Setup routine for controlling SMP activation
773 *
774 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
775 * activation entirely (the MPS table probe still happens, though).
776 *
777 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
778 * greater than 0, limits the maximum number of CPUs activated in
779 * SMP mode to <NUM>.
780 */
781
782void __weak arch_disable_smp_support(void) { }
783
784static int __init nosmp(char *str)
785{
786 setup_max_cpus = 0;
787 arch_disable_smp_support();
788
789 return 0;
790}
791
792early_param("nosmp", nosmp);
793
794/* this is hard limit */
795static int __init nrcpus(char *str)
796{
797 int nr_cpus;
798
Muchun Song58934352020-07-16 15:04:57 +0800799 if (get_option(&str, &nr_cpus) && nr_cpus > 0 && nr_cpus < nr_cpu_ids)
Amerigo Wang34db18a02011-03-22 16:34:06 -0700800 nr_cpu_ids = nr_cpus;
801
802 return 0;
803}
804
805early_param("nr_cpus", nrcpus);
806
807static int __init maxcpus(char *str)
808{
809 get_option(&str, &setup_max_cpus);
810 if (setup_max_cpus == 0)
811 arch_disable_smp_support();
812
813 return 0;
814}
815
816early_param("maxcpus", maxcpus);
817
818/* Setup number of possible processor ids */
Alexey Dobriyan9b130ad2017-09-08 16:14:18 -0700819unsigned int nr_cpu_ids __read_mostly = NR_CPUS;
Amerigo Wang34db18a02011-03-22 16:34:06 -0700820EXPORT_SYMBOL(nr_cpu_ids);
821
822/* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
823void __init setup_nr_cpu_ids(void)
824{
825 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
826}
827
828/* Called by boot processor to activate the rest. */
829void __init smp_init(void)
830{
Michael Ellerman92b23272016-10-26 16:37:54 +1100831 int num_nodes, num_cpus;
Amerigo Wang34db18a02011-03-22 16:34:06 -0700832
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700833 idle_threads_init();
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000834 cpuhp_threads_init();
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700835
Michael Ellerman51111dc2016-10-26 16:37:55 +1100836 pr_info("Bringing up secondary CPUs ...\n");
837
Qais Yousefb99a2652020-03-23 13:51:09 +0000838 bringup_nonboot_cpus(setup_max_cpus);
Amerigo Wang34db18a02011-03-22 16:34:06 -0700839
Michael Ellerman92b23272016-10-26 16:37:54 +1100840 num_nodes = num_online_nodes();
841 num_cpus = num_online_cpus();
842 pr_info("Brought up %d node%s, %d CPU%s\n",
843 num_nodes, (num_nodes > 1 ? "s" : ""),
844 num_cpus, (num_cpus > 1 ? "s" : ""));
845
Amerigo Wang34db18a02011-03-22 16:34:06 -0700846 /* Any cleanup work */
Amerigo Wang34db18a02011-03-22 16:34:06 -0700847 smp_cpus_done(setup_max_cpus);
848}
849
Amerigo Wang351f8f82011-01-12 16:59:39 -0800850/*
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700851 * on_each_cpu_cond(): Call a function on each processor for which
852 * the supplied function cond_func returns true, optionally waiting
853 * for all the required CPUs to finish. This may include the local
854 * processor.
855 * @cond_func: A callback function that is passed a cpu id and
Randy Dunlap7b7b8a22020-10-15 20:10:28 -0700856 * the info parameter. The function is called
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700857 * with preemption disabled. The function should
858 * return a blooean value indicating whether to IPI
859 * the specified CPU.
860 * @func: The function to run on all applicable CPUs.
861 * This must be fast and non-blocking.
862 * @info: An arbitrary pointer to pass to both functions.
863 * @wait: If true, wait (atomically) until function has
864 * completed on other CPUs.
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700865 *
866 * Preemption is disabled to protect against CPUs going offline but not online.
867 * CPUs going online during the call will not be seen or sent an IPI.
868 *
869 * You must not call this function with disabled interrupts or
870 * from a hardware interrupt handler or from a bottom half handler.
871 */
Sebastian Andrzej Siewior5671d812020-01-17 10:01:35 +0100872void on_each_cpu_cond_mask(smp_cond_func_t cond_func, smp_call_func_t func,
Sebastian Andrzej Siewiorcb923152020-01-17 10:01:37 +0100873 void *info, bool wait, const struct cpumask *mask)
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700874{
Nadav Amita32a4d82021-02-20 15:17:04 -0800875 unsigned int scf_flags = SCF_RUN_LOCAL;
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700876
Nadav Amita32a4d82021-02-20 15:17:04 -0800877 if (wait)
878 scf_flags |= SCF_WAIT;
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700879
Nadav Amita32a4d82021-02-20 15:17:04 -0800880 preempt_disable();
881 smp_call_function_many_cond(mask, func, info, scf_flags, cond_func);
882 preempt_enable();
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700883}
Rik van Riel7d49b282018-09-25 23:58:41 -0400884EXPORT_SYMBOL(on_each_cpu_cond_mask);
885
Thomas Gleixnerf37f4352012-05-07 17:59:48 +0000886static void do_nothing(void *unused)
887{
888}
889
890/**
891 * kick_all_cpus_sync - Force all cpus out of idle
892 *
893 * Used to synchronize the update of pm_idle function pointer. It's
894 * called after the pointer is updated and returns after the dummy
895 * callback function has been executed on all cpus. The execution of
896 * the function can only happen on the remote cpus after they have
897 * left the idle function which had been called via pm_idle function
898 * pointer. So it's guaranteed that nothing uses the previous pointer
899 * anymore.
900 */
901void kick_all_cpus_sync(void)
902{
903 /* Make sure the change is visible before we kick the cpus */
904 smp_mb();
905 smp_call_function(do_nothing, NULL, 1);
906}
907EXPORT_SYMBOL_GPL(kick_all_cpus_sync);
Chuansheng Liuc6f44592014-09-04 15:17:54 +0800908
909/**
910 * wake_up_all_idle_cpus - break all cpus out of idle
911 * wake_up_all_idle_cpus try to break all cpus which is in idle state even
912 * including idle polling cpus, for non-idle cpus, we will do nothing
913 * for them.
914 */
915void wake_up_all_idle_cpus(void)
916{
917 int cpu;
918
919 preempt_disable();
920 for_each_online_cpu(cpu) {
921 if (cpu == smp_processor_id())
922 continue;
923
924 wake_up_if_idle(cpu);
925 }
926 preempt_enable();
927}
928EXPORT_SYMBOL_GPL(wake_up_all_idle_cpus);
Juergen Grossdf8ce9d2016-08-29 08:48:44 +0200929
930/**
931 * smp_call_on_cpu - Call a function on a specific cpu
932 *
933 * Used to call a function on a specific cpu and wait for it to return.
934 * Optionally make sure the call is done on a specified physical cpu via vcpu
935 * pinning in order to support virtualized environments.
936 */
937struct smp_call_on_cpu_struct {
938 struct work_struct work;
939 struct completion done;
940 int (*func)(void *);
941 void *data;
942 int ret;
943 int cpu;
944};
945
946static void smp_call_on_cpu_callback(struct work_struct *work)
947{
948 struct smp_call_on_cpu_struct *sscs;
949
950 sscs = container_of(work, struct smp_call_on_cpu_struct, work);
951 if (sscs->cpu >= 0)
952 hypervisor_pin_vcpu(sscs->cpu);
953 sscs->ret = sscs->func(sscs->data);
954 if (sscs->cpu >= 0)
955 hypervisor_pin_vcpu(-1);
956
957 complete(&sscs->done);
958}
959
960int smp_call_on_cpu(unsigned int cpu, int (*func)(void *), void *par, bool phys)
961{
962 struct smp_call_on_cpu_struct sscs = {
Juergen Grossdf8ce9d2016-08-29 08:48:44 +0200963 .done = COMPLETION_INITIALIZER_ONSTACK(sscs.done),
964 .func = func,
965 .data = par,
966 .cpu = phys ? cpu : -1,
967 };
968
Peter Zijlstra8db54942016-09-11 10:36:26 +0200969 INIT_WORK_ONSTACK(&sscs.work, smp_call_on_cpu_callback);
970
Juergen Grossdf8ce9d2016-08-29 08:48:44 +0200971 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
972 return -ENXIO;
973
974 queue_work_on(cpu, system_wq, &sscs.work);
975 wait_for_completion(&sscs.done);
976
977 return sscs.ret;
978}
979EXPORT_SYMBOL_GPL(smp_call_on_cpu);