blob: faf1a3ace6a9984a0f53a72e13892b3ac0b51e75 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/gfp.h>
Jens Axboe3d442232008-06-26 11:21:34 +020018#include <linux/smp.h>
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010019#include <linux/cpu.h>
Chuansheng Liuc6f44592014-09-04 15:17:54 +080020#include <linux/sched.h>
Ingo Molnar4c822692017-02-01 16:36:40 +010021#include <linux/sched/idle.h>
Juergen Gross47ae4b02016-08-29 08:48:43 +020022#include <linux/hypervisor.h>
Paul E. McKenney35feb602020-06-30 13:22:54 -070023#include <linux/sched/clock.h>
24#include <linux/nmi.h>
25#include <linux/sched/debug.h>
Jens Axboe3d442232008-06-26 11:21:34 +020026
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070027#include "smpboot.h"
Ingo Molnar1f8db412020-05-28 11:01:34 +020028#include "sched/smp.h"
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070029
Peter Zijlstra545b8c82020-06-15 11:29:31 +020030#define CSD_TYPE(_csd) ((_csd)->node.u_flags & CSD_FLAG_TYPE_MASK)
Jens Axboe3d442232008-06-26 11:21:34 +020031
32struct call_function_data {
Ying Huang966a9672017-08-08 12:30:00 +080033 call_single_data_t __percpu *csd;
Ingo Molnar0b13fda2009-02-25 16:52:11 +010034 cpumask_var_t cpumask;
Aaron Lu3fc5b3b2017-05-19 15:53:31 +080035 cpumask_var_t cpumask_ipi;
Jens Axboe3d442232008-06-26 11:21:34 +020036};
37
Nadav Amita22793c2019-06-12 23:48:11 -070038static DEFINE_PER_CPU_ALIGNED(struct call_function_data, cfd_data);
Milton Millere03bcb62010-01-18 13:00:51 +110039
Christoph Hellwig6897fc22014-01-30 15:45:47 -080040static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010041
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -070042static void flush_smp_call_function_queue(bool warn_cpu_offline);
43
Richard Weinberger31487f82016-07-13 17:17:01 +000044int smpcfd_prepare_cpu(unsigned int cpu)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010045{
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010046 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
47
Richard Weinberger31487f82016-07-13 17:17:01 +000048 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
49 cpu_to_node(cpu)))
50 return -ENOMEM;
Aaron Lu3fc5b3b2017-05-19 15:53:31 +080051 if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL,
52 cpu_to_node(cpu))) {
53 free_cpumask_var(cfd->cpumask);
54 return -ENOMEM;
55 }
Ying Huang966a9672017-08-08 12:30:00 +080056 cfd->csd = alloc_percpu(call_single_data_t);
Richard Weinberger31487f82016-07-13 17:17:01 +000057 if (!cfd->csd) {
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010058 free_cpumask_var(cfd->cpumask);
Aaron Lu3fc5b3b2017-05-19 15:53:31 +080059 free_cpumask_var(cfd->cpumask_ipi);
Richard Weinberger31487f82016-07-13 17:17:01 +000060 return -ENOMEM;
61 }
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -070062
Richard Weinberger31487f82016-07-13 17:17:01 +000063 return 0;
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010064}
65
Richard Weinberger31487f82016-07-13 17:17:01 +000066int smpcfd_dead_cpu(unsigned int cpu)
67{
68 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
69
70 free_cpumask_var(cfd->cpumask);
Aaron Lu3fc5b3b2017-05-19 15:53:31 +080071 free_cpumask_var(cfd->cpumask_ipi);
Richard Weinberger31487f82016-07-13 17:17:01 +000072 free_percpu(cfd->csd);
73 return 0;
74}
75
76int smpcfd_dying_cpu(unsigned int cpu)
77{
78 /*
79 * The IPIs for the smp-call-function callbacks queued by other
80 * CPUs might arrive late, either due to hardware latencies or
81 * because this CPU disabled interrupts (inside stop-machine)
82 * before the IPIs were sent. So flush out any pending callbacks
83 * explicitly (without waiting for the IPIs to arrive), to
84 * ensure that the outgoing CPU doesn't go offline with work
85 * still pending.
86 */
87 flush_smp_call_function_queue(false);
Peter Zijlstraafaa6532020-05-26 18:11:00 +020088 irq_work_run();
Richard Weinberger31487f82016-07-13 17:17:01 +000089 return 0;
90}
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010091
Takao Indohd8ad7d12011-03-29 12:35:04 -040092void __init call_function_init(void)
Jens Axboe3d442232008-06-26 11:21:34 +020093{
94 int i;
95
Christoph Hellwig6897fc22014-01-30 15:45:47 -080096 for_each_possible_cpu(i)
97 init_llist_head(&per_cpu(call_single_queue, i));
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010098
Richard Weinberger31487f82016-07-13 17:17:01 +000099 smpcfd_prepare_cpu(smp_processor_id());
Jens Axboe3d442232008-06-26 11:21:34 +0200100}
101
Paul E. McKenney35feb602020-06-30 13:22:54 -0700102#ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
103
104static DEFINE_PER_CPU(call_single_data_t *, cur_csd);
105static DEFINE_PER_CPU(smp_call_func_t, cur_csd_func);
106static DEFINE_PER_CPU(void *, cur_csd_info);
107
108#define CSD_LOCK_TIMEOUT (5ULL * NSEC_PER_SEC)
Wei Yongjun2b722162020-07-06 21:49:41 +0800109static atomic_t csd_bug_count = ATOMIC_INIT(0);
Paul E. McKenney35feb602020-06-30 13:22:54 -0700110
111/* Record current CSD work for current CPU, NULL to erase. */
112static void csd_lock_record(call_single_data_t *csd)
113{
114 if (!csd) {
115 smp_mb(); /* NULL cur_csd after unlock. */
116 __this_cpu_write(cur_csd, NULL);
117 return;
118 }
119 __this_cpu_write(cur_csd_func, csd->func);
120 __this_cpu_write(cur_csd_info, csd->info);
121 smp_wmb(); /* func and info before csd. */
122 __this_cpu_write(cur_csd, csd);
123 smp_mb(); /* Update cur_csd before function call. */
124 /* Or before unlock, as the case may be. */
125}
126
127static __always_inline int csd_lock_wait_getcpu(call_single_data_t *csd)
128{
129 unsigned int csd_type;
130
131 csd_type = CSD_TYPE(csd);
132 if (csd_type == CSD_TYPE_ASYNC || csd_type == CSD_TYPE_SYNC)
133 return csd->dst; /* Other CSD_TYPE_ values might not have ->dst. */
134 return -1;
135}
136
137/*
138 * Complain if too much time spent waiting. Note that only
139 * the CSD_TYPE_SYNC/ASYNC types provide the destination CPU,
140 * so waiting on other types gets much less information.
141 */
142static __always_inline bool csd_lock_wait_toolong(call_single_data_t *csd, u64 ts0, u64 *ts1, int *bug_id)
143{
144 int cpu = -1;
145 int cpux;
146 bool firsttime;
147 u64 ts2, ts_delta;
148 call_single_data_t *cpu_cur_csd;
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200149 unsigned int flags = READ_ONCE(csd->node.u_flags);
Paul E. McKenney35feb602020-06-30 13:22:54 -0700150
151 if (!(flags & CSD_FLAG_LOCK)) {
152 if (!unlikely(*bug_id))
153 return true;
154 cpu = csd_lock_wait_getcpu(csd);
155 pr_alert("csd: CSD lock (#%d) got unstuck on CPU#%02d, CPU#%02d released the lock.\n",
156 *bug_id, raw_smp_processor_id(), cpu);
157 return true;
158 }
159
160 ts2 = sched_clock();
161 ts_delta = ts2 - *ts1;
162 if (likely(ts_delta <= CSD_LOCK_TIMEOUT))
163 return false;
164
165 firsttime = !*bug_id;
166 if (firsttime)
167 *bug_id = atomic_inc_return(&csd_bug_count);
168 cpu = csd_lock_wait_getcpu(csd);
169 if (WARN_ONCE(cpu < 0 || cpu >= nr_cpu_ids, "%s: cpu = %d\n", __func__, cpu))
170 cpux = 0;
171 else
172 cpux = cpu;
173 cpu_cur_csd = smp_load_acquire(&per_cpu(cur_csd, cpux)); /* Before func and info. */
174 pr_alert("csd: %s non-responsive CSD lock (#%d) on CPU#%d, waiting %llu ns for CPU#%02d %pS(%ps).\n",
175 firsttime ? "Detected" : "Continued", *bug_id, raw_smp_processor_id(), ts2 - ts0,
176 cpu, csd->func, csd->info);
177 if (cpu_cur_csd && csd != cpu_cur_csd) {
178 pr_alert("\tcsd: CSD lock (#%d) handling prior %pS(%ps) request.\n",
179 *bug_id, READ_ONCE(per_cpu(cur_csd_func, cpux)),
180 READ_ONCE(per_cpu(cur_csd_info, cpux)));
181 } else {
182 pr_alert("\tcsd: CSD lock (#%d) %s.\n",
183 *bug_id, !cpu_cur_csd ? "unresponsive" : "handling this request");
184 }
185 if (cpu >= 0) {
186 if (!trigger_single_cpu_backtrace(cpu))
187 dump_cpu_task(cpu);
188 if (!cpu_cur_csd) {
189 pr_alert("csd: Re-sending CSD lock (#%d) IPI from CPU#%02d to CPU#%02d\n", *bug_id, raw_smp_processor_id(), cpu);
190 arch_send_call_function_single_ipi(cpu);
191 }
192 }
193 dump_stack();
194 *ts1 = ts2;
195
196 return false;
197}
198
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100199/*
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100200 * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
201 *
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100202 * For non-synchronous ipi calls the csd can still be in use by the
203 * previous function call. For multi-cpu calls its even more interesting
204 * as we'll have to ensure no other cpu is observing our csd.
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100205 */
Ying Huang966a9672017-08-08 12:30:00 +0800206static __always_inline void csd_lock_wait(call_single_data_t *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100207{
Paul E. McKenney35feb602020-06-30 13:22:54 -0700208 int bug_id = 0;
209 u64 ts0, ts1;
210
211 ts1 = ts0 = sched_clock();
212 for (;;) {
213 if (csd_lock_wait_toolong(csd, ts0, &ts1, &bug_id))
214 break;
215 cpu_relax();
216 }
217 smp_acquire__after_ctrl_dep();
218}
219
220#else
221static void csd_lock_record(call_single_data_t *csd)
222{
223}
224
225static __always_inline void csd_lock_wait(call_single_data_t *csd)
226{
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200227 smp_cond_load_acquire(&csd->node.u_flags, !(VAL & CSD_FLAG_LOCK));
Peter Zijlstra6e275632009-02-25 13:59:48 +0100228}
Paul E. McKenney35feb602020-06-30 13:22:54 -0700229#endif
Peter Zijlstra6e275632009-02-25 13:59:48 +0100230
Ying Huang966a9672017-08-08 12:30:00 +0800231static __always_inline void csd_lock(call_single_data_t *csd)
Peter Zijlstra6e275632009-02-25 13:59:48 +0100232{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700233 csd_lock_wait(csd);
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200234 csd->node.u_flags |= CSD_FLAG_LOCK;
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100235
236 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100237 * prevent CPU from reordering the above assignment
238 * to ->flags with any subsequent assignments to other
Ying Huang966a9672017-08-08 12:30:00 +0800239 * fields of the specified call_single_data_t structure:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100240 */
Linus Torvalds80538712015-02-11 12:42:10 -0800241 smp_wmb();
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100242}
243
Ying Huang966a9672017-08-08 12:30:00 +0800244static __always_inline void csd_unlock(call_single_data_t *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100245{
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200246 WARN_ON(!(csd->node.u_flags & CSD_FLAG_LOCK));
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100247
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100248 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100249 * ensure we're all done before releasing data:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100250 */
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200251 smp_store_release(&csd->node.u_flags, 0);
Jens Axboe3d442232008-06-26 11:21:34 +0200252}
253
Ying Huang966a9672017-08-08 12:30:00 +0800254static DEFINE_PER_CPU_SHARED_ALIGNED(call_single_data_t, csd_data);
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100255
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200256void __smp_call_single_queue(int cpu, struct llist_node *node)
257{
258 /*
259 * The list addition should be visible before sending the IPI
260 * handler locks the list to pull the entry off it because of
261 * normal cache coherency rules implied by spinlocks.
262 *
263 * If IPIs can go out of order to the cache coherency protocol
264 * in an architecture, sufficient synchronisation should be added
265 * to arch code to make it appear to obey cache coherency WRT
266 * locking and barrier primitives. Generic code isn't really
267 * equipped to do the right thing...
268 */
269 if (llist_add(node, &per_cpu(call_single_queue, cpu)))
270 send_call_function_single_ipi(cpu);
271}
272
Jens Axboe3d442232008-06-26 11:21:34 +0200273/*
Ying Huang966a9672017-08-08 12:30:00 +0800274 * Insert a previously allocated call_single_data_t element
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100275 * for execution on the given CPU. data must already have
276 * ->func, ->info, and ->flags set.
Jens Axboe3d442232008-06-26 11:21:34 +0200277 */
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200278static int generic_exec_single(int cpu, call_single_data_t *csd)
Jens Axboe3d442232008-06-26 11:21:34 +0200279{
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100280 if (cpu == smp_processor_id()) {
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200281 smp_call_func_t func = csd->func;
282 void *info = csd->info;
Linus Torvalds80538712015-02-11 12:42:10 -0800283 unsigned long flags;
284
285 /*
286 * We can unlock early even for the synchronous on-stack case,
287 * since we're doing this from the same CPU..
288 */
Paul E. McKenney35feb602020-06-30 13:22:54 -0700289 csd_lock_record(csd);
Linus Torvalds80538712015-02-11 12:42:10 -0800290 csd_unlock(csd);
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100291 local_irq_save(flags);
292 func(info);
Paul E. McKenney35feb602020-06-30 13:22:54 -0700293 csd_lock_record(NULL);
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100294 local_irq_restore(flags);
295 return 0;
296 }
297
Linus Torvalds5224b962015-04-19 04:56:03 -0400298 if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu)) {
299 csd_unlock(csd);
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100300 return -ENXIO;
Linus Torvalds5224b962015-04-19 04:56:03 -0400301 }
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100302
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200303 __smp_call_single_queue(cpu, &csd->node.llist);
Jens Axboe3d442232008-06-26 11:21:34 +0200304
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100305 return 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200306}
307
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700308/**
309 * generic_smp_call_function_single_interrupt - Execute SMP IPI callbacks
310 *
311 * Invoked by arch to handle an IPI for call function single.
312 * Must be called with interrupts disabled.
Jens Axboe3d442232008-06-26 11:21:34 +0200313 */
314void generic_smp_call_function_single_interrupt(void)
315{
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700316 flush_smp_call_function_queue(true);
317}
318
319/**
320 * flush_smp_call_function_queue - Flush pending smp-call-function callbacks
321 *
322 * @warn_cpu_offline: If set to 'true', warn if callbacks were queued on an
323 * offline CPU. Skip this check if set to 'false'.
324 *
325 * Flush any pending smp-call-function callbacks queued on this CPU. This is
326 * invoked by the generic IPI handler, as well as by a CPU about to go offline,
327 * to ensure that all pending IPI callbacks are run before it goes completely
328 * offline.
329 *
330 * Loop through the call_single_queue and run all the queued callbacks.
331 * Must be called with interrupts disabled.
332 */
333static void flush_smp_call_function_queue(bool warn_cpu_offline)
334{
Ying Huang966a9672017-08-08 12:30:00 +0800335 call_single_data_t *csd, *csd_next;
Peter Zijlstra52103be2020-05-26 18:10:59 +0200336 struct llist_node *entry, *prev;
337 struct llist_head *head;
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700338 static bool warned;
339
Frederic Weisbecker83efcbd2017-11-06 16:01:22 +0100340 lockdep_assert_irqs_disabled();
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700341
Christoph Lameterbb964a92014-08-17 12:30:24 -0500342 head = this_cpu_ptr(&call_single_queue);
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700343 entry = llist_del_all(head);
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700344 entry = llist_reverse_order(entry);
Jens Axboe3d442232008-06-26 11:21:34 +0200345
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700346 /* There shouldn't be any pending callbacks on an offline CPU. */
347 if (unlikely(warn_cpu_offline && !cpu_online(smp_processor_id()) &&
348 !warned && !llist_empty(head))) {
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700349 warned = true;
350 WARN(1, "IPI on offline CPU %d\n", smp_processor_id());
Suresh Siddha269c8612009-08-19 18:05:35 -0700351
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700352 /*
353 * We don't have to use the _safe() variant here
354 * because we are not invoking the IPI handlers yet.
355 */
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200356 llist_for_each_entry(csd, entry, node.llist) {
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200357 switch (CSD_TYPE(csd)) {
358 case CSD_TYPE_ASYNC:
359 case CSD_TYPE_SYNC:
360 case CSD_TYPE_IRQ_WORK:
361 pr_warn("IPI callback %pS sent to offline CPU\n",
362 csd->func);
363 break;
Jens Axboe3d442232008-06-26 11:21:34 +0200364
Peter Zijlstraa14886642020-05-26 18:11:04 +0200365 case CSD_TYPE_TTWU:
366 pr_warn("IPI task-wakeup sent to offline CPU\n");
367 break;
Linus Torvalds80538712015-02-11 12:42:10 -0800368
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200369 default:
370 pr_warn("IPI callback, unknown type %d, sent to offline CPU\n",
371 CSD_TYPE(csd));
372 break;
373 }
Linus Torvalds80538712015-02-11 12:42:10 -0800374 }
Jens Axboe3d442232008-06-26 11:21:34 +0200375 }
Frederic Weisbecker47885012014-05-08 01:37:48 +0200376
377 /*
Peter Zijlstra52103be2020-05-26 18:10:59 +0200378 * First; run all SYNC callbacks, people are waiting for us.
Frederic Weisbecker47885012014-05-08 01:37:48 +0200379 */
Peter Zijlstra52103be2020-05-26 18:10:59 +0200380 prev = NULL;
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200381 llist_for_each_entry_safe(csd, csd_next, entry, node.llist) {
Jens Axboe3d442232008-06-26 11:21:34 +0200382 /* Do we wait until *after* callback? */
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200383 if (CSD_TYPE(csd) == CSD_TYPE_SYNC) {
384 smp_call_func_t func = csd->func;
385 void *info = csd->info;
386
Peter Zijlstra52103be2020-05-26 18:10:59 +0200387 if (prev) {
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200388 prev->next = &csd_next->node.llist;
Peter Zijlstra52103be2020-05-26 18:10:59 +0200389 } else {
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200390 entry = &csd_next->node.llist;
Peter Zijlstra52103be2020-05-26 18:10:59 +0200391 }
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200392
Paul E. McKenney35feb602020-06-30 13:22:54 -0700393 csd_lock_record(csd);
Jens Axboe3d442232008-06-26 11:21:34 +0200394 func(info);
Jens Axboe3d442232008-06-26 11:21:34 +0200395 csd_unlock(csd);
Paul E. McKenney35feb602020-06-30 13:22:54 -0700396 csd_lock_record(NULL);
Jens Axboe3d442232008-06-26 11:21:34 +0200397 } else {
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200398 prev = &csd->node.llist;
Jens Axboe3d442232008-06-26 11:21:34 +0200399 }
400 }
Frederic Weisbecker47885012014-05-08 01:37:48 +0200401
Peter Zijlstraa14886642020-05-26 18:11:04 +0200402 if (!entry)
403 return;
404
Frederic Weisbecker47885012014-05-08 01:37:48 +0200405 /*
Peter Zijlstra52103be2020-05-26 18:10:59 +0200406 * Second; run all !SYNC callbacks.
407 */
Peter Zijlstraa14886642020-05-26 18:11:04 +0200408 prev = NULL;
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200409 llist_for_each_entry_safe(csd, csd_next, entry, node.llist) {
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200410 int type = CSD_TYPE(csd);
Peter Zijlstra52103be2020-05-26 18:10:59 +0200411
Peter Zijlstraa14886642020-05-26 18:11:04 +0200412 if (type != CSD_TYPE_TTWU) {
413 if (prev) {
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200414 prev->next = &csd_next->node.llist;
Peter Zijlstraa14886642020-05-26 18:11:04 +0200415 } else {
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200416 entry = &csd_next->node.llist;
Peter Zijlstraa14886642020-05-26 18:11:04 +0200417 }
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200418
Peter Zijlstraa14886642020-05-26 18:11:04 +0200419 if (type == CSD_TYPE_ASYNC) {
420 smp_call_func_t func = csd->func;
421 void *info = csd->info;
422
Paul E. McKenney35feb602020-06-30 13:22:54 -0700423 csd_lock_record(csd);
Peter Zijlstraa14886642020-05-26 18:11:04 +0200424 csd_unlock(csd);
425 func(info);
Paul E. McKenney35feb602020-06-30 13:22:54 -0700426 csd_lock_record(NULL);
Peter Zijlstraa14886642020-05-26 18:11:04 +0200427 } else if (type == CSD_TYPE_IRQ_WORK) {
428 irq_work_single(csd);
429 }
430
431 } else {
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200432 prev = &csd->node.llist;
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200433 }
Peter Zijlstra52103be2020-05-26 18:10:59 +0200434 }
Peter Zijlstraa14886642020-05-26 18:11:04 +0200435
436 /*
437 * Third; only CSD_TYPE_TTWU is left, issue those.
438 */
439 if (entry)
440 sched_ttwu_pending(entry);
Jens Axboe3d442232008-06-26 11:21:34 +0200441}
442
Peter Zijlstrab2a02fc2020-05-26 18:11:01 +0200443void flush_smp_call_function_from_idle(void)
444{
445 unsigned long flags;
446
447 if (llist_empty(this_cpu_ptr(&call_single_queue)))
448 return;
449
450 local_irq_save(flags);
451 flush_smp_call_function_queue(true);
452 local_irq_restore(flags);
Jens Axboe3d442232008-06-26 11:21:34 +0200453}
454
455/*
456 * smp_call_function_single - Run a function on a specific CPU
457 * @func: The function to run. This must be fast and non-blocking.
458 * @info: An arbitrary pointer to pass to the function.
459 * @wait: If true, wait until function has completed on other CPUs.
460 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800461 * Returns 0 on success, else a negative status code.
Jens Axboe3d442232008-06-26 11:21:34 +0200462 */
David Howells3a5f65df2010-10-27 17:28:36 +0100463int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
Jens Axboe8691e5a2008-06-06 11:18:06 +0200464 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200465{
Ying Huang966a9672017-08-08 12:30:00 +0800466 call_single_data_t *csd;
467 call_single_data_t csd_stack = {
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200468 .node = { .u_flags = CSD_FLAG_LOCK | CSD_TYPE_SYNC, },
Ying Huang966a9672017-08-08 12:30:00 +0800469 };
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100470 int this_cpu;
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100471 int err;
Jens Axboe3d442232008-06-26 11:21:34 +0200472
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100473 /*
474 * prevent preemption and reschedule on another processor,
475 * as well as CPU removal
476 */
477 this_cpu = get_cpu();
478
Suresh Siddha269c8612009-08-19 18:05:35 -0700479 /*
480 * Can deadlock when called with interrupts disabled.
481 * We allow cpu's that are not yet online though, as no one else can
482 * send smp call function interrupt to this cpu and as such deadlocks
483 * can't happen.
484 */
485 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
486 && !oops_in_progress);
Jens Axboe3d442232008-06-26 11:21:34 +0200487
Peter Zijlstra19dbdcb2019-07-18 11:20:09 +0200488 /*
489 * When @wait we can deadlock when we interrupt between llist_add() and
490 * arch_send_call_function_ipi*(); when !@wait we can deadlock due to
491 * csd_lock() on because the interrupt context uses the same csd
492 * storage.
493 */
494 WARN_ON_ONCE(!in_task());
495
Linus Torvalds80538712015-02-11 12:42:10 -0800496 csd = &csd_stack;
497 if (!wait) {
498 csd = this_cpu_ptr(&csd_data);
499 csd_lock(csd);
500 }
501
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200502 csd->func = func;
503 csd->info = info;
Paul E. McKenney35feb602020-06-30 13:22:54 -0700504#ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200505 csd->node.src = smp_processor_id();
506 csd->node.dst = cpu;
Paul E. McKenneye48c15b2020-06-29 17:21:32 -0700507#endif
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200508
509 err = generic_exec_single(cpu, csd);
Linus Torvalds80538712015-02-11 12:42:10 -0800510
511 if (wait)
512 csd_lock_wait(csd);
Jens Axboe3d442232008-06-26 11:21:34 +0200513
514 put_cpu();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100515
H. Peter Anvinf73be6de2008-08-25 17:07:14 -0700516 return err;
Jens Axboe3d442232008-06-26 11:21:34 +0200517}
518EXPORT_SYMBOL(smp_call_function_single);
519
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100520/**
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100521 * smp_call_function_single_async(): Run an asynchronous function on a
522 * specific CPU.
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100523 * @cpu: The CPU to run on.
524 * @csd: Pre-allocated and setup data structure
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100525 *
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100526 * Like smp_call_function_single(), but the call is asynchonous and
527 * can thus be done from contexts with disabled interrupts.
528 *
529 * The caller passes his own pre-allocated data structure
530 * (ie: embedded in an object) and is responsible for synchronizing it
531 * such that the IPIs performed on the @csd are strictly serialized.
532 *
Peter Xu5a18cec2019-12-16 16:31:23 -0500533 * If the function is called with one csd which has not yet been
534 * processed by previous call to smp_call_function_single_async(), the
535 * function will return immediately with -EBUSY showing that the csd
536 * object is still in progress.
537 *
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100538 * NOTE: Be careful, there is unfortunately no current debugging facility to
539 * validate the correctness of this serialization.
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100540 */
Ying Huang966a9672017-08-08 12:30:00 +0800541int smp_call_function_single_async(int cpu, call_single_data_t *csd)
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100542{
543 int err = 0;
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100544
Frederic Weisbeckerfce8ad12014-02-24 16:40:01 +0100545 preempt_disable();
Linus Torvalds80538712015-02-11 12:42:10 -0800546
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200547 if (csd->node.u_flags & CSD_FLAG_LOCK) {
Peter Xu5a18cec2019-12-16 16:31:23 -0500548 err = -EBUSY;
549 goto out;
550 }
Linus Torvalds80538712015-02-11 12:42:10 -0800551
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200552 csd->node.u_flags = CSD_FLAG_LOCK;
Linus Torvalds80538712015-02-11 12:42:10 -0800553 smp_wmb();
554
Peter Zijlstra4b44a212020-05-26 18:11:02 +0200555 err = generic_exec_single(cpu, csd);
Peter Xu5a18cec2019-12-16 16:31:23 -0500556
557out:
Frederic Weisbeckerfce8ad12014-02-24 16:40:01 +0100558 preempt_enable();
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100559
560 return err;
561}
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100562EXPORT_SYMBOL_GPL(smp_call_function_single_async);
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100563
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800564/*
565 * smp_call_function_any - Run a function on any of the given cpus
566 * @mask: The mask of cpus it can run on.
567 * @func: The function to run. This must be fast and non-blocking.
568 * @info: An arbitrary pointer to pass to the function.
569 * @wait: If true, wait until function has completed.
570 *
571 * Returns 0 on success, else a negative status code (if no cpus were online).
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800572 *
573 * Selection preference:
574 * 1) current cpu if in @mask
575 * 2) any cpu of current node if in @mask
576 * 3) any other online cpu in @mask
577 */
578int smp_call_function_any(const struct cpumask *mask,
David Howells3a5f65df2010-10-27 17:28:36 +0100579 smp_call_func_t func, void *info, int wait)
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800580{
581 unsigned int cpu;
582 const struct cpumask *nodemask;
583 int ret;
584
585 /* Try for same CPU (cheapest) */
586 cpu = get_cpu();
587 if (cpumask_test_cpu(cpu, mask))
588 goto call;
589
590 /* Try for same node. */
David Johnaf2422c2010-01-15 17:01:23 -0800591 nodemask = cpumask_of_node(cpu_to_node(cpu));
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800592 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
593 cpu = cpumask_next_and(cpu, nodemask, mask)) {
594 if (cpu_online(cpu))
595 goto call;
596 }
597
598 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
599 cpu = cpumask_any_and(mask, cpu_online_mask);
600call:
601 ret = smp_call_function_single(cpu, func, info, wait);
602 put_cpu();
603 return ret;
604}
605EXPORT_SYMBOL_GPL(smp_call_function_any);
606
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100607static void smp_call_function_many_cond(const struct cpumask *mask,
608 smp_call_func_t func, void *info,
609 bool wait, smp_cond_func_t cond_func)
Jens Axboe3d442232008-06-26 11:21:34 +0200610{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700611 struct call_function_data *cfd;
Shaohua Li9a46ad62013-02-21 16:43:03 -0800612 int cpu, next_cpu, this_cpu = smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +0200613
Suresh Siddha269c8612009-08-19 18:05:35 -0700614 /*
615 * Can deadlock when called with interrupts disabled.
616 * We allow cpu's that are not yet online though, as no one else can
617 * send smp call function interrupt to this cpu and as such deadlocks
618 * can't happen.
619 */
620 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
Tejun Heobd924e82011-01-20 12:07:13 +0100621 && !oops_in_progress && !early_boot_irqs_disabled);
Jens Axboe3d442232008-06-26 11:21:34 +0200622
Peter Zijlstra19dbdcb2019-07-18 11:20:09 +0200623 /*
624 * When @wait we can deadlock when we interrupt between llist_add() and
625 * arch_send_call_function_ipi*(); when !@wait we can deadlock due to
626 * csd_lock() on because the interrupt context uses the same csd
627 * storage.
628 */
629 WARN_ON_ONCE(!in_task());
630
Milton Miller723aae22011-03-15 13:27:17 -0600631 /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
Rusty Russell54b11e62008-12-30 09:05:16 +1030632 cpu = cpumask_first_and(mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100633 if (cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030634 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100635
Rusty Russell54b11e62008-12-30 09:05:16 +1030636 /* No online cpus? We're done. */
637 if (cpu >= nr_cpu_ids)
638 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200639
Rusty Russell54b11e62008-12-30 09:05:16 +1030640 /* Do we have another CPU which isn't us? */
641 next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100642 if (next_cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030643 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
644
645 /* Fastpath: do that cpu by itself. */
646 if (next_cpu >= nr_cpu_ids) {
Sebastian Andrzej Siewior25a3a152020-01-27 09:39:15 +0100647 if (!cond_func || cond_func(cpu, info))
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100648 smp_call_function_single(cpu, func, info, wait);
Rusty Russell54b11e62008-12-30 09:05:16 +1030649 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200650 }
651
Christoph Lameterbb964a92014-08-17 12:30:24 -0500652 cfd = this_cpu_ptr(&cfd_data);
Milton Miller45a57912011-03-15 13:27:16 -0600653
Andrew Mortone1d12f32013-04-30 15:27:28 -0700654 cpumask_and(cfd->cpumask, mask, cpu_online_mask);
Peter Zijlstra6c8557b2017-05-19 12:58:25 +0200655 __cpumask_clear_cpu(this_cpu, cfd->cpumask);
Milton Miller723aae22011-03-15 13:27:17 -0600656
657 /* Some callers race with other cpus changing the passed mask */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700658 if (unlikely(!cpumask_weight(cfd->cpumask)))
Milton Miller723aae22011-03-15 13:27:17 -0600659 return;
Anton Blanchard6dc19892011-01-20 14:44:33 -0800660
Aaron Lu3fc5b3b2017-05-19 15:53:31 +0800661 cpumask_clear(cfd->cpumask_ipi);
Andrew Mortone1d12f32013-04-30 15:27:28 -0700662 for_each_cpu(cpu, cfd->cpumask) {
Ying Huang966a9672017-08-08 12:30:00 +0800663 call_single_data_t *csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800664
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100665 if (cond_func && !cond_func(cpu, info))
666 continue;
667
Shaohua Li9a46ad62013-02-21 16:43:03 -0800668 csd_lock(csd);
Linus Torvalds80538712015-02-11 12:42:10 -0800669 if (wait)
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200670 csd->node.u_flags |= CSD_TYPE_SYNC;
Shaohua Li9a46ad62013-02-21 16:43:03 -0800671 csd->func = func;
672 csd->info = info;
Paul E. McKenney35feb602020-06-30 13:22:54 -0700673#ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200674 csd->node.src = smp_processor_id();
675 csd->node.dst = cpu;
Paul E. McKenneye48c15b2020-06-29 17:21:32 -0700676#endif
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200677 if (llist_add(&csd->node.llist, &per_cpu(call_single_queue, cpu)))
Peter Zijlstra6c8557b2017-05-19 12:58:25 +0200678 __cpumask_set_cpu(cpu, cfd->cpumask_ipi);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800679 }
Suresh Siddha561920a02008-10-30 18:28:41 +0100680
Jens Axboe3d442232008-06-26 11:21:34 +0200681 /* Send a message to all CPUs in the map */
Aaron Lu3fc5b3b2017-05-19 15:53:31 +0800682 arch_send_call_function_ipi_mask(cfd->cpumask_ipi);
Jens Axboe3d442232008-06-26 11:21:34 +0200683
Shaohua Li9a46ad62013-02-21 16:43:03 -0800684 if (wait) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700685 for_each_cpu(cpu, cfd->cpumask) {
Ying Huang966a9672017-08-08 12:30:00 +0800686 call_single_data_t *csd;
Andrew Mortone1d12f32013-04-30 15:27:28 -0700687
688 csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800689 csd_lock_wait(csd);
690 }
691 }
Jens Axboe3d442232008-06-26 11:21:34 +0200692}
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100693
694/**
695 * smp_call_function_many(): Run a function on a set of other CPUs.
696 * @mask: The set of cpus to run on (only runs on online subset).
697 * @func: The function to run. This must be fast and non-blocking.
698 * @info: An arbitrary pointer to pass to the function.
699 * @wait: If true, wait (atomically) until function has completed
700 * on other CPUs.
701 *
702 * If @wait is true, then returns once @func has returned.
703 *
704 * You must not call this function with disabled interrupts or from a
705 * hardware interrupt handler or from a bottom half handler. Preemption
706 * must be disabled when calling this function.
707 */
708void smp_call_function_many(const struct cpumask *mask,
709 smp_call_func_t func, void *info, bool wait)
710{
711 smp_call_function_many_cond(mask, func, info, wait, NULL);
712}
Rusty Russell54b11e62008-12-30 09:05:16 +1030713EXPORT_SYMBOL(smp_call_function_many);
Jens Axboe3d442232008-06-26 11:21:34 +0200714
715/**
716 * smp_call_function(): Run a function on all other CPUs.
717 * @func: The function to run. This must be fast and non-blocking.
718 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100719 * @wait: If true, wait (atomically) until function has completed
720 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200721 *
Rusty Russell54b11e62008-12-30 09:05:16 +1030722 * Returns 0.
Jens Axboe3d442232008-06-26 11:21:34 +0200723 *
724 * If @wait is true, then returns once @func has returned; otherwise
Sheng Yang72f279b2009-10-22 19:19:34 +0800725 * it returns just before the target cpu calls @func.
Jens Axboe3d442232008-06-26 11:21:34 +0200726 *
727 * You must not call this function with disabled interrupts or from a
728 * hardware interrupt handler or from a bottom half handler.
729 */
Nadav Amitcaa75932019-06-12 23:48:05 -0700730void smp_call_function(smp_call_func_t func, void *info, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200731{
Jens Axboe3d442232008-06-26 11:21:34 +0200732 preempt_disable();
Rusty Russell54b11e62008-12-30 09:05:16 +1030733 smp_call_function_many(cpu_online_mask, func, info, wait);
Jens Axboe3d442232008-06-26 11:21:34 +0200734 preempt_enable();
Jens Axboe3d442232008-06-26 11:21:34 +0200735}
736EXPORT_SYMBOL(smp_call_function);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800737
Amerigo Wang34db18a02011-03-22 16:34:06 -0700738/* Setup configured maximum number of CPUs to activate */
739unsigned int setup_max_cpus = NR_CPUS;
740EXPORT_SYMBOL(setup_max_cpus);
741
742
743/*
744 * Setup routine for controlling SMP activation
745 *
746 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
747 * activation entirely (the MPS table probe still happens, though).
748 *
749 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
750 * greater than 0, limits the maximum number of CPUs activated in
751 * SMP mode to <NUM>.
752 */
753
754void __weak arch_disable_smp_support(void) { }
755
756static int __init nosmp(char *str)
757{
758 setup_max_cpus = 0;
759 arch_disable_smp_support();
760
761 return 0;
762}
763
764early_param("nosmp", nosmp);
765
766/* this is hard limit */
767static int __init nrcpus(char *str)
768{
769 int nr_cpus;
770
Muchun Song58934352020-07-16 15:04:57 +0800771 if (get_option(&str, &nr_cpus) && nr_cpus > 0 && nr_cpus < nr_cpu_ids)
Amerigo Wang34db18a02011-03-22 16:34:06 -0700772 nr_cpu_ids = nr_cpus;
773
774 return 0;
775}
776
777early_param("nr_cpus", nrcpus);
778
779static int __init maxcpus(char *str)
780{
781 get_option(&str, &setup_max_cpus);
782 if (setup_max_cpus == 0)
783 arch_disable_smp_support();
784
785 return 0;
786}
787
788early_param("maxcpus", maxcpus);
789
790/* Setup number of possible processor ids */
Alexey Dobriyan9b130ad2017-09-08 16:14:18 -0700791unsigned int nr_cpu_ids __read_mostly = NR_CPUS;
Amerigo Wang34db18a02011-03-22 16:34:06 -0700792EXPORT_SYMBOL(nr_cpu_ids);
793
794/* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
795void __init setup_nr_cpu_ids(void)
796{
797 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
798}
799
800/* Called by boot processor to activate the rest. */
801void __init smp_init(void)
802{
Michael Ellerman92b23272016-10-26 16:37:54 +1100803 int num_nodes, num_cpus;
Amerigo Wang34db18a02011-03-22 16:34:06 -0700804
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700805 idle_threads_init();
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000806 cpuhp_threads_init();
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700807
Michael Ellerman51111dc2016-10-26 16:37:55 +1100808 pr_info("Bringing up secondary CPUs ...\n");
809
Qais Yousefb99a2652020-03-23 13:51:09 +0000810 bringup_nonboot_cpus(setup_max_cpus);
Amerigo Wang34db18a02011-03-22 16:34:06 -0700811
Michael Ellerman92b23272016-10-26 16:37:54 +1100812 num_nodes = num_online_nodes();
813 num_cpus = num_online_cpus();
814 pr_info("Brought up %d node%s, %d CPU%s\n",
815 num_nodes, (num_nodes > 1 ? "s" : ""),
816 num_cpus, (num_cpus > 1 ? "s" : ""));
817
Amerigo Wang34db18a02011-03-22 16:34:06 -0700818 /* Any cleanup work */
Amerigo Wang34db18a02011-03-22 16:34:06 -0700819 smp_cpus_done(setup_max_cpus);
820}
821
Amerigo Wang351f8f82011-01-12 16:59:39 -0800822/*
Tejun Heobd924e82011-01-20 12:07:13 +0100823 * Call a function on all processors. May be used during early boot while
824 * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead
825 * of local_irq_disable/enable().
Amerigo Wang351f8f82011-01-12 16:59:39 -0800826 */
Kaitao Cheng58eb7b72020-04-18 00:24:51 +0800827void on_each_cpu(smp_call_func_t func, void *info, int wait)
Amerigo Wang351f8f82011-01-12 16:59:39 -0800828{
Tejun Heobd924e82011-01-20 12:07:13 +0100829 unsigned long flags;
Amerigo Wang351f8f82011-01-12 16:59:39 -0800830
831 preempt_disable();
Nadav Amitcaa75932019-06-12 23:48:05 -0700832 smp_call_function(func, info, wait);
Tejun Heobd924e82011-01-20 12:07:13 +0100833 local_irq_save(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800834 func(info);
Tejun Heobd924e82011-01-20 12:07:13 +0100835 local_irq_restore(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800836 preempt_enable();
Amerigo Wang351f8f82011-01-12 16:59:39 -0800837}
838EXPORT_SYMBOL(on_each_cpu);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700839
840/**
841 * on_each_cpu_mask(): Run a function on processors specified by
842 * cpumask, which may include the local processor.
843 * @mask: The set of cpus to run on (only runs on online subset).
844 * @func: The function to run. This must be fast and non-blocking.
845 * @info: An arbitrary pointer to pass to the function.
846 * @wait: If true, wait (atomically) until function has completed
847 * on other CPUs.
848 *
849 * If @wait is true, then returns once @func has returned.
850 *
David Daney202da402013-09-11 14:23:29 -0700851 * You must not call this function with disabled interrupts or from a
852 * hardware interrupt handler or from a bottom half handler. The
853 * exception is that it may be used during early boot while
854 * early_boot_irqs_disabled is set.
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700855 */
856void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
857 void *info, bool wait)
858{
859 int cpu = get_cpu();
860
861 smp_call_function_many(mask, func, info, wait);
862 if (cpumask_test_cpu(cpu, mask)) {
David Daney202da402013-09-11 14:23:29 -0700863 unsigned long flags;
864 local_irq_save(flags);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700865 func(info);
David Daney202da402013-09-11 14:23:29 -0700866 local_irq_restore(flags);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700867 }
868 put_cpu();
869}
870EXPORT_SYMBOL(on_each_cpu_mask);
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700871
872/*
873 * on_each_cpu_cond(): Call a function on each processor for which
874 * the supplied function cond_func returns true, optionally waiting
875 * for all the required CPUs to finish. This may include the local
876 * processor.
877 * @cond_func: A callback function that is passed a cpu id and
Randy Dunlap7b7b8a22020-10-15 20:10:28 -0700878 * the info parameter. The function is called
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700879 * with preemption disabled. The function should
880 * return a blooean value indicating whether to IPI
881 * the specified CPU.
882 * @func: The function to run on all applicable CPUs.
883 * This must be fast and non-blocking.
884 * @info: An arbitrary pointer to pass to both functions.
885 * @wait: If true, wait (atomically) until function has
886 * completed on other CPUs.
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700887 *
888 * Preemption is disabled to protect against CPUs going offline but not online.
889 * CPUs going online during the call will not be seen or sent an IPI.
890 *
891 * You must not call this function with disabled interrupts or
892 * from a hardware interrupt handler or from a bottom half handler.
893 */
Sebastian Andrzej Siewior5671d812020-01-17 10:01:35 +0100894void on_each_cpu_cond_mask(smp_cond_func_t cond_func, smp_call_func_t func,
Sebastian Andrzej Siewiorcb923152020-01-17 10:01:37 +0100895 void *info, bool wait, const struct cpumask *mask)
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700896{
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100897 int cpu = get_cpu();
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700898
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100899 smp_call_function_many_cond(mask, func, info, wait, cond_func);
900 if (cpumask_test_cpu(cpu, mask) && cond_func(cpu, info)) {
901 unsigned long flags;
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700902
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100903 local_irq_save(flags);
904 func(info);
905 local_irq_restore(flags);
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700906 }
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100907 put_cpu();
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700908}
Rik van Riel7d49b282018-09-25 23:58:41 -0400909EXPORT_SYMBOL(on_each_cpu_cond_mask);
910
Sebastian Andrzej Siewior5671d812020-01-17 10:01:35 +0100911void on_each_cpu_cond(smp_cond_func_t cond_func, smp_call_func_t func,
Sebastian Andrzej Siewiorcb923152020-01-17 10:01:37 +0100912 void *info, bool wait)
Rik van Riel7d49b282018-09-25 23:58:41 -0400913{
Sebastian Andrzej Siewiorcb923152020-01-17 10:01:37 +0100914 on_each_cpu_cond_mask(cond_func, func, info, wait, cpu_online_mask);
Rik van Riel7d49b282018-09-25 23:58:41 -0400915}
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700916EXPORT_SYMBOL(on_each_cpu_cond);
Thomas Gleixnerf37f4352012-05-07 17:59:48 +0000917
918static void do_nothing(void *unused)
919{
920}
921
922/**
923 * kick_all_cpus_sync - Force all cpus out of idle
924 *
925 * Used to synchronize the update of pm_idle function pointer. It's
926 * called after the pointer is updated and returns after the dummy
927 * callback function has been executed on all cpus. The execution of
928 * the function can only happen on the remote cpus after they have
929 * left the idle function which had been called via pm_idle function
930 * pointer. So it's guaranteed that nothing uses the previous pointer
931 * anymore.
932 */
933void kick_all_cpus_sync(void)
934{
935 /* Make sure the change is visible before we kick the cpus */
936 smp_mb();
937 smp_call_function(do_nothing, NULL, 1);
938}
939EXPORT_SYMBOL_GPL(kick_all_cpus_sync);
Chuansheng Liuc6f44592014-09-04 15:17:54 +0800940
941/**
942 * wake_up_all_idle_cpus - break all cpus out of idle
943 * wake_up_all_idle_cpus try to break all cpus which is in idle state even
944 * including idle polling cpus, for non-idle cpus, we will do nothing
945 * for them.
946 */
947void wake_up_all_idle_cpus(void)
948{
949 int cpu;
950
951 preempt_disable();
952 for_each_online_cpu(cpu) {
953 if (cpu == smp_processor_id())
954 continue;
955
956 wake_up_if_idle(cpu);
957 }
958 preempt_enable();
959}
960EXPORT_SYMBOL_GPL(wake_up_all_idle_cpus);
Juergen Grossdf8ce9d2016-08-29 08:48:44 +0200961
962/**
963 * smp_call_on_cpu - Call a function on a specific cpu
964 *
965 * Used to call a function on a specific cpu and wait for it to return.
966 * Optionally make sure the call is done on a specified physical cpu via vcpu
967 * pinning in order to support virtualized environments.
968 */
969struct smp_call_on_cpu_struct {
970 struct work_struct work;
971 struct completion done;
972 int (*func)(void *);
973 void *data;
974 int ret;
975 int cpu;
976};
977
978static void smp_call_on_cpu_callback(struct work_struct *work)
979{
980 struct smp_call_on_cpu_struct *sscs;
981
982 sscs = container_of(work, struct smp_call_on_cpu_struct, work);
983 if (sscs->cpu >= 0)
984 hypervisor_pin_vcpu(sscs->cpu);
985 sscs->ret = sscs->func(sscs->data);
986 if (sscs->cpu >= 0)
987 hypervisor_pin_vcpu(-1);
988
989 complete(&sscs->done);
990}
991
992int smp_call_on_cpu(unsigned int cpu, int (*func)(void *), void *par, bool phys)
993{
994 struct smp_call_on_cpu_struct sscs = {
Juergen Grossdf8ce9d2016-08-29 08:48:44 +0200995 .done = COMPLETION_INITIALIZER_ONSTACK(sscs.done),
996 .func = func,
997 .data = par,
998 .cpu = phys ? cpu : -1,
999 };
1000
Peter Zijlstra8db54942016-09-11 10:36:26 +02001001 INIT_WORK_ONSTACK(&sscs.work, smp_call_on_cpu_callback);
1002
Juergen Grossdf8ce9d2016-08-29 08:48:44 +02001003 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
1004 return -ENXIO;
1005
1006 queue_work_on(cpu, system_wq, &sscs.work);
1007 wait_for_completion(&sscs.done);
1008
1009 return sscs.ret;
1010}
1011EXPORT_SYMBOL_GPL(smp_call_on_cpu);