0001
0002
0003
0004
0005
0006
0007
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009
0010 #include <linux/irq_work.h>
0011 #include <linux/rcupdate.h>
0012 #include <linux/rculist.h>
0013 #include <linux/kernel.h>
0014 #include <linux/export.h>
0015 #include <linux/percpu.h>
0016 #include <linux/init.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/gfp.h>
0019 #include <linux/smp.h>
0020 #include <linux/cpu.h>
0021 #include <linux/sched.h>
0022 #include <linux/sched/idle.h>
0023 #include <linux/hypervisor.h>
0024 #include <linux/sched/clock.h>
0025 #include <linux/nmi.h>
0026 #include <linux/sched/debug.h>
0027 #include <linux/jump_label.h>
0028
0029 #include "smpboot.h"
0030 #include "sched/smp.h"
0031
0032 #define CSD_TYPE(_csd) ((_csd)->node.u_flags & CSD_FLAG_TYPE_MASK)
0033
0034 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
0035 union cfd_seq_cnt {
0036 u64 val;
0037 struct {
0038 u64 src:16;
0039 u64 dst:16;
0040 #define CFD_SEQ_NOCPU 0xffff
0041 u64 type:4;
0042 #define CFD_SEQ_QUEUE 0
0043 #define CFD_SEQ_IPI 1
0044 #define CFD_SEQ_NOIPI 2
0045 #define CFD_SEQ_PING 3
0046 #define CFD_SEQ_PINGED 4
0047 #define CFD_SEQ_HANDLE 5
0048 #define CFD_SEQ_DEQUEUE 6
0049 #define CFD_SEQ_IDLE 7
0050 #define CFD_SEQ_GOTIPI 8
0051 #define CFD_SEQ_HDLEND 9
0052 u64 cnt:28;
0053 } u;
0054 };
0055
0056 static char *seq_type[] = {
0057 [CFD_SEQ_QUEUE] = "queue",
0058 [CFD_SEQ_IPI] = "ipi",
0059 [CFD_SEQ_NOIPI] = "noipi",
0060 [CFD_SEQ_PING] = "ping",
0061 [CFD_SEQ_PINGED] = "pinged",
0062 [CFD_SEQ_HANDLE] = "handle",
0063 [CFD_SEQ_DEQUEUE] = "dequeue (src CPU 0 == empty)",
0064 [CFD_SEQ_IDLE] = "idle",
0065 [CFD_SEQ_GOTIPI] = "gotipi",
0066 [CFD_SEQ_HDLEND] = "hdlend (src CPU 0 == early)",
0067 };
0068
0069 struct cfd_seq_local {
0070 u64 ping;
0071 u64 pinged;
0072 u64 handle;
0073 u64 dequeue;
0074 u64 idle;
0075 u64 gotipi;
0076 u64 hdlend;
0077 };
0078 #endif
0079
0080 struct cfd_percpu {
0081 call_single_data_t csd;
0082 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
0083 u64 seq_queue;
0084 u64 seq_ipi;
0085 u64 seq_noipi;
0086 #endif
0087 };
0088
0089 struct call_function_data {
0090 struct cfd_percpu __percpu *pcpu;
0091 cpumask_var_t cpumask;
0092 cpumask_var_t cpumask_ipi;
0093 };
0094
0095 static DEFINE_PER_CPU_ALIGNED(struct call_function_data, cfd_data);
0096
0097 static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue);
0098
0099 static void __flush_smp_call_function_queue(bool warn_cpu_offline);
0100
0101 int smpcfd_prepare_cpu(unsigned int cpu)
0102 {
0103 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
0104
0105 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
0106 cpu_to_node(cpu)))
0107 return -ENOMEM;
0108 if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL,
0109 cpu_to_node(cpu))) {
0110 free_cpumask_var(cfd->cpumask);
0111 return -ENOMEM;
0112 }
0113 cfd->pcpu = alloc_percpu(struct cfd_percpu);
0114 if (!cfd->pcpu) {
0115 free_cpumask_var(cfd->cpumask);
0116 free_cpumask_var(cfd->cpumask_ipi);
0117 return -ENOMEM;
0118 }
0119
0120 return 0;
0121 }
0122
0123 int smpcfd_dead_cpu(unsigned int cpu)
0124 {
0125 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
0126
0127 free_cpumask_var(cfd->cpumask);
0128 free_cpumask_var(cfd->cpumask_ipi);
0129 free_percpu(cfd->pcpu);
0130 return 0;
0131 }
0132
0133 int smpcfd_dying_cpu(unsigned int cpu)
0134 {
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144 __flush_smp_call_function_queue(false);
0145 irq_work_run();
0146 return 0;
0147 }
0148
0149 void __init call_function_init(void)
0150 {
0151 int i;
0152
0153 for_each_possible_cpu(i)
0154 init_llist_head(&per_cpu(call_single_queue, i));
0155
0156 smpcfd_prepare_cpu(smp_processor_id());
0157 }
0158
0159 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
0160
0161 static DEFINE_STATIC_KEY_FALSE(csdlock_debug_enabled);
0162 static DEFINE_STATIC_KEY_FALSE(csdlock_debug_extended);
0163
0164 static int __init csdlock_debug(char *str)
0165 {
0166 unsigned int val = 0;
0167
0168 if (str && !strcmp(str, "ext")) {
0169 val = 1;
0170 static_branch_enable(&csdlock_debug_extended);
0171 } else
0172 get_option(&str, &val);
0173
0174 if (val)
0175 static_branch_enable(&csdlock_debug_enabled);
0176
0177 return 1;
0178 }
0179 __setup("csdlock_debug=", csdlock_debug);
0180
0181 static DEFINE_PER_CPU(call_single_data_t *, cur_csd);
0182 static DEFINE_PER_CPU(smp_call_func_t, cur_csd_func);
0183 static DEFINE_PER_CPU(void *, cur_csd_info);
0184 static DEFINE_PER_CPU(struct cfd_seq_local, cfd_seq_local);
0185
0186 static ulong csd_lock_timeout = 5000;
0187 module_param(csd_lock_timeout, ulong, 0444);
0188
0189 static atomic_t csd_bug_count = ATOMIC_INIT(0);
0190 static u64 cfd_seq;
0191
0192 #define CFD_SEQ(s, d, t, c) \
0193 (union cfd_seq_cnt){ .u.src = s, .u.dst = d, .u.type = t, .u.cnt = c }
0194
0195 static u64 cfd_seq_inc(unsigned int src, unsigned int dst, unsigned int type)
0196 {
0197 union cfd_seq_cnt new, old;
0198
0199 new = CFD_SEQ(src, dst, type, 0);
0200
0201 do {
0202 old.val = READ_ONCE(cfd_seq);
0203 new.u.cnt = old.u.cnt + 1;
0204 } while (cmpxchg(&cfd_seq, old.val, new.val) != old.val);
0205
0206 return old.val;
0207 }
0208
0209 #define cfd_seq_store(var, src, dst, type) \
0210 do { \
0211 if (static_branch_unlikely(&csdlock_debug_extended)) \
0212 var = cfd_seq_inc(src, dst, type); \
0213 } while (0)
0214
0215
0216 static void __csd_lock_record(struct __call_single_data *csd)
0217 {
0218 if (!csd) {
0219 smp_mb();
0220 __this_cpu_write(cur_csd, NULL);
0221 return;
0222 }
0223 __this_cpu_write(cur_csd_func, csd->func);
0224 __this_cpu_write(cur_csd_info, csd->info);
0225 smp_wmb();
0226 __this_cpu_write(cur_csd, csd);
0227 smp_mb();
0228
0229 }
0230
0231 static __always_inline void csd_lock_record(struct __call_single_data *csd)
0232 {
0233 if (static_branch_unlikely(&csdlock_debug_enabled))
0234 __csd_lock_record(csd);
0235 }
0236
0237 static int csd_lock_wait_getcpu(struct __call_single_data *csd)
0238 {
0239 unsigned int csd_type;
0240
0241 csd_type = CSD_TYPE(csd);
0242 if (csd_type == CSD_TYPE_ASYNC || csd_type == CSD_TYPE_SYNC)
0243 return csd->node.dst;
0244 return -1;
0245 }
0246
0247 static void cfd_seq_data_add(u64 val, unsigned int src, unsigned int dst,
0248 unsigned int type, union cfd_seq_cnt *data,
0249 unsigned int *n_data, unsigned int now)
0250 {
0251 union cfd_seq_cnt new[2];
0252 unsigned int i, j, k;
0253
0254 new[0].val = val;
0255 new[1] = CFD_SEQ(src, dst, type, new[0].u.cnt + 1);
0256
0257 for (i = 0; i < 2; i++) {
0258 if (new[i].u.cnt <= now)
0259 new[i].u.cnt |= 0x80000000U;
0260 for (j = 0; j < *n_data; j++) {
0261 if (new[i].u.cnt == data[j].u.cnt) {
0262
0263 if (i == 0)
0264 data[j].val = new[i].val;
0265 break;
0266 }
0267 if (new[i].u.cnt < data[j].u.cnt) {
0268 for (k = *n_data; k > j; k--)
0269 data[k].val = data[k - 1].val;
0270 data[j].val = new[i].val;
0271 (*n_data)++;
0272 break;
0273 }
0274 }
0275 if (j == *n_data) {
0276 data[j].val = new[i].val;
0277 (*n_data)++;
0278 }
0279 }
0280 }
0281
0282 static const char *csd_lock_get_type(unsigned int type)
0283 {
0284 return (type >= ARRAY_SIZE(seq_type)) ? "?" : seq_type[type];
0285 }
0286
0287 static void csd_lock_print_extended(struct __call_single_data *csd, int cpu)
0288 {
0289 struct cfd_seq_local *seq = &per_cpu(cfd_seq_local, cpu);
0290 unsigned int srccpu = csd->node.src;
0291 struct call_function_data *cfd = per_cpu_ptr(&cfd_data, srccpu);
0292 struct cfd_percpu *pcpu = per_cpu_ptr(cfd->pcpu, cpu);
0293 unsigned int now;
0294 union cfd_seq_cnt data[2 * ARRAY_SIZE(seq_type)];
0295 unsigned int n_data = 0, i;
0296
0297 data[0].val = READ_ONCE(cfd_seq);
0298 now = data[0].u.cnt;
0299
0300 cfd_seq_data_add(pcpu->seq_queue, srccpu, cpu, CFD_SEQ_QUEUE, data, &n_data, now);
0301 cfd_seq_data_add(pcpu->seq_ipi, srccpu, cpu, CFD_SEQ_IPI, data, &n_data, now);
0302 cfd_seq_data_add(pcpu->seq_noipi, srccpu, cpu, CFD_SEQ_NOIPI, data, &n_data, now);
0303
0304 cfd_seq_data_add(per_cpu(cfd_seq_local.ping, srccpu), srccpu, CFD_SEQ_NOCPU, CFD_SEQ_PING, data, &n_data, now);
0305 cfd_seq_data_add(per_cpu(cfd_seq_local.pinged, srccpu), srccpu, CFD_SEQ_NOCPU, CFD_SEQ_PINGED, data, &n_data, now);
0306
0307 cfd_seq_data_add(seq->idle, CFD_SEQ_NOCPU, cpu, CFD_SEQ_IDLE, data, &n_data, now);
0308 cfd_seq_data_add(seq->gotipi, CFD_SEQ_NOCPU, cpu, CFD_SEQ_GOTIPI, data, &n_data, now);
0309 cfd_seq_data_add(seq->handle, CFD_SEQ_NOCPU, cpu, CFD_SEQ_HANDLE, data, &n_data, now);
0310 cfd_seq_data_add(seq->dequeue, CFD_SEQ_NOCPU, cpu, CFD_SEQ_DEQUEUE, data, &n_data, now);
0311 cfd_seq_data_add(seq->hdlend, CFD_SEQ_NOCPU, cpu, CFD_SEQ_HDLEND, data, &n_data, now);
0312
0313 for (i = 0; i < n_data; i++) {
0314 pr_alert("\tcsd: cnt(%07x): %04x->%04x %s\n",
0315 data[i].u.cnt & ~0x80000000U, data[i].u.src,
0316 data[i].u.dst, csd_lock_get_type(data[i].u.type));
0317 }
0318 pr_alert("\tcsd: cnt now: %07x\n", now);
0319 }
0320
0321
0322
0323
0324
0325
0326 static bool csd_lock_wait_toolong(struct __call_single_data *csd, u64 ts0, u64 *ts1, int *bug_id)
0327 {
0328 int cpu = -1;
0329 int cpux;
0330 bool firsttime;
0331 u64 ts2, ts_delta;
0332 call_single_data_t *cpu_cur_csd;
0333 unsigned int flags = READ_ONCE(csd->node.u_flags);
0334 unsigned long long csd_lock_timeout_ns = csd_lock_timeout * NSEC_PER_MSEC;
0335
0336 if (!(flags & CSD_FLAG_LOCK)) {
0337 if (!unlikely(*bug_id))
0338 return true;
0339 cpu = csd_lock_wait_getcpu(csd);
0340 pr_alert("csd: CSD lock (#%d) got unstuck on CPU#%02d, CPU#%02d released the lock.\n",
0341 *bug_id, raw_smp_processor_id(), cpu);
0342 return true;
0343 }
0344
0345 ts2 = sched_clock();
0346 ts_delta = ts2 - *ts1;
0347 if (likely(ts_delta <= csd_lock_timeout_ns || csd_lock_timeout_ns == 0))
0348 return false;
0349
0350 firsttime = !*bug_id;
0351 if (firsttime)
0352 *bug_id = atomic_inc_return(&csd_bug_count);
0353 cpu = csd_lock_wait_getcpu(csd);
0354 if (WARN_ONCE(cpu < 0 || cpu >= nr_cpu_ids, "%s: cpu = %d\n", __func__, cpu))
0355 cpux = 0;
0356 else
0357 cpux = cpu;
0358 cpu_cur_csd = smp_load_acquire(&per_cpu(cur_csd, cpux));
0359 pr_alert("csd: %s non-responsive CSD lock (#%d) on CPU#%d, waiting %llu ns for CPU#%02d %pS(%ps).\n",
0360 firsttime ? "Detected" : "Continued", *bug_id, raw_smp_processor_id(), ts2 - ts0,
0361 cpu, csd->func, csd->info);
0362 if (cpu_cur_csd && csd != cpu_cur_csd) {
0363 pr_alert("\tcsd: CSD lock (#%d) handling prior %pS(%ps) request.\n",
0364 *bug_id, READ_ONCE(per_cpu(cur_csd_func, cpux)),
0365 READ_ONCE(per_cpu(cur_csd_info, cpux)));
0366 } else {
0367 pr_alert("\tcsd: CSD lock (#%d) %s.\n",
0368 *bug_id, !cpu_cur_csd ? "unresponsive" : "handling this request");
0369 }
0370 if (cpu >= 0) {
0371 if (static_branch_unlikely(&csdlock_debug_extended))
0372 csd_lock_print_extended(csd, cpu);
0373 if (!trigger_single_cpu_backtrace(cpu))
0374 dump_cpu_task(cpu);
0375 if (!cpu_cur_csd) {
0376 pr_alert("csd: Re-sending CSD lock (#%d) IPI from CPU#%02d to CPU#%02d\n", *bug_id, raw_smp_processor_id(), cpu);
0377 arch_send_call_function_single_ipi(cpu);
0378 }
0379 }
0380 dump_stack();
0381 *ts1 = ts2;
0382
0383 return false;
0384 }
0385
0386
0387
0388
0389
0390
0391
0392
0393 static void __csd_lock_wait(struct __call_single_data *csd)
0394 {
0395 int bug_id = 0;
0396 u64 ts0, ts1;
0397
0398 ts1 = ts0 = sched_clock();
0399 for (;;) {
0400 if (csd_lock_wait_toolong(csd, ts0, &ts1, &bug_id))
0401 break;
0402 cpu_relax();
0403 }
0404 smp_acquire__after_ctrl_dep();
0405 }
0406
0407 static __always_inline void csd_lock_wait(struct __call_single_data *csd)
0408 {
0409 if (static_branch_unlikely(&csdlock_debug_enabled)) {
0410 __csd_lock_wait(csd);
0411 return;
0412 }
0413
0414 smp_cond_load_acquire(&csd->node.u_flags, !(VAL & CSD_FLAG_LOCK));
0415 }
0416
0417 static void __smp_call_single_queue_debug(int cpu, struct llist_node *node)
0418 {
0419 unsigned int this_cpu = smp_processor_id();
0420 struct cfd_seq_local *seq = this_cpu_ptr(&cfd_seq_local);
0421 struct call_function_data *cfd = this_cpu_ptr(&cfd_data);
0422 struct cfd_percpu *pcpu = per_cpu_ptr(cfd->pcpu, cpu);
0423
0424 cfd_seq_store(pcpu->seq_queue, this_cpu, cpu, CFD_SEQ_QUEUE);
0425 if (llist_add(node, &per_cpu(call_single_queue, cpu))) {
0426 cfd_seq_store(pcpu->seq_ipi, this_cpu, cpu, CFD_SEQ_IPI);
0427 cfd_seq_store(seq->ping, this_cpu, cpu, CFD_SEQ_PING);
0428 send_call_function_single_ipi(cpu);
0429 cfd_seq_store(seq->pinged, this_cpu, cpu, CFD_SEQ_PINGED);
0430 } else {
0431 cfd_seq_store(pcpu->seq_noipi, this_cpu, cpu, CFD_SEQ_NOIPI);
0432 }
0433 }
0434 #else
0435 #define cfd_seq_store(var, src, dst, type)
0436
0437 static void csd_lock_record(struct __call_single_data *csd)
0438 {
0439 }
0440
0441 static __always_inline void csd_lock_wait(struct __call_single_data *csd)
0442 {
0443 smp_cond_load_acquire(&csd->node.u_flags, !(VAL & CSD_FLAG_LOCK));
0444 }
0445 #endif
0446
0447 static __always_inline void csd_lock(struct __call_single_data *csd)
0448 {
0449 csd_lock_wait(csd);
0450 csd->node.u_flags |= CSD_FLAG_LOCK;
0451
0452
0453
0454
0455
0456
0457 smp_wmb();
0458 }
0459
0460 static __always_inline void csd_unlock(struct __call_single_data *csd)
0461 {
0462 WARN_ON(!(csd->node.u_flags & CSD_FLAG_LOCK));
0463
0464
0465
0466
0467 smp_store_release(&csd->node.u_flags, 0);
0468 }
0469
0470 static DEFINE_PER_CPU_SHARED_ALIGNED(call_single_data_t, csd_data);
0471
0472 void __smp_call_single_queue(int cpu, struct llist_node *node)
0473 {
0474 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
0475 if (static_branch_unlikely(&csdlock_debug_extended)) {
0476 unsigned int type;
0477
0478 type = CSD_TYPE(container_of(node, call_single_data_t,
0479 node.llist));
0480 if (type == CSD_TYPE_SYNC || type == CSD_TYPE_ASYNC) {
0481 __smp_call_single_queue_debug(cpu, node);
0482 return;
0483 }
0484 }
0485 #endif
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498 if (llist_add(node, &per_cpu(call_single_queue, cpu)))
0499 send_call_function_single_ipi(cpu);
0500 }
0501
0502
0503
0504
0505
0506
0507 static int generic_exec_single(int cpu, struct __call_single_data *csd)
0508 {
0509 if (cpu == smp_processor_id()) {
0510 smp_call_func_t func = csd->func;
0511 void *info = csd->info;
0512 unsigned long flags;
0513
0514
0515
0516
0517
0518 csd_lock_record(csd);
0519 csd_unlock(csd);
0520 local_irq_save(flags);
0521 func(info);
0522 csd_lock_record(NULL);
0523 local_irq_restore(flags);
0524 return 0;
0525 }
0526
0527 if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu)) {
0528 csd_unlock(csd);
0529 return -ENXIO;
0530 }
0531
0532 __smp_call_single_queue(cpu, &csd->node.llist);
0533
0534 return 0;
0535 }
0536
0537
0538
0539
0540
0541
0542
0543 void generic_smp_call_function_single_interrupt(void)
0544 {
0545 cfd_seq_store(this_cpu_ptr(&cfd_seq_local)->gotipi, CFD_SEQ_NOCPU,
0546 smp_processor_id(), CFD_SEQ_GOTIPI);
0547 __flush_smp_call_function_queue(true);
0548 }
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562
0563
0564 static void __flush_smp_call_function_queue(bool warn_cpu_offline)
0565 {
0566 call_single_data_t *csd, *csd_next;
0567 struct llist_node *entry, *prev;
0568 struct llist_head *head;
0569 static bool warned;
0570
0571 lockdep_assert_irqs_disabled();
0572
0573 head = this_cpu_ptr(&call_single_queue);
0574 cfd_seq_store(this_cpu_ptr(&cfd_seq_local)->handle, CFD_SEQ_NOCPU,
0575 smp_processor_id(), CFD_SEQ_HANDLE);
0576 entry = llist_del_all(head);
0577 cfd_seq_store(this_cpu_ptr(&cfd_seq_local)->dequeue,
0578
0579 entry ? CFD_SEQ_NOCPU : 0,
0580 smp_processor_id(), CFD_SEQ_DEQUEUE);
0581 entry = llist_reverse_order(entry);
0582
0583
0584 if (unlikely(warn_cpu_offline && !cpu_online(smp_processor_id()) &&
0585 !warned && entry != NULL)) {
0586 warned = true;
0587 WARN(1, "IPI on offline CPU %d\n", smp_processor_id());
0588
0589
0590
0591
0592
0593 llist_for_each_entry(csd, entry, node.llist) {
0594 switch (CSD_TYPE(csd)) {
0595 case CSD_TYPE_ASYNC:
0596 case CSD_TYPE_SYNC:
0597 case CSD_TYPE_IRQ_WORK:
0598 pr_warn("IPI callback %pS sent to offline CPU\n",
0599 csd->func);
0600 break;
0601
0602 case CSD_TYPE_TTWU:
0603 pr_warn("IPI task-wakeup sent to offline CPU\n");
0604 break;
0605
0606 default:
0607 pr_warn("IPI callback, unknown type %d, sent to offline CPU\n",
0608 CSD_TYPE(csd));
0609 break;
0610 }
0611 }
0612 }
0613
0614
0615
0616
0617 prev = NULL;
0618 llist_for_each_entry_safe(csd, csd_next, entry, node.llist) {
0619
0620 if (CSD_TYPE(csd) == CSD_TYPE_SYNC) {
0621 smp_call_func_t func = csd->func;
0622 void *info = csd->info;
0623
0624 if (prev) {
0625 prev->next = &csd_next->node.llist;
0626 } else {
0627 entry = &csd_next->node.llist;
0628 }
0629
0630 csd_lock_record(csd);
0631 func(info);
0632 csd_unlock(csd);
0633 csd_lock_record(NULL);
0634 } else {
0635 prev = &csd->node.llist;
0636 }
0637 }
0638
0639 if (!entry) {
0640 cfd_seq_store(this_cpu_ptr(&cfd_seq_local)->hdlend,
0641 0, smp_processor_id(),
0642 CFD_SEQ_HDLEND);
0643 return;
0644 }
0645
0646
0647
0648
0649 prev = NULL;
0650 llist_for_each_entry_safe(csd, csd_next, entry, node.llist) {
0651 int type = CSD_TYPE(csd);
0652
0653 if (type != CSD_TYPE_TTWU) {
0654 if (prev) {
0655 prev->next = &csd_next->node.llist;
0656 } else {
0657 entry = &csd_next->node.llist;
0658 }
0659
0660 if (type == CSD_TYPE_ASYNC) {
0661 smp_call_func_t func = csd->func;
0662 void *info = csd->info;
0663
0664 csd_lock_record(csd);
0665 csd_unlock(csd);
0666 func(info);
0667 csd_lock_record(NULL);
0668 } else if (type == CSD_TYPE_IRQ_WORK) {
0669 irq_work_single(csd);
0670 }
0671
0672 } else {
0673 prev = &csd->node.llist;
0674 }
0675 }
0676
0677
0678
0679
0680 if (entry)
0681 sched_ttwu_pending(entry);
0682
0683 cfd_seq_store(this_cpu_ptr(&cfd_seq_local)->hdlend, CFD_SEQ_NOCPU,
0684 smp_processor_id(), CFD_SEQ_HDLEND);
0685 }
0686
0687
0688
0689
0690
0691
0692
0693
0694
0695
0696
0697
0698
0699
0700 void flush_smp_call_function_queue(void)
0701 {
0702 unsigned int was_pending;
0703 unsigned long flags;
0704
0705 if (llist_empty(this_cpu_ptr(&call_single_queue)))
0706 return;
0707
0708 cfd_seq_store(this_cpu_ptr(&cfd_seq_local)->idle, CFD_SEQ_NOCPU,
0709 smp_processor_id(), CFD_SEQ_IDLE);
0710 local_irq_save(flags);
0711
0712 was_pending = local_softirq_pending();
0713 __flush_smp_call_function_queue(true);
0714 if (local_softirq_pending())
0715 do_softirq_post_smp_call_flush(was_pending);
0716
0717 local_irq_restore(flags);
0718 }
0719
0720
0721
0722
0723
0724
0725
0726
0727
0728 int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
0729 int wait)
0730 {
0731 call_single_data_t *csd;
0732 call_single_data_t csd_stack = {
0733 .node = { .u_flags = CSD_FLAG_LOCK | CSD_TYPE_SYNC, },
0734 };
0735 int this_cpu;
0736 int err;
0737
0738
0739
0740
0741
0742 this_cpu = get_cpu();
0743
0744
0745
0746
0747
0748
0749
0750 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
0751 && !oops_in_progress);
0752
0753
0754
0755
0756
0757
0758
0759 WARN_ON_ONCE(!in_task());
0760
0761 csd = &csd_stack;
0762 if (!wait) {
0763 csd = this_cpu_ptr(&csd_data);
0764 csd_lock(csd);
0765 }
0766
0767 csd->func = func;
0768 csd->info = info;
0769 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
0770 csd->node.src = smp_processor_id();
0771 csd->node.dst = cpu;
0772 #endif
0773
0774 err = generic_exec_single(cpu, csd);
0775
0776 if (wait)
0777 csd_lock_wait(csd);
0778
0779 put_cpu();
0780
0781 return err;
0782 }
0783 EXPORT_SYMBOL(smp_call_function_single);
0784
0785
0786
0787
0788
0789
0790
0791
0792
0793
0794
0795
0796
0797
0798
0799
0800
0801
0802
0803
0804
0805
0806
0807
0808 int smp_call_function_single_async(int cpu, struct __call_single_data *csd)
0809 {
0810 int err = 0;
0811
0812 preempt_disable();
0813
0814 if (csd->node.u_flags & CSD_FLAG_LOCK) {
0815 err = -EBUSY;
0816 goto out;
0817 }
0818
0819 csd->node.u_flags = CSD_FLAG_LOCK;
0820 smp_wmb();
0821
0822 err = generic_exec_single(cpu, csd);
0823
0824 out:
0825 preempt_enable();
0826
0827 return err;
0828 }
0829 EXPORT_SYMBOL_GPL(smp_call_function_single_async);
0830
0831
0832
0833
0834
0835
0836
0837
0838
0839
0840
0841
0842
0843
0844
0845 int smp_call_function_any(const struct cpumask *mask,
0846 smp_call_func_t func, void *info, int wait)
0847 {
0848 unsigned int cpu;
0849 const struct cpumask *nodemask;
0850 int ret;
0851
0852
0853 cpu = get_cpu();
0854 if (cpumask_test_cpu(cpu, mask))
0855 goto call;
0856
0857
0858 nodemask = cpumask_of_node(cpu_to_node(cpu));
0859 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
0860 cpu = cpumask_next_and(cpu, nodemask, mask)) {
0861 if (cpu_online(cpu))
0862 goto call;
0863 }
0864
0865
0866 cpu = cpumask_any_and(mask, cpu_online_mask);
0867 call:
0868 ret = smp_call_function_single(cpu, func, info, wait);
0869 put_cpu();
0870 return ret;
0871 }
0872 EXPORT_SYMBOL_GPL(smp_call_function_any);
0873
0874
0875
0876
0877
0878
0879
0880 #define SCF_WAIT (1U << 0)
0881 #define SCF_RUN_LOCAL (1U << 1)
0882
0883 static void smp_call_function_many_cond(const struct cpumask *mask,
0884 smp_call_func_t func, void *info,
0885 unsigned int scf_flags,
0886 smp_cond_func_t cond_func)
0887 {
0888 int cpu, last_cpu, this_cpu = smp_processor_id();
0889 struct call_function_data *cfd;
0890 bool wait = scf_flags & SCF_WAIT;
0891 bool run_remote = false;
0892 bool run_local = false;
0893 int nr_cpus = 0;
0894
0895 lockdep_assert_preemption_disabled();
0896
0897
0898
0899
0900
0901
0902
0903 if (cpu_online(this_cpu) && !oops_in_progress &&
0904 !early_boot_irqs_disabled)
0905 lockdep_assert_irqs_enabled();
0906
0907
0908
0909
0910
0911
0912
0913 WARN_ON_ONCE(!in_task());
0914
0915
0916 if ((scf_flags & SCF_RUN_LOCAL) && cpumask_test_cpu(this_cpu, mask))
0917 run_local = true;
0918
0919
0920 cpu = cpumask_first_and(mask, cpu_online_mask);
0921 if (cpu == this_cpu)
0922 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
0923 if (cpu < nr_cpu_ids)
0924 run_remote = true;
0925
0926 if (run_remote) {
0927 cfd = this_cpu_ptr(&cfd_data);
0928 cpumask_and(cfd->cpumask, mask, cpu_online_mask);
0929 __cpumask_clear_cpu(this_cpu, cfd->cpumask);
0930
0931 cpumask_clear(cfd->cpumask_ipi);
0932 for_each_cpu(cpu, cfd->cpumask) {
0933 struct cfd_percpu *pcpu = per_cpu_ptr(cfd->pcpu, cpu);
0934 call_single_data_t *csd = &pcpu->csd;
0935
0936 if (cond_func && !cond_func(cpu, info))
0937 continue;
0938
0939 csd_lock(csd);
0940 if (wait)
0941 csd->node.u_flags |= CSD_TYPE_SYNC;
0942 csd->func = func;
0943 csd->info = info;
0944 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
0945 csd->node.src = smp_processor_id();
0946 csd->node.dst = cpu;
0947 #endif
0948 cfd_seq_store(pcpu->seq_queue, this_cpu, cpu, CFD_SEQ_QUEUE);
0949 if (llist_add(&csd->node.llist, &per_cpu(call_single_queue, cpu))) {
0950 __cpumask_set_cpu(cpu, cfd->cpumask_ipi);
0951 nr_cpus++;
0952 last_cpu = cpu;
0953
0954 cfd_seq_store(pcpu->seq_ipi, this_cpu, cpu, CFD_SEQ_IPI);
0955 } else {
0956 cfd_seq_store(pcpu->seq_noipi, this_cpu, cpu, CFD_SEQ_NOIPI);
0957 }
0958 }
0959
0960 cfd_seq_store(this_cpu_ptr(&cfd_seq_local)->ping, this_cpu, CFD_SEQ_NOCPU, CFD_SEQ_PING);
0961
0962
0963
0964
0965
0966
0967 if (nr_cpus == 1)
0968 send_call_function_single_ipi(last_cpu);
0969 else if (likely(nr_cpus > 1))
0970 arch_send_call_function_ipi_mask(cfd->cpumask_ipi);
0971
0972 cfd_seq_store(this_cpu_ptr(&cfd_seq_local)->pinged, this_cpu, CFD_SEQ_NOCPU, CFD_SEQ_PINGED);
0973 }
0974
0975 if (run_local && (!cond_func || cond_func(this_cpu, info))) {
0976 unsigned long flags;
0977
0978 local_irq_save(flags);
0979 func(info);
0980 local_irq_restore(flags);
0981 }
0982
0983 if (run_remote && wait) {
0984 for_each_cpu(cpu, cfd->cpumask) {
0985 call_single_data_t *csd;
0986
0987 csd = &per_cpu_ptr(cfd->pcpu, cpu)->csd;
0988 csd_lock_wait(csd);
0989 }
0990 }
0991 }
0992
0993
0994
0995
0996
0997
0998
0999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009 void smp_call_function_many(const struct cpumask *mask,
1010 smp_call_func_t func, void *info, bool wait)
1011 {
1012 smp_call_function_many_cond(mask, func, info, wait * SCF_WAIT, NULL);
1013 }
1014 EXPORT_SYMBOL(smp_call_function_many);
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031 void smp_call_function(smp_call_func_t func, void *info, int wait)
1032 {
1033 preempt_disable();
1034 smp_call_function_many(cpu_online_mask, func, info, wait);
1035 preempt_enable();
1036 }
1037 EXPORT_SYMBOL(smp_call_function);
1038
1039
1040 unsigned int setup_max_cpus = NR_CPUS;
1041 EXPORT_SYMBOL(setup_max_cpus);
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055 void __weak arch_disable_smp_support(void) { }
1056
1057 static int __init nosmp(char *str)
1058 {
1059 setup_max_cpus = 0;
1060 arch_disable_smp_support();
1061
1062 return 0;
1063 }
1064
1065 early_param("nosmp", nosmp);
1066
1067
1068 static int __init nrcpus(char *str)
1069 {
1070 int nr_cpus;
1071
1072 if (get_option(&str, &nr_cpus) && nr_cpus > 0 && nr_cpus < nr_cpu_ids)
1073 nr_cpu_ids = nr_cpus;
1074
1075 return 0;
1076 }
1077
1078 early_param("nr_cpus", nrcpus);
1079
1080 static int __init maxcpus(char *str)
1081 {
1082 get_option(&str, &setup_max_cpus);
1083 if (setup_max_cpus == 0)
1084 arch_disable_smp_support();
1085
1086 return 0;
1087 }
1088
1089 early_param("maxcpus", maxcpus);
1090
1091
1092 unsigned int nr_cpu_ids __read_mostly = NR_CPUS;
1093 EXPORT_SYMBOL(nr_cpu_ids);
1094
1095
1096 void __init setup_nr_cpu_ids(void)
1097 {
1098 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
1099 }
1100
1101
1102 void __init smp_init(void)
1103 {
1104 int num_nodes, num_cpus;
1105
1106 idle_threads_init();
1107 cpuhp_threads_init();
1108
1109 pr_info("Bringing up secondary CPUs ...\n");
1110
1111 bringup_nonboot_cpus(setup_max_cpus);
1112
1113 num_nodes = num_online_nodes();
1114 num_cpus = num_online_cpus();
1115 pr_info("Brought up %d node%s, %d CPU%s\n",
1116 num_nodes, (num_nodes > 1 ? "s" : ""),
1117 num_cpus, (num_cpus > 1 ? "s" : ""));
1118
1119
1120 smp_cpus_done(setup_max_cpus);
1121 }
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145 void on_each_cpu_cond_mask(smp_cond_func_t cond_func, smp_call_func_t func,
1146 void *info, bool wait, const struct cpumask *mask)
1147 {
1148 unsigned int scf_flags = SCF_RUN_LOCAL;
1149
1150 if (wait)
1151 scf_flags |= SCF_WAIT;
1152
1153 preempt_disable();
1154 smp_call_function_many_cond(mask, func, info, scf_flags, cond_func);
1155 preempt_enable();
1156 }
1157 EXPORT_SYMBOL(on_each_cpu_cond_mask);
1158
1159 static void do_nothing(void *unused)
1160 {
1161 }
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174 void kick_all_cpus_sync(void)
1175 {
1176
1177 smp_mb();
1178 smp_call_function(do_nothing, NULL, 1);
1179 }
1180 EXPORT_SYMBOL_GPL(kick_all_cpus_sync);
1181
1182
1183
1184
1185
1186
1187
1188 void wake_up_all_idle_cpus(void)
1189 {
1190 int cpu;
1191
1192 for_each_possible_cpu(cpu) {
1193 preempt_disable();
1194 if (cpu != smp_processor_id() && cpu_online(cpu))
1195 wake_up_if_idle(cpu);
1196 preempt_enable();
1197 }
1198 }
1199 EXPORT_SYMBOL_GPL(wake_up_all_idle_cpus);
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214 struct smp_call_on_cpu_struct {
1215 struct work_struct work;
1216 struct completion done;
1217 int (*func)(void *);
1218 void *data;
1219 int ret;
1220 int cpu;
1221 };
1222
1223 static void smp_call_on_cpu_callback(struct work_struct *work)
1224 {
1225 struct smp_call_on_cpu_struct *sscs;
1226
1227 sscs = container_of(work, struct smp_call_on_cpu_struct, work);
1228 if (sscs->cpu >= 0)
1229 hypervisor_pin_vcpu(sscs->cpu);
1230 sscs->ret = sscs->func(sscs->data);
1231 if (sscs->cpu >= 0)
1232 hypervisor_pin_vcpu(-1);
1233
1234 complete(&sscs->done);
1235 }
1236
1237 int smp_call_on_cpu(unsigned int cpu, int (*func)(void *), void *par, bool phys)
1238 {
1239 struct smp_call_on_cpu_struct sscs = {
1240 .done = COMPLETION_INITIALIZER_ONSTACK(sscs.done),
1241 .func = func,
1242 .data = par,
1243 .cpu = phys ? cpu : -1,
1244 };
1245
1246 INIT_WORK_ONSTACK(&sscs.work, smp_call_on_cpu_callback);
1247
1248 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
1249 return -ENXIO;
1250
1251 queue_work_on(cpu, system_wq, &sscs.work);
1252 wait_for_completion(&sscs.done);
1253
1254 return sscs.ret;
1255 }
1256 EXPORT_SYMBOL_GPL(smp_call_on_cpu);