1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23d442233SJens Axboe /*
33d442233SJens Axboe * Generic helpers for smp ipi calls
43d442233SJens Axboe *
53d442233SJens Axboe * (C) Jens Axboe <jens.axboe@oracle.com> 2008
63d442233SJens Axboe */
7ca7dfdbbSMichael Ellerman
8ca7dfdbbSMichael Ellerman #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9ca7dfdbbSMichael Ellerman
1047885016SFrederic Weisbecker #include <linux/irq_work.h>
113d442233SJens Axboe #include <linux/rcupdate.h>
1259190f42SLinus Torvalds #include <linux/rculist.h>
13641cd4cfSIngo Molnar #include <linux/kernel.h>
149984de1aSPaul Gortmaker #include <linux/export.h>
150b13fda1SIngo Molnar #include <linux/percpu.h>
160b13fda1SIngo Molnar #include <linux/init.h>
17f9d34595SSebastian Andrzej Siewior #include <linux/interrupt.h>
185a0e3ad6STejun Heo #include <linux/gfp.h>
193d442233SJens Axboe #include <linux/smp.h>
208969a5edSPeter Zijlstra #include <linux/cpu.h>
21c6f4459fSChuansheng Liu #include <linux/sched.h>
224c822698SIngo Molnar #include <linux/sched/idle.h>
2347ae4b05SJuergen Gross #include <linux/hypervisor.h>
2435feb604SPaul E. McKenney #include <linux/sched/clock.h>
2535feb604SPaul E. McKenney #include <linux/nmi.h>
2635feb604SPaul E. McKenney #include <linux/sched/debug.h>
278d0968ccSJuergen Gross #include <linux/jump_label.h>
283d442233SJens Axboe
29cc9cb0a7SValentin Schneider #include <trace/events/ipi.h>
30949fa3f1SLeonardo Bras #define CREATE_TRACE_POINTS
31949fa3f1SLeonardo Bras #include <trace/events/csd.h>
32949fa3f1SLeonardo Bras #undef CREATE_TRACE_POINTS
33cc9cb0a7SValentin Schneider
343bb5d2eeSSuresh Siddha #include "smpboot.h"
351f8db415SIngo Molnar #include "sched/smp.h"
363bb5d2eeSSuresh Siddha
37545b8c8dSPeter Zijlstra #define CSD_TYPE(_csd) ((_csd)->node.u_flags & CSD_FLAG_TYPE_MASK)
383d442233SJens Axboe
393d442233SJens Axboe struct call_function_data {
406366d062SPaul E. McKenney call_single_data_t __percpu *csd;
418969a5edSPeter Zijlstra cpumask_var_t cpumask;
423fc5b3b6SAaron Lu cpumask_var_t cpumask_ipi;
433d442233SJens Axboe };
443d442233SJens Axboe
45a22793c7SNadav Amit static DEFINE_PER_CPU_ALIGNED(struct call_function_data, cfd_data);
46e03bcb68SMilton Miller
476897fc22SChristoph Hellwig static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue);
488969a5edSPeter Zijlstra
490d3a00b3SImran Khan static DEFINE_PER_CPU(atomic_t, trigger_backtrace) = ATOMIC_INIT(1);
500d3a00b3SImran Khan
5116bf5a5eSThomas Gleixner static void __flush_smp_call_function_queue(bool warn_cpu_offline);
528d056c48SSrivatsa S. Bhat
smpcfd_prepare_cpu(unsigned int cpu)5331487f83SRichard Weinberger int smpcfd_prepare_cpu(unsigned int cpu)
548969a5edSPeter Zijlstra {
558969a5edSPeter Zijlstra struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
568969a5edSPeter Zijlstra
57eaa95840SYinghai Lu if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
588969a5edSPeter Zijlstra cpu_to_node(cpu)))
5931487f83SRichard Weinberger return -ENOMEM;
603fc5b3b6SAaron Lu if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL,
613fc5b3b6SAaron Lu cpu_to_node(cpu))) {
623fc5b3b6SAaron Lu free_cpumask_var(cfd->cpumask);
633fc5b3b6SAaron Lu return -ENOMEM;
643fc5b3b6SAaron Lu }
656366d062SPaul E. McKenney cfd->csd = alloc_percpu(call_single_data_t);
666366d062SPaul E. McKenney if (!cfd->csd) {
679a46ad6dSShaohua Li free_cpumask_var(cfd->cpumask);
683fc5b3b6SAaron Lu free_cpumask_var(cfd->cpumask_ipi);
6931487f83SRichard Weinberger return -ENOMEM;
709a46ad6dSShaohua Li }
718969a5edSPeter Zijlstra
7231487f83SRichard Weinberger return 0;
7331487f83SRichard Weinberger }
748969a5edSPeter Zijlstra
smpcfd_dead_cpu(unsigned int cpu)7531487f83SRichard Weinberger int smpcfd_dead_cpu(unsigned int cpu)
7631487f83SRichard Weinberger {
7731487f83SRichard Weinberger struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
7831487f83SRichard Weinberger
798969a5edSPeter Zijlstra free_cpumask_var(cfd->cpumask);
803fc5b3b6SAaron Lu free_cpumask_var(cfd->cpumask_ipi);
816366d062SPaul E. McKenney free_percpu(cfd->csd);
8231487f83SRichard Weinberger return 0;
8331487f83SRichard Weinberger }
848d056c48SSrivatsa S. Bhat
smpcfd_dying_cpu(unsigned int cpu)8531487f83SRichard Weinberger int smpcfd_dying_cpu(unsigned int cpu)
8631487f83SRichard Weinberger {
878d056c48SSrivatsa S. Bhat /*
888d056c48SSrivatsa S. Bhat * The IPIs for the smp-call-function callbacks queued by other
898d056c48SSrivatsa S. Bhat * CPUs might arrive late, either due to hardware latencies or
908d056c48SSrivatsa S. Bhat * because this CPU disabled interrupts (inside stop-machine)
918d056c48SSrivatsa S. Bhat * before the IPIs were sent. So flush out any pending callbacks
928d056c48SSrivatsa S. Bhat * explicitly (without waiting for the IPIs to arrive), to
938d056c48SSrivatsa S. Bhat * ensure that the outgoing CPU doesn't go offline with work
948d056c48SSrivatsa S. Bhat * still pending.
958d056c48SSrivatsa S. Bhat */
9616bf5a5eSThomas Gleixner __flush_smp_call_function_queue(false);
97afaa653cSPeter Zijlstra irq_work_run();
9831487f83SRichard Weinberger return 0;
998969a5edSPeter Zijlstra }
1008969a5edSPeter Zijlstra
call_function_init(void)101d8ad7d11STakao Indoh void __init call_function_init(void)
1023d442233SJens Axboe {
1033d442233SJens Axboe int i;
1043d442233SJens Axboe
1056897fc22SChristoph Hellwig for_each_possible_cpu(i)
1066897fc22SChristoph Hellwig init_llist_head(&per_cpu(call_single_queue, i));
1078969a5edSPeter Zijlstra
10831487f83SRichard Weinberger smpcfd_prepare_cpu(smp_processor_id());
1093d442233SJens Axboe }
1103d442233SJens Axboe
11108407b5fSValentin Schneider static __always_inline void
send_call_function_single_ipi(int cpu)1125c312497SPeter Zijlstra send_call_function_single_ipi(int cpu)
11308407b5fSValentin Schneider {
11468f4ff04SValentin Schneider if (call_function_single_prep_ipi(cpu)) {
1155c312497SPeter Zijlstra trace_ipi_send_cpu(cpu, _RET_IP_,
1165c312497SPeter Zijlstra generic_smp_call_function_single_interrupt);
11768f4ff04SValentin Schneider arch_send_call_function_single_ipi(cpu);
11868f4ff04SValentin Schneider }
11968f4ff04SValentin Schneider }
12068f4ff04SValentin Schneider
12168f4ff04SValentin Schneider static __always_inline void
send_call_function_ipi_mask(struct cpumask * mask)1225c312497SPeter Zijlstra send_call_function_ipi_mask(struct cpumask *mask)
12368f4ff04SValentin Schneider {
1245c312497SPeter Zijlstra trace_ipi_send_cpumask(mask, _RET_IP_,
1255c312497SPeter Zijlstra generic_smp_call_function_single_interrupt);
12608407b5fSValentin Schneider arch_send_call_function_ipi_mask(mask);
12708407b5fSValentin Schneider }
12808407b5fSValentin Schneider
129949fa3f1SLeonardo Bras static __always_inline void
csd_do_func(smp_call_func_t func,void * info,struct __call_single_data * csd)130949fa3f1SLeonardo Bras csd_do_func(smp_call_func_t func, void *info, struct __call_single_data *csd)
131949fa3f1SLeonardo Bras {
132949fa3f1SLeonardo Bras trace_csd_function_entry(func, csd);
133949fa3f1SLeonardo Bras func(info);
134949fa3f1SLeonardo Bras trace_csd_function_exit(func, csd);
135949fa3f1SLeonardo Bras }
136949fa3f1SLeonardo Bras
13735feb604SPaul E. McKenney #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
13835feb604SPaul E. McKenney
139c5219860SPaul E. McKenney static DEFINE_STATIC_KEY_MAYBE(CONFIG_CSD_LOCK_WAIT_DEBUG_DEFAULT, csdlock_debug_enabled);
1408d0968ccSJuergen Gross
1411771257cSPaul E. McKenney /*
1421771257cSPaul E. McKenney * Parse the csdlock_debug= kernel boot parameter.
1431771257cSPaul E. McKenney *
1441771257cSPaul E. McKenney * If you need to restore the old "ext" value that once provided
1451771257cSPaul E. McKenney * additional debugging information, reapply the following commits:
1461771257cSPaul E. McKenney *
1471771257cSPaul E. McKenney * de7b09ef658d ("locking/csd_lock: Prepare more CSD lock debugging")
1481771257cSPaul E. McKenney * a5aabace5fb8 ("locking/csd_lock: Add more data to CSD lock debugging")
1491771257cSPaul E. McKenney */
csdlock_debug(char * str)1508d0968ccSJuergen Gross static int __init csdlock_debug(char *str)
1518d0968ccSJuergen Gross {
152203e4358SPaul E. McKenney int ret;
1538d0968ccSJuergen Gross unsigned int val = 0;
1548d0968ccSJuergen Gross
155203e4358SPaul E. McKenney ret = get_option(&str, &val);
156203e4358SPaul E. McKenney if (ret) {
1578d0968ccSJuergen Gross if (val)
1588d0968ccSJuergen Gross static_branch_enable(&csdlock_debug_enabled);
159203e4358SPaul E. McKenney else
160203e4358SPaul E. McKenney static_branch_disable(&csdlock_debug_enabled);
161203e4358SPaul E. McKenney }
1628d0968ccSJuergen Gross
1639c9b26b0SChen Zhongjin return 1;
1648d0968ccSJuergen Gross }
1659c9b26b0SChen Zhongjin __setup("csdlock_debug=", csdlock_debug);
1668d0968ccSJuergen Gross
16735feb604SPaul E. McKenney static DEFINE_PER_CPU(call_single_data_t *, cur_csd);
16835feb604SPaul E. McKenney static DEFINE_PER_CPU(smp_call_func_t, cur_csd_func);
16935feb604SPaul E. McKenney static DEFINE_PER_CPU(void *, cur_csd_info);
17035feb604SPaul E. McKenney
1713791a223SPaul E. McKenney static ulong csd_lock_timeout = 5000; /* CSD lock timeout in milliseconds. */
1723791a223SPaul E. McKenney module_param(csd_lock_timeout, ulong, 0444);
173f6cc3d85SRik van Riel static int panic_on_ipistall; /* CSD panic timeout in milliseconds, 300000 for five minutes. */
174f6cc3d85SRik van Riel module_param(panic_on_ipistall, int, 0444);
1753791a223SPaul E. McKenney
1762b722160SWei Yongjun static atomic_t csd_bug_count = ATOMIC_INIT(0);
17735feb604SPaul E. McKenney
17835feb604SPaul E. McKenney /* Record current CSD work for current CPU, NULL to erase. */
__csd_lock_record(struct __call_single_data * csd)1791139aeb1SArnd Bergmann static void __csd_lock_record(struct __call_single_data *csd)
18035feb604SPaul E. McKenney {
18135feb604SPaul E. McKenney if (!csd) {
18235feb604SPaul E. McKenney smp_mb(); /* NULL cur_csd after unlock. */
18335feb604SPaul E. McKenney __this_cpu_write(cur_csd, NULL);
18435feb604SPaul E. McKenney return;
18535feb604SPaul E. McKenney }
18635feb604SPaul E. McKenney __this_cpu_write(cur_csd_func, csd->func);
18735feb604SPaul E. McKenney __this_cpu_write(cur_csd_info, csd->info);
18835feb604SPaul E. McKenney smp_wmb(); /* func and info before csd. */
18935feb604SPaul E. McKenney __this_cpu_write(cur_csd, csd);
19035feb604SPaul E. McKenney smp_mb(); /* Update cur_csd before function call. */
19135feb604SPaul E. McKenney /* Or before unlock, as the case may be. */
19235feb604SPaul E. McKenney }
19335feb604SPaul E. McKenney
csd_lock_record(struct __call_single_data * csd)1941139aeb1SArnd Bergmann static __always_inline void csd_lock_record(struct __call_single_data *csd)
1958d0968ccSJuergen Gross {
1968d0968ccSJuergen Gross if (static_branch_unlikely(&csdlock_debug_enabled))
1978d0968ccSJuergen Gross __csd_lock_record(csd);
1988d0968ccSJuergen Gross }
1998d0968ccSJuergen Gross
csd_lock_wait_getcpu(struct __call_single_data * csd)2001139aeb1SArnd Bergmann static int csd_lock_wait_getcpu(struct __call_single_data *csd)
20135feb604SPaul E. McKenney {
20235feb604SPaul E. McKenney unsigned int csd_type;
20335feb604SPaul E. McKenney
20435feb604SPaul E. McKenney csd_type = CSD_TYPE(csd);
20535feb604SPaul E. McKenney if (csd_type == CSD_TYPE_ASYNC || csd_type == CSD_TYPE_SYNC)
206a787bdafSIngo Molnar return csd->node.dst; /* Other CSD_TYPE_ values might not have ->dst. */
20735feb604SPaul E. McKenney return -1;
20835feb604SPaul E. McKenney }
20935feb604SPaul E. McKenney
21035feb604SPaul E. McKenney /*
21135feb604SPaul E. McKenney * Complain if too much time spent waiting. Note that only
21235feb604SPaul E. McKenney * the CSD_TYPE_SYNC/ASYNC types provide the destination CPU,
21335feb604SPaul E. McKenney * so waiting on other types gets much less information.
21435feb604SPaul E. McKenney */
csd_lock_wait_toolong(struct __call_single_data * csd,u64 ts0,u64 * ts1,int * bug_id)2151139aeb1SArnd Bergmann static bool csd_lock_wait_toolong(struct __call_single_data *csd, u64 ts0, u64 *ts1, int *bug_id)
21635feb604SPaul E. McKenney {
21735feb604SPaul E. McKenney int cpu = -1;
21835feb604SPaul E. McKenney int cpux;
21935feb604SPaul E. McKenney bool firsttime;
22035feb604SPaul E. McKenney u64 ts2, ts_delta;
22135feb604SPaul E. McKenney call_single_data_t *cpu_cur_csd;
222545b8c8dSPeter Zijlstra unsigned int flags = READ_ONCE(csd->node.u_flags);
2233791a223SPaul E. McKenney unsigned long long csd_lock_timeout_ns = csd_lock_timeout * NSEC_PER_MSEC;
22435feb604SPaul E. McKenney
22535feb604SPaul E. McKenney if (!(flags & CSD_FLAG_LOCK)) {
22635feb604SPaul E. McKenney if (!unlikely(*bug_id))
22735feb604SPaul E. McKenney return true;
22835feb604SPaul E. McKenney cpu = csd_lock_wait_getcpu(csd);
22935feb604SPaul E. McKenney pr_alert("csd: CSD lock (#%d) got unstuck on CPU#%02d, CPU#%02d released the lock.\n",
23035feb604SPaul E. McKenney *bug_id, raw_smp_processor_id(), cpu);
23135feb604SPaul E. McKenney return true;
23235feb604SPaul E. McKenney }
23335feb604SPaul E. McKenney
23435feb604SPaul E. McKenney ts2 = sched_clock();
235f6cc3d85SRik van Riel /* How long since we last checked for a stuck CSD lock.*/
23635feb604SPaul E. McKenney ts_delta = ts2 - *ts1;
2373791a223SPaul E. McKenney if (likely(ts_delta <= csd_lock_timeout_ns || csd_lock_timeout_ns == 0))
23835feb604SPaul E. McKenney return false;
23935feb604SPaul E. McKenney
24035feb604SPaul E. McKenney firsttime = !*bug_id;
24135feb604SPaul E. McKenney if (firsttime)
24235feb604SPaul E. McKenney *bug_id = atomic_inc_return(&csd_bug_count);
24335feb604SPaul E. McKenney cpu = csd_lock_wait_getcpu(csd);
24435feb604SPaul E. McKenney if (WARN_ONCE(cpu < 0 || cpu >= nr_cpu_ids, "%s: cpu = %d\n", __func__, cpu))
24535feb604SPaul E. McKenney cpux = 0;
24635feb604SPaul E. McKenney else
24735feb604SPaul E. McKenney cpux = cpu;
24835feb604SPaul E. McKenney cpu_cur_csd = smp_load_acquire(&per_cpu(cur_csd, cpux)); /* Before func and info. */
249f6cc3d85SRik van Riel /* How long since this CSD lock was stuck. */
250f6cc3d85SRik van Riel ts_delta = ts2 - ts0;
25135feb604SPaul E. McKenney pr_alert("csd: %s non-responsive CSD lock (#%d) on CPU#%d, waiting %llu ns for CPU#%02d %pS(%ps).\n",
252f6cc3d85SRik van Riel firsttime ? "Detected" : "Continued", *bug_id, raw_smp_processor_id(), ts_delta,
25335feb604SPaul E. McKenney cpu, csd->func, csd->info);
254f6cc3d85SRik van Riel /*
255f6cc3d85SRik van Riel * If the CSD lock is still stuck after 5 minutes, it is unlikely
256f6cc3d85SRik van Riel * to become unstuck. Use a signed comparison to avoid triggering
257f6cc3d85SRik van Riel * on underflows when the TSC is out of sync between sockets.
258f6cc3d85SRik van Riel */
259f6cc3d85SRik van Riel BUG_ON(panic_on_ipistall > 0 && (s64)ts_delta > ((s64)panic_on_ipistall * NSEC_PER_MSEC));
26035feb604SPaul E. McKenney if (cpu_cur_csd && csd != cpu_cur_csd) {
26135feb604SPaul E. McKenney pr_alert("\tcsd: CSD lock (#%d) handling prior %pS(%ps) request.\n",
26235feb604SPaul E. McKenney *bug_id, READ_ONCE(per_cpu(cur_csd_func, cpux)),
26335feb604SPaul E. McKenney READ_ONCE(per_cpu(cur_csd_info, cpux)));
26435feb604SPaul E. McKenney } else {
26535feb604SPaul E. McKenney pr_alert("\tcsd: CSD lock (#%d) %s.\n",
26635feb604SPaul E. McKenney *bug_id, !cpu_cur_csd ? "unresponsive" : "handling this request");
26735feb604SPaul E. McKenney }
26835feb604SPaul E. McKenney if (cpu >= 0) {
2690d3a00b3SImran Khan if (atomic_cmpxchg_acquire(&per_cpu(trigger_backtrace, cpu), 1, 0))
27035feb604SPaul E. McKenney dump_cpu_task(cpu);
27135feb604SPaul E. McKenney if (!cpu_cur_csd) {
27235feb604SPaul E. McKenney pr_alert("csd: Re-sending CSD lock (#%d) IPI from CPU#%02d to CPU#%02d\n", *bug_id, raw_smp_processor_id(), cpu);
27335feb604SPaul E. McKenney arch_send_call_function_single_ipi(cpu);
27435feb604SPaul E. McKenney }
27535feb604SPaul E. McKenney }
2765bd00f6dSImran Khan if (firsttime)
27735feb604SPaul E. McKenney dump_stack();
27835feb604SPaul E. McKenney *ts1 = ts2;
27935feb604SPaul E. McKenney
28035feb604SPaul E. McKenney return false;
28135feb604SPaul E. McKenney }
28235feb604SPaul E. McKenney
2838969a5edSPeter Zijlstra /*
2848969a5edSPeter Zijlstra * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
2858969a5edSPeter Zijlstra *
2860b13fda1SIngo Molnar * For non-synchronous ipi calls the csd can still be in use by the
2870b13fda1SIngo Molnar * previous function call. For multi-cpu calls its even more interesting
2880b13fda1SIngo Molnar * as we'll have to ensure no other cpu is observing our csd.
2898969a5edSPeter Zijlstra */
__csd_lock_wait(struct __call_single_data * csd)2901139aeb1SArnd Bergmann static void __csd_lock_wait(struct __call_single_data *csd)
2918969a5edSPeter Zijlstra {
29235feb604SPaul E. McKenney int bug_id = 0;
29335feb604SPaul E. McKenney u64 ts0, ts1;
29435feb604SPaul E. McKenney
29535feb604SPaul E. McKenney ts1 = ts0 = sched_clock();
29635feb604SPaul E. McKenney for (;;) {
29735feb604SPaul E. McKenney if (csd_lock_wait_toolong(csd, ts0, &ts1, &bug_id))
29835feb604SPaul E. McKenney break;
29935feb604SPaul E. McKenney cpu_relax();
30035feb604SPaul E. McKenney }
30135feb604SPaul E. McKenney smp_acquire__after_ctrl_dep();
30235feb604SPaul E. McKenney }
30335feb604SPaul E. McKenney
csd_lock_wait(struct __call_single_data * csd)3041139aeb1SArnd Bergmann static __always_inline void csd_lock_wait(struct __call_single_data *csd)
3058d0968ccSJuergen Gross {
3068d0968ccSJuergen Gross if (static_branch_unlikely(&csdlock_debug_enabled)) {
3078d0968ccSJuergen Gross __csd_lock_wait(csd);
3088d0968ccSJuergen Gross return;
3098d0968ccSJuergen Gross }
3108d0968ccSJuergen Gross
3118d0968ccSJuergen Gross smp_cond_load_acquire(&csd->node.u_flags, !(VAL & CSD_FLAG_LOCK));
3128d0968ccSJuergen Gross }
31335feb604SPaul E. McKenney #else
csd_lock_record(struct __call_single_data * csd)3141139aeb1SArnd Bergmann static void csd_lock_record(struct __call_single_data *csd)
31535feb604SPaul E. McKenney {
31635feb604SPaul E. McKenney }
31735feb604SPaul E. McKenney
csd_lock_wait(struct __call_single_data * csd)3181139aeb1SArnd Bergmann static __always_inline void csd_lock_wait(struct __call_single_data *csd)
31935feb604SPaul E. McKenney {
320545b8c8dSPeter Zijlstra smp_cond_load_acquire(&csd->node.u_flags, !(VAL & CSD_FLAG_LOCK));
3216e275637SPeter Zijlstra }
32235feb604SPaul E. McKenney #endif
3236e275637SPeter Zijlstra
csd_lock(struct __call_single_data * csd)3241139aeb1SArnd Bergmann static __always_inline void csd_lock(struct __call_single_data *csd)
3256e275637SPeter Zijlstra {
326e1d12f32SAndrew Morton csd_lock_wait(csd);
327545b8c8dSPeter Zijlstra csd->node.u_flags |= CSD_FLAG_LOCK;
3288969a5edSPeter Zijlstra
3298969a5edSPeter Zijlstra /*
3300b13fda1SIngo Molnar * prevent CPU from reordering the above assignment
3310b13fda1SIngo Molnar * to ->flags with any subsequent assignments to other
332966a9671SYing Huang * fields of the specified call_single_data_t structure:
3338969a5edSPeter Zijlstra */
3348053871dSLinus Torvalds smp_wmb();
3358969a5edSPeter Zijlstra }
3368969a5edSPeter Zijlstra
csd_unlock(struct __call_single_data * csd)3371139aeb1SArnd Bergmann static __always_inline void csd_unlock(struct __call_single_data *csd)
3388969a5edSPeter Zijlstra {
339545b8c8dSPeter Zijlstra WARN_ON(!(csd->node.u_flags & CSD_FLAG_LOCK));
3400b13fda1SIngo Molnar
3418969a5edSPeter Zijlstra /*
3420b13fda1SIngo Molnar * ensure we're all done before releasing data:
3438969a5edSPeter Zijlstra */
344545b8c8dSPeter Zijlstra smp_store_release(&csd->node.u_flags, 0);
3453d442233SJens Axboe }
3463d442233SJens Axboe
34768f4ff04SValentin Schneider static DEFINE_PER_CPU_SHARED_ALIGNED(call_single_data_t, csd_data);
34868f4ff04SValentin Schneider
__smp_call_single_queue(int cpu,struct llist_node * node)34968f4ff04SValentin Schneider void __smp_call_single_queue(int cpu, struct llist_node *node)
35068f4ff04SValentin Schneider {
35168f4ff04SValentin Schneider /*
35268f4ff04SValentin Schneider * We have to check the type of the CSD before queueing it, because
35368f4ff04SValentin Schneider * once queued it can have its flags cleared by
35468f4ff04SValentin Schneider * flush_smp_call_function_queue()
35568f4ff04SValentin Schneider * even if we haven't sent the smp_call IPI yet (e.g. the stopper
35668f4ff04SValentin Schneider * executes migration_cpu_stop() on the remote CPU).
35768f4ff04SValentin Schneider */
358bf5a8c26SLeonardo Bras if (trace_csd_queue_cpu_enabled()) {
35968f4ff04SValentin Schneider call_single_data_t *csd;
36068f4ff04SValentin Schneider smp_call_func_t func;
36168f4ff04SValentin Schneider
36268f4ff04SValentin Schneider csd = container_of(node, call_single_data_t, node.llist);
36368f4ff04SValentin Schneider func = CSD_TYPE(csd) == CSD_TYPE_TTWU ?
36468f4ff04SValentin Schneider sched_ttwu_pending : csd->func;
36568f4ff04SValentin Schneider
366bf5a8c26SLeonardo Bras trace_csd_queue_cpu(cpu, _RET_IP_, func, csd);
36768f4ff04SValentin Schneider }
3685c312497SPeter Zijlstra
3695c312497SPeter Zijlstra /*
3705c312497SPeter Zijlstra * The list addition should be visible to the target CPU when it pops
3715c312497SPeter Zijlstra * the head of the list to pull the entry off it in the IPI handler
3725c312497SPeter Zijlstra * because of normal cache coherency rules implied by the underlying
3735c312497SPeter Zijlstra * llist ops.
3745c312497SPeter Zijlstra *
3755c312497SPeter Zijlstra * If IPIs can go out of order to the cache coherency protocol
3765c312497SPeter Zijlstra * in an architecture, sufficient synchronisation should be added
3775c312497SPeter Zijlstra * to arch code to make it appear to obey cache coherency WRT
3785c312497SPeter Zijlstra * locking and barrier primitives. Generic code isn't really
3795c312497SPeter Zijlstra * equipped to do the right thing...
3805c312497SPeter Zijlstra */
3815c312497SPeter Zijlstra if (llist_add(node, &per_cpu(call_single_queue, cpu)))
3825c312497SPeter Zijlstra send_call_function_single_ipi(cpu);
3834b44a21dSPeter Zijlstra }
3844b44a21dSPeter Zijlstra
3853d442233SJens Axboe /*
386966a9671SYing Huang * Insert a previously allocated call_single_data_t element
3870b13fda1SIngo Molnar * for execution on the given CPU. data must already have
3880b13fda1SIngo Molnar * ->func, ->info, and ->flags set.
3893d442233SJens Axboe */
generic_exec_single(int cpu,struct __call_single_data * csd)3901139aeb1SArnd Bergmann static int generic_exec_single(int cpu, struct __call_single_data *csd)
3913d442233SJens Axboe {
3928053871dSLinus Torvalds if (cpu == smp_processor_id()) {
3934b44a21dSPeter Zijlstra smp_call_func_t func = csd->func;
3944b44a21dSPeter Zijlstra void *info = csd->info;
3958b28499aSFrederic Weisbecker unsigned long flags;
3968b28499aSFrederic Weisbecker
3978053871dSLinus Torvalds /*
3988053871dSLinus Torvalds * We can unlock early even for the synchronous on-stack case,
3998053871dSLinus Torvalds * since we're doing this from the same CPU..
4008053871dSLinus Torvalds */
40135feb604SPaul E. McKenney csd_lock_record(csd);
4028053871dSLinus Torvalds csd_unlock(csd);
4038b28499aSFrederic Weisbecker local_irq_save(flags);
404949fa3f1SLeonardo Bras csd_do_func(func, info, NULL);
40535feb604SPaul E. McKenney csd_lock_record(NULL);
4068b28499aSFrederic Weisbecker local_irq_restore(flags);
4078b28499aSFrederic Weisbecker return 0;
4088b28499aSFrederic Weisbecker }
4098b28499aSFrederic Weisbecker
4105224b961SLinus Torvalds if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu)) {
4115224b961SLinus Torvalds csd_unlock(csd);
4128b28499aSFrederic Weisbecker return -ENXIO;
4135224b961SLinus Torvalds }
4148b28499aSFrederic Weisbecker
415545b8c8dSPeter Zijlstra __smp_call_single_queue(cpu, &csd->node.llist);
4163d442233SJens Axboe
4178b28499aSFrederic Weisbecker return 0;
4183d442233SJens Axboe }
4193d442233SJens Axboe
4208d056c48SSrivatsa S. Bhat /**
4218d056c48SSrivatsa S. Bhat * generic_smp_call_function_single_interrupt - Execute SMP IPI callbacks
4228d056c48SSrivatsa S. Bhat *
4238d056c48SSrivatsa S. Bhat * Invoked by arch to handle an IPI for call function single.
4248d056c48SSrivatsa S. Bhat * Must be called with interrupts disabled.
4253d442233SJens Axboe */
generic_smp_call_function_single_interrupt(void)4263d442233SJens Axboe void generic_smp_call_function_single_interrupt(void)
4273d442233SJens Axboe {
42816bf5a5eSThomas Gleixner __flush_smp_call_function_queue(true);
4298d056c48SSrivatsa S. Bhat }
4308d056c48SSrivatsa S. Bhat
4318d056c48SSrivatsa S. Bhat /**
43216bf5a5eSThomas Gleixner * __flush_smp_call_function_queue - Flush pending smp-call-function callbacks
4338d056c48SSrivatsa S. Bhat *
4348d056c48SSrivatsa S. Bhat * @warn_cpu_offline: If set to 'true', warn if callbacks were queued on an
4358d056c48SSrivatsa S. Bhat * offline CPU. Skip this check if set to 'false'.
4368d056c48SSrivatsa S. Bhat *
4378d056c48SSrivatsa S. Bhat * Flush any pending smp-call-function callbacks queued on this CPU. This is
4388d056c48SSrivatsa S. Bhat * invoked by the generic IPI handler, as well as by a CPU about to go offline,
4398d056c48SSrivatsa S. Bhat * to ensure that all pending IPI callbacks are run before it goes completely
4408d056c48SSrivatsa S. Bhat * offline.
4418d056c48SSrivatsa S. Bhat *
4428d056c48SSrivatsa S. Bhat * Loop through the call_single_queue and run all the queued callbacks.
4438d056c48SSrivatsa S. Bhat * Must be called with interrupts disabled.
4448d056c48SSrivatsa S. Bhat */
__flush_smp_call_function_queue(bool warn_cpu_offline)44516bf5a5eSThomas Gleixner static void __flush_smp_call_function_queue(bool warn_cpu_offline)
4468d056c48SSrivatsa S. Bhat {
447966a9671SYing Huang call_single_data_t *csd, *csd_next;
44852103be0SPeter Zijlstra struct llist_node *entry, *prev;
44952103be0SPeter Zijlstra struct llist_head *head;
450a219ccf4SSrivatsa S. Bhat static bool warned;
4510d3a00b3SImran Khan atomic_t *tbt;
452a219ccf4SSrivatsa S. Bhat
45383efcbd0SFrederic Weisbecker lockdep_assert_irqs_disabled();
4548d056c48SSrivatsa S. Bhat
4550d3a00b3SImran Khan /* Allow waiters to send backtrace NMI from here onwards */
4560d3a00b3SImran Khan tbt = this_cpu_ptr(&trigger_backtrace);
4570d3a00b3SImran Khan atomic_set_release(tbt, 1);
4580d3a00b3SImran Khan
459bb964a92SChristoph Lameter head = this_cpu_ptr(&call_single_queue);
4608d056c48SSrivatsa S. Bhat entry = llist_del_all(head);
461a219ccf4SSrivatsa S. Bhat entry = llist_reverse_order(entry);
4623d442233SJens Axboe
4638d056c48SSrivatsa S. Bhat /* There shouldn't be any pending callbacks on an offline CPU. */
4648d056c48SSrivatsa S. Bhat if (unlikely(warn_cpu_offline && !cpu_online(smp_processor_id()) &&
4659e949a38SNadav Amit !warned && entry != NULL)) {
466a219ccf4SSrivatsa S. Bhat warned = true;
467a219ccf4SSrivatsa S. Bhat WARN(1, "IPI on offline CPU %d\n", smp_processor_id());
468269c861bSSuresh Siddha
469a219ccf4SSrivatsa S. Bhat /*
470a219ccf4SSrivatsa S. Bhat * We don't have to use the _safe() variant here
471a219ccf4SSrivatsa S. Bhat * because we are not invoking the IPI handlers yet.
472a219ccf4SSrivatsa S. Bhat */
473545b8c8dSPeter Zijlstra llist_for_each_entry(csd, entry, node.llist) {
4744b44a21dSPeter Zijlstra switch (CSD_TYPE(csd)) {
4754b44a21dSPeter Zijlstra case CSD_TYPE_ASYNC:
4764b44a21dSPeter Zijlstra case CSD_TYPE_SYNC:
4774b44a21dSPeter Zijlstra case CSD_TYPE_IRQ_WORK:
478a219ccf4SSrivatsa S. Bhat pr_warn("IPI callback %pS sent to offline CPU\n",
479a219ccf4SSrivatsa S. Bhat csd->func);
4804b44a21dSPeter Zijlstra break;
4814b44a21dSPeter Zijlstra
482a1488664SPeter Zijlstra case CSD_TYPE_TTWU:
483a1488664SPeter Zijlstra pr_warn("IPI task-wakeup sent to offline CPU\n");
484a1488664SPeter Zijlstra break;
485a1488664SPeter Zijlstra
4864b44a21dSPeter Zijlstra default:
4874b44a21dSPeter Zijlstra pr_warn("IPI callback, unknown type %d, sent to offline CPU\n",
4884b44a21dSPeter Zijlstra CSD_TYPE(csd));
4894b44a21dSPeter Zijlstra break;
490a219ccf4SSrivatsa S. Bhat }
4918053871dSLinus Torvalds }
4923d442233SJens Axboe }
49347885016SFrederic Weisbecker
49447885016SFrederic Weisbecker /*
49552103be0SPeter Zijlstra * First; run all SYNC callbacks, people are waiting for us.
49647885016SFrederic Weisbecker */
49752103be0SPeter Zijlstra prev = NULL;
498545b8c8dSPeter Zijlstra llist_for_each_entry_safe(csd, csd_next, entry, node.llist) {
4994b44a21dSPeter Zijlstra /* Do we wait until *after* callback? */
5004b44a21dSPeter Zijlstra if (CSD_TYPE(csd) == CSD_TYPE_SYNC) {
5013d442233SJens Axboe smp_call_func_t func = csd->func;
5023d442233SJens Axboe void *info = csd->info;
5033d442233SJens Axboe
50452103be0SPeter Zijlstra if (prev) {
505545b8c8dSPeter Zijlstra prev->next = &csd_next->node.llist;
50652103be0SPeter Zijlstra } else {
507545b8c8dSPeter Zijlstra entry = &csd_next->node.llist;
50852103be0SPeter Zijlstra }
5094b44a21dSPeter Zijlstra
51035feb604SPaul E. McKenney csd_lock_record(csd);
511949fa3f1SLeonardo Bras csd_do_func(func, info, csd);
5123d442233SJens Axboe csd_unlock(csd);
51335feb604SPaul E. McKenney csd_lock_record(NULL);
5143d442233SJens Axboe } else {
515545b8c8dSPeter Zijlstra prev = &csd->node.llist;
51652103be0SPeter Zijlstra }
51752103be0SPeter Zijlstra }
51852103be0SPeter Zijlstra
5191771257cSPaul E. McKenney if (!entry)
520a1488664SPeter Zijlstra return;
521a1488664SPeter Zijlstra
52252103be0SPeter Zijlstra /*
52352103be0SPeter Zijlstra * Second; run all !SYNC callbacks.
52452103be0SPeter Zijlstra */
525a1488664SPeter Zijlstra prev = NULL;
526545b8c8dSPeter Zijlstra llist_for_each_entry_safe(csd, csd_next, entry, node.llist) {
5274b44a21dSPeter Zijlstra int type = CSD_TYPE(csd);
5284b44a21dSPeter Zijlstra
529a1488664SPeter Zijlstra if (type != CSD_TYPE_TTWU) {
530a1488664SPeter Zijlstra if (prev) {
531545b8c8dSPeter Zijlstra prev->next = &csd_next->node.llist;
532a1488664SPeter Zijlstra } else {
533545b8c8dSPeter Zijlstra entry = &csd_next->node.llist;
534a1488664SPeter Zijlstra }
535a1488664SPeter Zijlstra
5364b44a21dSPeter Zijlstra if (type == CSD_TYPE_ASYNC) {
53752103be0SPeter Zijlstra smp_call_func_t func = csd->func;
53852103be0SPeter Zijlstra void *info = csd->info;
53952103be0SPeter Zijlstra
54035feb604SPaul E. McKenney csd_lock_record(csd);
5413d442233SJens Axboe csd_unlock(csd);
542949fa3f1SLeonardo Bras csd_do_func(func, info, csd);
54335feb604SPaul E. McKenney csd_lock_record(NULL);
5444b44a21dSPeter Zijlstra } else if (type == CSD_TYPE_IRQ_WORK) {
5454b44a21dSPeter Zijlstra irq_work_single(csd);
5464b44a21dSPeter Zijlstra }
547a1488664SPeter Zijlstra
548a1488664SPeter Zijlstra } else {
549545b8c8dSPeter Zijlstra prev = &csd->node.llist;
5503d442233SJens Axboe }
5513d442233SJens Axboe }
5523d442233SJens Axboe
553a1488664SPeter Zijlstra /*
554a1488664SPeter Zijlstra * Third; only CSD_TYPE_TTWU is left, issue those.
555a1488664SPeter Zijlstra */
556949fa3f1SLeonardo Bras if (entry) {
557949fa3f1SLeonardo Bras csd = llist_entry(entry, typeof(*csd), node.llist);
558949fa3f1SLeonardo Bras csd_do_func(sched_ttwu_pending, entry, csd);
559949fa3f1SLeonardo Bras }
560a1488664SPeter Zijlstra }
561a1488664SPeter Zijlstra
56216bf5a5eSThomas Gleixner
56316bf5a5eSThomas Gleixner /**
56416bf5a5eSThomas Gleixner * flush_smp_call_function_queue - Flush pending smp-call-function callbacks
56516bf5a5eSThomas Gleixner * from task context (idle, migration thread)
56616bf5a5eSThomas Gleixner *
56716bf5a5eSThomas Gleixner * When TIF_POLLING_NRFLAG is supported and a CPU is in idle and has it
56816bf5a5eSThomas Gleixner * set, then remote CPUs can avoid sending IPIs and wake the idle CPU by
56916bf5a5eSThomas Gleixner * setting TIF_NEED_RESCHED. The idle task on the woken up CPU has to
57016bf5a5eSThomas Gleixner * handle queued SMP function calls before scheduling.
57116bf5a5eSThomas Gleixner *
57216bf5a5eSThomas Gleixner * The migration thread has to ensure that an eventually pending wakeup has
57316bf5a5eSThomas Gleixner * been handled before it migrates a task.
57416bf5a5eSThomas Gleixner */
flush_smp_call_function_queue(void)57516bf5a5eSThomas Gleixner void flush_smp_call_function_queue(void)
576b2a02fc4SPeter Zijlstra {
5771a90bfd2SSebastian Andrzej Siewior unsigned int was_pending;
578b2a02fc4SPeter Zijlstra unsigned long flags;
579b2a02fc4SPeter Zijlstra
580b2a02fc4SPeter Zijlstra if (llist_empty(this_cpu_ptr(&call_single_queue)))
581b2a02fc4SPeter Zijlstra return;
582b2a02fc4SPeter Zijlstra
583b2a02fc4SPeter Zijlstra local_irq_save(flags);
5841a90bfd2SSebastian Andrzej Siewior /* Get the already pending soft interrupts for RT enabled kernels */
5851a90bfd2SSebastian Andrzej Siewior was_pending = local_softirq_pending();
58616bf5a5eSThomas Gleixner __flush_smp_call_function_queue(true);
587f9d34595SSebastian Andrzej Siewior if (local_softirq_pending())
5881a90bfd2SSebastian Andrzej Siewior do_softirq_post_smp_call_flush(was_pending);
589f9d34595SSebastian Andrzej Siewior
590b2a02fc4SPeter Zijlstra local_irq_restore(flags);
5913d442233SJens Axboe }
5923d442233SJens Axboe
5933d442233SJens Axboe /*
5943d442233SJens Axboe * smp_call_function_single - Run a function on a specific CPU
5953d442233SJens Axboe * @func: The function to run. This must be fast and non-blocking.
5963d442233SJens Axboe * @info: An arbitrary pointer to pass to the function.
5973d442233SJens Axboe * @wait: If true, wait until function has completed on other CPUs.
5983d442233SJens Axboe *
59972f279b2SSheng Yang * Returns 0 on success, else a negative status code.
6003d442233SJens Axboe */
smp_call_function_single(int cpu,smp_call_func_t func,void * info,int wait)6013a5f65dfSDavid Howells int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
6028691e5a8SJens Axboe int wait)
6033d442233SJens Axboe {
604966a9671SYing Huang call_single_data_t *csd;
605966a9671SYing Huang call_single_data_t csd_stack = {
606545b8c8dSPeter Zijlstra .node = { .u_flags = CSD_FLAG_LOCK | CSD_TYPE_SYNC, },
607966a9671SYing Huang };
6080b13fda1SIngo Molnar int this_cpu;
6098b28499aSFrederic Weisbecker int err;
6103d442233SJens Axboe
6110b13fda1SIngo Molnar /*
6120b13fda1SIngo Molnar * prevent preemption and reschedule on another processor,
6130b13fda1SIngo Molnar * as well as CPU removal
6140b13fda1SIngo Molnar */
6150b13fda1SIngo Molnar this_cpu = get_cpu();
6160b13fda1SIngo Molnar
617269c861bSSuresh Siddha /*
618269c861bSSuresh Siddha * Can deadlock when called with interrupts disabled.
619269c861bSSuresh Siddha * We allow cpu's that are not yet online though, as no one else can
620269c861bSSuresh Siddha * send smp call function interrupt to this cpu and as such deadlocks
621269c861bSSuresh Siddha * can't happen.
622269c861bSSuresh Siddha */
623269c861bSSuresh Siddha WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
624269c861bSSuresh Siddha && !oops_in_progress);
6253d442233SJens Axboe
62619dbdcb8SPeter Zijlstra /*
62719dbdcb8SPeter Zijlstra * When @wait we can deadlock when we interrupt between llist_add() and
62819dbdcb8SPeter Zijlstra * arch_send_call_function_ipi*(); when !@wait we can deadlock due to
62919dbdcb8SPeter Zijlstra * csd_lock() on because the interrupt context uses the same csd
63019dbdcb8SPeter Zijlstra * storage.
63119dbdcb8SPeter Zijlstra */
63219dbdcb8SPeter Zijlstra WARN_ON_ONCE(!in_task());
63319dbdcb8SPeter Zijlstra
6348053871dSLinus Torvalds csd = &csd_stack;
6358053871dSLinus Torvalds if (!wait) {
6368053871dSLinus Torvalds csd = this_cpu_ptr(&csd_data);
6378053871dSLinus Torvalds csd_lock(csd);
6388053871dSLinus Torvalds }
6398053871dSLinus Torvalds
6404b44a21dSPeter Zijlstra csd->func = func;
6414b44a21dSPeter Zijlstra csd->info = info;
64235feb604SPaul E. McKenney #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
643545b8c8dSPeter Zijlstra csd->node.src = smp_processor_id();
644545b8c8dSPeter Zijlstra csd->node.dst = cpu;
645e48c15b7SPaul E. McKenney #endif
6464b44a21dSPeter Zijlstra
6474b44a21dSPeter Zijlstra err = generic_exec_single(cpu, csd);
6488053871dSLinus Torvalds
6498053871dSLinus Torvalds if (wait)
6508053871dSLinus Torvalds csd_lock_wait(csd);
6513d442233SJens Axboe
6523d442233SJens Axboe put_cpu();
6530b13fda1SIngo Molnar
654f73be6deSH. Peter Anvin return err;
6553d442233SJens Axboe }
6563d442233SJens Axboe EXPORT_SYMBOL(smp_call_function_single);
6573d442233SJens Axboe
658d7877c03SFrederic Weisbecker /**
65949b3bd21SRandy Dunlap * smp_call_function_single_async() - Run an asynchronous function on a
660c46fff2aSFrederic Weisbecker * specific CPU.
661d7877c03SFrederic Weisbecker * @cpu: The CPU to run on.
662d7877c03SFrederic Weisbecker * @csd: Pre-allocated and setup data structure
663d7877c03SFrederic Weisbecker *
664c46fff2aSFrederic Weisbecker * Like smp_call_function_single(), but the call is asynchonous and
665c46fff2aSFrederic Weisbecker * can thus be done from contexts with disabled interrupts.
666c46fff2aSFrederic Weisbecker *
667c46fff2aSFrederic Weisbecker * The caller passes his own pre-allocated data structure
668c46fff2aSFrederic Weisbecker * (ie: embedded in an object) and is responsible for synchronizing it
669c46fff2aSFrederic Weisbecker * such that the IPIs performed on the @csd are strictly serialized.
670c46fff2aSFrederic Weisbecker *
6715a18cecaSPeter Xu * If the function is called with one csd which has not yet been
6725a18cecaSPeter Xu * processed by previous call to smp_call_function_single_async(), the
6735a18cecaSPeter Xu * function will return immediately with -EBUSY showing that the csd
6745a18cecaSPeter Xu * object is still in progress.
6755a18cecaSPeter Xu *
676c46fff2aSFrederic Weisbecker * NOTE: Be careful, there is unfortunately no current debugging facility to
677c46fff2aSFrederic Weisbecker * validate the correctness of this serialization.
67849b3bd21SRandy Dunlap *
67949b3bd21SRandy Dunlap * Return: %0 on success or negative errno value on error
680d7877c03SFrederic Weisbecker */
smp_call_function_single_async(int cpu,struct __call_single_data * csd)6811139aeb1SArnd Bergmann int smp_call_function_single_async(int cpu, struct __call_single_data *csd)
682d7877c03SFrederic Weisbecker {
683d7877c03SFrederic Weisbecker int err = 0;
684d7877c03SFrederic Weisbecker
685fce8ad15SFrederic Weisbecker preempt_disable();
6868053871dSLinus Torvalds
687545b8c8dSPeter Zijlstra if (csd->node.u_flags & CSD_FLAG_LOCK) {
6885a18cecaSPeter Xu err = -EBUSY;
6895a18cecaSPeter Xu goto out;
6905a18cecaSPeter Xu }
6918053871dSLinus Torvalds
692545b8c8dSPeter Zijlstra csd->node.u_flags = CSD_FLAG_LOCK;
6938053871dSLinus Torvalds smp_wmb();
6948053871dSLinus Torvalds
6954b44a21dSPeter Zijlstra err = generic_exec_single(cpu, csd);
6965a18cecaSPeter Xu
6975a18cecaSPeter Xu out:
698fce8ad15SFrederic Weisbecker preempt_enable();
699d7877c03SFrederic Weisbecker
700d7877c03SFrederic Weisbecker return err;
701d7877c03SFrederic Weisbecker }
702c46fff2aSFrederic Weisbecker EXPORT_SYMBOL_GPL(smp_call_function_single_async);
703d7877c03SFrederic Weisbecker
7042ea6dec4SRusty Russell /*
7052ea6dec4SRusty Russell * smp_call_function_any - Run a function on any of the given cpus
7062ea6dec4SRusty Russell * @mask: The mask of cpus it can run on.
7072ea6dec4SRusty Russell * @func: The function to run. This must be fast and non-blocking.
7082ea6dec4SRusty Russell * @info: An arbitrary pointer to pass to the function.
7092ea6dec4SRusty Russell * @wait: If true, wait until function has completed.
7102ea6dec4SRusty Russell *
7112ea6dec4SRusty Russell * Returns 0 on success, else a negative status code (if no cpus were online).
7122ea6dec4SRusty Russell *
7132ea6dec4SRusty Russell * Selection preference:
7142ea6dec4SRusty Russell * 1) current cpu if in @mask
7152ea6dec4SRusty Russell * 2) any cpu of current node if in @mask
7162ea6dec4SRusty Russell * 3) any other online cpu in @mask
7172ea6dec4SRusty Russell */
smp_call_function_any(const struct cpumask * mask,smp_call_func_t func,void * info,int wait)7182ea6dec4SRusty Russell int smp_call_function_any(const struct cpumask *mask,
7193a5f65dfSDavid Howells smp_call_func_t func, void *info, int wait)
7202ea6dec4SRusty Russell {
7212ea6dec4SRusty Russell unsigned int cpu;
7222ea6dec4SRusty Russell const struct cpumask *nodemask;
7232ea6dec4SRusty Russell int ret;
7242ea6dec4SRusty Russell
7252ea6dec4SRusty Russell /* Try for same CPU (cheapest) */
7262ea6dec4SRusty Russell cpu = get_cpu();
7272ea6dec4SRusty Russell if (cpumask_test_cpu(cpu, mask))
7282ea6dec4SRusty Russell goto call;
7292ea6dec4SRusty Russell
7302ea6dec4SRusty Russell /* Try for same node. */
731af2422c4SDavid John nodemask = cpumask_of_node(cpu_to_node(cpu));
7322ea6dec4SRusty Russell for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
7332ea6dec4SRusty Russell cpu = cpumask_next_and(cpu, nodemask, mask)) {
7342ea6dec4SRusty Russell if (cpu_online(cpu))
7352ea6dec4SRusty Russell goto call;
7362ea6dec4SRusty Russell }
7372ea6dec4SRusty Russell
7382ea6dec4SRusty Russell /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
7392ea6dec4SRusty Russell cpu = cpumask_any_and(mask, cpu_online_mask);
7402ea6dec4SRusty Russell call:
7412ea6dec4SRusty Russell ret = smp_call_function_single(cpu, func, info, wait);
7422ea6dec4SRusty Russell put_cpu();
7432ea6dec4SRusty Russell return ret;
7442ea6dec4SRusty Russell }
7452ea6dec4SRusty Russell EXPORT_SYMBOL_GPL(smp_call_function_any);
7462ea6dec4SRusty Russell
747a32a4d8aSNadav Amit /*
748a32a4d8aSNadav Amit * Flags to be used as scf_flags argument of smp_call_function_many_cond().
749a32a4d8aSNadav Amit *
750a32a4d8aSNadav Amit * %SCF_WAIT: Wait until function execution is completed
751a32a4d8aSNadav Amit * %SCF_RUN_LOCAL: Run also locally if local cpu is set in cpumask
752a32a4d8aSNadav Amit */
753a32a4d8aSNadav Amit #define SCF_WAIT (1U << 0)
754a32a4d8aSNadav Amit #define SCF_RUN_LOCAL (1U << 1)
755a32a4d8aSNadav Amit
smp_call_function_many_cond(const struct cpumask * mask,smp_call_func_t func,void * info,unsigned int scf_flags,smp_cond_func_t cond_func)75667719ef2SSebastian Andrzej Siewior static void smp_call_function_many_cond(const struct cpumask *mask,
75767719ef2SSebastian Andrzej Siewior smp_call_func_t func, void *info,
758a32a4d8aSNadav Amit unsigned int scf_flags,
759a32a4d8aSNadav Amit smp_cond_func_t cond_func)
7603d442233SJens Axboe {
761a32a4d8aSNadav Amit int cpu, last_cpu, this_cpu = smp_processor_id();
762e1d12f32SAndrew Morton struct call_function_data *cfd;
763a32a4d8aSNadav Amit bool wait = scf_flags & SCF_WAIT;
764bf5a8c26SLeonardo Bras int nr_cpus = 0;
765a32a4d8aSNadav Amit bool run_remote = false;
766a32a4d8aSNadav Amit bool run_local = false;
767a32a4d8aSNadav Amit
768a32a4d8aSNadav Amit lockdep_assert_preemption_disabled();
7693d442233SJens Axboe
770269c861bSSuresh Siddha /*
771269c861bSSuresh Siddha * Can deadlock when called with interrupts disabled.
772269c861bSSuresh Siddha * We allow cpu's that are not yet online though, as no one else can
773269c861bSSuresh Siddha * send smp call function interrupt to this cpu and as such deadlocks
774269c861bSSuresh Siddha * can't happen.
775269c861bSSuresh Siddha */
776a32a4d8aSNadav Amit if (cpu_online(this_cpu) && !oops_in_progress &&
777a32a4d8aSNadav Amit !early_boot_irqs_disabled)
778a32a4d8aSNadav Amit lockdep_assert_irqs_enabled();
7793d442233SJens Axboe
78019dbdcb8SPeter Zijlstra /*
78119dbdcb8SPeter Zijlstra * When @wait we can deadlock when we interrupt between llist_add() and
78219dbdcb8SPeter Zijlstra * arch_send_call_function_ipi*(); when !@wait we can deadlock due to
78319dbdcb8SPeter Zijlstra * csd_lock() on because the interrupt context uses the same csd
78419dbdcb8SPeter Zijlstra * storage.
78519dbdcb8SPeter Zijlstra */
78619dbdcb8SPeter Zijlstra WARN_ON_ONCE(!in_task());
78719dbdcb8SPeter Zijlstra
788a32a4d8aSNadav Amit /* Check if we need local execution. */
789a32a4d8aSNadav Amit if ((scf_flags & SCF_RUN_LOCAL) && cpumask_test_cpu(this_cpu, mask))
790a32a4d8aSNadav Amit run_local = true;
791a32a4d8aSNadav Amit
792a32a4d8aSNadav Amit /* Check if we need remote execution, i.e., any CPU excluding this one. */
79354b11e6dSRusty Russell cpu = cpumask_first_and(mask, cpu_online_mask);
7940b13fda1SIngo Molnar if (cpu == this_cpu)
79554b11e6dSRusty Russell cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
796a32a4d8aSNadav Amit if (cpu < nr_cpu_ids)
797a32a4d8aSNadav Amit run_remote = true;
7980b13fda1SIngo Molnar
799a32a4d8aSNadav Amit if (run_remote) {
800bb964a92SChristoph Lameter cfd = this_cpu_ptr(&cfd_data);
801e1d12f32SAndrew Morton cpumask_and(cfd->cpumask, mask, cpu_online_mask);
8026c8557bdSPeter Zijlstra __cpumask_clear_cpu(this_cpu, cfd->cpumask);
803723aae25SMilton Miller
8043fc5b3b6SAaron Lu cpumask_clear(cfd->cpumask_ipi);
805e1d12f32SAndrew Morton for_each_cpu(cpu, cfd->cpumask) {
8066366d062SPaul E. McKenney call_single_data_t *csd = per_cpu_ptr(cfd->csd, cpu);
8079a46ad6dSShaohua Li
8085c312497SPeter Zijlstra if (cond_func && !cond_func(cpu, info)) {
8095c312497SPeter Zijlstra __cpumask_clear_cpu(cpu, cfd->cpumask);
81067719ef2SSebastian Andrzej Siewior continue;
8115c312497SPeter Zijlstra }
81267719ef2SSebastian Andrzej Siewior
8139a46ad6dSShaohua Li csd_lock(csd);
8148053871dSLinus Torvalds if (wait)
815545b8c8dSPeter Zijlstra csd->node.u_flags |= CSD_TYPE_SYNC;
8169a46ad6dSShaohua Li csd->func = func;
8179a46ad6dSShaohua Li csd->info = info;
81835feb604SPaul E. McKenney #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
819545b8c8dSPeter Zijlstra csd->node.src = smp_processor_id();
820545b8c8dSPeter Zijlstra csd->node.dst = cpu;
821e48c15b7SPaul E. McKenney #endif
822bf5a8c26SLeonardo Bras trace_csd_queue_cpu(cpu, _RET_IP_, func, csd);
823bf5a8c26SLeonardo Bras
824a32a4d8aSNadav Amit if (llist_add(&csd->node.llist, &per_cpu(call_single_queue, cpu))) {
8256c8557bdSPeter Zijlstra __cpumask_set_cpu(cpu, cfd->cpumask_ipi);
826a32a4d8aSNadav Amit nr_cpus++;
827a32a4d8aSNadav Amit last_cpu = cpu;
828a32a4d8aSNadav Amit }
8299a46ad6dSShaohua Li }
830561920a0SSuresh Siddha
831a32a4d8aSNadav Amit /*
832a32a4d8aSNadav Amit * Choose the most efficient way to send an IPI. Note that the
833a32a4d8aSNadav Amit * number of CPUs might be zero due to concurrent changes to the
834a32a4d8aSNadav Amit * provided mask.
835a32a4d8aSNadav Amit */
836a32a4d8aSNadav Amit if (nr_cpus == 1)
8375c312497SPeter Zijlstra send_call_function_single_ipi(last_cpu);
838a32a4d8aSNadav Amit else if (likely(nr_cpus > 1))
8395c312497SPeter Zijlstra send_call_function_ipi_mask(cfd->cpumask_ipi);
840a32a4d8aSNadav Amit }
8413d442233SJens Axboe
842a32a4d8aSNadav Amit if (run_local && (!cond_func || cond_func(this_cpu, info))) {
843a32a4d8aSNadav Amit unsigned long flags;
844a32a4d8aSNadav Amit
845a32a4d8aSNadav Amit local_irq_save(flags);
846949fa3f1SLeonardo Bras csd_do_func(func, info, NULL);
847a32a4d8aSNadav Amit local_irq_restore(flags);
848a32a4d8aSNadav Amit }
849a32a4d8aSNadav Amit
850a32a4d8aSNadav Amit if (run_remote && wait) {
851e1d12f32SAndrew Morton for_each_cpu(cpu, cfd->cpumask) {
852966a9671SYing Huang call_single_data_t *csd;
853e1d12f32SAndrew Morton
8546366d062SPaul E. McKenney csd = per_cpu_ptr(cfd->csd, cpu);
8559a46ad6dSShaohua Li csd_lock_wait(csd);
8569a46ad6dSShaohua Li }
8579a46ad6dSShaohua Li }
858cc7a486cSNick Piggin }
85967719ef2SSebastian Andrzej Siewior
86067719ef2SSebastian Andrzej Siewior /**
861a32a4d8aSNadav Amit * smp_call_function_many(): Run a function on a set of CPUs.
86267719ef2SSebastian Andrzej Siewior * @mask: The set of cpus to run on (only runs on online subset).
86367719ef2SSebastian Andrzej Siewior * @func: The function to run. This must be fast and non-blocking.
86467719ef2SSebastian Andrzej Siewior * @info: An arbitrary pointer to pass to the function.
86549b3bd21SRandy Dunlap * @wait: Bitmask that controls the operation. If %SCF_WAIT is set, wait
866a32a4d8aSNadav Amit * (atomically) until function has completed on other CPUs. If
867a32a4d8aSNadav Amit * %SCF_RUN_LOCAL is set, the function will also be run locally
868a32a4d8aSNadav Amit * if the local CPU is set in the @cpumask.
86967719ef2SSebastian Andrzej Siewior *
87067719ef2SSebastian Andrzej Siewior * If @wait is true, then returns once @func has returned.
87167719ef2SSebastian Andrzej Siewior *
87267719ef2SSebastian Andrzej Siewior * You must not call this function with disabled interrupts or from a
87367719ef2SSebastian Andrzej Siewior * hardware interrupt handler or from a bottom half handler. Preemption
87467719ef2SSebastian Andrzej Siewior * must be disabled when calling this function.
87567719ef2SSebastian Andrzej Siewior */
smp_call_function_many(const struct cpumask * mask,smp_call_func_t func,void * info,bool wait)87667719ef2SSebastian Andrzej Siewior void smp_call_function_many(const struct cpumask *mask,
87767719ef2SSebastian Andrzej Siewior smp_call_func_t func, void *info, bool wait)
87867719ef2SSebastian Andrzej Siewior {
879a32a4d8aSNadav Amit smp_call_function_many_cond(mask, func, info, wait * SCF_WAIT, NULL);
88067719ef2SSebastian Andrzej Siewior }
88154b11e6dSRusty Russell EXPORT_SYMBOL(smp_call_function_many);
8823d442233SJens Axboe
8833d442233SJens Axboe /**
8843d442233SJens Axboe * smp_call_function(): Run a function on all other CPUs.
8853d442233SJens Axboe * @func: The function to run. This must be fast and non-blocking.
8863d442233SJens Axboe * @info: An arbitrary pointer to pass to the function.
8870b13fda1SIngo Molnar * @wait: If true, wait (atomically) until function has completed
8880b13fda1SIngo Molnar * on other CPUs.
8893d442233SJens Axboe *
89054b11e6dSRusty Russell * Returns 0.
8913d442233SJens Axboe *
8923d442233SJens Axboe * If @wait is true, then returns once @func has returned; otherwise
89372f279b2SSheng Yang * it returns just before the target cpu calls @func.
8943d442233SJens Axboe *
8953d442233SJens Axboe * You must not call this function with disabled interrupts or from a
8963d442233SJens Axboe * hardware interrupt handler or from a bottom half handler.
8973d442233SJens Axboe */
smp_call_function(smp_call_func_t func,void * info,int wait)898caa75932SNadav Amit void smp_call_function(smp_call_func_t func, void *info, int wait)
8993d442233SJens Axboe {
9003d442233SJens Axboe preempt_disable();
90154b11e6dSRusty Russell smp_call_function_many(cpu_online_mask, func, info, wait);
9023d442233SJens Axboe preempt_enable();
9033d442233SJens Axboe }
9043d442233SJens Axboe EXPORT_SYMBOL(smp_call_function);
905351f8f8eSAmerigo Wang
90634db18a0SAmerigo Wang /* Setup configured maximum number of CPUs to activate */
90734db18a0SAmerigo Wang unsigned int setup_max_cpus = NR_CPUS;
90834db18a0SAmerigo Wang EXPORT_SYMBOL(setup_max_cpus);
90934db18a0SAmerigo Wang
91034db18a0SAmerigo Wang
91134db18a0SAmerigo Wang /*
91234db18a0SAmerigo Wang * Setup routine for controlling SMP activation
91334db18a0SAmerigo Wang *
91434db18a0SAmerigo Wang * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
91534db18a0SAmerigo Wang * activation entirely (the MPS table probe still happens, though).
91634db18a0SAmerigo Wang *
91734db18a0SAmerigo Wang * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
91834db18a0SAmerigo Wang * greater than 0, limits the maximum number of CPUs activated in
91934db18a0SAmerigo Wang * SMP mode to <NUM>.
92034db18a0SAmerigo Wang */
92134db18a0SAmerigo Wang
arch_disable_smp_support(void)922ba831b7bSThomas Gleixner void __weak __init arch_disable_smp_support(void) { }
92334db18a0SAmerigo Wang
nosmp(char * str)92434db18a0SAmerigo Wang static int __init nosmp(char *str)
92534db18a0SAmerigo Wang {
92634db18a0SAmerigo Wang setup_max_cpus = 0;
92734db18a0SAmerigo Wang arch_disable_smp_support();
92834db18a0SAmerigo Wang
92934db18a0SAmerigo Wang return 0;
93034db18a0SAmerigo Wang }
93134db18a0SAmerigo Wang
93234db18a0SAmerigo Wang early_param("nosmp", nosmp);
93334db18a0SAmerigo Wang
93434db18a0SAmerigo Wang /* this is hard limit */
nrcpus(char * str)93534db18a0SAmerigo Wang static int __init nrcpus(char *str)
93634db18a0SAmerigo Wang {
93734db18a0SAmerigo Wang int nr_cpus;
93834db18a0SAmerigo Wang
93958934356SMuchun Song if (get_option(&str, &nr_cpus) && nr_cpus > 0 && nr_cpus < nr_cpu_ids)
94038bef8e5SYury Norov set_nr_cpu_ids(nr_cpus);
94134db18a0SAmerigo Wang
94234db18a0SAmerigo Wang return 0;
94334db18a0SAmerigo Wang }
94434db18a0SAmerigo Wang
94534db18a0SAmerigo Wang early_param("nr_cpus", nrcpus);
94634db18a0SAmerigo Wang
maxcpus(char * str)94734db18a0SAmerigo Wang static int __init maxcpus(char *str)
94834db18a0SAmerigo Wang {
94934db18a0SAmerigo Wang get_option(&str, &setup_max_cpus);
95034db18a0SAmerigo Wang if (setup_max_cpus == 0)
95134db18a0SAmerigo Wang arch_disable_smp_support();
95234db18a0SAmerigo Wang
95334db18a0SAmerigo Wang return 0;
95434db18a0SAmerigo Wang }
95534db18a0SAmerigo Wang
95634db18a0SAmerigo Wang early_param("maxcpus", maxcpus);
95734db18a0SAmerigo Wang
9586f9c07beSYury Norov #if (NR_CPUS > 1) && !defined(CONFIG_FORCE_NR_CPUS)
95934db18a0SAmerigo Wang /* Setup number of possible processor ids */
9609b130ad5SAlexey Dobriyan unsigned int nr_cpu_ids __read_mostly = NR_CPUS;
96134db18a0SAmerigo Wang EXPORT_SYMBOL(nr_cpu_ids);
96253fc190cSYury Norov #endif
96334db18a0SAmerigo Wang
96434db18a0SAmerigo Wang /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
setup_nr_cpu_ids(void)96534db18a0SAmerigo Wang void __init setup_nr_cpu_ids(void)
96634db18a0SAmerigo Wang {
96738bef8e5SYury Norov set_nr_cpu_ids(find_last_bit(cpumask_bits(cpu_possible_mask), NR_CPUS) + 1);
96834db18a0SAmerigo Wang }
96934db18a0SAmerigo Wang
97034db18a0SAmerigo Wang /* Called by boot processor to activate the rest. */
smp_init(void)97134db18a0SAmerigo Wang void __init smp_init(void)
97234db18a0SAmerigo Wang {
97392b23278SMichael Ellerman int num_nodes, num_cpus;
97434db18a0SAmerigo Wang
9753bb5d2eeSSuresh Siddha idle_threads_init();
9764cb28cedSThomas Gleixner cpuhp_threads_init();
9773bb5d2eeSSuresh Siddha
97851111dceSMichael Ellerman pr_info("Bringing up secondary CPUs ...\n");
97951111dceSMichael Ellerman
980b99a2659SQais Yousef bringup_nonboot_cpus(setup_max_cpus);
98134db18a0SAmerigo Wang
98292b23278SMichael Ellerman num_nodes = num_online_nodes();
98392b23278SMichael Ellerman num_cpus = num_online_cpus();
98492b23278SMichael Ellerman pr_info("Brought up %d node%s, %d CPU%s\n",
98592b23278SMichael Ellerman num_nodes, (num_nodes > 1 ? "s" : ""),
98692b23278SMichael Ellerman num_cpus, (num_cpus > 1 ? "s" : ""));
98792b23278SMichael Ellerman
98834db18a0SAmerigo Wang /* Any cleanup work */
98934db18a0SAmerigo Wang smp_cpus_done(setup_max_cpus);
99034db18a0SAmerigo Wang }
99134db18a0SAmerigo Wang
992351f8f8eSAmerigo Wang /*
993b3a7e98eSGilad Ben-Yossef * on_each_cpu_cond(): Call a function on each processor for which
994b3a7e98eSGilad Ben-Yossef * the supplied function cond_func returns true, optionally waiting
995b3a7e98eSGilad Ben-Yossef * for all the required CPUs to finish. This may include the local
996b3a7e98eSGilad Ben-Yossef * processor.
997b3a7e98eSGilad Ben-Yossef * @cond_func: A callback function that is passed a cpu id and
9987b7b8a2cSRandy Dunlap * the info parameter. The function is called
999b3a7e98eSGilad Ben-Yossef * with preemption disabled. The function should
1000b3a7e98eSGilad Ben-Yossef * return a blooean value indicating whether to IPI
1001b3a7e98eSGilad Ben-Yossef * the specified CPU.
1002b3a7e98eSGilad Ben-Yossef * @func: The function to run on all applicable CPUs.
1003b3a7e98eSGilad Ben-Yossef * This must be fast and non-blocking.
1004b3a7e98eSGilad Ben-Yossef * @info: An arbitrary pointer to pass to both functions.
1005b3a7e98eSGilad Ben-Yossef * @wait: If true, wait (atomically) until function has
1006b3a7e98eSGilad Ben-Yossef * completed on other CPUs.
1007b3a7e98eSGilad Ben-Yossef *
1008b3a7e98eSGilad Ben-Yossef * Preemption is disabled to protect against CPUs going offline but not online.
1009b3a7e98eSGilad Ben-Yossef * CPUs going online during the call will not be seen or sent an IPI.
1010b3a7e98eSGilad Ben-Yossef *
1011b3a7e98eSGilad Ben-Yossef * You must not call this function with disabled interrupts or
1012b3a7e98eSGilad Ben-Yossef * from a hardware interrupt handler or from a bottom half handler.
1013b3a7e98eSGilad Ben-Yossef */
on_each_cpu_cond_mask(smp_cond_func_t cond_func,smp_call_func_t func,void * info,bool wait,const struct cpumask * mask)10145671d814SSebastian Andrzej Siewior void on_each_cpu_cond_mask(smp_cond_func_t cond_func, smp_call_func_t func,
1015cb923159SSebastian Andrzej Siewior void *info, bool wait, const struct cpumask *mask)
1016b3a7e98eSGilad Ben-Yossef {
1017a32a4d8aSNadav Amit unsigned int scf_flags = SCF_RUN_LOCAL;
1018b3a7e98eSGilad Ben-Yossef
1019a32a4d8aSNadav Amit if (wait)
1020a32a4d8aSNadav Amit scf_flags |= SCF_WAIT;
1021b3a7e98eSGilad Ben-Yossef
1022a32a4d8aSNadav Amit preempt_disable();
1023a32a4d8aSNadav Amit smp_call_function_many_cond(mask, func, info, scf_flags, cond_func);
1024a32a4d8aSNadav Amit preempt_enable();
1025b3a7e98eSGilad Ben-Yossef }
10267d49b28aSRik van Riel EXPORT_SYMBOL(on_each_cpu_cond_mask);
10277d49b28aSRik van Riel
do_nothing(void * unused)1028f37f435fSThomas Gleixner static void do_nothing(void *unused)
1029f37f435fSThomas Gleixner {
1030f37f435fSThomas Gleixner }
1031f37f435fSThomas Gleixner
1032f37f435fSThomas Gleixner /**
1033f37f435fSThomas Gleixner * kick_all_cpus_sync - Force all cpus out of idle
1034f37f435fSThomas Gleixner *
1035f37f435fSThomas Gleixner * Used to synchronize the update of pm_idle function pointer. It's
1036f37f435fSThomas Gleixner * called after the pointer is updated and returns after the dummy
1037f37f435fSThomas Gleixner * callback function has been executed on all cpus. The execution of
1038f37f435fSThomas Gleixner * the function can only happen on the remote cpus after they have
1039f37f435fSThomas Gleixner * left the idle function which had been called via pm_idle function
1040f37f435fSThomas Gleixner * pointer. So it's guaranteed that nothing uses the previous pointer
1041f37f435fSThomas Gleixner * anymore.
1042f37f435fSThomas Gleixner */
kick_all_cpus_sync(void)1043f37f435fSThomas Gleixner void kick_all_cpus_sync(void)
1044f37f435fSThomas Gleixner {
1045f37f435fSThomas Gleixner /* Make sure the change is visible before we kick the cpus */
1046f37f435fSThomas Gleixner smp_mb();
1047f37f435fSThomas Gleixner smp_call_function(do_nothing, NULL, 1);
1048f37f435fSThomas Gleixner }
1049f37f435fSThomas Gleixner EXPORT_SYMBOL_GPL(kick_all_cpus_sync);
1050c6f4459fSChuansheng Liu
1051c6f4459fSChuansheng Liu /**
1052c6f4459fSChuansheng Liu * wake_up_all_idle_cpus - break all cpus out of idle
1053c6f4459fSChuansheng Liu * wake_up_all_idle_cpus try to break all cpus which is in idle state even
1054c6f4459fSChuansheng Liu * including idle polling cpus, for non-idle cpus, we will do nothing
1055c6f4459fSChuansheng Liu * for them.
1056c6f4459fSChuansheng Liu */
wake_up_all_idle_cpus(void)1057c6f4459fSChuansheng Liu void wake_up_all_idle_cpus(void)
1058c6f4459fSChuansheng Liu {
1059c6f4459fSChuansheng Liu int cpu;
1060c6f4459fSChuansheng Liu
106196611c26SPeter Zijlstra for_each_possible_cpu(cpu) {
106296611c26SPeter Zijlstra preempt_disable();
106396611c26SPeter Zijlstra if (cpu != smp_processor_id() && cpu_online(cpu))
1064c6f4459fSChuansheng Liu wake_up_if_idle(cpu);
106596611c26SPeter Zijlstra preempt_enable();
1066c6f4459fSChuansheng Liu }
1067c6f4459fSChuansheng Liu }
1068c6f4459fSChuansheng Liu EXPORT_SYMBOL_GPL(wake_up_all_idle_cpus);
1069df8ce9d7SJuergen Gross
1070df8ce9d7SJuergen Gross /**
107149b3bd21SRandy Dunlap * struct smp_call_on_cpu_struct - Call a function on a specific CPU
107249b3bd21SRandy Dunlap * @work: &work_struct
107349b3bd21SRandy Dunlap * @done: &completion to signal
107449b3bd21SRandy Dunlap * @func: function to call
107549b3bd21SRandy Dunlap * @data: function's data argument
107649b3bd21SRandy Dunlap * @ret: return value from @func
107749b3bd21SRandy Dunlap * @cpu: target CPU (%-1 for any CPU)
1078df8ce9d7SJuergen Gross *
1079df8ce9d7SJuergen Gross * Used to call a function on a specific cpu and wait for it to return.
1080df8ce9d7SJuergen Gross * Optionally make sure the call is done on a specified physical cpu via vcpu
1081df8ce9d7SJuergen Gross * pinning in order to support virtualized environments.
1082df8ce9d7SJuergen Gross */
1083df8ce9d7SJuergen Gross struct smp_call_on_cpu_struct {
1084df8ce9d7SJuergen Gross struct work_struct work;
1085df8ce9d7SJuergen Gross struct completion done;
1086df8ce9d7SJuergen Gross int (*func)(void *);
1087df8ce9d7SJuergen Gross void *data;
1088df8ce9d7SJuergen Gross int ret;
1089df8ce9d7SJuergen Gross int cpu;
1090df8ce9d7SJuergen Gross };
1091df8ce9d7SJuergen Gross
smp_call_on_cpu_callback(struct work_struct * work)1092df8ce9d7SJuergen Gross static void smp_call_on_cpu_callback(struct work_struct *work)
1093df8ce9d7SJuergen Gross {
1094df8ce9d7SJuergen Gross struct smp_call_on_cpu_struct *sscs;
1095df8ce9d7SJuergen Gross
1096df8ce9d7SJuergen Gross sscs = container_of(work, struct smp_call_on_cpu_struct, work);
1097df8ce9d7SJuergen Gross if (sscs->cpu >= 0)
1098df8ce9d7SJuergen Gross hypervisor_pin_vcpu(sscs->cpu);
1099df8ce9d7SJuergen Gross sscs->ret = sscs->func(sscs->data);
1100df8ce9d7SJuergen Gross if (sscs->cpu >= 0)
1101df8ce9d7SJuergen Gross hypervisor_pin_vcpu(-1);
1102df8ce9d7SJuergen Gross
1103df8ce9d7SJuergen Gross complete(&sscs->done);
1104df8ce9d7SJuergen Gross }
1105df8ce9d7SJuergen Gross
smp_call_on_cpu(unsigned int cpu,int (* func)(void *),void * par,bool phys)1106df8ce9d7SJuergen Gross int smp_call_on_cpu(unsigned int cpu, int (*func)(void *), void *par, bool phys)
1107df8ce9d7SJuergen Gross {
1108df8ce9d7SJuergen Gross struct smp_call_on_cpu_struct sscs = {
1109df8ce9d7SJuergen Gross .done = COMPLETION_INITIALIZER_ONSTACK(sscs.done),
1110df8ce9d7SJuergen Gross .func = func,
1111df8ce9d7SJuergen Gross .data = par,
1112df8ce9d7SJuergen Gross .cpu = phys ? cpu : -1,
1113df8ce9d7SJuergen Gross };
1114df8ce9d7SJuergen Gross
11158db54949SPeter Zijlstra INIT_WORK_ONSTACK(&sscs.work, smp_call_on_cpu_callback);
11168db54949SPeter Zijlstra
1117df8ce9d7SJuergen Gross if (cpu >= nr_cpu_ids || !cpu_online(cpu))
1118df8ce9d7SJuergen Gross return -ENXIO;
1119df8ce9d7SJuergen Gross
1120df8ce9d7SJuergen Gross queue_work_on(cpu, system_wq, &sscs.work);
1121df8ce9d7SJuergen Gross wait_for_completion(&sscs.done);
1122*6b1b0a86SZqiang destroy_work_on_stack(&sscs.work);
1123df8ce9d7SJuergen Gross
1124df8ce9d7SJuergen Gross return sscs.ret;
1125df8ce9d7SJuergen Gross }
1126df8ce9d7SJuergen Gross EXPORT_SYMBOL_GPL(smp_call_on_cpu);
1127