cpu.c 17 KB

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