cpu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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/oom.h>
  14. #include <linux/rcupdate.h>
  15. #include <linux/export.h>
  16. #include <linux/bug.h>
  17. #include <linux/kthread.h>
  18. #include <linux/stop_machine.h>
  19. #include <linux/mutex.h>
  20. #include <linux/gfp.h>
  21. #include <linux/suspend.h>
  22. #include <linux/lockdep.h>
  23. #include <trace/events/power.h>
  24. #include "smpboot.h"
  25. #include "mt_sched_mon.h"
  26. #ifdef CONFIG_SMP
  27. /* Serializes the updates to cpu_online_mask, cpu_present_mask */
  28. static DEFINE_MUTEX(cpu_add_remove_lock);
  29. /*
  30. * The following two APIs (cpu_maps_update_begin/done) must be used when
  31. * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
  32. * The APIs cpu_notifier_register_begin/done() must be used to protect CPU
  33. * hotplug callback (un)registration performed using __register_cpu_notifier()
  34. * or __unregister_cpu_notifier().
  35. */
  36. void cpu_maps_update_begin(void)
  37. {
  38. mutex_lock(&cpu_add_remove_lock);
  39. }
  40. EXPORT_SYMBOL(cpu_notifier_register_begin);
  41. void cpu_maps_update_done(void)
  42. {
  43. mutex_unlock(&cpu_add_remove_lock);
  44. }
  45. EXPORT_SYMBOL(cpu_notifier_register_done);
  46. #if defined(MTK_CPU_HOTPLUG_DEBUG_1) || defined(MTK_CPU_HOTPLUG_DEBUG_2)
  47. RAW_NOTIFIER_HEAD(cpu_chain);
  48. #else
  49. static RAW_NOTIFIER_HEAD(cpu_chain);
  50. #endif
  51. /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
  52. * Should always be manipulated under cpu_add_remove_lock
  53. */
  54. static int cpu_hotplug_disabled;
  55. #ifdef CONFIG_HOTPLUG_CPU
  56. static struct {
  57. struct task_struct *active_writer;
  58. /* wait queue to wake up the active_writer */
  59. wait_queue_head_t wq;
  60. /* verifies that no writer will get active while readers are active */
  61. struct mutex lock;
  62. /*
  63. * Also blocks the new readers during
  64. * an ongoing cpu hotplug operation.
  65. */
  66. atomic_t refcount;
  67. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  68. struct lockdep_map dep_map;
  69. #endif
  70. } cpu_hotplug = {
  71. .active_writer = NULL,
  72. .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  73. .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
  74. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  75. .dep_map = {.name = "cpu_hotplug.lock" },
  76. #endif
  77. };
  78. /* Lockdep annotations for get/put_online_cpus() and cpu_hotplug_begin/end() */
  79. #define cpuhp_lock_acquire_read() lock_map_acquire_read(&cpu_hotplug.dep_map)
  80. #define cpuhp_lock_acquire_tryread() \
  81. lock_map_acquire_tryread(&cpu_hotplug.dep_map)
  82. #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
  83. #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
  84. void get_online_cpus(void)
  85. {
  86. might_sleep();
  87. if (cpu_hotplug.active_writer == current)
  88. return;
  89. cpuhp_lock_acquire_read();
  90. mutex_lock(&cpu_hotplug.lock);
  91. atomic_inc(&cpu_hotplug.refcount);
  92. mutex_unlock(&cpu_hotplug.lock);
  93. }
  94. EXPORT_SYMBOL_GPL(get_online_cpus);
  95. bool try_get_online_cpus(void)
  96. {
  97. if (cpu_hotplug.active_writer == current)
  98. return true;
  99. if (!mutex_trylock(&cpu_hotplug.lock))
  100. return false;
  101. cpuhp_lock_acquire_tryread();
  102. atomic_inc(&cpu_hotplug.refcount);
  103. mutex_unlock(&cpu_hotplug.lock);
  104. return true;
  105. }
  106. EXPORT_SYMBOL_GPL(try_get_online_cpus);
  107. void put_online_cpus(void)
  108. {
  109. int refcount;
  110. if (cpu_hotplug.active_writer == current)
  111. return;
  112. refcount = atomic_dec_return(&cpu_hotplug.refcount);
  113. if (WARN_ON(refcount < 0)) /* try to fix things up */
  114. atomic_inc(&cpu_hotplug.refcount);
  115. if (refcount <= 0 && waitqueue_active(&cpu_hotplug.wq))
  116. wake_up(&cpu_hotplug.wq);
  117. cpuhp_lock_release();
  118. }
  119. EXPORT_SYMBOL_GPL(put_online_cpus);
  120. /*
  121. * This ensures that the hotplug operation can begin only when the
  122. * refcount goes to zero.
  123. *
  124. * Note that during a cpu-hotplug operation, the new readers, if any,
  125. * will be blocked by the cpu_hotplug.lock
  126. *
  127. * Since cpu_hotplug_begin() is always called after invoking
  128. * cpu_maps_update_begin(), we can be sure that only one writer is active.
  129. *
  130. * Note that theoretically, there is a possibility of a livelock:
  131. * - Refcount goes to zero, last reader wakes up the sleeping
  132. * writer.
  133. * - Last reader unlocks the cpu_hotplug.lock.
  134. * - A new reader arrives at this moment, bumps up the refcount.
  135. * - The writer acquires the cpu_hotplug.lock finds the refcount
  136. * non zero and goes to sleep again.
  137. *
  138. * However, this is very difficult to achieve in practice since
  139. * get_online_cpus() not an api which is called all that often.
  140. *
  141. */
  142. void cpu_hotplug_begin(void)
  143. {
  144. DEFINE_WAIT(wait);
  145. cpu_hotplug.active_writer = current;
  146. cpuhp_lock_acquire();
  147. for (;;) {
  148. mutex_lock(&cpu_hotplug.lock);
  149. prepare_to_wait(&cpu_hotplug.wq, &wait, TASK_UNINTERRUPTIBLE);
  150. if (likely(!atomic_read(&cpu_hotplug.refcount)))
  151. break;
  152. mutex_unlock(&cpu_hotplug.lock);
  153. schedule();
  154. }
  155. finish_wait(&cpu_hotplug.wq, &wait);
  156. }
  157. void cpu_hotplug_done(void)
  158. {
  159. cpu_hotplug.active_writer = NULL;
  160. mutex_unlock(&cpu_hotplug.lock);
  161. cpuhp_lock_release();
  162. }
  163. /*
  164. * Wait for currently running CPU hotplug operations to complete (if any) and
  165. * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
  166. * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
  167. * hotplug path before performing hotplug operations. So acquiring that lock
  168. * guarantees mutual exclusion from any currently running hotplug operations.
  169. */
  170. void cpu_hotplug_disable(void)
  171. {
  172. cpu_maps_update_begin();
  173. cpu_hotplug_disabled = 1;
  174. cpu_maps_update_done();
  175. }
  176. void cpu_hotplug_enable(void)
  177. {
  178. cpu_maps_update_begin();
  179. cpu_hotplug_disabled = 0;
  180. cpu_maps_update_done();
  181. }
  182. #endif /* CONFIG_HOTPLUG_CPU */
  183. /* Need to know about CPUs going up/down? */
  184. int __ref register_cpu_notifier(struct notifier_block *nb)
  185. {
  186. int ret;
  187. #ifdef MTK_CPU_HOTPLUG_DEBUG_0
  188. int index = 0;
  189. #ifdef CONFIG_KALLSYMS
  190. char namebuf[128] = {0};
  191. const char *symname;
  192. symname = kallsyms_lookup((unsigned long)nb->notifier_call,
  193. NULL, NULL, NULL, namebuf);
  194. if (symname)
  195. pr_info("[cpu_ntf] <%02d>%08lx (%s)\n",
  196. index++, (unsigned long)nb->notifier_call, symname);
  197. else
  198. pr_info("[cpu_ntf] <%02d>%08lx\n",
  199. index++, (unsigned long)nb->notifier_call);
  200. #else
  201. pr_info("[cpu_ntf] <%02d>%08lx\n",
  202. index++, (unsigned long)nb->notifier_call);
  203. #endif
  204. #endif /* MTK_CPU_HOTPLUG_DEBUG_0 */
  205. cpu_maps_update_begin();
  206. ret = raw_notifier_chain_register(&cpu_chain, nb);
  207. cpu_maps_update_done();
  208. return ret;
  209. }
  210. int __ref __register_cpu_notifier(struct notifier_block *nb)
  211. {
  212. return raw_notifier_chain_register(&cpu_chain, nb);
  213. }
  214. static int __cpu_notify(unsigned long val, void *v, int nr_to_call,
  215. int *nr_calls)
  216. {
  217. int ret;
  218. ret = __raw_notifier_call_chain(&cpu_chain, val, v, nr_to_call,
  219. nr_calls);
  220. return notifier_to_errno(ret);
  221. }
  222. static int cpu_notify(unsigned long val, void *v)
  223. {
  224. return __cpu_notify(val, v, -1, NULL);
  225. }
  226. #ifdef CONFIG_HOTPLUG_CPU
  227. static void cpu_notify_nofail(unsigned long val, void *v)
  228. {
  229. BUG_ON(cpu_notify(val, v));
  230. }
  231. EXPORT_SYMBOL(register_cpu_notifier);
  232. EXPORT_SYMBOL(__register_cpu_notifier);
  233. void __ref unregister_cpu_notifier(struct notifier_block *nb)
  234. {
  235. cpu_maps_update_begin();
  236. raw_notifier_chain_unregister(&cpu_chain, nb);
  237. cpu_maps_update_done();
  238. }
  239. EXPORT_SYMBOL(unregister_cpu_notifier);
  240. void __ref __unregister_cpu_notifier(struct notifier_block *nb)
  241. {
  242. raw_notifier_chain_unregister(&cpu_chain, nb);
  243. }
  244. EXPORT_SYMBOL(__unregister_cpu_notifier);
  245. /**
  246. * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
  247. * @cpu: a CPU id
  248. *
  249. * This function walks all processes, finds a valid mm struct for each one and
  250. * then clears a corresponding bit in mm's cpumask. While this all sounds
  251. * trivial, there are various non-obvious corner cases, which this function
  252. * tries to solve in a safe manner.
  253. *
  254. * Also note that the function uses a somewhat relaxed locking scheme, so it may
  255. * be called only for an already offlined CPU.
  256. */
  257. void clear_tasks_mm_cpumask(int cpu)
  258. {
  259. struct task_struct *p;
  260. /*
  261. * This function is called after the cpu is taken down and marked
  262. * offline, so its not like new tasks will ever get this cpu set in
  263. * their mm mask. -- Peter Zijlstra
  264. * Thus, we may use rcu_read_lock() here, instead of grabbing
  265. * full-fledged tasklist_lock.
  266. */
  267. WARN_ON(cpu_online(cpu));
  268. rcu_read_lock();
  269. for_each_process(p) {
  270. struct task_struct *t;
  271. /*
  272. * Main thread might exit, but other threads may still have
  273. * a valid mm. Find one.
  274. */
  275. t = find_lock_task_mm(p);
  276. if (!t)
  277. continue;
  278. cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
  279. task_unlock(t);
  280. }
  281. rcu_read_unlock();
  282. }
  283. static inline void check_for_tasks(int dead_cpu)
  284. {
  285. struct task_struct *g, *p;
  286. read_lock_irq(&tasklist_lock);
  287. do_each_thread(g, p) {
  288. if (!p->on_rq)
  289. continue;
  290. /*
  291. * We do the check with unlocked task_rq(p)->lock.
  292. * Order the reading to do not warn about a task,
  293. * which was running on this cpu in the past, and
  294. * it's just been woken on another cpu.
  295. */
  296. rmb();
  297. if (task_cpu(p) != dead_cpu)
  298. continue;
  299. pr_warn("Task %s (pid=%d) is on cpu %d (state=%ld, flags=%x)\n",
  300. p->comm, task_pid_nr(p), dead_cpu, p->state, p->flags);
  301. } while_each_thread(g, p);
  302. read_unlock_irq(&tasklist_lock);
  303. }
  304. struct take_cpu_down_param {
  305. unsigned long mod;
  306. void *hcpu;
  307. };
  308. /* Take this CPU down. */
  309. static int __ref take_cpu_down(void *_param)
  310. {
  311. struct take_cpu_down_param *param = _param;
  312. int err;
  313. /* Ensure this CPU doesn't handle any more interrupts. */
  314. err = __cpu_disable();
  315. if (err < 0)
  316. return err;
  317. cpu_notify(CPU_DYING | param->mod, param->hcpu);
  318. /* Park the stopper thread */
  319. kthread_park(current);
  320. return 0;
  321. }
  322. /* Requires cpu_add_remove_lock to be held */
  323. static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
  324. {
  325. int err, nr_calls = 0;
  326. void *hcpu = (void *)(long)cpu;
  327. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  328. struct take_cpu_down_param tcd_param = {
  329. .mod = mod,
  330. .hcpu = hcpu,
  331. };
  332. if (num_online_cpus() == 1)
  333. return -EBUSY;
  334. if (!cpu_online(cpu))
  335. return -EINVAL;
  336. cpu_hotplug_begin();
  337. err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
  338. if (err) {
  339. nr_calls--;
  340. __cpu_notify(CPU_DOWN_FAILED | mod, hcpu, nr_calls, NULL);
  341. pr_warn("%s: attempt to take down CPU %u failed\n",
  342. __func__, cpu);
  343. goto out_release;
  344. }
  345. smpboot_park_threads(cpu);
  346. err = __stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu));
  347. if (err) {
  348. /* CPU didn't die: tell everyone. Can't complain. */
  349. smpboot_unpark_threads(cpu);
  350. cpu_notify_nofail(CPU_DOWN_FAILED | mod, hcpu);
  351. goto out_release;
  352. }
  353. BUG_ON(cpu_online(cpu));
  354. /*
  355. * The migration_call() CPU_DYING callback will have removed all
  356. * runnable tasks from the cpu, there's only the idle task left now
  357. * that the migration thread is done doing the stop_machine thing.
  358. *
  359. * Wait for the stop thread to go away.
  360. */
  361. while (!idle_cpu(cpu))
  362. cpu_relax();
  363. #ifdef CONFIG_MT_SCHED_MONITOR
  364. mt_save_irq_counts(CPU_DOWN);
  365. #endif
  366. /* This actually kills the CPU. */
  367. __cpu_die(cpu);
  368. /* CPU is completely dead: tell everyone. Too late to complain. */
  369. cpu_notify_nofail(CPU_DEAD | mod, hcpu);
  370. check_for_tasks(cpu);
  371. out_release:
  372. cpu_hotplug_done();
  373. if (!err)
  374. cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu);
  375. return err;
  376. }
  377. int __ref cpu_down(unsigned int cpu)
  378. {
  379. int err;
  380. cpu_maps_update_begin();
  381. if (cpu_hotplug_disabled) {
  382. err = -EBUSY;
  383. goto out;
  384. }
  385. err = _cpu_down(cpu, 0);
  386. out:
  387. cpu_maps_update_done();
  388. return err;
  389. }
  390. EXPORT_SYMBOL(cpu_down);
  391. #endif /*CONFIG_HOTPLUG_CPU*/
  392. /*
  393. * Unpark per-CPU smpboot kthreads at CPU-online time.
  394. */
  395. static int smpboot_thread_call(struct notifier_block *nfb,
  396. unsigned long action, void *hcpu)
  397. {
  398. int cpu = (long)hcpu;
  399. switch (action & ~CPU_TASKS_FROZEN) {
  400. case CPU_ONLINE:
  401. smpboot_unpark_threads(cpu);
  402. break;
  403. default:
  404. break;
  405. }
  406. return NOTIFY_OK;
  407. }
  408. static struct notifier_block smpboot_thread_notifier = {
  409. .notifier_call = smpboot_thread_call,
  410. .priority = CPU_PRI_SMPBOOT,
  411. };
  412. void __cpuinit smpboot_thread_init(void)
  413. {
  414. register_cpu_notifier(&smpboot_thread_notifier);
  415. }
  416. /* Requires cpu_add_remove_lock to be held */
  417. static int _cpu_up(unsigned int cpu, int tasks_frozen)
  418. {
  419. int ret, nr_calls = 0;
  420. void *hcpu = (void *)(long)cpu;
  421. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  422. struct task_struct *idle;
  423. cpu_hotplug_begin();
  424. if (cpu_online(cpu) || !cpu_present(cpu)) {
  425. ret = -EINVAL;
  426. goto out;
  427. }
  428. idle = idle_thread_get(cpu);
  429. if (IS_ERR(idle)) {
  430. ret = PTR_ERR(idle);
  431. goto out;
  432. }
  433. ret = smpboot_create_threads(cpu);
  434. if (ret)
  435. goto out;
  436. ret = __cpu_notify(CPU_UP_PREPARE | mod, hcpu, -1, &nr_calls);
  437. if (ret) {
  438. nr_calls--;
  439. pr_warn("%s: attempt to bring up CPU %u failed\n",
  440. __func__, cpu);
  441. goto out_notify;
  442. }
  443. /* Arch-specific enabling code. */
  444. ret = __cpu_up(cpu, idle);
  445. if (ret != 0)
  446. goto out_notify;
  447. BUG_ON(!cpu_online(cpu));
  448. #if 0
  449. /* Wake the per cpu threads */
  450. smpboot_unpark_threads(cpu);
  451. #endif
  452. /* Now call notifier in preparation. */
  453. cpu_notify(CPU_ONLINE | mod, hcpu);
  454. out_notify:
  455. if (ret != 0)
  456. __cpu_notify(CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
  457. out:
  458. cpu_hotplug_done();
  459. return ret;
  460. }
  461. int cpu_up(unsigned int cpu)
  462. {
  463. int err = 0;
  464. if (!cpu_possible(cpu)) {
  465. pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
  466. cpu);
  467. #if defined(CONFIG_IA64)
  468. pr_err("please check additional_cpus= boot parameter\n");
  469. #endif
  470. return -EINVAL;
  471. }
  472. err = try_online_node(cpu_to_node(cpu));
  473. if (err)
  474. return err;
  475. cpu_maps_update_begin();
  476. if (cpu_hotplug_disabled) {
  477. err = -EBUSY;
  478. goto out;
  479. }
  480. err = _cpu_up(cpu, 0);
  481. out:
  482. cpu_maps_update_done();
  483. return err;
  484. }
  485. EXPORT_SYMBOL_GPL(cpu_up);
  486. #ifdef CONFIG_PM_SLEEP_SMP
  487. static cpumask_var_t frozen_cpus;
  488. int disable_nonboot_cpus(void)
  489. {
  490. int cpu, first_cpu, error = 0;
  491. cpu_maps_update_begin();
  492. first_cpu = cpumask_first(cpu_online_mask);
  493. /*
  494. * We take down all of the non-boot CPUs in one shot to avoid races
  495. * with the userspace trying to use the CPU hotplug at the same time
  496. */
  497. cpumask_clear(frozen_cpus);
  498. pr_info("Disabling non-boot CPUs ...\n");
  499. for_each_online_cpu(cpu) {
  500. if (cpu == first_cpu)
  501. continue;
  502. trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
  503. error = _cpu_down(cpu, 1);
  504. trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
  505. if (!error)
  506. cpumask_set_cpu(cpu, frozen_cpus);
  507. else {
  508. pr_err("Error taking CPU%d down: %d\n", cpu, error);
  509. break;
  510. }
  511. }
  512. if (!error) {
  513. BUG_ON(num_online_cpus() > 1);
  514. /* Make sure the CPUs won't be enabled by someone else */
  515. cpu_hotplug_disabled = 1;
  516. } else {
  517. pr_err("Non-boot CPUs are not disabled\n");
  518. }
  519. cpu_maps_update_done();
  520. return error;
  521. }
  522. EXPORT_SYMBOL_GPL(disable_nonboot_cpus);
  523. void __weak arch_enable_nonboot_cpus_begin(void)
  524. {
  525. }
  526. void __weak arch_enable_nonboot_cpus_end(void)
  527. {
  528. }
  529. void __ref enable_nonboot_cpus(void)
  530. {
  531. int cpu, error;
  532. /* Allow everyone to use the CPU hotplug again */
  533. cpu_maps_update_begin();
  534. cpu_hotplug_disabled = 0;
  535. if (cpumask_empty(frozen_cpus))
  536. goto out;
  537. pr_info("Enabling non-boot CPUs ...\n");
  538. arch_enable_nonboot_cpus_begin();
  539. for_each_cpu(cpu, frozen_cpus) {
  540. trace_suspend_resume(TPS("CPU_ON"), cpu, true);
  541. error = _cpu_up(cpu, 1);
  542. trace_suspend_resume(TPS("CPU_ON"), cpu, false);
  543. if (!error) {
  544. pr_info("CPU%d is up\n", cpu);
  545. continue;
  546. }
  547. pr_warn("Error taking CPU%d up: %d\n", cpu, error);
  548. }
  549. arch_enable_nonboot_cpus_end();
  550. cpumask_clear(frozen_cpus);
  551. out:
  552. cpu_maps_update_done();
  553. }
  554. EXPORT_SYMBOL_GPL(enable_nonboot_cpus);
  555. static int __init alloc_frozen_cpus(void)
  556. {
  557. if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
  558. return -ENOMEM;
  559. return 0;
  560. }
  561. core_initcall(alloc_frozen_cpus);
  562. /*
  563. * When callbacks for CPU hotplug notifications are being executed, we must
  564. * ensure that the state of the system with respect to the tasks being frozen
  565. * or not, as reported by the notification, remains unchanged *throughout the
  566. * duration* of the execution of the callbacks.
  567. * Hence we need to prevent the freezer from racing with regular CPU hotplug.
  568. *
  569. * This synchronization is implemented by mutually excluding regular CPU
  570. * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
  571. * Hibernate notifications.
  572. */
  573. static int
  574. cpu_hotplug_pm_callback(struct notifier_block *nb,
  575. unsigned long action, void *ptr)
  576. {
  577. switch (action) {
  578. case PM_SUSPEND_PREPARE:
  579. case PM_HIBERNATION_PREPARE:
  580. cpu_hotplug_disable();
  581. break;
  582. case PM_POST_SUSPEND:
  583. case PM_POST_HIBERNATION:
  584. cpu_hotplug_enable();
  585. break;
  586. default:
  587. return NOTIFY_DONE;
  588. }
  589. return NOTIFY_OK;
  590. }
  591. static int __init cpu_hotplug_pm_sync_init(void)
  592. {
  593. /*
  594. * cpu_hotplug_pm_callback has higher priority than x86
  595. * bsp_pm_callback which depends on cpu_hotplug_pm_callback
  596. * to disable cpu hotplug to avoid cpu hotplug race.
  597. */
  598. pm_notifier(cpu_hotplug_pm_callback, 0);
  599. return 0;
  600. }
  601. core_initcall(cpu_hotplug_pm_sync_init);
  602. #endif /* CONFIG_PM_SLEEP_SMP */
  603. /**
  604. * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
  605. * @cpu: cpu that just started
  606. *
  607. * This function calls the cpu_chain notifiers with CPU_STARTING.
  608. * It must be called by the arch code on the new cpu, before the new cpu
  609. * enables interrupts and before the "boot" cpu returns from __cpu_up().
  610. */
  611. void notify_cpu_starting(unsigned int cpu)
  612. {
  613. unsigned long val = CPU_STARTING;
  614. #ifdef CONFIG_PM_SLEEP_SMP
  615. if (frozen_cpus != NULL && cpumask_test_cpu(cpu, frozen_cpus))
  616. val = CPU_STARTING_FROZEN;
  617. #endif /* CONFIG_PM_SLEEP_SMP */
  618. cpu_notify(val, (void *)(long)cpu);
  619. }
  620. #endif /* CONFIG_SMP */
  621. /*
  622. * cpu_bit_bitmap[] is a special, "compressed" data structure that
  623. * represents all NR_CPUS bits binary values of 1<<nr.
  624. *
  625. * It is used by cpumask_of() to get a constant address to a CPU
  626. * mask value that has a single bit set only.
  627. */
  628. /* cpu_bit_bitmap[0] is empty - so we can back into it */
  629. #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
  630. #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
  631. #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
  632. #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
  633. const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
  634. MASK_DECLARE_8(0), MASK_DECLARE_8(8),
  635. MASK_DECLARE_8(16), MASK_DECLARE_8(24),
  636. #if BITS_PER_LONG > 32
  637. MASK_DECLARE_8(32), MASK_DECLARE_8(40),
  638. MASK_DECLARE_8(48), MASK_DECLARE_8(56),
  639. #endif
  640. };
  641. EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
  642. const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
  643. EXPORT_SYMBOL(cpu_all_bits);
  644. #ifdef CONFIG_INIT_ALL_POSSIBLE
  645. static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
  646. = CPU_BITS_ALL;
  647. #else
  648. static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
  649. #endif
  650. const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits);
  651. EXPORT_SYMBOL(cpu_possible_mask);
  652. static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly;
  653. const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits);
  654. EXPORT_SYMBOL(cpu_online_mask);
  655. static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly;
  656. const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits);
  657. EXPORT_SYMBOL(cpu_present_mask);
  658. static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
  659. const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
  660. EXPORT_SYMBOL(cpu_active_mask);
  661. void set_cpu_possible(unsigned int cpu, bool possible)
  662. {
  663. if (possible)
  664. cpumask_set_cpu(cpu, to_cpumask(cpu_possible_bits));
  665. else
  666. cpumask_clear_cpu(cpu, to_cpumask(cpu_possible_bits));
  667. }
  668. void set_cpu_present(unsigned int cpu, bool present)
  669. {
  670. if (present)
  671. cpumask_set_cpu(cpu, to_cpumask(cpu_present_bits));
  672. else
  673. cpumask_clear_cpu(cpu, to_cpumask(cpu_present_bits));
  674. }
  675. void set_cpu_online(unsigned int cpu, bool online)
  676. {
  677. if (online) {
  678. cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
  679. cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
  680. } else {
  681. cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
  682. }
  683. }
  684. void set_cpu_active(unsigned int cpu, bool active)
  685. {
  686. if (active)
  687. cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
  688. else
  689. cpumask_clear_cpu(cpu, to_cpumask(cpu_active_bits));
  690. }
  691. void init_cpu_present(const struct cpumask *src)
  692. {
  693. cpumask_copy(to_cpumask(cpu_present_bits), src);
  694. }
  695. void init_cpu_possible(const struct cpumask *src)
  696. {
  697. cpumask_copy(to_cpumask(cpu_possible_bits), src);
  698. }
  699. void init_cpu_online(const struct cpumask *src)
  700. {
  701. cpumask_copy(to_cpumask(cpu_online_bits), src);
  702. }
  703. static ATOMIC_NOTIFIER_HEAD(idle_notifier);
  704. void idle_notifier_register(struct notifier_block *n)
  705. {
  706. atomic_notifier_chain_register(&idle_notifier, n);
  707. }
  708. EXPORT_SYMBOL_GPL(idle_notifier_register);
  709. void idle_notifier_unregister(struct notifier_block *n)
  710. {
  711. atomic_notifier_chain_unregister(&idle_notifier, n);
  712. }
  713. EXPORT_SYMBOL_GPL(idle_notifier_unregister);
  714. void idle_notifier_call_chain(unsigned long val)
  715. {
  716. atomic_notifier_call_chain(&idle_notifier, val, NULL);
  717. }
  718. EXPORT_SYMBOL_GPL(idle_notifier_call_chain);