1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Generic helpers for smp ipi calls 4 * 5 * (C) Jens Axboe <jens.axboe@oracle.com> 2008 6 */ 7 8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9 10 #include <linux/irq_work.h> 11 #include <linux/rcupdate.h> 12 #include <linux/rculist.h> 13 #include <linux/kernel.h> 14 #include <linux/export.h> 15 #include <linux/percpu.h> 16 #include <linux/init.h> 17 #include <linux/interrupt.h> 18 #include <linux/gfp.h> 19 #include <linux/slab.h> 20 #include <linux/smp.h> 21 #include <linux/cpu.h> 22 #include <linux/sched.h> 23 #include <linux/sched/idle.h> 24 #include <linux/hypervisor.h> 25 #include <linux/sched/clock.h> 26 #include <linux/nmi.h> 27 #include <linux/sched/debug.h> 28 #include <linux/jump_label.h> 29 #include <linux/string_choices.h> 30 31 #include <trace/events/ipi.h> 32 #define CREATE_TRACE_POINTS 33 #include <trace/events/csd.h> 34 #undef CREATE_TRACE_POINTS 35 36 #include "smpboot.h" 37 #include "sched/smp.h" 38 39 #define CSD_TYPE(_csd) ((_csd)->node.u_flags & CSD_FLAG_TYPE_MASK) 40 41 struct call_function_data { 42 call_single_data_t __percpu *csd; 43 cpumask_var_t cpumask; 44 cpumask_var_t cpumask_ipi; 45 }; 46 47 static DEFINE_PER_CPU_ALIGNED(struct call_function_data, cfd_data); 48 49 static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue); 50 51 static DEFINE_PER_CPU(atomic_t, trigger_backtrace) = ATOMIC_INIT(1); 52 53 static void __flush_smp_call_function_queue(bool warn_cpu_offline); 54 55 int smpcfd_prepare_cpu(unsigned int cpu) 56 { 57 struct call_function_data *cfd = &per_cpu(cfd_data, cpu); 58 59 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL, 60 cpu_to_node(cpu))) 61 return -ENOMEM; 62 if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL, 63 cpu_to_node(cpu))) { 64 free_cpumask_var(cfd->cpumask); 65 return -ENOMEM; 66 } 67 68 /* 69 * Allocate the per-CPU CSD the first time a CPU comes up. It is 70 * not freed when the CPU is offlined, so csd_lock_wait() can access 71 * it even when the CPU was offlined after preemption was re-enabled. 72 */ 73 if (!cfd->csd) 74 cfd->csd = alloc_percpu(call_single_data_t); 75 if (!cfd->csd) { 76 free_cpumask_var(cfd->cpumask); 77 free_cpumask_var(cfd->cpumask_ipi); 78 return -ENOMEM; 79 } 80 81 return 0; 82 } 83 84 int smpcfd_dead_cpu(unsigned int cpu) 85 { 86 struct call_function_data *cfd = &per_cpu(cfd_data, cpu); 87 88 free_cpumask_var(cfd->cpumask); 89 free_cpumask_var(cfd->cpumask_ipi); 90 irq_work_run_cpu(cpu); 91 return 0; 92 } 93 94 int smpcfd_dying_cpu(unsigned int cpu) 95 { 96 /* 97 * The IPIs for the smp-call-function callbacks queued by other CPUs 98 * might arrive late, either due to hardware latencies or because this 99 * CPU disabled interrupts (inside stop-machine) before the IPIs were 100 * sent. So flush out any pending callbacks explicitly (without waiting 101 * for the IPIs to arrive), to ensure that the outgoing CPU doesn't go 102 * offline with work still pending. 103 * 104 * This runs with interrupts disabled inside the stopper task invoked by 105 * stop_machine(), ensuring mutually exclusive CPU offlining and IPI flush. 106 */ 107 __flush_smp_call_function_queue(false); 108 irq_work_run(); 109 return 0; 110 } 111 112 void __init call_function_init(void) 113 { 114 int i; 115 116 for_each_possible_cpu(i) 117 init_llist_head(&per_cpu(call_single_queue, i)); 118 119 smpcfd_prepare_cpu(smp_processor_id()); 120 } 121 122 static __always_inline void 123 send_call_function_single_ipi(int cpu) 124 { 125 if (call_function_single_prep_ipi(cpu)) { 126 trace_ipi_send_cpu(cpu, _RET_IP_, 127 generic_smp_call_function_single_interrupt); 128 arch_send_call_function_single_ipi(cpu); 129 } 130 } 131 132 static __always_inline void 133 send_call_function_ipi_mask(struct cpumask *mask) 134 { 135 trace_ipi_send_cpumask(mask, _RET_IP_, 136 generic_smp_call_function_single_interrupt); 137 arch_send_call_function_ipi_mask(mask); 138 } 139 140 static __always_inline void 141 csd_do_func(smp_call_func_t func, void *info, call_single_data_t *csd) 142 { 143 trace_csd_function_entry(func, csd); 144 func(info); 145 trace_csd_function_exit(func, csd); 146 } 147 148 static DEFINE_STATIC_KEY_MAYBE(CONFIG_CSD_LOCK_WAIT_DEBUG_DEFAULT, csdlock_debug_enabled); 149 150 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG 151 152 /* 153 * Parse the csdlock_debug= kernel boot parameter. 154 * 155 * If you need to restore the old "ext" value that once provided 156 * additional debugging information, reapply the following commits: 157 * 158 * de7b09ef658d ("locking/csd_lock: Prepare more CSD lock debugging") 159 * a5aabace5fb8 ("locking/csd_lock: Add more data to CSD lock debugging") 160 */ 161 static int __init csdlock_debug(char *str) 162 { 163 int ret; 164 unsigned int val = 0; 165 166 ret = get_option(&str, &val); 167 if (ret) { 168 if (val) 169 static_branch_enable(&csdlock_debug_enabled); 170 else 171 static_branch_disable(&csdlock_debug_enabled); 172 } 173 174 return 1; 175 } 176 __setup("csdlock_debug=", csdlock_debug); 177 178 static DEFINE_PER_CPU(call_single_data_t *, cur_csd); 179 static DEFINE_PER_CPU(smp_call_func_t, cur_csd_func); 180 static DEFINE_PER_CPU(void *, cur_csd_info); 181 182 static ulong csd_lock_timeout = 5000; /* CSD lock timeout in milliseconds. */ 183 module_param(csd_lock_timeout, ulong, 0644); 184 static int panic_on_ipistall; /* CSD panic timeout in milliseconds, 300000 for five minutes. */ 185 module_param(panic_on_ipistall, int, 0644); 186 187 static atomic_t csd_bug_count = ATOMIC_INIT(0); 188 189 /* Record current CSD work for current CPU, NULL to erase. */ 190 static void __csd_lock_record(call_single_data_t *csd) 191 { 192 if (!csd) { 193 /* 194 * Pairs with smp_load_acquire() of cur_csd in 195 * csd_lock_wait_toolong(): orders any preceding CSD 196 * callback/unlock before a remote reader observes NULL. 197 */ 198 smp_store_release(this_cpu_ptr(&cur_csd), NULL); 199 return; 200 } 201 __this_cpu_write(cur_csd_func, csd->func); 202 __this_cpu_write(cur_csd_info, csd->info); 203 /* 204 * Pairs with smp_load_acquire() of cur_csd in 205 * csd_lock_wait_toolong(): publishes cur_csd_func and 206 * cur_csd_info before the non-NULL pointer becomes visible. 207 */ 208 smp_store_release(this_cpu_ptr(&cur_csd), csd); 209 } 210 211 static __always_inline void csd_lock_record(call_single_data_t *csd) 212 { 213 if (static_branch_unlikely(&csdlock_debug_enabled)) 214 __csd_lock_record(csd); 215 } 216 217 static int csd_lock_wait_getcpu(call_single_data_t *csd) 218 { 219 unsigned int csd_type; 220 221 csd_type = CSD_TYPE(csd); 222 if (csd_type == CSD_TYPE_ASYNC || csd_type == CSD_TYPE_SYNC) 223 return csd->node.dst; /* Other CSD_TYPE_ values might not have ->dst. */ 224 return -1; 225 } 226 227 static atomic_t n_csd_lock_stuck; 228 229 /** 230 * csd_lock_is_stuck - Has a CSD-lock acquisition been stuck too long? 231 * 232 * Returns: @true if a CSD-lock acquisition is stuck and has been stuck 233 * long enough for a "non-responsive CSD lock" message to be printed. 234 */ 235 bool csd_lock_is_stuck(void) 236 { 237 return !!atomic_read(&n_csd_lock_stuck); 238 } 239 240 /* 241 * Complain if too much time spent waiting. Note that only 242 * the CSD_TYPE_SYNC/ASYNC types provide the destination CPU, 243 * so waiting on other types gets much less information. 244 */ 245 static bool csd_lock_wait_toolong(call_single_data_t *csd, u64 ts0, u64 *ts1, int *bug_id, unsigned long *nmessages) 246 { 247 int cpu = -1; 248 int cpux; 249 bool firsttime; 250 u64 ts2, ts_delta; 251 call_single_data_t *cpu_cur_csd; 252 unsigned int flags = READ_ONCE(csd->node.u_flags); 253 unsigned long long csd_lock_timeout_ns = csd_lock_timeout * NSEC_PER_MSEC; 254 255 if (!(flags & CSD_FLAG_LOCK)) { 256 if (!unlikely(*bug_id)) 257 return true; 258 cpu = csd_lock_wait_getcpu(csd); 259 pr_alert("csd: CSD lock (#%d) got unstuck on CPU#%02d, CPU#%02d released the lock.\n", 260 *bug_id, raw_smp_processor_id(), cpu); 261 atomic_dec(&n_csd_lock_stuck); 262 return true; 263 } 264 265 ts2 = ktime_get_mono_fast_ns(); 266 /* How long since we last checked for a stuck CSD lock.*/ 267 ts_delta = ts2 - *ts1; 268 if (likely(ts_delta <= csd_lock_timeout_ns * (*nmessages + 1) * 269 (!*nmessages ? 1 : (ilog2(num_online_cpus()) / 2 + 1)) || 270 csd_lock_timeout_ns == 0)) 271 return false; 272 273 if (ts0 > ts2) { 274 /* Our own sched_clock went backward; don't blame another CPU. */ 275 ts_delta = ts0 - ts2; 276 pr_alert("sched_clock on CPU %d went backward by %llu ns\n", raw_smp_processor_id(), ts_delta); 277 *ts1 = ts2; 278 return false; 279 } 280 281 firsttime = !*bug_id; 282 if (firsttime) 283 *bug_id = atomic_inc_return(&csd_bug_count); 284 cpu = csd_lock_wait_getcpu(csd); 285 if (WARN_ONCE(cpu < 0 || cpu >= nr_cpu_ids, "%s: cpu = %d\n", __func__, cpu)) 286 cpux = 0; 287 else 288 cpux = cpu; 289 /* 290 * Pairs with smp_store_release() of cur_csd in __csd_lock_record(): 291 * a non-NULL cur_csd here implies cur_csd_func and cur_csd_info 292 * are the matching publication; a NULL value is ordered after any 293 * preceding CSD callback/unlock on the remote CPU. 294 */ 295 cpu_cur_csd = smp_load_acquire(&per_cpu(cur_csd, cpux)); 296 /* How long since this CSD lock was stuck. */ 297 ts_delta = ts2 - ts0; 298 pr_alert("csd: %s non-responsive CSD lock (#%d) on CPU#%d, waiting %lld ns for CPU#%02d %pS(%ps).\n", 299 firsttime ? "Detected" : "Continued", *bug_id, raw_smp_processor_id(), (s64)ts_delta, 300 cpu, csd->func, csd->info); 301 (*nmessages)++; 302 if (firsttime) 303 atomic_inc(&n_csd_lock_stuck); 304 /* 305 * If the CSD lock is still stuck after 5 minutes, it is unlikely 306 * to become unstuck. Use a signed comparison to avoid triggering 307 * on underflows when the TSC is out of sync between sockets. 308 */ 309 BUG_ON(panic_on_ipistall > 0 && (s64)ts_delta > ((s64)panic_on_ipistall * NSEC_PER_MSEC)); 310 if (cpu_cur_csd && csd != cpu_cur_csd) { 311 pr_alert("\tcsd: CSD lock (#%d) handling prior %pS(%ps) request.\n", 312 *bug_id, READ_ONCE(per_cpu(cur_csd_func, cpux)), 313 READ_ONCE(per_cpu(cur_csd_info, cpux))); 314 } else { 315 pr_alert("\tcsd: CSD lock (#%d) %s.\n", 316 *bug_id, !cpu_cur_csd ? "unresponsive" : "handling this request"); 317 } 318 if (cpu >= 0) { 319 if (atomic_cmpxchg_acquire(&per_cpu(trigger_backtrace, cpu), 1, 0)) 320 dump_cpu_task(cpu); 321 if (!cpu_cur_csd) { 322 pr_alert("csd: Re-sending CSD lock (#%d) IPI from CPU#%02d to CPU#%02d\n", *bug_id, raw_smp_processor_id(), cpu); 323 arch_send_call_function_single_ipi(cpu); 324 } 325 } 326 if (firsttime) 327 dump_stack(); 328 *ts1 = ts2; 329 330 return false; 331 } 332 333 /* 334 * csd_lock/csd_unlock used to serialize access to per-cpu csd resources 335 * 336 * For non-synchronous ipi calls the csd can still be in use by the 337 * previous function call. For multi-cpu calls its even more interesting 338 * as we'll have to ensure no other cpu is observing our csd. 339 */ 340 static void __csd_lock_wait(call_single_data_t *csd) 341 { 342 unsigned long nmessages = 0; 343 int bug_id = 0; 344 u64 ts0, ts1; 345 346 guard(preempt)(); 347 348 ts1 = ts0 = ktime_get_mono_fast_ns(); 349 for (;;) { 350 if (csd_lock_wait_toolong(csd, ts0, &ts1, &bug_id, &nmessages)) 351 break; 352 cpu_relax(); 353 } 354 smp_acquire__after_ctrl_dep(); 355 } 356 357 static __always_inline void csd_lock_wait(call_single_data_t *csd) 358 { 359 if (static_branch_unlikely(&csdlock_debug_enabled)) { 360 __csd_lock_wait(csd); 361 return; 362 } 363 364 smp_cond_load_acquire(&csd->node.u_flags, !(VAL & CSD_FLAG_LOCK)); 365 } 366 #else 367 static __always_inline void __csd_lock_wait(call_single_data_t *csd) 368 { 369 } 370 371 static void csd_lock_record(call_single_data_t *csd) 372 { 373 } 374 375 static __always_inline void csd_lock_wait(call_single_data_t *csd) 376 { 377 smp_cond_load_acquire(&csd->node.u_flags, !(VAL & CSD_FLAG_LOCK)); 378 } 379 #endif 380 381 static __always_inline void csd_lock(call_single_data_t *csd) 382 { 383 if (IS_ENABLED(CONFIG_CSD_LOCK_WAIT_DEBUG) && 384 static_branch_unlikely(&csdlock_debug_enabled)) { 385 386 for (;;) { 387 unsigned int flags; 388 389 __csd_lock_wait(csd); 390 flags = READ_ONCE(csd->node.u_flags); 391 392 if (!(flags & CSD_FLAG_LOCK) && 393 try_cmpxchg_acquire(&csd->node.u_flags, &flags, flags | CSD_FLAG_LOCK)) 394 break; 395 } 396 } else { 397 csd_lock_wait(csd); 398 csd->node.u_flags |= CSD_FLAG_LOCK; 399 } 400 401 /* 402 * prevent CPU from reordering the above assignment 403 * to ->flags with any subsequent assignments to other 404 * fields of the specified call_single_data_t structure: 405 */ 406 smp_wmb(); 407 } 408 409 static __always_inline void csd_unlock(call_single_data_t *csd) 410 { 411 WARN_ON(!(csd->node.u_flags & CSD_FLAG_LOCK)); 412 413 /* 414 * ensure we're all done before releasing data: 415 */ 416 smp_store_release(&csd->node.u_flags, 0); 417 } 418 419 static DEFINE_PER_CPU_SHARED_ALIGNED(call_single_data_t, csd_data); 420 421 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG 422 static call_single_data_t *get_single_csd_data(int cpu) 423 { 424 if (static_branch_unlikely(&csdlock_debug_enabled) && 425 (unsigned int)cpu < nr_cpu_ids) 426 return per_cpu_ptr(&csd_data, cpu); 427 return this_cpu_ptr(&csd_data); 428 } 429 #else 430 static call_single_data_t *get_single_csd_data(int cpu) 431 { 432 return this_cpu_ptr(&csd_data); 433 } 434 #endif 435 436 void __smp_call_single_queue(int cpu, struct llist_node *node) 437 { 438 /* 439 * We have to check the type of the CSD before queueing it, because 440 * once queued it can have its flags cleared by 441 * flush_smp_call_function_queue() 442 * even if we haven't sent the smp_call IPI yet (e.g. the stopper 443 * executes migration_cpu_stop() on the remote CPU). 444 */ 445 if (trace_csd_queue_cpu_enabled()) { 446 call_single_data_t *csd; 447 smp_call_func_t func; 448 449 csd = container_of(node, call_single_data_t, node.llist); 450 func = CSD_TYPE(csd) == CSD_TYPE_TTWU ? 451 sched_ttwu_pending : csd->func; 452 453 trace_call__csd_queue_cpu(cpu, _RET_IP_, func, csd); 454 } 455 456 /* 457 * The list addition should be visible to the target CPU when it pops 458 * the head of the list to pull the entry off it in the IPI handler 459 * because of normal cache coherency rules implied by the underlying 460 * llist ops. 461 * 462 * If IPIs can go out of order to the cache coherency protocol 463 * in an architecture, sufficient synchronisation should be added 464 * to arch code to make it appear to obey cache coherency WRT 465 * locking and barrier primitives. Generic code isn't really 466 * equipped to do the right thing... 467 */ 468 if (llist_add(node, &per_cpu(call_single_queue, cpu))) 469 send_call_function_single_ipi(cpu); 470 } 471 472 /* 473 * Insert a previously allocated call_single_data_t element 474 * for execution on the given CPU. data must already have 475 * ->func, ->info, and ->flags set. 476 */ 477 static int generic_exec_single(int cpu, call_single_data_t *csd) 478 { 479 /* 480 * Preemption already disabled here so stopper cannot run on this CPU, 481 * ensuring mutually exclusive CPU offlining and last IPI flush. 482 */ 483 if (cpu == smp_processor_id()) { 484 smp_call_func_t func = csd->func; 485 void *info = csd->info; 486 unsigned long flags; 487 488 /* 489 * We can unlock early even for the synchronous on-stack case, 490 * since we're doing this from the same CPU.. 491 */ 492 csd_lock_record(csd); 493 csd_unlock(csd); 494 local_irq_save(flags); 495 csd_do_func(func, info, NULL); 496 csd_lock_record(NULL); 497 local_irq_restore(flags); 498 return 0; 499 } 500 501 if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu)) { 502 csd_unlock(csd); 503 return -ENXIO; 504 } 505 506 __smp_call_single_queue(cpu, &csd->node.llist); 507 508 return 0; 509 } 510 511 /** 512 * generic_smp_call_function_single_interrupt - Execute SMP IPI callbacks 513 * 514 * Invoked by arch to handle an IPI for call function single. 515 * Must be called with interrupts disabled. 516 */ 517 void generic_smp_call_function_single_interrupt(void) 518 { 519 __flush_smp_call_function_queue(true); 520 } 521 522 /** 523 * __flush_smp_call_function_queue - Flush pending smp-call-function callbacks 524 * 525 * @warn_cpu_offline: If set to 'true', warn if callbacks were queued on an 526 * offline CPU. Skip this check if set to 'false'. 527 * 528 * Flush any pending smp-call-function callbacks queued on this CPU. This is 529 * invoked by the generic IPI handler, as well as by a CPU about to go offline, 530 * to ensure that all pending IPI callbacks are run before it goes completely 531 * offline. 532 * 533 * Loop through the call_single_queue and run all the queued callbacks. 534 * Must be called with interrupts disabled. 535 */ 536 static void __flush_smp_call_function_queue(bool warn_cpu_offline) 537 { 538 call_single_data_t *csd, *csd_next; 539 struct llist_node *entry, *prev; 540 struct llist_head *head; 541 static bool warned; 542 atomic_t *tbt; 543 544 lockdep_assert_irqs_disabled(); 545 546 /* Allow waiters to send backtrace NMI from here onwards */ 547 tbt = this_cpu_ptr(&trigger_backtrace); 548 atomic_set_release(tbt, 1); 549 550 head = this_cpu_ptr(&call_single_queue); 551 entry = llist_del_all(head); 552 entry = llist_reverse_order(entry); 553 554 /* There shouldn't be any pending callbacks on an offline CPU. */ 555 if (unlikely(warn_cpu_offline && !cpu_online(smp_processor_id()) && 556 !warned && entry != NULL)) { 557 warned = true; 558 WARN(1, "IPI on offline CPU %d\n", smp_processor_id()); 559 560 /* 561 * We don't have to use the _safe() variant here 562 * because we are not invoking the IPI handlers yet. 563 */ 564 llist_for_each_entry(csd, entry, node.llist) { 565 switch (CSD_TYPE(csd)) { 566 case CSD_TYPE_ASYNC: 567 case CSD_TYPE_SYNC: 568 case CSD_TYPE_IRQ_WORK: 569 pr_warn("IPI callback %pS sent to offline CPU\n", 570 csd->func); 571 break; 572 573 case CSD_TYPE_TTWU: 574 pr_warn("IPI task-wakeup sent to offline CPU\n"); 575 break; 576 577 default: 578 pr_warn("IPI callback, unknown type %d, sent to offline CPU\n", 579 CSD_TYPE(csd)); 580 break; 581 } 582 } 583 } 584 585 /* 586 * First; run all SYNC callbacks, people are waiting for us. 587 */ 588 prev = NULL; 589 llist_for_each_entry_safe(csd, csd_next, entry, node.llist) { 590 /* Do we wait until *after* callback? */ 591 if (CSD_TYPE(csd) == CSD_TYPE_SYNC) { 592 smp_call_func_t func = csd->func; 593 void *info = csd->info; 594 595 if (prev) { 596 prev->next = &csd_next->node.llist; 597 } else { 598 entry = &csd_next->node.llist; 599 } 600 601 csd_lock_record(csd); 602 csd_do_func(func, info, csd); 603 csd_unlock(csd); 604 csd_lock_record(NULL); 605 } else { 606 prev = &csd->node.llist; 607 } 608 } 609 610 if (!entry) 611 return; 612 613 /* 614 * Second; run all !SYNC callbacks. 615 */ 616 prev = NULL; 617 llist_for_each_entry_safe(csd, csd_next, entry, node.llist) { 618 int type = CSD_TYPE(csd); 619 620 if (type != CSD_TYPE_TTWU) { 621 if (prev) { 622 prev->next = &csd_next->node.llist; 623 } else { 624 entry = &csd_next->node.llist; 625 } 626 627 if (type == CSD_TYPE_ASYNC) { 628 smp_call_func_t func = csd->func; 629 void *info = csd->info; 630 631 csd_lock_record(csd); 632 csd_unlock(csd); 633 csd_do_func(func, info, csd); 634 csd_lock_record(NULL); 635 } else if (type == CSD_TYPE_IRQ_WORK) { 636 irq_work_single(csd); 637 } 638 639 } else { 640 prev = &csd->node.llist; 641 } 642 } 643 644 /* 645 * Third; only CSD_TYPE_TTWU is left, issue those. 646 */ 647 if (entry) { 648 csd = llist_entry(entry, typeof(*csd), node.llist); 649 csd_do_func(sched_ttwu_pending, entry, csd); 650 } 651 } 652 653 654 /** 655 * flush_smp_call_function_queue - Flush pending smp-call-function callbacks 656 * from task context (idle, migration thread) 657 * 658 * When TIF_POLLING_NRFLAG is supported and a CPU is in idle and has it 659 * set, then remote CPUs can avoid sending IPIs and wake the idle CPU by 660 * setting TIF_NEED_RESCHED. The idle task on the woken up CPU has to 661 * handle queued SMP function calls before scheduling. 662 * 663 * The migration thread has to ensure that an eventually pending wakeup has 664 * been handled before it migrates a task. 665 */ 666 void flush_smp_call_function_queue(void) 667 { 668 unsigned int was_pending; 669 unsigned long flags; 670 671 if (llist_empty(this_cpu_ptr(&call_single_queue))) 672 return; 673 674 local_irq_save(flags); 675 /* Get the already pending soft interrupts for RT enabled kernels */ 676 was_pending = local_softirq_pending(); 677 __flush_smp_call_function_queue(true); 678 if (local_softirq_pending()) 679 do_softirq_post_smp_call_flush(was_pending); 680 681 local_irq_restore(flags); 682 } 683 684 static int __smp_call_function_single(int cpu, smp_call_func_t func, 685 void *info, const struct cpumask *mask, 686 bool wait) 687 { 688 call_single_data_t *csd; 689 call_single_data_t csd_stack = { 690 .node = { .u_flags = CSD_FLAG_LOCK | CSD_TYPE_SYNC, }, 691 }; 692 int this_cpu; 693 int err; 694 695 /* 696 * Prevent preemption and reschedule on another CPU, as well as CPU 697 * removal. This prevents stopper from running on this CPU, thus 698 * providing mutual exclusion of the below cpu_online() check and 699 * IPI sending ensuring IPI are not missed by CPU going offline. 700 */ 701 this_cpu = get_cpu(); 702 703 if (mask) { 704 /* Try for same CPU (cheapest) */ 705 if (!cpumask_test_cpu(this_cpu, mask)) 706 cpu = sched_numa_find_nth_cpu(mask, 0, cpu_to_node(this_cpu)); 707 else 708 cpu = this_cpu; 709 } 710 711 /* 712 * Can deadlock when called with interrupts disabled. 713 * We allow cpu's that are not yet online though, as no one else can 714 * send smp call function interrupt to this cpu and as such deadlocks 715 * can't happen. 716 */ 717 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled() 718 && !oops_in_progress); 719 720 /* 721 * When @wait we can deadlock when we interrupt between llist_add() and 722 * arch_send_call_function_ipi*(); when !@wait we can deadlock due to 723 * csd_lock() on because the interrupt context uses the same csd 724 * storage. 725 */ 726 WARN_ON_ONCE(!in_task()); 727 728 csd = &csd_stack; 729 if (!wait) { 730 csd = get_single_csd_data(cpu); 731 csd_lock(csd); 732 } 733 734 csd->func = func; 735 csd->info = info; 736 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG 737 csd->node.src = this_cpu; 738 csd->node.dst = cpu; 739 #endif 740 741 err = generic_exec_single(cpu, csd); 742 743 /* 744 * @csd is stack-allocated when @wait is true. No concurrent access 745 * except from the IPI completion path, so we can re-enable preemption 746 * early to reduce latency. 747 */ 748 put_cpu(); 749 750 if (wait) 751 csd_lock_wait(csd); 752 753 return err; 754 } 755 756 /** 757 * smp_call_function_single - Run a function on a specific CPU 758 * @cpu: Specific target CPU for this function. 759 * @func: The function to run. This must be fast and non-blocking. 760 * @info: An arbitrary pointer to pass to the function. 761 * @wait: If true, wait until function has completed on other CPUs. 762 * 763 * Returns: %0 on success, else a negative status code. 764 */ 765 int smp_call_function_single(int cpu, smp_call_func_t func, void *info, bool wait) 766 { 767 return __smp_call_function_single(cpu, func, info, NULL, wait); 768 } 769 EXPORT_SYMBOL(smp_call_function_single); 770 771 /** 772 * smp_call_function_single_async() - Run an asynchronous function on a 773 * specific CPU. 774 * @cpu: The CPU to run on. 775 * @csd: Pre-allocated and setup data structure 776 * 777 * Like smp_call_function_single(), but the call is asynchonous and 778 * can thus be done from contexts with disabled interrupts. 779 * 780 * The caller passes his own pre-allocated data structure 781 * (ie: embedded in an object) and is responsible for synchronizing it 782 * such that the IPIs performed on the @csd are strictly serialized. 783 * 784 * If the function is called with one csd which has not yet been 785 * processed by previous call to smp_call_function_single_async(), the 786 * function will return immediately with -EBUSY showing that the csd 787 * object is still in progress. 788 * 789 * NOTE: Be careful, there is unfortunately no current debugging facility to 790 * validate the correctness of this serialization. 791 * 792 * Return: %0 on success or negative errno value on error 793 */ 794 int smp_call_function_single_async(int cpu, call_single_data_t *csd) 795 { 796 int err = 0; 797 798 preempt_disable(); 799 800 if (csd->node.u_flags & CSD_FLAG_LOCK) { 801 err = -EBUSY; 802 goto out; 803 } 804 805 csd->node.u_flags = CSD_FLAG_LOCK; 806 smp_wmb(); 807 808 err = generic_exec_single(cpu, csd); 809 810 out: 811 preempt_enable(); 812 813 return err; 814 } 815 EXPORT_SYMBOL_GPL(smp_call_function_single_async); 816 817 /** 818 * smp_call_function_any - Run a function on any of the given cpus 819 * @mask: The mask of cpus it can run on. 820 * @func: The function to run. This must be fast and non-blocking. 821 * @info: An arbitrary pointer to pass to the function. 822 * @wait: If true, wait until function has completed. 823 * 824 * Selection preference: 825 * 1) current cpu if in @mask 826 * 2) nearest cpu in @mask, based on NUMA topology 827 * 828 * Returns: %0 on success, else a negative status code (if no cpus were online). 829 */ 830 int smp_call_function_any(const struct cpumask *mask, 831 smp_call_func_t func, void *info, int wait) 832 { 833 return __smp_call_function_single(-1, func, info, mask, wait); 834 } 835 EXPORT_SYMBOL_GPL(smp_call_function_any); 836 837 static DEFINE_STATIC_KEY_FALSE(ipi_mask_inlined); 838 839 #ifdef CONFIG_PREEMPTION 840 841 int smp_task_ipi_mask_alloc(struct task_struct *task) 842 { 843 if (static_branch_unlikely(&ipi_mask_inlined)) 844 return 0; 845 846 ACCESS_PRIVATE(task, ipi_mask).ipi_mask_ptr = 847 kmalloc(cpumask_size(), GFP_KERNEL); 848 if (!ACCESS_PRIVATE(task, ipi_mask).ipi_mask_ptr) 849 return -ENOMEM; 850 851 return 0; 852 } 853 854 void smp_task_ipi_mask_free(struct task_struct *task) 855 { 856 if (static_branch_unlikely(&ipi_mask_inlined)) 857 return; 858 859 kfree(ACCESS_PRIVATE(task, ipi_mask).ipi_mask_ptr); 860 } 861 862 static cpumask_t *smp_task_ipi_mask(struct task_struct *cur) 863 { 864 /* 865 * If cpumask_size() is smaller than or equal to the pointer 866 * size, it stashes the cpumask in the pointer itself to 867 * avoid extra memory allocations. 868 */ 869 if (static_branch_unlikely(&ipi_mask_inlined)) 870 return (cpumask_t *)&ACCESS_PRIVATE(cur, ipi_mask).ipi_mask_val; 871 872 return ACCESS_PRIVATE(cur, ipi_mask).ipi_mask_ptr; 873 } 874 #else 875 static cpumask_t *smp_task_ipi_mask(struct task_struct *cur) 876 { 877 return NULL; 878 } 879 #endif 880 881 /* 882 * Flags to be used as scf_flags argument of smp_call_function_many_cond(). 883 * 884 * %SCF_WAIT: Wait until function execution is completed 885 * %SCF_RUN_LOCAL: Run also locally if local cpu is set in cpumask 886 */ 887 #define SCF_WAIT (1U << 0) 888 #define SCF_RUN_LOCAL (1U << 1) 889 890 static void smp_call_function_many_cond(const struct cpumask *mask, 891 smp_call_func_t func, void *info, 892 unsigned int scf_flags, 893 smp_cond_func_t cond_func) 894 { 895 struct cpumask *cpumask, *task_mask; 896 bool wait = scf_flags & SCF_WAIT; 897 struct call_function_data *cfd; 898 int cpu, last_cpu, this_cpu; 899 bool run_remote = false; 900 int nr_cpus = 0; 901 902 this_cpu = get_cpu(); 903 cfd = this_cpu_ptr(&cfd_data); 904 task_mask = smp_task_ipi_mask(current); 905 if (task_mask) 906 cpumask = task_mask; 907 else 908 cpumask = cfd->cpumask; 909 910 /* 911 * Can deadlock when called with interrupts disabled. 912 * We allow cpu's that are not yet online though, as no one else can 913 * send smp call function interrupt to this cpu and as such deadlocks 914 * can't happen. 915 */ 916 if (cpu_online(this_cpu) && !oops_in_progress && 917 !early_boot_irqs_disabled) 918 lockdep_assert_irqs_enabled(); 919 920 /* 921 * When @wait we can deadlock when we interrupt between llist_add() and 922 * arch_send_call_function_ipi*(); when !@wait we can deadlock due to 923 * csd_lock() on because the interrupt context uses the same csd 924 * storage. 925 */ 926 WARN_ON_ONCE(!in_task()); 927 928 /* Check if we need remote execution, i.e., any CPU excluding this one. */ 929 if (cpumask_any_and_but(mask, cpu_online_mask, this_cpu) < nr_cpu_ids) { 930 cpumask_and(cpumask, mask, cpu_online_mask); 931 __cpumask_clear_cpu(this_cpu, cpumask); 932 933 cpumask_clear(cfd->cpumask_ipi); 934 for_each_cpu(cpu, cpumask) { 935 call_single_data_t *csd = per_cpu_ptr(cfd->csd, cpu); 936 937 if (cond_func && !cond_func(cpu, info)) { 938 __cpumask_clear_cpu(cpu, cpumask); 939 continue; 940 } 941 942 /* Work is enqueued on a remote CPU. */ 943 run_remote = true; 944 945 csd_lock(csd); 946 if (wait) 947 csd->node.u_flags |= CSD_TYPE_SYNC; 948 csd->func = func; 949 csd->info = info; 950 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG 951 csd->node.src = this_cpu; 952 csd->node.dst = cpu; 953 #endif 954 trace_csd_queue_cpu(cpu, _RET_IP_, func, csd); 955 956 /* 957 * Kick the remote CPU if this is the first work 958 * item enqueued. 959 */ 960 if (llist_add(&csd->node.llist, &per_cpu(call_single_queue, cpu))) { 961 __cpumask_set_cpu(cpu, cfd->cpumask_ipi); 962 nr_cpus++; 963 last_cpu = cpu; 964 } 965 } 966 967 /* 968 * Choose the most efficient way to send an IPI. Note that the 969 * number of CPUs might be zero due to concurrent changes to the 970 * provided mask. 971 */ 972 if (nr_cpus == 1) 973 send_call_function_single_ipi(last_cpu); 974 else if (likely(nr_cpus > 1)) 975 send_call_function_ipi_mask(cfd->cpumask_ipi); 976 } 977 978 /* Check if we need local execution. */ 979 if ((scf_flags & SCF_RUN_LOCAL) && cpumask_test_cpu(this_cpu, mask) && 980 (!cond_func || cond_func(this_cpu, info))) { 981 unsigned long flags; 982 983 local_irq_save(flags); 984 csd_do_func(func, info, NULL); 985 local_irq_restore(flags); 986 } 987 988 /* 989 * The IPI work has been queued and dispatched. On PREEMPT kernels, 990 * tasks created through dup_task_struct() have task-local wait masks. 991 * The boot init_task can fall back to cfd->cpumask when the mask is 992 * not inlined, but other tasks still use task-local masks and cannot 993 * overwrite it. On !PREEMPT kernels, preempt_enable() cannot schedule 994 * another task, so the per-CPU mask remains protected. 995 */ 996 put_cpu(); 997 998 if (run_remote && wait) { 999 for_each_cpu(cpu, cpumask) { 1000 call_single_data_t *csd; 1001 1002 csd = per_cpu_ptr(cfd->csd, cpu); 1003 csd_lock_wait(csd); 1004 } 1005 } 1006 } 1007 1008 /** 1009 * smp_call_function_many() - Run a function on a set of CPUs. 1010 * @mask: The set of cpus to run on (only runs on online subset). 1011 * @func: The function to run. This must be fast and non-blocking. 1012 * @info: An arbitrary pointer to pass to the function. 1013 * @wait: If true, wait (atomically) until function has completed 1014 * on other CPUs. 1015 * 1016 * You must not call this function with disabled interrupts or from a 1017 * hardware interrupt handler or from a bottom half handler. 1018 * 1019 * @func is not called on the local CPU even if @mask contains it. Consider 1020 * using on_each_cpu_cond_mask() instead if this is not desirable. 1021 */ 1022 void smp_call_function_many(const struct cpumask *mask, 1023 smp_call_func_t func, void *info, bool wait) 1024 { 1025 smp_call_function_many_cond(mask, func, info, wait * SCF_WAIT, NULL); 1026 } 1027 EXPORT_SYMBOL(smp_call_function_many); 1028 1029 /** 1030 * smp_call_function() - Run a function on all other CPUs. 1031 * @func: The function to run. This must be fast and non-blocking. 1032 * @info: An arbitrary pointer to pass to the function. 1033 * @wait: If true, wait (atomically) until function has completed 1034 * on other CPUs. 1035 * 1036 * If @wait is true, then returns once @func has returned; otherwise 1037 * it returns just before the target cpu calls @func. 1038 * 1039 * You must not call this function with disabled interrupts or from a 1040 * hardware interrupt handler or from a bottom half handler. 1041 */ 1042 void smp_call_function(smp_call_func_t func, void *info, int wait) 1043 { 1044 smp_call_function_many_cond(cpu_online_mask, func, info, 1045 wait ? SCF_WAIT : 0, NULL); 1046 } 1047 EXPORT_SYMBOL(smp_call_function); 1048 1049 /* Setup configured maximum number of CPUs to activate */ 1050 unsigned int setup_max_cpus = NR_CPUS; 1051 EXPORT_SYMBOL(setup_max_cpus); 1052 1053 1054 /* 1055 * Setup routine for controlling SMP activation 1056 * 1057 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP 1058 * activation entirely (the MPS table probe still happens, though). 1059 * 1060 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer 1061 * greater than 0, limits the maximum number of CPUs activated in 1062 * SMP mode to <NUM>. 1063 */ 1064 1065 void __weak __init arch_disable_smp_support(void) { } 1066 1067 static int __init nosmp(char *str) 1068 { 1069 setup_max_cpus = 0; 1070 arch_disable_smp_support(); 1071 1072 return 0; 1073 } 1074 1075 early_param("nosmp", nosmp); 1076 1077 /* this is hard limit */ 1078 static int __init nrcpus(char *str) 1079 { 1080 int nr_cpus; 1081 1082 if (get_option(&str, &nr_cpus) && nr_cpus > 0 && nr_cpus < nr_cpu_ids) 1083 set_nr_cpu_ids(nr_cpus); 1084 1085 return 0; 1086 } 1087 1088 early_param("nr_cpus", nrcpus); 1089 1090 static int __init maxcpus(char *str) 1091 { 1092 get_option(&str, &setup_max_cpus); 1093 if (setup_max_cpus == 0) 1094 arch_disable_smp_support(); 1095 1096 return 0; 1097 } 1098 1099 early_param("maxcpus", maxcpus); 1100 1101 #if (NR_CPUS > 1) && !defined(CONFIG_FORCE_NR_CPUS) 1102 /* Setup number of possible processor ids */ 1103 unsigned int nr_cpu_ids __read_mostly = NR_CPUS; 1104 EXPORT_SYMBOL(nr_cpu_ids); 1105 #endif 1106 1107 /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */ 1108 void __init setup_nr_cpu_ids(void) 1109 { 1110 set_nr_cpu_ids(find_last_bit(cpumask_bits(cpu_possible_mask), NR_CPUS) + 1); 1111 1112 if (IS_ENABLED(CONFIG_PREEMPTION) && cpumask_size() <= sizeof(unsigned long)) 1113 static_branch_enable(&ipi_mask_inlined); 1114 } 1115 1116 /* Called by boot processor to activate the rest. */ 1117 void __init smp_init(void) 1118 { 1119 int num_nodes, num_cpus; 1120 1121 idle_threads_init(); 1122 cpuhp_threads_init(); 1123 1124 pr_info("Bringing up secondary CPUs ...\n"); 1125 1126 bringup_nonboot_cpus(setup_max_cpus); 1127 1128 num_nodes = num_online_nodes(); 1129 num_cpus = num_online_cpus(); 1130 pr_info("Brought up %d node%s, %d CPU%s\n", 1131 num_nodes, str_plural(num_nodes), num_cpus, str_plural(num_cpus)); 1132 1133 /* Any cleanup work */ 1134 smp_cpus_done(setup_max_cpus); 1135 } 1136 1137 /** 1138 * on_each_cpu_cond_mask() - Call a function on each processor for which 1139 * the supplied function cond_func returns true, optionally waiting 1140 * for all the required CPUs to finish. This may include the local 1141 * processor. 1142 * @cond_func: A callback function that is passed a cpu id and 1143 * the info parameter. The function is called 1144 * with preemption disabled. The function should 1145 * return a boolean value indicating whether to IPI 1146 * the specified CPU. 1147 * @func: The function to run on all applicable CPUs. 1148 * This must be fast and non-blocking. 1149 * @info: An arbitrary pointer to pass to both functions. 1150 * @wait: If true, wait until function has completed on other CPUs. 1151 * @mask: The set of cpus to run on (only runs on online subset). 1152 * 1153 * Target CPU selection and work queueing are done with preemption 1154 * disabled. This protects against CPUs going offline, but not against 1155 * CPUs coming online concurrently; newly online CPUs are not guaranteed 1156 * to be seen or sent an IPI. If @wait is true, the final wait for remote 1157 * completion happens after that preemption-disabled section. 1158 * 1159 * You must not call this function with disabled interrupts or 1160 * from a hardware interrupt handler or from a bottom half handler. 1161 */ 1162 void on_each_cpu_cond_mask(smp_cond_func_t cond_func, smp_call_func_t func, 1163 void *info, bool wait, const struct cpumask *mask) 1164 { 1165 unsigned int scf_flags = SCF_RUN_LOCAL; 1166 1167 if (wait) 1168 scf_flags |= SCF_WAIT; 1169 1170 smp_call_function_many_cond(mask, func, info, scf_flags, cond_func); 1171 } 1172 EXPORT_SYMBOL(on_each_cpu_cond_mask); 1173 1174 static void do_nothing(void *unused) 1175 { 1176 } 1177 1178 /** 1179 * kick_all_cpus_sync - Force all cpus out of idle 1180 * 1181 * Used to synchronize the update of pm_idle function pointer. It's 1182 * called after the pointer is updated and returns after the dummy 1183 * callback function has been executed on all cpus. The execution of 1184 * the function can only happen on the remote cpus after they have 1185 * left the idle function which had been called via pm_idle function 1186 * pointer. So it's guaranteed that nothing uses the previous pointer 1187 * anymore. 1188 */ 1189 void kick_all_cpus_sync(void) 1190 { 1191 /* Make sure the change is visible before we kick the cpus */ 1192 smp_mb(); 1193 smp_call_function(do_nothing, NULL, 1); 1194 } 1195 EXPORT_SYMBOL_GPL(kick_all_cpus_sync); 1196 1197 /** 1198 * wake_up_all_idle_cpus - break all cpus out of idle 1199 * wake_up_all_idle_cpus try to break all cpus which is in idle state even 1200 * including idle polling cpus, for non-idle cpus, we will do nothing 1201 * for them. 1202 */ 1203 void wake_up_all_idle_cpus(void) 1204 { 1205 int cpu; 1206 1207 for_each_possible_cpu(cpu) { 1208 preempt_disable(); 1209 if (cpu != smp_processor_id() && cpu_online(cpu)) 1210 wake_up_if_idle(cpu); 1211 preempt_enable(); 1212 } 1213 } 1214 EXPORT_SYMBOL_GPL(wake_up_all_idle_cpus); 1215 1216 /** 1217 * cpus_peek_for_pending_ipi - Check for pending IPI for CPUs 1218 * @mask: The CPU mask for the CPUs to check. 1219 * 1220 * This function walks through the @mask to check if there are any pending IPIs 1221 * scheduled, for any of the CPUs in the @mask. It does not guarantee 1222 * correctness as it only provides a racy snapshot. 1223 * 1224 * Returns: true if there is a pending IPI scheduled and false otherwise. 1225 */ 1226 bool cpus_peek_for_pending_ipi(const struct cpumask *mask) 1227 { 1228 unsigned int cpu; 1229 1230 for_each_cpu(cpu, mask) { 1231 if (!llist_empty(per_cpu_ptr(&call_single_queue, cpu))) 1232 return true; 1233 } 1234 1235 return false; 1236 } 1237 1238 /** 1239 * struct smp_call_on_cpu_struct - Call a function on a specific CPU 1240 * @work: &work_struct 1241 * @done: &completion to signal 1242 * @func: function to call 1243 * @data: function's data argument 1244 * @ret: return value from @func 1245 * @cpu: target CPU (%-1 for any CPU) 1246 * 1247 * Used to call a function on a specific cpu and wait for it to return. 1248 * Optionally make sure the call is done on a specified physical cpu via vcpu 1249 * pinning in order to support virtualized environments. 1250 */ 1251 struct smp_call_on_cpu_struct { 1252 struct work_struct work; 1253 struct completion done; 1254 int (*func)(void *); 1255 void *data; 1256 int ret; 1257 int cpu; 1258 }; 1259 1260 static void smp_call_on_cpu_callback(struct work_struct *work) 1261 { 1262 struct smp_call_on_cpu_struct *sscs; 1263 1264 sscs = container_of(work, struct smp_call_on_cpu_struct, work); 1265 if (sscs->cpu >= 0) 1266 hypervisor_pin_vcpu(sscs->cpu); 1267 sscs->ret = sscs->func(sscs->data); 1268 if (sscs->cpu >= 0) 1269 hypervisor_pin_vcpu(-1); 1270 1271 complete(&sscs->done); 1272 } 1273 1274 /** 1275 * smp_call_on_cpu() - Call a function on a specific CPU and wait 1276 * for it to return. 1277 * @cpu: The CPU to run on. 1278 * @func: The function to run 1279 * @par: An arbitrary pointer parameter for @func. 1280 * @phys: If @true, force to run on physical @cpu. See 1281 * &struct smp_call_on_cpu_struct for more info. 1282 * 1283 * Returns: %-ENXIO if the @cpu is invalid; otherwise the return value 1284 * from @func. 1285 */ 1286 int smp_call_on_cpu(unsigned int cpu, int (*func)(void *), void *par, bool phys) 1287 { 1288 struct smp_call_on_cpu_struct sscs = { 1289 .done = COMPLETION_INITIALIZER_ONSTACK(sscs.done), 1290 .func = func, 1291 .data = par, 1292 .cpu = phys ? cpu : -1, 1293 }; 1294 1295 INIT_WORK_ONSTACK(&sscs.work, smp_call_on_cpu_callback); 1296 1297 if (cpu >= nr_cpu_ids || !cpu_online(cpu)) 1298 return -ENXIO; 1299 1300 queue_work_on(cpu, system_percpu_wq, &sscs.work); 1301 wait_for_completion(&sscs.done); 1302 destroy_work_on_stack(&sscs.work); 1303 1304 return sscs.ret; 1305 } 1306 EXPORT_SYMBOL_GPL(smp_call_on_cpu); 1307