cpu.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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. #include <trace/events/sched.h>
  20. #include "smpboot.h"
  21. #ifdef CONFIG_SMP
  22. /* Serializes the updates to cpu_online_mask, cpu_present_mask */
  23. static DEFINE_MUTEX(cpu_add_remove_lock);
  24. /*
  25. * The following two APIs (cpu_maps_update_begin/done) must be used when
  26. * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
  27. * The APIs cpu_notifier_register_begin/done() must be used to protect CPU
  28. * hotplug callback (un)registration performed using __register_cpu_notifier()
  29. * or __unregister_cpu_notifier().
  30. */
  31. void cpu_maps_update_begin(void)
  32. {
  33. mutex_lock(&cpu_add_remove_lock);
  34. }
  35. EXPORT_SYMBOL(cpu_notifier_register_begin);
  36. void cpu_maps_update_done(void)
  37. {
  38. mutex_unlock(&cpu_add_remove_lock);
  39. }
  40. EXPORT_SYMBOL(cpu_notifier_register_done);
  41. static RAW_NOTIFIER_HEAD(cpu_chain);
  42. /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
  43. * Should always be manipulated under cpu_add_remove_lock
  44. */
  45. static int cpu_hotplug_disabled;
  46. #ifdef CONFIG_HOTPLUG_CPU
  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. void get_online_cpus(void)
  61. {
  62. might_sleep();
  63. if (cpu_hotplug.active_writer == current)
  64. return;
  65. mutex_lock(&cpu_hotplug.lock);
  66. cpu_hotplug.refcount++;
  67. mutex_unlock(&cpu_hotplug.lock);
  68. }
  69. EXPORT_SYMBOL_GPL(get_online_cpus);
  70. void put_online_cpus(void)
  71. {
  72. if (cpu_hotplug.active_writer == current)
  73. return;
  74. mutex_lock(&cpu_hotplug.lock);
  75. if (WARN_ON(!cpu_hotplug.refcount))
  76. cpu_hotplug.refcount++; /* try to fix things up */
  77. if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer))
  78. wake_up_process(cpu_hotplug.active_writer);
  79. mutex_unlock(&cpu_hotplug.lock);
  80. }
  81. EXPORT_SYMBOL_GPL(put_online_cpus);
  82. /*
  83. * This ensures that the hotplug operation can begin only when the
  84. * refcount goes to zero.
  85. *
  86. * Note that during a cpu-hotplug operation, the new readers, if any,
  87. * will be blocked by the cpu_hotplug.lock
  88. *
  89. * Since cpu_hotplug_begin() is always called after invoking
  90. * cpu_maps_update_begin(), we can be sure that only one writer is active.
  91. *
  92. * Note that theoretically, there is a possibility of a livelock:
  93. * - Refcount goes to zero, last reader wakes up the sleeping
  94. * writer.
  95. * - Last reader unlocks the cpu_hotplug.lock.
  96. * - A new reader arrives at this moment, bumps up the refcount.
  97. * - The writer acquires the cpu_hotplug.lock finds the refcount
  98. * non zero and goes to sleep again.
  99. *
  100. * However, this is very difficult to achieve in practice since
  101. * get_online_cpus() not an api which is called all that often.
  102. *
  103. */
  104. static void cpu_hotplug_begin(void)
  105. {
  106. cpu_hotplug.active_writer = current;
  107. for (;;) {
  108. mutex_lock(&cpu_hotplug.lock);
  109. if (likely(!cpu_hotplug.refcount))
  110. break;
  111. __set_current_state(TASK_UNINTERRUPTIBLE);
  112. mutex_unlock(&cpu_hotplug.lock);
  113. schedule();
  114. }
  115. }
  116. static void cpu_hotplug_done(void)
  117. {
  118. cpu_hotplug.active_writer = NULL;
  119. mutex_unlock(&cpu_hotplug.lock);
  120. }
  121. /*
  122. * Wait for currently running CPU hotplug operations to complete (if any) and
  123. * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
  124. * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
  125. * hotplug path before performing hotplug operations. So acquiring that lock
  126. * guarantees mutual exclusion from any currently running hotplug operations.
  127. */
  128. void cpu_hotplug_disable(void)
  129. {
  130. cpu_maps_update_begin();
  131. cpu_hotplug_disabled = 1;
  132. cpu_maps_update_done();
  133. }
  134. void cpu_hotplug_enable(void)
  135. {
  136. cpu_maps_update_begin();
  137. cpu_hotplug_disabled = 0;
  138. cpu_maps_update_done();
  139. }
  140. #else /* #if CONFIG_HOTPLUG_CPU */
  141. static void cpu_hotplug_begin(void) {}
  142. static void cpu_hotplug_done(void) {}
  143. #endif /* #else #if CONFIG_HOTPLUG_CPU */
  144. /* Need to know about CPUs going up/down? */
  145. int __ref register_cpu_notifier(struct notifier_block *nb)
  146. {
  147. int ret;
  148. cpu_maps_update_begin();
  149. ret = raw_notifier_chain_register(&cpu_chain, nb);
  150. cpu_maps_update_done();
  151. return ret;
  152. }
  153. int __ref __register_cpu_notifier(struct notifier_block *nb)
  154. {
  155. return raw_notifier_chain_register(&cpu_chain, nb);
  156. }
  157. static int __cpu_notify(unsigned long val, void *v, int nr_to_call,
  158. int *nr_calls)
  159. {
  160. int ret;
  161. ret = __raw_notifier_call_chain(&cpu_chain, val, v, nr_to_call,
  162. nr_calls);
  163. return notifier_to_errno(ret);
  164. }
  165. static int cpu_notify(unsigned long val, void *v)
  166. {
  167. return __cpu_notify(val, v, -1, NULL);
  168. }
  169. #ifdef CONFIG_HOTPLUG_CPU
  170. static void cpu_notify_nofail(unsigned long val, void *v)
  171. {
  172. BUG_ON(cpu_notify(val, v));
  173. }
  174. EXPORT_SYMBOL(register_cpu_notifier);
  175. EXPORT_SYMBOL(__register_cpu_notifier);
  176. void __ref unregister_cpu_notifier(struct notifier_block *nb)
  177. {
  178. cpu_maps_update_begin();
  179. raw_notifier_chain_unregister(&cpu_chain, nb);
  180. cpu_maps_update_done();
  181. }
  182. EXPORT_SYMBOL(unregister_cpu_notifier);
  183. void __ref __unregister_cpu_notifier(struct notifier_block *nb)
  184. {
  185. raw_notifier_chain_unregister(&cpu_chain, nb);
  186. }
  187. EXPORT_SYMBOL(__unregister_cpu_notifier);
  188. static inline void check_for_tasks(int cpu)
  189. {
  190. struct task_struct *p;
  191. write_lock_irq(&tasklist_lock);
  192. for_each_process(p) {
  193. if (task_cpu(p) == cpu && p->state == TASK_RUNNING &&
  194. (p->utime || p->stime))
  195. printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d "
  196. "(state = %ld, flags = %x)\n",
  197. p->comm, task_pid_nr(p), cpu,
  198. p->state, p->flags);
  199. }
  200. write_unlock_irq(&tasklist_lock);
  201. }
  202. struct take_cpu_down_param {
  203. unsigned long mod;
  204. void *hcpu;
  205. };
  206. /* Take this CPU down. */
  207. static int __ref take_cpu_down(void *_param)
  208. {
  209. struct take_cpu_down_param *param = _param;
  210. int err;
  211. /* Ensure this CPU doesn't handle any more interrupts. */
  212. err = __cpu_disable();
  213. if (err < 0)
  214. return err;
  215. cpu_notify(CPU_DYING | param->mod, param->hcpu);
  216. /* Park the stopper thread */
  217. kthread_park(current);
  218. return 0;
  219. }
  220. /* Requires cpu_add_remove_lock to be held */
  221. static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
  222. {
  223. int err, nr_calls = 0;
  224. void *hcpu = (void *)(long)cpu;
  225. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  226. struct take_cpu_down_param tcd_param = {
  227. .mod = mod,
  228. .hcpu = hcpu,
  229. };
  230. if (num_online_cpus() == 1)
  231. return -EBUSY;
  232. if (!cpu_online(cpu))
  233. return -EINVAL;
  234. cpu_hotplug_begin();
  235. err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
  236. if (err) {
  237. nr_calls--;
  238. __cpu_notify(CPU_DOWN_FAILED | mod, hcpu, nr_calls, NULL);
  239. printk("%s: attempt to take down CPU %u failed\n",
  240. __func__, cpu);
  241. goto out_release;
  242. }
  243. smpboot_park_threads(cpu);
  244. err = __stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu));
  245. if (err) {
  246. /* CPU didn't die: tell everyone. Can't complain. */
  247. cpu_notify_nofail(CPU_DOWN_FAILED | mod, hcpu);
  248. goto out_release;
  249. }
  250. BUG_ON(cpu_online(cpu));
  251. /*
  252. * The migration_call() CPU_DYING callback will have removed all
  253. * runnable tasks from the cpu, there's only the idle task left now
  254. * that the migration thread is done doing the stop_machine thing.
  255. *
  256. * Wait for the stop thread to go away.
  257. */
  258. while (!idle_cpu(cpu))
  259. cpu_relax();
  260. /* This actually kills the CPU. */
  261. __cpu_die(cpu);
  262. /* CPU is completely dead: tell everyone. Too late to complain. */
  263. cpu_notify_nofail(CPU_DEAD | mod, hcpu);
  264. check_for_tasks(cpu);
  265. out_release:
  266. cpu_hotplug_done();
  267. trace_sched_cpu_hotplug(cpu, err, 0);
  268. if (!err)
  269. cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu);
  270. return err;
  271. }
  272. int __ref cpu_down(unsigned int cpu)
  273. {
  274. int err;
  275. cpu_maps_update_begin();
  276. if (cpu_hotplug_disabled) {
  277. err = -EBUSY;
  278. goto out;
  279. }
  280. err = _cpu_down(cpu, 0);
  281. out:
  282. cpu_maps_update_done();
  283. return err;
  284. }
  285. EXPORT_SYMBOL(cpu_down);
  286. #endif /*CONFIG_HOTPLUG_CPU*/
  287. /*
  288. * Unpark per-CPU smpboot kthreads at CPU-online time.
  289. */
  290. static int smpboot_thread_call(struct notifier_block *nfb,
  291. unsigned long action, void *hcpu)
  292. {
  293. int cpu = (long)hcpu;
  294. switch (action & ~CPU_TASKS_FROZEN) {
  295. case CPU_DOWN_FAILED:
  296. case CPU_ONLINE:
  297. smpboot_unpark_threads(cpu);
  298. break;
  299. default:
  300. break;
  301. }
  302. return NOTIFY_OK;
  303. }
  304. static struct notifier_block smpboot_thread_notifier = {
  305. .notifier_call = smpboot_thread_call,
  306. .priority = CPU_PRI_SMPBOOT,
  307. };
  308. void smpboot_thread_init(void)
  309. {
  310. register_cpu_notifier(&smpboot_thread_notifier);
  311. }
  312. /* Requires cpu_add_remove_lock to be held */
  313. static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)
  314. {
  315. int ret, nr_calls = 0;
  316. void *hcpu = (void *)(long)cpu;
  317. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  318. struct task_struct *idle;
  319. if (cpu_online(cpu) || !cpu_present(cpu))
  320. return -EINVAL;
  321. cpu_hotplug_begin();
  322. idle = idle_thread_get(cpu);
  323. if (IS_ERR(idle)) {
  324. ret = PTR_ERR(idle);
  325. goto out;
  326. }
  327. ret = smpboot_create_threads(cpu);
  328. if (ret)
  329. goto out;
  330. ret = __cpu_notify(CPU_UP_PREPARE | mod, hcpu, -1, &nr_calls);
  331. if (ret) {
  332. nr_calls--;
  333. printk(KERN_WARNING "%s: attempt to bring up CPU %u failed\n",
  334. __func__, cpu);
  335. goto out_notify;
  336. }
  337. /* Arch-specific enabling code. */
  338. ret = __cpu_up(cpu);
  339. if (ret != 0)
  340. goto out_notify;
  341. BUG_ON(!cpu_online(cpu));
  342. /* Now call notifier in preparation. */
  343. cpu_notify(CPU_ONLINE | mod, hcpu);
  344. out_notify:
  345. if (ret != 0)
  346. __cpu_notify(CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
  347. out:
  348. cpu_hotplug_done();
  349. trace_sched_cpu_hotplug(cpu, ret, 1);
  350. return ret;
  351. }
  352. int __cpuinit cpu_up(unsigned int cpu)
  353. {
  354. int err = 0;
  355. #ifdef CONFIG_MEMORY_HOTPLUG
  356. int nid;
  357. pg_data_t *pgdat;
  358. #endif
  359. if (!cpu_possible(cpu)) {
  360. printk(KERN_ERR "can't online cpu %d because it is not "
  361. "configured as may-hotadd at boot time\n", cpu);
  362. #if defined(CONFIG_IA64)
  363. printk(KERN_ERR "please check additional_cpus= boot "
  364. "parameter\n");
  365. #endif
  366. return -EINVAL;
  367. }
  368. #ifdef CONFIG_MEMORY_HOTPLUG
  369. nid = cpu_to_node(cpu);
  370. if (!node_online(nid)) {
  371. err = mem_online_node(nid);
  372. if (err)
  373. return err;
  374. }
  375. pgdat = NODE_DATA(nid);
  376. if (!pgdat) {
  377. printk(KERN_ERR
  378. "Can't online cpu %d due to NULL pgdat\n", cpu);
  379. return -ENOMEM;
  380. }
  381. if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
  382. mutex_lock(&zonelists_mutex);
  383. build_all_zonelists(NULL);
  384. mutex_unlock(&zonelists_mutex);
  385. }
  386. #endif
  387. cpu_maps_update_begin();
  388. if (cpu_hotplug_disabled) {
  389. err = -EBUSY;
  390. goto out;
  391. }
  392. err = _cpu_up(cpu, 0);
  393. out:
  394. cpu_maps_update_done();
  395. return err;
  396. }
  397. EXPORT_SYMBOL_GPL(cpu_up);
  398. #ifdef CONFIG_PM_SLEEP_SMP
  399. static cpumask_var_t frozen_cpus;
  400. void __weak arch_disable_nonboot_cpus_begin(void)
  401. {
  402. }
  403. void __weak arch_disable_nonboot_cpus_end(void)
  404. {
  405. }
  406. int disable_nonboot_cpus(void)
  407. {
  408. int cpu, first_cpu, error = 0;
  409. cpu_maps_update_begin();
  410. first_cpu = cpumask_first(cpu_online_mask);
  411. /*
  412. * We take down all of the non-boot CPUs in one shot to avoid races
  413. * with the userspace trying to use the CPU hotplug at the same time
  414. */
  415. cpumask_clear(frozen_cpus);
  416. arch_disable_nonboot_cpus_begin();
  417. printk("Disabling non-boot CPUs ...\n");
  418. for_each_online_cpu(cpu) {
  419. if (cpu == first_cpu)
  420. continue;
  421. error = _cpu_down(cpu, 1);
  422. if (!error)
  423. cpumask_set_cpu(cpu, frozen_cpus);
  424. else {
  425. printk(KERN_ERR "Error taking CPU%d down: %d\n",
  426. cpu, error);
  427. break;
  428. }
  429. }
  430. arch_disable_nonboot_cpus_end();
  431. if (!error) {
  432. BUG_ON(num_online_cpus() > 1);
  433. /* Make sure the CPUs won't be enabled by someone else */
  434. cpu_hotplug_disabled = 1;
  435. } else {
  436. printk(KERN_ERR "Non-boot CPUs are not disabled\n");
  437. }
  438. cpu_maps_update_done();
  439. return error;
  440. }
  441. void __weak arch_enable_nonboot_cpus_begin(void)
  442. {
  443. }
  444. void __weak arch_enable_nonboot_cpus_end(void)
  445. {
  446. }
  447. void __ref enable_nonboot_cpus(void)
  448. {
  449. int cpu, error;
  450. struct device *cpu_device;
  451. /* Allow everyone to use the CPU hotplug again */
  452. cpu_maps_update_begin();
  453. cpu_hotplug_disabled = 0;
  454. if (cpumask_empty(frozen_cpus))
  455. goto out;
  456. printk(KERN_INFO "Enabling non-boot CPUs ...\n");
  457. arch_enable_nonboot_cpus_begin();
  458. for_each_cpu(cpu, frozen_cpus) {
  459. error = _cpu_up(cpu, 1);
  460. if (!error) {
  461. printk(KERN_INFO "CPU%d is up\n", cpu);
  462. cpu_device = get_cpu_device(cpu);
  463. if (!cpu_device)
  464. pr_err("%s: failed to get cpu%d device\n",
  465. __func__, cpu);
  466. else
  467. kobject_uevent(&cpu_device->kobj, KOBJ_ONLINE);
  468. continue;
  469. }
  470. printk(KERN_WARNING "Error taking CPU%d up: %d\n", cpu, error);
  471. }
  472. arch_enable_nonboot_cpus_end();
  473. cpumask_clear(frozen_cpus);
  474. out:
  475. cpu_maps_update_done();
  476. }
  477. static int __init alloc_frozen_cpus(void)
  478. {
  479. if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
  480. return -ENOMEM;
  481. return 0;
  482. }
  483. core_initcall(alloc_frozen_cpus);
  484. /*
  485. * When callbacks for CPU hotplug notifications are being executed, we must
  486. * ensure that the state of the system with respect to the tasks being frozen
  487. * or not, as reported by the notification, remains unchanged *throughout the
  488. * duration* of the execution of the callbacks.
  489. * Hence we need to prevent the freezer from racing with regular CPU hotplug.
  490. *
  491. * This synchronization is implemented by mutually excluding regular CPU
  492. * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
  493. * Hibernate notifications.
  494. */
  495. static int
  496. cpu_hotplug_pm_callback(struct notifier_block *nb,
  497. unsigned long action, void *ptr)
  498. {
  499. switch (action) {
  500. case PM_SUSPEND_PREPARE:
  501. case PM_HIBERNATION_PREPARE:
  502. cpu_hotplug_disable();
  503. break;
  504. case PM_POST_SUSPEND:
  505. case PM_POST_HIBERNATION:
  506. cpu_hotplug_enable();
  507. break;
  508. default:
  509. return NOTIFY_DONE;
  510. }
  511. return NOTIFY_OK;
  512. }
  513. static int __init cpu_hotplug_pm_sync_init(void)
  514. {
  515. pm_notifier(cpu_hotplug_pm_callback, 0);
  516. return 0;
  517. }
  518. core_initcall(cpu_hotplug_pm_sync_init);
  519. #endif /* CONFIG_PM_SLEEP_SMP */
  520. /**
  521. * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
  522. * @cpu: cpu that just started
  523. *
  524. * This function calls the cpu_chain notifiers with CPU_STARTING.
  525. * It must be called by the arch code on the new cpu, before the new cpu
  526. * enables interrupts and before the "boot" cpu returns from __cpu_up().
  527. */
  528. void __cpuinit notify_cpu_starting(unsigned int cpu)
  529. {
  530. unsigned long val = CPU_STARTING;
  531. #ifdef CONFIG_PM_SLEEP_SMP
  532. if (frozen_cpus != NULL && cpumask_test_cpu(cpu, frozen_cpus))
  533. val = CPU_STARTING_FROZEN;
  534. #endif /* CONFIG_PM_SLEEP_SMP */
  535. cpu_notify(val, (void *)(long)cpu);
  536. }
  537. #endif /* CONFIG_SMP */
  538. /*
  539. * cpu_bit_bitmap[] is a special, "compressed" data structure that
  540. * represents all NR_CPUS bits binary values of 1<<nr.
  541. *
  542. * It is used by cpumask_of() to get a constant address to a CPU
  543. * mask value that has a single bit set only.
  544. */
  545. /* cpu_bit_bitmap[0] is empty - so we can back into it */
  546. #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
  547. #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
  548. #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
  549. #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
  550. const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
  551. MASK_DECLARE_8(0), MASK_DECLARE_8(8),
  552. MASK_DECLARE_8(16), MASK_DECLARE_8(24),
  553. #if BITS_PER_LONG > 32
  554. MASK_DECLARE_8(32), MASK_DECLARE_8(40),
  555. MASK_DECLARE_8(48), MASK_DECLARE_8(56),
  556. #endif
  557. };
  558. EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
  559. const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
  560. EXPORT_SYMBOL(cpu_all_bits);
  561. #ifdef CONFIG_INIT_ALL_POSSIBLE
  562. static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
  563. = CPU_BITS_ALL;
  564. #else
  565. static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
  566. #endif
  567. const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits);
  568. EXPORT_SYMBOL(cpu_possible_mask);
  569. static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly;
  570. const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits);
  571. EXPORT_SYMBOL(cpu_online_mask);
  572. static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly;
  573. const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits);
  574. EXPORT_SYMBOL(cpu_present_mask);
  575. static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
  576. const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
  577. EXPORT_SYMBOL(cpu_active_mask);
  578. void set_cpu_possible(unsigned int cpu, bool possible)
  579. {
  580. if (possible)
  581. cpumask_set_cpu(cpu, to_cpumask(cpu_possible_bits));
  582. else
  583. cpumask_clear_cpu(cpu, to_cpumask(cpu_possible_bits));
  584. }
  585. void set_cpu_present(unsigned int cpu, bool present)
  586. {
  587. if (present)
  588. cpumask_set_cpu(cpu, to_cpumask(cpu_present_bits));
  589. else
  590. cpumask_clear_cpu(cpu, to_cpumask(cpu_present_bits));
  591. }
  592. void set_cpu_online(unsigned int cpu, bool online)
  593. {
  594. if (online) {
  595. cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
  596. cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
  597. } else {
  598. cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
  599. }
  600. }
  601. void set_cpu_active(unsigned int cpu, bool active)
  602. {
  603. if (active)
  604. cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
  605. else
  606. cpumask_clear_cpu(cpu, to_cpumask(cpu_active_bits));
  607. }
  608. void init_cpu_present(const struct cpumask *src)
  609. {
  610. cpumask_copy(to_cpumask(cpu_present_bits), src);
  611. }
  612. void init_cpu_possible(const struct cpumask *src)
  613. {
  614. cpumask_copy(to_cpumask(cpu_possible_bits), src);
  615. }
  616. void init_cpu_online(const struct cpumask *src)
  617. {
  618. cpumask_copy(to_cpumask(cpu_online_bits), src);
  619. }
  620. static ATOMIC_NOTIFIER_HEAD(idle_notifier);
  621. void idle_notifier_register(struct notifier_block *n)
  622. {
  623. atomic_notifier_chain_register(&idle_notifier, n);
  624. }
  625. EXPORT_SYMBOL_GPL(idle_notifier_register);
  626. void idle_notifier_unregister(struct notifier_block *n)
  627. {
  628. atomic_notifier_chain_unregister(&idle_notifier, n);
  629. }
  630. EXPORT_SYMBOL_GPL(idle_notifier_unregister);
  631. void idle_notifier_call_chain(unsigned long val)
  632. {
  633. atomic_notifier_call_chain(&idle_notifier, val, NULL);
  634. }
  635. EXPORT_SYMBOL_GPL(idle_notifier_call_chain);