cpu.c 17 KB

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