lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* CPU control. |
| 2 | * (C) 2001, 2002, 2003, 2004 Rusty Russell |
| 3 | * |
| 4 | * This code is licenced under the GPL. |
| 5 | */ |
| 6 | #include <linux/proc_fs.h> |
| 7 | #include <linux/smp.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/notifier.h> |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/unistd.h> |
| 12 | #include <linux/cpu.h> |
| 13 | #include <linux/export.h> |
| 14 | #include <linux/kthread.h> |
| 15 | #include <linux/stop_machine.h> |
| 16 | #include <linux/mutex.h> |
| 17 | #include <linux/gfp.h> |
| 18 | #include <linux/suspend.h> |
| 19 | |
| 20 | #ifdef CONFIG_SMP |
| 21 | /* Serializes the updates to cpu_online_mask, cpu_present_mask */ |
| 22 | static DEFINE_MUTEX(cpu_add_remove_lock); |
| 23 | |
| 24 | /* |
| 25 | * The following two API's must be used when attempting |
| 26 | * to serialize the updates to cpu_online_mask, cpu_present_mask. |
| 27 | */ |
| 28 | void cpu_maps_update_begin(void) |
| 29 | { |
| 30 | mutex_lock(&cpu_add_remove_lock); |
| 31 | } |
| 32 | |
| 33 | void cpu_maps_update_done(void) |
| 34 | { |
| 35 | mutex_unlock(&cpu_add_remove_lock); |
| 36 | } |
| 37 | |
| 38 | static RAW_NOTIFIER_HEAD(cpu_chain); |
| 39 | |
| 40 | /* If set, cpu_up and cpu_down will return -EBUSY and do nothing. |
| 41 | * Should always be manipulated under cpu_add_remove_lock |
| 42 | */ |
| 43 | static int cpu_hotplug_disabled; |
| 44 | |
| 45 | #ifdef CONFIG_HOTPLUG_CPU |
| 46 | |
| 47 | static struct { |
| 48 | struct task_struct *active_writer; |
| 49 | struct mutex lock; /* Synchronizes accesses to refcount, */ |
| 50 | /* |
| 51 | * Also blocks the new readers during |
| 52 | * an ongoing cpu hotplug operation. |
| 53 | */ |
| 54 | int refcount; |
| 55 | } cpu_hotplug = { |
| 56 | .active_writer = NULL, |
| 57 | .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock), |
| 58 | .refcount = 0, |
| 59 | }; |
| 60 | |
| 61 | /** |
| 62 | * hotplug_pcp - per cpu hotplug descriptor |
| 63 | * @unplug: set when pin_current_cpu() needs to sync tasks |
| 64 | * @sync_tsk: the task that waits for tasks to finish pinned sections |
| 65 | * @refcount: counter of tasks in pinned sections |
| 66 | * @grab_lock: set when the tasks entering pinned sections should wait |
| 67 | * @synced: notifier for @sync_tsk to tell cpu_down it's finished |
| 68 | * @mutex: the mutex to make tasks wait (used when @grab_lock is true) |
| 69 | * @mutex_init: zero if the mutex hasn't been initialized yet. |
| 70 | * |
| 71 | * Although @unplug and @sync_tsk may point to the same task, the @unplug |
| 72 | * is used as a flag and still exists after @sync_tsk has exited and |
| 73 | * @sync_tsk set to NULL. |
| 74 | */ |
| 75 | struct hotplug_pcp { |
| 76 | struct task_struct *unplug; |
| 77 | struct task_struct *sync_tsk; |
| 78 | int refcount; |
| 79 | int grab_lock; |
| 80 | struct completion synced; |
| 81 | struct completion unplug_wait; |
| 82 | #ifdef CONFIG_PREEMPT_RT_FULL |
| 83 | spinlock_t lock; |
| 84 | #else |
| 85 | struct mutex mutex; |
| 86 | #endif |
| 87 | int mutex_init; |
| 88 | }; |
| 89 | |
| 90 | #ifdef CONFIG_PREEMPT_RT_FULL |
| 91 | # define hotplug_lock(hp) rt_spin_lock(&(hp)->lock) |
| 92 | # define hotplug_unlock(hp) rt_spin_unlock(&(hp)->lock) |
| 93 | #else |
| 94 | # define hotplug_lock(hp) mutex_lock(&(hp)->mutex) |
| 95 | # define hotplug_unlock(hp) mutex_unlock(&(hp)->mutex) |
| 96 | #endif |
| 97 | |
| 98 | static DEFINE_PER_CPU(struct hotplug_pcp, hotplug_pcp); |
| 99 | |
| 100 | /** |
| 101 | * pin_current_cpu - Prevent the current cpu from being unplugged |
| 102 | * |
| 103 | * Lightweight version of get_online_cpus() to prevent cpu from being |
| 104 | * unplugged when code runs in a migration disabled region. |
| 105 | * |
| 106 | * Must be called with preemption disabled (preempt_count = 1)! |
| 107 | */ |
| 108 | void pin_current_cpu(void) |
| 109 | { |
| 110 | struct hotplug_pcp *hp; |
| 111 | int force = 0; |
| 112 | |
| 113 | retry: |
| 114 | hp = &__get_cpu_var(hotplug_pcp); |
| 115 | |
| 116 | if (!hp->unplug || hp->refcount || force || preempt_count() > 1 || |
| 117 | hp->unplug == current || (current->flags & PF_STOMPER)) { |
| 118 | hp->refcount++; |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | if (hp->grab_lock) { |
| 123 | preempt_enable(); |
| 124 | hotplug_lock(hp); |
| 125 | hotplug_unlock(hp); |
| 126 | } else { |
| 127 | preempt_enable(); |
| 128 | /* |
| 129 | * Try to push this task off of this CPU. |
| 130 | */ |
| 131 | if (!migrate_me()) { |
| 132 | preempt_disable(); |
| 133 | hp = &__get_cpu_var(hotplug_pcp); |
| 134 | if (!hp->grab_lock) { |
| 135 | /* |
| 136 | * Just let it continue it's already pinned |
| 137 | * or about to sleep. |
| 138 | */ |
| 139 | force = 1; |
| 140 | goto retry; |
| 141 | } |
| 142 | preempt_enable(); |
| 143 | } |
| 144 | } |
| 145 | preempt_disable(); |
| 146 | goto retry; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * unpin_current_cpu - Allow unplug of current cpu |
| 151 | * |
| 152 | * Must be called with preemption or interrupts disabled! |
| 153 | */ |
| 154 | void unpin_current_cpu(void) |
| 155 | { |
| 156 | struct hotplug_pcp *hp = &__get_cpu_var(hotplug_pcp); |
| 157 | |
| 158 | WARN_ON(hp->refcount <= 0); |
| 159 | |
| 160 | /* This is safe. sync_unplug_thread is pinned to this cpu */ |
| 161 | if (!--hp->refcount && hp->unplug && hp->unplug != current && |
| 162 | !(current->flags & PF_STOMPER)) |
| 163 | wake_up_process(hp->unplug); |
| 164 | } |
| 165 | |
| 166 | static void wait_for_pinned_cpus(struct hotplug_pcp *hp) |
| 167 | { |
| 168 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 169 | while (hp->refcount) { |
| 170 | schedule_preempt_disabled(); |
| 171 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static int sync_unplug_thread(void *data) |
| 176 | { |
| 177 | struct hotplug_pcp *hp = data; |
| 178 | |
| 179 | wait_for_completion(&hp->unplug_wait); |
| 180 | preempt_disable(); |
| 181 | hp->unplug = current; |
| 182 | wait_for_pinned_cpus(hp); |
| 183 | |
| 184 | /* |
| 185 | * This thread will synchronize the cpu_down() with threads |
| 186 | * that have pinned the CPU. When the pinned CPU count reaches |
| 187 | * zero, we inform the cpu_down code to continue to the next step. |
| 188 | */ |
| 189 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 190 | preempt_enable(); |
| 191 | complete(&hp->synced); |
| 192 | |
| 193 | /* |
| 194 | * If all succeeds, the next step will need tasks to wait till |
| 195 | * the CPU is offline before continuing. To do this, the grab_lock |
| 196 | * is set and tasks going into pin_current_cpu() will block on the |
| 197 | * mutex. But we still need to wait for those that are already in |
| 198 | * pinned CPU sections. If the cpu_down() failed, the kthread_should_stop() |
| 199 | * will kick this thread out. |
| 200 | */ |
| 201 | while (!hp->grab_lock && !kthread_should_stop()) { |
| 202 | schedule(); |
| 203 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 204 | } |
| 205 | |
| 206 | /* Make sure grab_lock is seen before we see a stale completion */ |
| 207 | smp_mb(); |
| 208 | |
| 209 | /* |
| 210 | * Now just before cpu_down() enters stop machine, we need to make |
| 211 | * sure all tasks that are in pinned CPU sections are out, and new |
| 212 | * tasks will now grab the lock, keeping them from entering pinned |
| 213 | * CPU sections. |
| 214 | */ |
| 215 | if (!kthread_should_stop()) { |
| 216 | preempt_disable(); |
| 217 | wait_for_pinned_cpus(hp); |
| 218 | preempt_enable(); |
| 219 | complete(&hp->synced); |
| 220 | } |
| 221 | |
| 222 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 223 | while (!kthread_should_stop()) { |
| 224 | schedule(); |
| 225 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 226 | } |
| 227 | set_current_state(TASK_RUNNING); |
| 228 | |
| 229 | /* |
| 230 | * Force this thread off this CPU as it's going down and |
| 231 | * we don't want any more work on this CPU. |
| 232 | */ |
| 233 | current->flags &= ~PF_THREAD_BOUND; |
| 234 | do_set_cpus_allowed(current, cpu_present_mask); |
| 235 | migrate_me(); |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static void __cpu_unplug_sync(struct hotplug_pcp *hp) |
| 240 | { |
| 241 | wake_up_process(hp->sync_tsk); |
| 242 | wait_for_completion(&hp->synced); |
| 243 | } |
| 244 | |
| 245 | static void __cpu_unplug_wait(unsigned int cpu) |
| 246 | { |
| 247 | struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu); |
| 248 | |
| 249 | complete(&hp->unplug_wait); |
| 250 | wait_for_completion(&hp->synced); |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Start the sync_unplug_thread on the target cpu and wait for it to |
| 255 | * complete. |
| 256 | */ |
| 257 | static int cpu_unplug_begin(unsigned int cpu) |
| 258 | { |
| 259 | struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu); |
| 260 | int err; |
| 261 | |
| 262 | /* Protected by cpu_hotplug.lock */ |
| 263 | if (!hp->mutex_init) { |
| 264 | #ifdef CONFIG_PREEMPT_RT_FULL |
| 265 | spin_lock_init(&hp->lock); |
| 266 | #else |
| 267 | mutex_init(&hp->mutex); |
| 268 | #endif |
| 269 | hp->mutex_init = 1; |
| 270 | } |
| 271 | |
| 272 | /* Inform the scheduler to migrate tasks off this CPU */ |
| 273 | tell_sched_cpu_down_begin(cpu); |
| 274 | |
| 275 | init_completion(&hp->synced); |
| 276 | init_completion(&hp->unplug_wait); |
| 277 | |
| 278 | hp->sync_tsk = kthread_create(sync_unplug_thread, hp, "sync_unplug/%d", cpu); |
| 279 | if (IS_ERR(hp->sync_tsk)) { |
| 280 | err = PTR_ERR(hp->sync_tsk); |
| 281 | hp->sync_tsk = NULL; |
| 282 | return err; |
| 283 | } |
| 284 | kthread_bind(hp->sync_tsk, cpu); |
| 285 | |
| 286 | /* |
| 287 | * Wait for tasks to get out of the pinned sections, |
| 288 | * it's still OK if new tasks enter. Some CPU notifiers will |
| 289 | * wait for tasks that are going to enter these sections and |
| 290 | * we must not have them block. |
| 291 | */ |
| 292 | wake_up_process(hp->sync_tsk); |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static void cpu_unplug_sync(unsigned int cpu) |
| 297 | { |
| 298 | struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu); |
| 299 | |
| 300 | init_completion(&hp->synced); |
| 301 | /* The completion needs to be initialzied before setting grab_lock */ |
| 302 | smp_wmb(); |
| 303 | |
| 304 | /* Grab the mutex before setting grab_lock */ |
| 305 | hotplug_lock(hp); |
| 306 | hp->grab_lock = 1; |
| 307 | |
| 308 | /* |
| 309 | * The CPU notifiers have been completed. |
| 310 | * Wait for tasks to get out of pinned CPU sections and have new |
| 311 | * tasks block until the CPU is completely down. |
| 312 | */ |
| 313 | __cpu_unplug_sync(hp); |
| 314 | |
| 315 | /* All done with the sync thread */ |
| 316 | kthread_stop(hp->sync_tsk); |
| 317 | hp->sync_tsk = NULL; |
| 318 | } |
| 319 | |
| 320 | static void cpu_unplug_done(unsigned int cpu) |
| 321 | { |
| 322 | struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu); |
| 323 | |
| 324 | hp->unplug = NULL; |
| 325 | /* Let all tasks know cpu unplug is finished before cleaning up */ |
| 326 | smp_wmb(); |
| 327 | |
| 328 | if (hp->sync_tsk) |
| 329 | kthread_stop(hp->sync_tsk); |
| 330 | |
| 331 | if (hp->grab_lock) { |
| 332 | hotplug_unlock(hp); |
| 333 | /* protected by cpu_hotplug.lock */ |
| 334 | hp->grab_lock = 0; |
| 335 | } |
| 336 | tell_sched_cpu_down_done(cpu); |
| 337 | } |
| 338 | |
| 339 | void get_online_cpus(void) |
| 340 | { |
| 341 | might_sleep(); |
| 342 | if (cpu_hotplug.active_writer == current) |
| 343 | return; |
| 344 | mutex_lock(&cpu_hotplug.lock); |
| 345 | cpu_hotplug.refcount++; |
| 346 | mutex_unlock(&cpu_hotplug.lock); |
| 347 | |
| 348 | } |
| 349 | EXPORT_SYMBOL_GPL(get_online_cpus); |
| 350 | |
| 351 | void put_online_cpus(void) |
| 352 | { |
| 353 | if (cpu_hotplug.active_writer == current) |
| 354 | return; |
| 355 | mutex_lock(&cpu_hotplug.lock); |
| 356 | if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer)) |
| 357 | wake_up_process(cpu_hotplug.active_writer); |
| 358 | mutex_unlock(&cpu_hotplug.lock); |
| 359 | |
| 360 | } |
| 361 | EXPORT_SYMBOL_GPL(put_online_cpus); |
| 362 | |
| 363 | /* |
| 364 | * This ensures that the hotplug operation can begin only when the |
| 365 | * refcount goes to zero. |
| 366 | * |
| 367 | * Note that during a cpu-hotplug operation, the new readers, if any, |
| 368 | * will be blocked by the cpu_hotplug.lock |
| 369 | * |
| 370 | * Since cpu_hotplug_begin() is always called after invoking |
| 371 | * cpu_maps_update_begin(), we can be sure that only one writer is active. |
| 372 | * |
| 373 | * Note that theoretically, there is a possibility of a livelock: |
| 374 | * - Refcount goes to zero, last reader wakes up the sleeping |
| 375 | * writer. |
| 376 | * - Last reader unlocks the cpu_hotplug.lock. |
| 377 | * - A new reader arrives at this moment, bumps up the refcount. |
| 378 | * - The writer acquires the cpu_hotplug.lock finds the refcount |
| 379 | * non zero and goes to sleep again. |
| 380 | * |
| 381 | * However, this is very difficult to achieve in practice since |
| 382 | * get_online_cpus() not an api which is called all that often. |
| 383 | * |
| 384 | */ |
| 385 | static void cpu_hotplug_begin(void) |
| 386 | { |
| 387 | cpu_hotplug.active_writer = current; |
| 388 | |
| 389 | for (;;) { |
| 390 | mutex_lock(&cpu_hotplug.lock); |
| 391 | if (likely(!cpu_hotplug.refcount)) |
| 392 | break; |
| 393 | __set_current_state(TASK_UNINTERRUPTIBLE); |
| 394 | mutex_unlock(&cpu_hotplug.lock); |
| 395 | schedule(); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | static void cpu_hotplug_done(void) |
| 400 | { |
| 401 | cpu_hotplug.active_writer = NULL; |
| 402 | mutex_unlock(&cpu_hotplug.lock); |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Wait for currently running CPU hotplug operations to complete (if any) and |
| 407 | * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects |
| 408 | * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the |
| 409 | * hotplug path before performing hotplug operations. So acquiring that lock |
| 410 | * guarantees mutual exclusion from any currently running hotplug operations. |
| 411 | */ |
| 412 | void cpu_hotplug_disable(void) |
| 413 | { |
| 414 | cpu_maps_update_begin(); |
| 415 | cpu_hotplug_disabled = 1; |
| 416 | cpu_maps_update_done(); |
| 417 | } |
| 418 | |
| 419 | void cpu_hotplug_enable(void) |
| 420 | { |
| 421 | cpu_maps_update_begin(); |
| 422 | cpu_hotplug_disabled = 0; |
| 423 | cpu_maps_update_done(); |
| 424 | } |
| 425 | |
| 426 | #else /* #if CONFIG_HOTPLUG_CPU */ |
| 427 | static void cpu_hotplug_begin(void) {} |
| 428 | static void cpu_hotplug_done(void) {} |
| 429 | #endif /* #else #if CONFIG_HOTPLUG_CPU */ |
| 430 | |
| 431 | /* Need to know about CPUs going up/down? */ |
| 432 | int __ref register_cpu_notifier(struct notifier_block *nb) |
| 433 | { |
| 434 | int ret; |
| 435 | cpu_maps_update_begin(); |
| 436 | ret = raw_notifier_chain_register(&cpu_chain, nb); |
| 437 | cpu_maps_update_done(); |
| 438 | return ret; |
| 439 | } |
| 440 | |
| 441 | static int __cpu_notify(unsigned long val, void *v, int nr_to_call, |
| 442 | int *nr_calls) |
| 443 | { |
| 444 | int ret; |
| 445 | |
| 446 | ret = __raw_notifier_call_chain(&cpu_chain, val, v, nr_to_call, |
| 447 | nr_calls); |
| 448 | |
| 449 | return notifier_to_errno(ret); |
| 450 | } |
| 451 | |
| 452 | static int cpu_notify(unsigned long val, void *v) |
| 453 | { |
| 454 | return __cpu_notify(val, v, -1, NULL); |
| 455 | } |
| 456 | |
| 457 | #ifdef CONFIG_HOTPLUG_CPU |
| 458 | |
| 459 | static void cpu_notify_nofail(unsigned long val, void *v) |
| 460 | { |
| 461 | BUG_ON(cpu_notify(val, v)); |
| 462 | } |
| 463 | EXPORT_SYMBOL(register_cpu_notifier); |
| 464 | |
| 465 | void __ref unregister_cpu_notifier(struct notifier_block *nb) |
| 466 | { |
| 467 | cpu_maps_update_begin(); |
| 468 | raw_notifier_chain_unregister(&cpu_chain, nb); |
| 469 | cpu_maps_update_done(); |
| 470 | } |
| 471 | EXPORT_SYMBOL(unregister_cpu_notifier); |
| 472 | |
| 473 | static inline void check_for_tasks(int cpu) |
| 474 | { |
| 475 | struct task_struct *p; |
| 476 | |
| 477 | write_lock_irq(&tasklist_lock); |
| 478 | for_each_process(p) { |
| 479 | if (task_cpu(p) == cpu && p->state == TASK_RUNNING && |
| 480 | (p->utime || p->stime)) |
| 481 | printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d " |
| 482 | "(state = %ld, flags = %x)\n", |
| 483 | p->comm, task_pid_nr(p), cpu, |
| 484 | p->state, p->flags); |
| 485 | } |
| 486 | write_unlock_irq(&tasklist_lock); |
| 487 | } |
| 488 | |
| 489 | struct take_cpu_down_param { |
| 490 | unsigned long mod; |
| 491 | void *hcpu; |
| 492 | }; |
| 493 | |
| 494 | /* Take this CPU down. */ |
| 495 | static int __ref take_cpu_down(void *_param) |
| 496 | { |
| 497 | struct take_cpu_down_param *param = _param; |
| 498 | int err; |
| 499 | |
| 500 | /* Ensure this CPU doesn't handle any more interrupts. */ |
| 501 | err = __cpu_disable(); |
| 502 | if (err < 0) |
| 503 | return err; |
| 504 | |
| 505 | cpu_notify(CPU_DYING | param->mod, param->hcpu); |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | /* Requires cpu_add_remove_lock to be held */ |
| 510 | static int __ref _cpu_down(unsigned int cpu, int tasks_frozen) |
| 511 | { |
| 512 | int mycpu, err, nr_calls = 0; |
| 513 | void *hcpu = (void *)(long)cpu; |
| 514 | unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0; |
| 515 | struct take_cpu_down_param tcd_param = { |
| 516 | .mod = mod, |
| 517 | .hcpu = hcpu, |
| 518 | }; |
| 519 | cpumask_var_t cpumask; |
| 520 | cpumask_var_t cpumask_org; |
| 521 | |
| 522 | if (num_online_cpus() == 1) |
| 523 | return -EBUSY; |
| 524 | |
| 525 | if (!cpu_online(cpu)) |
| 526 | return -EINVAL; |
| 527 | |
| 528 | /* Move the downtaker off the unplug cpu */ |
| 529 | if (!alloc_cpumask_var(&cpumask, GFP_KERNEL)) |
| 530 | return -ENOMEM; |
| 531 | if (!alloc_cpumask_var(&cpumask_org, GFP_KERNEL)) { |
| 532 | free_cpumask_var(cpumask); |
| 533 | return -ENOMEM; |
| 534 | } |
| 535 | |
| 536 | cpumask_copy(cpumask_org, tsk_cpus_allowed(current)); |
| 537 | cpumask_andnot(cpumask, cpu_online_mask, cpumask_of(cpu)); |
| 538 | set_cpus_allowed_ptr(current, cpumask); |
| 539 | free_cpumask_var(cpumask); |
| 540 | migrate_disable(); |
| 541 | mycpu = smp_processor_id(); |
| 542 | if (mycpu == cpu) { |
| 543 | printk(KERN_ERR "Yuck! Still on unplug CPU\n!"); |
| 544 | migrate_enable(); |
| 545 | err = -EBUSY; |
| 546 | goto restore_cpus; |
| 547 | } |
| 548 | migrate_enable(); |
| 549 | |
| 550 | cpu_hotplug_begin(); |
| 551 | err = cpu_unplug_begin(cpu); |
| 552 | if (err) { |
| 553 | printk("cpu_unplug_begin(%d) failed\n", cpu); |
| 554 | goto out_cancel; |
| 555 | } |
| 556 | |
| 557 | err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls); |
| 558 | if (err) { |
| 559 | nr_calls--; |
| 560 | __cpu_notify(CPU_DOWN_FAILED | mod, hcpu, nr_calls, NULL); |
| 561 | printk("%s: attempt to take down CPU %u failed\n", |
| 562 | __func__, cpu); |
| 563 | goto out_release; |
| 564 | } |
| 565 | |
| 566 | __cpu_unplug_wait(cpu); |
| 567 | |
| 568 | /* Notifiers are done. Don't let any more tasks pin this CPU. */ |
| 569 | cpu_unplug_sync(cpu); |
| 570 | |
| 571 | err = __stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu)); |
| 572 | if (err) { |
| 573 | /* CPU didn't die: tell everyone. Can't complain. */ |
| 574 | cpu_notify_nofail(CPU_DOWN_FAILED | mod, hcpu); |
| 575 | |
| 576 | goto out_release; |
| 577 | } |
| 578 | BUG_ON(cpu_online(cpu)); |
| 579 | |
| 580 | /* |
| 581 | * The migration_call() CPU_DYING callback will have removed all |
| 582 | * runnable tasks from the cpu, there's only the idle task left now |
| 583 | * that the migration thread is done doing the stop_machine thing. |
| 584 | * |
| 585 | * Wait for the stop thread to go away. |
| 586 | */ |
| 587 | while (!idle_cpu(cpu)) |
| 588 | cpu_relax(); |
| 589 | |
| 590 | /* This actually kills the CPU. */ |
| 591 | __cpu_die(cpu); |
| 592 | |
| 593 | /* CPU is completely dead: tell everyone. Too late to complain. */ |
| 594 | cpu_notify_nofail(CPU_DEAD | mod, hcpu); |
| 595 | |
| 596 | check_for_tasks(cpu); |
| 597 | |
| 598 | out_release: |
| 599 | cpu_unplug_done(cpu); |
| 600 | out_cancel: |
| 601 | cpu_hotplug_done(); |
| 602 | if (!err) |
| 603 | cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu); |
| 604 | restore_cpus: |
| 605 | set_cpus_allowed_ptr(current, cpumask_org); |
| 606 | free_cpumask_var(cpumask_org); |
| 607 | return err; |
| 608 | } |
| 609 | |
| 610 | int __ref cpu_down(unsigned int cpu) |
| 611 | { |
| 612 | int err; |
| 613 | |
| 614 | cpu_maps_update_begin(); |
| 615 | |
| 616 | if (cpu_hotplug_disabled) { |
| 617 | err = -EBUSY; |
| 618 | goto out; |
| 619 | } |
| 620 | |
| 621 | err = _cpu_down(cpu, 0); |
| 622 | |
| 623 | out: |
| 624 | cpu_maps_update_done(); |
| 625 | return err; |
| 626 | } |
| 627 | EXPORT_SYMBOL(cpu_down); |
| 628 | #endif /*CONFIG_HOTPLUG_CPU*/ |
| 629 | |
| 630 | /* Requires cpu_add_remove_lock to be held */ |
| 631 | static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen) |
| 632 | { |
| 633 | int ret, nr_calls = 0; |
| 634 | void *hcpu = (void *)(long)cpu; |
| 635 | unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0; |
| 636 | |
| 637 | if (cpu_online(cpu) || !cpu_present(cpu)) |
| 638 | return -EINVAL; |
| 639 | |
| 640 | cpu_hotplug_begin(); |
| 641 | ret = __cpu_notify(CPU_UP_PREPARE | mod, hcpu, -1, &nr_calls); |
| 642 | if (ret) { |
| 643 | nr_calls--; |
| 644 | printk(KERN_WARNING "%s: attempt to bring up CPU %u failed\n", |
| 645 | __func__, cpu); |
| 646 | goto out_notify; |
| 647 | } |
| 648 | |
| 649 | /* Arch-specific enabling code. */ |
| 650 | ret = __cpu_up(cpu); |
| 651 | if (ret != 0) |
| 652 | goto out_notify; |
| 653 | BUG_ON(!cpu_online(cpu)); |
| 654 | |
| 655 | /* Now call notifier in preparation. */ |
| 656 | cpu_notify(CPU_ONLINE | mod, hcpu); |
| 657 | |
| 658 | out_notify: |
| 659 | if (ret != 0) |
| 660 | __cpu_notify(CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL); |
| 661 | cpu_hotplug_done(); |
| 662 | |
| 663 | return ret; |
| 664 | } |
| 665 | |
| 666 | int __cpuinit cpu_up(unsigned int cpu) |
| 667 | { |
| 668 | int err = 0; |
| 669 | |
| 670 | #ifdef CONFIG_MEMORY_HOTPLUG |
| 671 | int nid; |
| 672 | pg_data_t *pgdat; |
| 673 | #endif |
| 674 | |
| 675 | if (!cpu_possible(cpu)) { |
| 676 | printk(KERN_ERR "can't online cpu %d because it is not " |
| 677 | "configured as may-hotadd at boot time\n", cpu); |
| 678 | #if defined(CONFIG_IA64) |
| 679 | printk(KERN_ERR "please check additional_cpus= boot " |
| 680 | "parameter\n"); |
| 681 | #endif |
| 682 | return -EINVAL; |
| 683 | } |
| 684 | |
| 685 | #ifdef CONFIG_MEMORY_HOTPLUG |
| 686 | nid = cpu_to_node(cpu); |
| 687 | if (!node_online(nid)) { |
| 688 | err = mem_online_node(nid); |
| 689 | if (err) |
| 690 | return err; |
| 691 | } |
| 692 | |
| 693 | pgdat = NODE_DATA(nid); |
| 694 | if (!pgdat) { |
| 695 | printk(KERN_ERR |
| 696 | "Can't online cpu %d due to NULL pgdat\n", cpu); |
| 697 | return -ENOMEM; |
| 698 | } |
| 699 | |
| 700 | if (pgdat->node_zonelists->_zonerefs->zone == NULL) { |
| 701 | mutex_lock(&zonelists_mutex); |
| 702 | build_all_zonelists(NULL); |
| 703 | mutex_unlock(&zonelists_mutex); |
| 704 | } |
| 705 | #endif |
| 706 | |
| 707 | cpu_maps_update_begin(); |
| 708 | |
| 709 | if (cpu_hotplug_disabled) { |
| 710 | err = -EBUSY; |
| 711 | goto out; |
| 712 | } |
| 713 | |
| 714 | err = _cpu_up(cpu, 0); |
| 715 | |
| 716 | out: |
| 717 | cpu_maps_update_done(); |
| 718 | return err; |
| 719 | } |
| 720 | EXPORT_SYMBOL_GPL(cpu_up); |
| 721 | |
| 722 | #ifdef CONFIG_PM_SLEEP_SMP |
| 723 | static cpumask_var_t frozen_cpus; |
| 724 | |
| 725 | void __weak arch_disable_nonboot_cpus_begin(void) |
| 726 | { |
| 727 | } |
| 728 | |
| 729 | void __weak arch_disable_nonboot_cpus_end(void) |
| 730 | { |
| 731 | } |
| 732 | |
| 733 | int disable_nonboot_cpus(void) |
| 734 | { |
| 735 | int cpu, first_cpu, error = 0; |
| 736 | |
| 737 | cpu_maps_update_begin(); |
| 738 | first_cpu = cpumask_first(cpu_online_mask); |
| 739 | /* |
| 740 | * We take down all of the non-boot CPUs in one shot to avoid races |
| 741 | * with the userspace trying to use the CPU hotplug at the same time |
| 742 | */ |
| 743 | cpumask_clear(frozen_cpus); |
| 744 | arch_disable_nonboot_cpus_begin(); |
| 745 | |
| 746 | printk("Disabling non-boot CPUs ...\n"); |
| 747 | for_each_online_cpu(cpu) { |
| 748 | if (cpu == first_cpu) |
| 749 | continue; |
| 750 | error = _cpu_down(cpu, 1); |
| 751 | if (!error) |
| 752 | cpumask_set_cpu(cpu, frozen_cpus); |
| 753 | else { |
| 754 | printk(KERN_ERR "Error taking CPU%d down: %d\n", |
| 755 | cpu, error); |
| 756 | break; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | arch_disable_nonboot_cpus_end(); |
| 761 | |
| 762 | if (!error) { |
| 763 | BUG_ON(num_online_cpus() > 1); |
| 764 | /* Make sure the CPUs won't be enabled by someone else */ |
| 765 | cpu_hotplug_disabled = 1; |
| 766 | } else { |
| 767 | printk(KERN_ERR "Non-boot CPUs are not disabled\n"); |
| 768 | } |
| 769 | cpu_maps_update_done(); |
| 770 | return error; |
| 771 | } |
| 772 | |
| 773 | void __weak arch_enable_nonboot_cpus_begin(void) |
| 774 | { |
| 775 | } |
| 776 | |
| 777 | void __weak arch_enable_nonboot_cpus_end(void) |
| 778 | { |
| 779 | } |
| 780 | |
| 781 | void __ref enable_nonboot_cpus(void) |
| 782 | { |
| 783 | int cpu, error; |
| 784 | |
| 785 | /* Allow everyone to use the CPU hotplug again */ |
| 786 | cpu_maps_update_begin(); |
| 787 | cpu_hotplug_disabled = 0; |
| 788 | if (cpumask_empty(frozen_cpus)) |
| 789 | goto out; |
| 790 | |
| 791 | printk(KERN_INFO "Enabling non-boot CPUs ...\n"); |
| 792 | |
| 793 | arch_enable_nonboot_cpus_begin(); |
| 794 | |
| 795 | for_each_cpu(cpu, frozen_cpus) { |
| 796 | error = _cpu_up(cpu, 1); |
| 797 | if (!error) { |
| 798 | printk(KERN_INFO "CPU%d is up\n", cpu); |
| 799 | continue; |
| 800 | } |
| 801 | printk(KERN_WARNING "Error taking CPU%d up: %d\n", cpu, error); |
| 802 | } |
| 803 | |
| 804 | arch_enable_nonboot_cpus_end(); |
| 805 | |
| 806 | cpumask_clear(frozen_cpus); |
| 807 | out: |
| 808 | cpu_maps_update_done(); |
| 809 | } |
| 810 | |
| 811 | static int __init alloc_frozen_cpus(void) |
| 812 | { |
| 813 | if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO)) |
| 814 | return -ENOMEM; |
| 815 | return 0; |
| 816 | } |
| 817 | core_initcall(alloc_frozen_cpus); |
| 818 | |
| 819 | /* |
| 820 | * When callbacks for CPU hotplug notifications are being executed, we must |
| 821 | * ensure that the state of the system with respect to the tasks being frozen |
| 822 | * or not, as reported by the notification, remains unchanged *throughout the |
| 823 | * duration* of the execution of the callbacks. |
| 824 | * Hence we need to prevent the freezer from racing with regular CPU hotplug. |
| 825 | * |
| 826 | * This synchronization is implemented by mutually excluding regular CPU |
| 827 | * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/ |
| 828 | * Hibernate notifications. |
| 829 | */ |
| 830 | static int |
| 831 | cpu_hotplug_pm_callback(struct notifier_block *nb, |
| 832 | unsigned long action, void *ptr) |
| 833 | { |
| 834 | switch (action) { |
| 835 | |
| 836 | case PM_SUSPEND_PREPARE: |
| 837 | case PM_HIBERNATION_PREPARE: |
| 838 | cpu_hotplug_disable(); |
| 839 | break; |
| 840 | |
| 841 | case PM_POST_SUSPEND: |
| 842 | case PM_POST_HIBERNATION: |
| 843 | cpu_hotplug_enable(); |
| 844 | break; |
| 845 | |
| 846 | default: |
| 847 | return NOTIFY_DONE; |
| 848 | } |
| 849 | |
| 850 | return NOTIFY_OK; |
| 851 | } |
| 852 | |
| 853 | |
| 854 | static int __init cpu_hotplug_pm_sync_init(void) |
| 855 | { |
| 856 | pm_notifier(cpu_hotplug_pm_callback, 0); |
| 857 | return 0; |
| 858 | } |
| 859 | core_initcall(cpu_hotplug_pm_sync_init); |
| 860 | |
| 861 | #endif /* CONFIG_PM_SLEEP_SMP */ |
| 862 | |
| 863 | /** |
| 864 | * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers |
| 865 | * @cpu: cpu that just started |
| 866 | * |
| 867 | * This function calls the cpu_chain notifiers with CPU_STARTING. |
| 868 | * It must be called by the arch code on the new cpu, before the new cpu |
| 869 | * enables interrupts and before the "boot" cpu returns from __cpu_up(). |
| 870 | */ |
| 871 | void __cpuinit notify_cpu_starting(unsigned int cpu) |
| 872 | { |
| 873 | unsigned long val = CPU_STARTING; |
| 874 | |
| 875 | #ifdef CONFIG_PM_SLEEP_SMP |
| 876 | if (frozen_cpus != NULL && cpumask_test_cpu(cpu, frozen_cpus)) |
| 877 | val = CPU_STARTING_FROZEN; |
| 878 | #endif /* CONFIG_PM_SLEEP_SMP */ |
| 879 | cpu_notify(val, (void *)(long)cpu); |
| 880 | } |
| 881 | |
| 882 | #endif /* CONFIG_SMP */ |
| 883 | |
| 884 | /* |
| 885 | * cpu_bit_bitmap[] is a special, "compressed" data structure that |
| 886 | * represents all NR_CPUS bits binary values of 1<<nr. |
| 887 | * |
| 888 | * It is used by cpumask_of() to get a constant address to a CPU |
| 889 | * mask value that has a single bit set only. |
| 890 | */ |
| 891 | |
| 892 | /* cpu_bit_bitmap[0] is empty - so we can back into it */ |
| 893 | #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x)) |
| 894 | #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1) |
| 895 | #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2) |
| 896 | #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4) |
| 897 | |
| 898 | const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = { |
| 899 | |
| 900 | MASK_DECLARE_8(0), MASK_DECLARE_8(8), |
| 901 | MASK_DECLARE_8(16), MASK_DECLARE_8(24), |
| 902 | #if BITS_PER_LONG > 32 |
| 903 | MASK_DECLARE_8(32), MASK_DECLARE_8(40), |
| 904 | MASK_DECLARE_8(48), MASK_DECLARE_8(56), |
| 905 | #endif |
| 906 | }; |
| 907 | EXPORT_SYMBOL_GPL(cpu_bit_bitmap); |
| 908 | |
| 909 | const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL; |
| 910 | EXPORT_SYMBOL(cpu_all_bits); |
| 911 | |
| 912 | #ifdef CONFIG_INIT_ALL_POSSIBLE |
| 913 | static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly |
| 914 | = CPU_BITS_ALL; |
| 915 | #else |
| 916 | static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly; |
| 917 | #endif |
| 918 | const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits); |
| 919 | EXPORT_SYMBOL(cpu_possible_mask); |
| 920 | |
| 921 | static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly; |
| 922 | const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits); |
| 923 | EXPORT_SYMBOL(cpu_online_mask); |
| 924 | |
| 925 | static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly; |
| 926 | const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits); |
| 927 | EXPORT_SYMBOL(cpu_present_mask); |
| 928 | |
| 929 | static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly; |
| 930 | const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits); |
| 931 | EXPORT_SYMBOL(cpu_active_mask); |
| 932 | |
| 933 | void set_cpu_possible(unsigned int cpu, bool possible) |
| 934 | { |
| 935 | if (possible) |
| 936 | cpumask_set_cpu(cpu, to_cpumask(cpu_possible_bits)); |
| 937 | else |
| 938 | cpumask_clear_cpu(cpu, to_cpumask(cpu_possible_bits)); |
| 939 | } |
| 940 | |
| 941 | void set_cpu_present(unsigned int cpu, bool present) |
| 942 | { |
| 943 | if (present) |
| 944 | cpumask_set_cpu(cpu, to_cpumask(cpu_present_bits)); |
| 945 | else |
| 946 | cpumask_clear_cpu(cpu, to_cpumask(cpu_present_bits)); |
| 947 | } |
| 948 | |
| 949 | void set_cpu_online(unsigned int cpu, bool online) |
| 950 | { |
| 951 | if (online) { |
| 952 | cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits)); |
| 953 | cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits)); |
| 954 | } else { |
| 955 | cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits)); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | void set_cpu_active(unsigned int cpu, bool active) |
| 960 | { |
| 961 | if (active) |
| 962 | cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits)); |
| 963 | else |
| 964 | cpumask_clear_cpu(cpu, to_cpumask(cpu_active_bits)); |
| 965 | } |
| 966 | |
| 967 | void init_cpu_present(const struct cpumask *src) |
| 968 | { |
| 969 | cpumask_copy(to_cpumask(cpu_present_bits), src); |
| 970 | } |
| 971 | |
| 972 | void init_cpu_possible(const struct cpumask *src) |
| 973 | { |
| 974 | cpumask_copy(to_cpumask(cpu_possible_bits), src); |
| 975 | } |
| 976 | |
| 977 | void init_cpu_online(const struct cpumask *src) |
| 978 | { |
| 979 | cpumask_copy(to_cpumask(cpu_online_bits), src); |
| 980 | } |
| 981 | |
| 982 | static ATOMIC_NOTIFIER_HEAD(idle_notifier); |
| 983 | |
| 984 | void idle_notifier_register(struct notifier_block *n) |
| 985 | { |
| 986 | atomic_notifier_chain_register(&idle_notifier, n); |
| 987 | } |
| 988 | EXPORT_SYMBOL_GPL(idle_notifier_register); |
| 989 | |
| 990 | void idle_notifier_unregister(struct notifier_block *n) |
| 991 | { |
| 992 | atomic_notifier_chain_unregister(&idle_notifier, n); |
| 993 | } |
| 994 | EXPORT_SYMBOL_GPL(idle_notifier_unregister); |
| 995 | |
| 996 | void idle_notifier_call_chain(unsigned long val) |
| 997 | { |
| 998 | atomic_notifier_call_chain(&idle_notifier, val, NULL); |
| 999 | } |
| 1000 | EXPORT_SYMBOL_GPL(idle_notifier_call_chain); |