cpu.c 18 KB

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