cpu.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951
  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 <linux/tick.h>
  24. #include <linux/irq.h>
  25. #include <linux/smpboot.h>
  26. #include <linux/relay.h>
  27. #include <linux/slab.h>
  28. #include <trace/events/power.h>
  29. #define CREATE_TRACE_POINTS
  30. #include <trace/events/cpuhp.h>
  31. #include "smpboot.h"
  32. /**
  33. * cpuhp_cpu_state - Per cpu hotplug state storage
  34. * @state: The current cpu state
  35. * @target: The target state
  36. * @thread: Pointer to the hotplug thread
  37. * @should_run: Thread should execute
  38. * @rollback: Perform a rollback
  39. * @single: Single callback invocation
  40. * @bringup: Single callback bringup or teardown selector
  41. * @cb_state: The state for a single callback (install/uninstall)
  42. * @result: Result of the operation
  43. * @done: Signal completion to the issuer of the task
  44. */
  45. struct cpuhp_cpu_state {
  46. enum cpuhp_state state;
  47. enum cpuhp_state target;
  48. #ifdef CONFIG_SMP
  49. struct task_struct *thread;
  50. bool should_run;
  51. bool rollback;
  52. bool single;
  53. bool bringup;
  54. struct hlist_node *node;
  55. enum cpuhp_state cb_state;
  56. int result;
  57. struct completion done;
  58. #endif
  59. };
  60. static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state);
  61. #if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
  62. static struct lock_class_key cpuhp_state_key;
  63. static struct lockdep_map cpuhp_state_lock_map =
  64. STATIC_LOCKDEP_MAP_INIT("cpuhp_state", &cpuhp_state_key);
  65. #endif
  66. /**
  67. * cpuhp_step - Hotplug state machine step
  68. * @name: Name of the step
  69. * @startup: Startup function of the step
  70. * @teardown: Teardown function of the step
  71. * @skip_onerr: Do not invoke the functions on error rollback
  72. * Will go away once the notifiers are gone
  73. * @cant_stop: Bringup/teardown can't be stopped at this step
  74. */
  75. struct cpuhp_step {
  76. const char *name;
  77. union {
  78. int (*single)(unsigned int cpu);
  79. int (*multi)(unsigned int cpu,
  80. struct hlist_node *node);
  81. } startup;
  82. union {
  83. int (*single)(unsigned int cpu);
  84. int (*multi)(unsigned int cpu,
  85. struct hlist_node *node);
  86. } teardown;
  87. struct hlist_head list;
  88. bool skip_onerr;
  89. bool cant_stop;
  90. bool multi_instance;
  91. };
  92. static DEFINE_MUTEX(cpuhp_state_mutex);
  93. static struct cpuhp_step cpuhp_bp_states[];
  94. static struct cpuhp_step cpuhp_ap_states[];
  95. static bool cpuhp_is_ap_state(enum cpuhp_state state)
  96. {
  97. /*
  98. * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
  99. * purposes as that state is handled explicitly in cpu_down.
  100. */
  101. return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;
  102. }
  103. static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
  104. {
  105. struct cpuhp_step *sp;
  106. sp = cpuhp_is_ap_state(state) ? cpuhp_ap_states : cpuhp_bp_states;
  107. return sp + state;
  108. }
  109. /**
  110. * cpuhp_invoke_callback _ Invoke the callbacks for a given state
  111. * @cpu: The cpu for which the callback should be invoked
  112. * @step: The step in the state machine
  113. * @bringup: True if the bringup callback should be invoked
  114. *
  115. * Called from cpu hotplug and from the state register machinery.
  116. */
  117. static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
  118. bool bringup, struct hlist_node *node)
  119. {
  120. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  121. struct cpuhp_step *step = cpuhp_get_step(state);
  122. int (*cbm)(unsigned int cpu, struct hlist_node *node);
  123. int (*cb)(unsigned int cpu);
  124. int ret, cnt;
  125. if (!step->multi_instance) {
  126. cb = bringup ? step->startup.single : step->teardown.single;
  127. if (!cb)
  128. return 0;
  129. trace_cpuhp_enter(cpu, st->target, state, cb);
  130. ret = cb(cpu);
  131. trace_cpuhp_exit(cpu, st->state, state, ret);
  132. return ret;
  133. }
  134. cbm = bringup ? step->startup.multi : step->teardown.multi;
  135. if (!cbm)
  136. return 0;
  137. /* Single invocation for instance add/remove */
  138. if (node) {
  139. trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
  140. ret = cbm(cpu, node);
  141. trace_cpuhp_exit(cpu, st->state, state, ret);
  142. return ret;
  143. }
  144. /* State transition. Invoke on all instances */
  145. cnt = 0;
  146. hlist_for_each(node, &step->list) {
  147. trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
  148. ret = cbm(cpu, node);
  149. trace_cpuhp_exit(cpu, st->state, state, ret);
  150. if (ret)
  151. goto err;
  152. cnt++;
  153. }
  154. return 0;
  155. err:
  156. /* Rollback the instances if one failed */
  157. cbm = !bringup ? step->startup.multi : step->teardown.multi;
  158. if (!cbm)
  159. return ret;
  160. hlist_for_each(node, &step->list) {
  161. if (!cnt--)
  162. break;
  163. cbm(cpu, node);
  164. }
  165. return ret;
  166. }
  167. #ifdef CONFIG_SMP
  168. /* Serializes the updates to cpu_online_mask, cpu_present_mask */
  169. static DEFINE_MUTEX(cpu_add_remove_lock);
  170. bool cpuhp_tasks_frozen;
  171. EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
  172. /*
  173. * The following two APIs (cpu_maps_update_begin/done) must be used when
  174. * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
  175. * The APIs cpu_notifier_register_begin/done() must be used to protect CPU
  176. * hotplug callback (un)registration performed using __register_cpu_notifier()
  177. * or __unregister_cpu_notifier().
  178. */
  179. void cpu_maps_update_begin(void)
  180. {
  181. mutex_lock(&cpu_add_remove_lock);
  182. }
  183. EXPORT_SYMBOL(cpu_notifier_register_begin);
  184. void cpu_maps_update_done(void)
  185. {
  186. mutex_unlock(&cpu_add_remove_lock);
  187. }
  188. EXPORT_SYMBOL(cpu_notifier_register_done);
  189. static RAW_NOTIFIER_HEAD(cpu_chain);
  190. /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
  191. * Should always be manipulated under cpu_add_remove_lock
  192. */
  193. static int cpu_hotplug_disabled;
  194. #ifdef CONFIG_HOTPLUG_CPU
  195. static struct {
  196. struct task_struct *active_writer;
  197. /* wait queue to wake up the active_writer */
  198. wait_queue_head_t wq;
  199. /* verifies that no writer will get active while readers are active */
  200. struct mutex lock;
  201. /*
  202. * Also blocks the new readers during
  203. * an ongoing cpu hotplug operation.
  204. */
  205. atomic_t refcount;
  206. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  207. struct lockdep_map dep_map;
  208. #endif
  209. } cpu_hotplug = {
  210. .active_writer = NULL,
  211. .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  212. .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
  213. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  214. .dep_map = STATIC_LOCKDEP_MAP_INIT("cpu_hotplug.dep_map", &cpu_hotplug.dep_map),
  215. #endif
  216. };
  217. /* Lockdep annotations for get/put_online_cpus() and cpu_hotplug_begin/end() */
  218. #define cpuhp_lock_acquire_read() lock_map_acquire_read(&cpu_hotplug.dep_map)
  219. #define cpuhp_lock_acquire_tryread() \
  220. lock_map_acquire_tryread(&cpu_hotplug.dep_map)
  221. #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
  222. #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
  223. void get_online_cpus(void)
  224. {
  225. might_sleep();
  226. if (cpu_hotplug.active_writer == current)
  227. return;
  228. cpuhp_lock_acquire_read();
  229. mutex_lock(&cpu_hotplug.lock);
  230. atomic_inc(&cpu_hotplug.refcount);
  231. mutex_unlock(&cpu_hotplug.lock);
  232. }
  233. EXPORT_SYMBOL_GPL(get_online_cpus);
  234. void put_online_cpus(void)
  235. {
  236. int refcount;
  237. if (cpu_hotplug.active_writer == current)
  238. return;
  239. refcount = atomic_dec_return(&cpu_hotplug.refcount);
  240. if (WARN_ON(refcount < 0)) /* try to fix things up */
  241. atomic_inc(&cpu_hotplug.refcount);
  242. if (refcount <= 0 && waitqueue_active(&cpu_hotplug.wq))
  243. wake_up(&cpu_hotplug.wq);
  244. cpuhp_lock_release();
  245. }
  246. EXPORT_SYMBOL_GPL(put_online_cpus);
  247. /*
  248. * This ensures that the hotplug operation can begin only when the
  249. * refcount goes to zero.
  250. *
  251. * Note that during a cpu-hotplug operation, the new readers, if any,
  252. * will be blocked by the cpu_hotplug.lock
  253. *
  254. * Since cpu_hotplug_begin() is always called after invoking
  255. * cpu_maps_update_begin(), we can be sure that only one writer is active.
  256. *
  257. * Note that theoretically, there is a possibility of a livelock:
  258. * - Refcount goes to zero, last reader wakes up the sleeping
  259. * writer.
  260. * - Last reader unlocks the cpu_hotplug.lock.
  261. * - A new reader arrives at this moment, bumps up the refcount.
  262. * - The writer acquires the cpu_hotplug.lock finds the refcount
  263. * non zero and goes to sleep again.
  264. *
  265. * However, this is very difficult to achieve in practice since
  266. * get_online_cpus() not an api which is called all that often.
  267. *
  268. */
  269. void cpu_hotplug_begin(void)
  270. {
  271. DEFINE_WAIT(wait);
  272. cpu_hotplug.active_writer = current;
  273. cpuhp_lock_acquire();
  274. for (;;) {
  275. mutex_lock(&cpu_hotplug.lock);
  276. prepare_to_wait(&cpu_hotplug.wq, &wait, TASK_UNINTERRUPTIBLE);
  277. if (likely(!atomic_read(&cpu_hotplug.refcount)))
  278. break;
  279. mutex_unlock(&cpu_hotplug.lock);
  280. schedule();
  281. }
  282. finish_wait(&cpu_hotplug.wq, &wait);
  283. }
  284. void cpu_hotplug_done(void)
  285. {
  286. cpu_hotplug.active_writer = NULL;
  287. mutex_unlock(&cpu_hotplug.lock);
  288. cpuhp_lock_release();
  289. }
  290. /*
  291. * Wait for currently running CPU hotplug operations to complete (if any) and
  292. * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
  293. * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
  294. * hotplug path before performing hotplug operations. So acquiring that lock
  295. * guarantees mutual exclusion from any currently running hotplug operations.
  296. */
  297. void cpu_hotplug_disable(void)
  298. {
  299. cpu_maps_update_begin();
  300. cpu_hotplug_disabled++;
  301. cpu_maps_update_done();
  302. }
  303. EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
  304. static void __cpu_hotplug_enable(void)
  305. {
  306. if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable\n"))
  307. return;
  308. cpu_hotplug_disabled--;
  309. }
  310. void cpu_hotplug_enable(void)
  311. {
  312. cpu_maps_update_begin();
  313. __cpu_hotplug_enable();
  314. cpu_maps_update_done();
  315. }
  316. EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
  317. #endif /* CONFIG_HOTPLUG_CPU */
  318. /* Need to know about CPUs going up/down? */
  319. int register_cpu_notifier(struct notifier_block *nb)
  320. {
  321. int ret;
  322. cpu_maps_update_begin();
  323. ret = raw_notifier_chain_register(&cpu_chain, nb);
  324. cpu_maps_update_done();
  325. return ret;
  326. }
  327. int __register_cpu_notifier(struct notifier_block *nb)
  328. {
  329. return raw_notifier_chain_register(&cpu_chain, nb);
  330. }
  331. static int __cpu_notify(unsigned long val, unsigned int cpu, int nr_to_call,
  332. int *nr_calls)
  333. {
  334. unsigned long mod = cpuhp_tasks_frozen ? CPU_TASKS_FROZEN : 0;
  335. void *hcpu = (void *)(long)cpu;
  336. int ret;
  337. ret = __raw_notifier_call_chain(&cpu_chain, val | mod, hcpu, nr_to_call,
  338. nr_calls);
  339. return notifier_to_errno(ret);
  340. }
  341. static int cpu_notify(unsigned long val, unsigned int cpu)
  342. {
  343. return __cpu_notify(val, cpu, -1, NULL);
  344. }
  345. static void cpu_notify_nofail(unsigned long val, unsigned int cpu)
  346. {
  347. BUG_ON(cpu_notify(val, cpu));
  348. }
  349. /* Notifier wrappers for transitioning to state machine */
  350. static int notify_prepare(unsigned int cpu)
  351. {
  352. int nr_calls = 0;
  353. int ret;
  354. ret = __cpu_notify(CPU_UP_PREPARE, cpu, -1, &nr_calls);
  355. if (ret) {
  356. nr_calls--;
  357. printk(KERN_WARNING "%s: attempt to bring up CPU %u failed\n",
  358. __func__, cpu);
  359. __cpu_notify(CPU_UP_CANCELED, cpu, nr_calls, NULL);
  360. }
  361. return ret;
  362. }
  363. static int notify_online(unsigned int cpu)
  364. {
  365. cpu_notify(CPU_ONLINE, cpu);
  366. return 0;
  367. }
  368. static void __cpuhp_kick_ap_work(struct cpuhp_cpu_state *st);
  369. static int bringup_wait_for_ap(unsigned int cpu)
  370. {
  371. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  372. /* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
  373. wait_for_completion(&st->done);
  374. if (WARN_ON_ONCE((!cpu_online(cpu))))
  375. return -ECANCELED;
  376. /* Unpark the stopper thread and the hotplug thread of the target cpu */
  377. stop_machine_unpark(cpu);
  378. kthread_unpark(st->thread);
  379. /* Should we go further up ? */
  380. if (st->target > CPUHP_AP_ONLINE_IDLE) {
  381. __cpuhp_kick_ap_work(st);
  382. wait_for_completion(&st->done);
  383. }
  384. return st->result;
  385. }
  386. static int bringup_cpu(unsigned int cpu)
  387. {
  388. struct task_struct *idle = idle_thread_get(cpu);
  389. int ret;
  390. /*
  391. * Some architectures have to walk the irq descriptors to
  392. * setup the vector space for the cpu which comes online.
  393. * Prevent irq alloc/free across the bringup.
  394. */
  395. irq_lock_sparse();
  396. /* Arch-specific enabling code. */
  397. ret = __cpu_up(cpu, idle);
  398. irq_unlock_sparse();
  399. if (ret) {
  400. cpu_notify(CPU_UP_CANCELED, cpu);
  401. return ret;
  402. }
  403. return bringup_wait_for_ap(cpu);
  404. }
  405. /*
  406. * Hotplug state machine related functions
  407. */
  408. static void undo_cpu_down(unsigned int cpu, struct cpuhp_cpu_state *st)
  409. {
  410. for (st->state++; st->state < st->target; st->state++) {
  411. struct cpuhp_step *step = cpuhp_get_step(st->state);
  412. if (!step->skip_onerr)
  413. cpuhp_invoke_callback(cpu, st->state, true, NULL);
  414. }
  415. }
  416. static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
  417. enum cpuhp_state target)
  418. {
  419. enum cpuhp_state prev_state = st->state;
  420. int ret = 0;
  421. for (; st->state > target; st->state--) {
  422. ret = cpuhp_invoke_callback(cpu, st->state, false, NULL);
  423. if (ret) {
  424. st->target = prev_state;
  425. undo_cpu_down(cpu, st);
  426. break;
  427. }
  428. }
  429. return ret;
  430. }
  431. static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st)
  432. {
  433. for (st->state--; st->state > st->target; st->state--) {
  434. struct cpuhp_step *step = cpuhp_get_step(st->state);
  435. if (!step->skip_onerr)
  436. cpuhp_invoke_callback(cpu, st->state, false, NULL);
  437. }
  438. }
  439. static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
  440. enum cpuhp_state target)
  441. {
  442. enum cpuhp_state prev_state = st->state;
  443. int ret = 0;
  444. while (st->state < target) {
  445. st->state++;
  446. ret = cpuhp_invoke_callback(cpu, st->state, true, NULL);
  447. if (ret) {
  448. st->target = prev_state;
  449. undo_cpu_up(cpu, st);
  450. break;
  451. }
  452. }
  453. return ret;
  454. }
  455. /*
  456. * The cpu hotplug threads manage the bringup and teardown of the cpus
  457. */
  458. static void cpuhp_create(unsigned int cpu)
  459. {
  460. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  461. init_completion(&st->done);
  462. }
  463. static int cpuhp_should_run(unsigned int cpu)
  464. {
  465. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  466. return st->should_run;
  467. }
  468. /* Execute the teardown callbacks. Used to be CPU_DOWN_PREPARE */
  469. static int cpuhp_ap_offline(unsigned int cpu, struct cpuhp_cpu_state *st)
  470. {
  471. enum cpuhp_state target = max((int)st->target, CPUHP_TEARDOWN_CPU);
  472. return cpuhp_down_callbacks(cpu, st, target);
  473. }
  474. /* Execute the online startup callbacks. Used to be CPU_ONLINE */
  475. static int cpuhp_ap_online(unsigned int cpu, struct cpuhp_cpu_state *st)
  476. {
  477. return cpuhp_up_callbacks(cpu, st, st->target);
  478. }
  479. /*
  480. * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
  481. * callbacks when a state gets [un]installed at runtime.
  482. */
  483. static void cpuhp_thread_fun(unsigned int cpu)
  484. {
  485. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  486. int ret = 0;
  487. /*
  488. * Paired with the mb() in cpuhp_kick_ap_work and
  489. * cpuhp_invoke_ap_callback, so the work set is consistent visible.
  490. */
  491. smp_mb();
  492. if (!st->should_run)
  493. return;
  494. st->should_run = false;
  495. lock_map_acquire(&cpuhp_state_lock_map);
  496. /* Single callback invocation for [un]install ? */
  497. if (st->single) {
  498. if (st->cb_state < CPUHP_AP_ONLINE) {
  499. local_irq_disable();
  500. ret = cpuhp_invoke_callback(cpu, st->cb_state,
  501. st->bringup, st->node);
  502. local_irq_enable();
  503. } else {
  504. ret = cpuhp_invoke_callback(cpu, st->cb_state,
  505. st->bringup, st->node);
  506. }
  507. } else if (st->rollback) {
  508. BUG_ON(st->state < CPUHP_AP_ONLINE_IDLE);
  509. undo_cpu_down(cpu, st);
  510. /*
  511. * This is a momentary workaround to keep the notifier users
  512. * happy. Will go away once we got rid of the notifiers.
  513. */
  514. cpu_notify_nofail(CPU_DOWN_FAILED, cpu);
  515. st->rollback = false;
  516. } else {
  517. /* Cannot happen .... */
  518. BUG_ON(st->state < CPUHP_AP_ONLINE_IDLE);
  519. /* Regular hotplug work */
  520. if (st->state < st->target)
  521. ret = cpuhp_ap_online(cpu, st);
  522. else if (st->state > st->target)
  523. ret = cpuhp_ap_offline(cpu, st);
  524. }
  525. lock_map_release(&cpuhp_state_lock_map);
  526. st->result = ret;
  527. complete(&st->done);
  528. }
  529. /* Invoke a single callback on a remote cpu */
  530. static int
  531. cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
  532. struct hlist_node *node)
  533. {
  534. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  535. if (!cpu_online(cpu))
  536. return 0;
  537. lock_map_acquire(&cpuhp_state_lock_map);
  538. lock_map_release(&cpuhp_state_lock_map);
  539. /*
  540. * If we are up and running, use the hotplug thread. For early calls
  541. * we invoke the thread function directly.
  542. */
  543. if (!st->thread)
  544. return cpuhp_invoke_callback(cpu, state, bringup, node);
  545. st->cb_state = state;
  546. st->single = true;
  547. st->bringup = bringup;
  548. st->node = node;
  549. /*
  550. * Make sure the above stores are visible before should_run becomes
  551. * true. Paired with the mb() above in cpuhp_thread_fun()
  552. */
  553. smp_mb();
  554. st->should_run = true;
  555. wake_up_process(st->thread);
  556. wait_for_completion(&st->done);
  557. return st->result;
  558. }
  559. /* Regular hotplug invocation of the AP hotplug thread */
  560. static void __cpuhp_kick_ap_work(struct cpuhp_cpu_state *st)
  561. {
  562. st->result = 0;
  563. st->single = false;
  564. /*
  565. * Make sure the above stores are visible before should_run becomes
  566. * true. Paired with the mb() above in cpuhp_thread_fun()
  567. */
  568. smp_mb();
  569. st->should_run = true;
  570. wake_up_process(st->thread);
  571. }
  572. static int cpuhp_kick_ap_work(unsigned int cpu)
  573. {
  574. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  575. enum cpuhp_state state = st->state;
  576. trace_cpuhp_enter(cpu, st->target, state, cpuhp_kick_ap_work);
  577. lock_map_acquire(&cpuhp_state_lock_map);
  578. lock_map_release(&cpuhp_state_lock_map);
  579. __cpuhp_kick_ap_work(st);
  580. wait_for_completion(&st->done);
  581. trace_cpuhp_exit(cpu, st->state, state, st->result);
  582. return st->result;
  583. }
  584. static struct smp_hotplug_thread cpuhp_threads = {
  585. .store = &cpuhp_state.thread,
  586. .create = &cpuhp_create,
  587. .thread_should_run = cpuhp_should_run,
  588. .thread_fn = cpuhp_thread_fun,
  589. .thread_comm = "cpuhp/%u",
  590. .selfparking = true,
  591. };
  592. void __init cpuhp_threads_init(void)
  593. {
  594. BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads));
  595. kthread_unpark(this_cpu_read(cpuhp_state.thread));
  596. }
  597. EXPORT_SYMBOL(register_cpu_notifier);
  598. EXPORT_SYMBOL(__register_cpu_notifier);
  599. void unregister_cpu_notifier(struct notifier_block *nb)
  600. {
  601. cpu_maps_update_begin();
  602. raw_notifier_chain_unregister(&cpu_chain, nb);
  603. cpu_maps_update_done();
  604. }
  605. EXPORT_SYMBOL(unregister_cpu_notifier);
  606. void __unregister_cpu_notifier(struct notifier_block *nb)
  607. {
  608. raw_notifier_chain_unregister(&cpu_chain, nb);
  609. }
  610. EXPORT_SYMBOL(__unregister_cpu_notifier);
  611. #ifdef CONFIG_HOTPLUG_CPU
  612. /**
  613. * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
  614. * @cpu: a CPU id
  615. *
  616. * This function walks all processes, finds a valid mm struct for each one and
  617. * then clears a corresponding bit in mm's cpumask. While this all sounds
  618. * trivial, there are various non-obvious corner cases, which this function
  619. * tries to solve in a safe manner.
  620. *
  621. * Also note that the function uses a somewhat relaxed locking scheme, so it may
  622. * be called only for an already offlined CPU.
  623. */
  624. void clear_tasks_mm_cpumask(int cpu)
  625. {
  626. struct task_struct *p;
  627. /*
  628. * This function is called after the cpu is taken down and marked
  629. * offline, so its not like new tasks will ever get this cpu set in
  630. * their mm mask. -- Peter Zijlstra
  631. * Thus, we may use rcu_read_lock() here, instead of grabbing
  632. * full-fledged tasklist_lock.
  633. */
  634. WARN_ON(cpu_online(cpu));
  635. rcu_read_lock();
  636. for_each_process(p) {
  637. struct task_struct *t;
  638. /*
  639. * Main thread might exit, but other threads may still have
  640. * a valid mm. Find one.
  641. */
  642. t = find_lock_task_mm(p);
  643. if (!t)
  644. continue;
  645. cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
  646. task_unlock(t);
  647. }
  648. rcu_read_unlock();
  649. }
  650. static inline void check_for_tasks(int dead_cpu)
  651. {
  652. struct task_struct *g, *p;
  653. read_lock(&tasklist_lock);
  654. for_each_process_thread(g, p) {
  655. if (!p->on_rq)
  656. continue;
  657. /*
  658. * We do the check with unlocked task_rq(p)->lock.
  659. * Order the reading to do not warn about a task,
  660. * which was running on this cpu in the past, and
  661. * it's just been woken on another cpu.
  662. */
  663. rmb();
  664. if (task_cpu(p) != dead_cpu)
  665. continue;
  666. pr_warn("Task %s (pid=%d) is on cpu %d (state=%ld, flags=%x)\n",
  667. p->comm, task_pid_nr(p), dead_cpu, p->state, p->flags);
  668. }
  669. read_unlock(&tasklist_lock);
  670. }
  671. static int notify_down_prepare(unsigned int cpu)
  672. {
  673. int err, nr_calls = 0;
  674. err = __cpu_notify(CPU_DOWN_PREPARE, cpu, -1, &nr_calls);
  675. if (err) {
  676. nr_calls--;
  677. __cpu_notify(CPU_DOWN_FAILED, cpu, nr_calls, NULL);
  678. pr_warn("%s: attempt to take down CPU %u failed\n",
  679. __func__, cpu);
  680. }
  681. return err;
  682. }
  683. /* Take this CPU down. */
  684. static int take_cpu_down(void *_param)
  685. {
  686. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  687. enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
  688. int err, cpu = smp_processor_id();
  689. /* Ensure this CPU doesn't handle any more interrupts. */
  690. err = __cpu_disable();
  691. if (err < 0)
  692. return err;
  693. /*
  694. * We get here while we are in CPUHP_TEARDOWN_CPU state and we must not
  695. * do this step again.
  696. */
  697. WARN_ON(st->state != CPUHP_TEARDOWN_CPU);
  698. st->state--;
  699. /* Invoke the former CPU_DYING callbacks */
  700. for (; st->state > target; st->state--)
  701. cpuhp_invoke_callback(cpu, st->state, false, NULL);
  702. /* Give up timekeeping duties */
  703. tick_handover_do_timer();
  704. /* Park the stopper thread */
  705. stop_machine_park(cpu);
  706. return 0;
  707. }
  708. static int takedown_cpu(unsigned int cpu)
  709. {
  710. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  711. int err;
  712. /* Park the smpboot threads */
  713. kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
  714. smpboot_park_threads(cpu);
  715. /*
  716. * Prevent irq alloc/free while the dying cpu reorganizes the
  717. * interrupt affinities.
  718. */
  719. irq_lock_sparse();
  720. /*
  721. * So now all preempt/rcu users must observe !cpu_active().
  722. */
  723. err = stop_machine(take_cpu_down, NULL, cpumask_of(cpu));
  724. if (err) {
  725. /* CPU refused to die */
  726. irq_unlock_sparse();
  727. /* Unpark the hotplug thread so we can rollback there */
  728. kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread);
  729. return err;
  730. }
  731. BUG_ON(cpu_online(cpu));
  732. /*
  733. * The CPUHP_AP_SCHED_MIGRATE_DYING callback will have removed all
  734. * runnable tasks from the cpu, there's only the idle task left now
  735. * that the migration thread is done doing the stop_machine thing.
  736. *
  737. * Wait for the stop thread to go away.
  738. */
  739. wait_for_completion(&st->done);
  740. BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
  741. /* Interrupts are moved away from the dying cpu, reenable alloc/free */
  742. irq_unlock_sparse();
  743. hotplug_cpu__broadcast_tick_pull(cpu);
  744. /* This actually kills the CPU. */
  745. __cpu_die(cpu);
  746. tick_cleanup_dead_cpu(cpu);
  747. return 0;
  748. }
  749. static int notify_dead(unsigned int cpu)
  750. {
  751. cpu_notify_nofail(CPU_DEAD, cpu);
  752. check_for_tasks(cpu);
  753. return 0;
  754. }
  755. static void cpuhp_complete_idle_dead(void *arg)
  756. {
  757. struct cpuhp_cpu_state *st = arg;
  758. complete(&st->done);
  759. }
  760. void cpuhp_report_idle_dead(void)
  761. {
  762. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  763. BUG_ON(st->state != CPUHP_AP_OFFLINE);
  764. rcu_report_dead(smp_processor_id());
  765. st->state = CPUHP_AP_IDLE_DEAD;
  766. /*
  767. * We cannot call complete after rcu_report_dead() so we delegate it
  768. * to an online cpu.
  769. */
  770. smp_call_function_single(cpumask_first(cpu_online_mask),
  771. cpuhp_complete_idle_dead, st, 0);
  772. }
  773. #else
  774. #define notify_down_prepare NULL
  775. #define takedown_cpu NULL
  776. #define notify_dead NULL
  777. #endif
  778. #ifdef CONFIG_HOTPLUG_CPU
  779. /* Requires cpu_add_remove_lock to be held */
  780. static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
  781. enum cpuhp_state target)
  782. {
  783. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  784. int prev_state, ret = 0;
  785. bool hasdied = false;
  786. if (num_online_cpus() == 1)
  787. return -EBUSY;
  788. if (!cpu_present(cpu))
  789. return -EINVAL;
  790. cpu_hotplug_begin();
  791. cpuhp_tasks_frozen = tasks_frozen;
  792. prev_state = st->state;
  793. st->target = target;
  794. /*
  795. * If the current CPU state is in the range of the AP hotplug thread,
  796. * then we need to kick the thread.
  797. */
  798. if (st->state > CPUHP_TEARDOWN_CPU) {
  799. ret = cpuhp_kick_ap_work(cpu);
  800. /*
  801. * The AP side has done the error rollback already. Just
  802. * return the error code..
  803. */
  804. if (ret)
  805. goto out;
  806. /*
  807. * We might have stopped still in the range of the AP hotplug
  808. * thread. Nothing to do anymore.
  809. */
  810. if (st->state > CPUHP_TEARDOWN_CPU)
  811. goto out;
  812. }
  813. /*
  814. * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
  815. * to do the further cleanups.
  816. */
  817. ret = cpuhp_down_callbacks(cpu, st, target);
  818. if (ret && st->state > CPUHP_TEARDOWN_CPU && st->state < prev_state) {
  819. st->target = prev_state;
  820. st->rollback = true;
  821. cpuhp_kick_ap_work(cpu);
  822. }
  823. hasdied = prev_state != st->state && st->state == CPUHP_OFFLINE;
  824. out:
  825. cpu_hotplug_done();
  826. /* This post dead nonsense must die */
  827. if (!ret && hasdied)
  828. cpu_notify_nofail(CPU_POST_DEAD, cpu);
  829. return ret;
  830. }
  831. static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
  832. {
  833. int err;
  834. cpu_maps_update_begin();
  835. if (cpu_hotplug_disabled) {
  836. err = -EBUSY;
  837. goto out;
  838. }
  839. err = _cpu_down(cpu, 0, target);
  840. out:
  841. cpu_maps_update_done();
  842. return err;
  843. }
  844. int cpu_down(unsigned int cpu)
  845. {
  846. return do_cpu_down(cpu, CPUHP_OFFLINE);
  847. }
  848. EXPORT_SYMBOL(cpu_down);
  849. #endif /*CONFIG_HOTPLUG_CPU*/
  850. /**
  851. * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
  852. * @cpu: cpu that just started
  853. *
  854. * It must be called by the arch code on the new cpu, before the new cpu
  855. * enables interrupts and before the "boot" cpu returns from __cpu_up().
  856. */
  857. void notify_cpu_starting(unsigned int cpu)
  858. {
  859. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  860. enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
  861. rcu_cpu_starting(cpu); /* Enables RCU usage on this CPU. */
  862. while (st->state < target) {
  863. st->state++;
  864. cpuhp_invoke_callback(cpu, st->state, true, NULL);
  865. }
  866. }
  867. /*
  868. * Called from the idle task. Wake up the controlling task which brings the
  869. * stopper and the hotplug thread of the upcoming CPU up and then delegates
  870. * the rest of the online bringup to the hotplug thread.
  871. */
  872. void cpuhp_online_idle(enum cpuhp_state state)
  873. {
  874. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  875. /* Happens for the boot cpu */
  876. if (state != CPUHP_AP_ONLINE_IDLE)
  877. return;
  878. st->state = CPUHP_AP_ONLINE_IDLE;
  879. complete(&st->done);
  880. }
  881. /* Requires cpu_add_remove_lock to be held */
  882. static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
  883. {
  884. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  885. struct task_struct *idle;
  886. int ret = 0;
  887. cpu_hotplug_begin();
  888. if (!cpu_present(cpu)) {
  889. ret = -EINVAL;
  890. goto out;
  891. }
  892. /*
  893. * The caller of do_cpu_up might have raced with another
  894. * caller. Ignore it for now.
  895. */
  896. if (st->state >= target)
  897. goto out;
  898. if (st->state == CPUHP_OFFLINE) {
  899. /* Let it fail before we try to bring the cpu up */
  900. idle = idle_thread_get(cpu);
  901. if (IS_ERR(idle)) {
  902. ret = PTR_ERR(idle);
  903. goto out;
  904. }
  905. }
  906. cpuhp_tasks_frozen = tasks_frozen;
  907. st->target = target;
  908. /*
  909. * If the current CPU state is in the range of the AP hotplug thread,
  910. * then we need to kick the thread once more.
  911. */
  912. if (st->state > CPUHP_BRINGUP_CPU) {
  913. ret = cpuhp_kick_ap_work(cpu);
  914. /*
  915. * The AP side has done the error rollback already. Just
  916. * return the error code..
  917. */
  918. if (ret)
  919. goto out;
  920. }
  921. /*
  922. * Try to reach the target state. We max out on the BP at
  923. * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
  924. * responsible for bringing it up to the target state.
  925. */
  926. target = min((int)target, CPUHP_BRINGUP_CPU);
  927. ret = cpuhp_up_callbacks(cpu, st, target);
  928. out:
  929. cpu_hotplug_done();
  930. return ret;
  931. }
  932. static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
  933. {
  934. int err = 0;
  935. if (!cpu_possible(cpu)) {
  936. pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
  937. cpu);
  938. #if defined(CONFIG_IA64)
  939. pr_err("please check additional_cpus= boot parameter\n");
  940. #endif
  941. return -EINVAL;
  942. }
  943. err = try_online_node(cpu_to_node(cpu));
  944. if (err)
  945. return err;
  946. cpu_maps_update_begin();
  947. if (cpu_hotplug_disabled) {
  948. err = -EBUSY;
  949. goto out;
  950. }
  951. err = _cpu_up(cpu, 0, target);
  952. out:
  953. cpu_maps_update_done();
  954. return err;
  955. }
  956. int cpu_up(unsigned int cpu)
  957. {
  958. return do_cpu_up(cpu, CPUHP_ONLINE);
  959. }
  960. EXPORT_SYMBOL_GPL(cpu_up);
  961. #ifdef CONFIG_PM_SLEEP_SMP
  962. static cpumask_var_t frozen_cpus;
  963. int freeze_secondary_cpus(int primary)
  964. {
  965. int cpu, error = 0;
  966. cpu_maps_update_begin();
  967. if (!cpu_online(primary))
  968. primary = cpumask_first(cpu_online_mask);
  969. /*
  970. * We take down all of the non-boot CPUs in one shot to avoid races
  971. * with the userspace trying to use the CPU hotplug at the same time
  972. */
  973. cpumask_clear(frozen_cpus);
  974. pr_info("Disabling non-boot CPUs ...\n");
  975. for_each_online_cpu(cpu) {
  976. if (cpu == primary)
  977. continue;
  978. trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
  979. error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
  980. trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
  981. if (!error)
  982. cpumask_set_cpu(cpu, frozen_cpus);
  983. else {
  984. pr_err("Error taking CPU%d down: %d\n", cpu, error);
  985. break;
  986. }
  987. }
  988. if (!error)
  989. BUG_ON(num_online_cpus() > 1);
  990. else
  991. pr_err("Non-boot CPUs are not disabled\n");
  992. /*
  993. * Make sure the CPUs won't be enabled by someone else. We need to do
  994. * this even in case of failure as all disable_nonboot_cpus() users are
  995. * supposed to do enable_nonboot_cpus() on the failure path.
  996. */
  997. cpu_hotplug_disabled++;
  998. cpu_maps_update_done();
  999. return error;
  1000. }
  1001. void __weak arch_enable_nonboot_cpus_begin(void)
  1002. {
  1003. }
  1004. void __weak arch_enable_nonboot_cpus_end(void)
  1005. {
  1006. }
  1007. void enable_nonboot_cpus(void)
  1008. {
  1009. int cpu, error;
  1010. /* Allow everyone to use the CPU hotplug again */
  1011. cpu_maps_update_begin();
  1012. __cpu_hotplug_enable();
  1013. if (cpumask_empty(frozen_cpus))
  1014. goto out;
  1015. pr_info("Enabling non-boot CPUs ...\n");
  1016. arch_enable_nonboot_cpus_begin();
  1017. for_each_cpu(cpu, frozen_cpus) {
  1018. trace_suspend_resume(TPS("CPU_ON"), cpu, true);
  1019. error = _cpu_up(cpu, 1, CPUHP_ONLINE);
  1020. trace_suspend_resume(TPS("CPU_ON"), cpu, false);
  1021. if (!error) {
  1022. pr_info("CPU%d is up\n", cpu);
  1023. continue;
  1024. }
  1025. pr_warn("Error taking CPU%d up: %d\n", cpu, error);
  1026. }
  1027. arch_enable_nonboot_cpus_end();
  1028. cpumask_clear(frozen_cpus);
  1029. out:
  1030. cpu_maps_update_done();
  1031. }
  1032. static int __init alloc_frozen_cpus(void)
  1033. {
  1034. if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
  1035. return -ENOMEM;
  1036. return 0;
  1037. }
  1038. core_initcall(alloc_frozen_cpus);
  1039. /*
  1040. * When callbacks for CPU hotplug notifications are being executed, we must
  1041. * ensure that the state of the system with respect to the tasks being frozen
  1042. * or not, as reported by the notification, remains unchanged *throughout the
  1043. * duration* of the execution of the callbacks.
  1044. * Hence we need to prevent the freezer from racing with regular CPU hotplug.
  1045. *
  1046. * This synchronization is implemented by mutually excluding regular CPU
  1047. * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
  1048. * Hibernate notifications.
  1049. */
  1050. static int
  1051. cpu_hotplug_pm_callback(struct notifier_block *nb,
  1052. unsigned long action, void *ptr)
  1053. {
  1054. switch (action) {
  1055. case PM_SUSPEND_PREPARE:
  1056. case PM_HIBERNATION_PREPARE:
  1057. cpu_hotplug_disable();
  1058. break;
  1059. case PM_POST_SUSPEND:
  1060. case PM_POST_HIBERNATION:
  1061. cpu_hotplug_enable();
  1062. break;
  1063. default:
  1064. return NOTIFY_DONE;
  1065. }
  1066. return NOTIFY_OK;
  1067. }
  1068. static int __init cpu_hotplug_pm_sync_init(void)
  1069. {
  1070. /*
  1071. * cpu_hotplug_pm_callback has higher priority than x86
  1072. * bsp_pm_callback which depends on cpu_hotplug_pm_callback
  1073. * to disable cpu hotplug to avoid cpu hotplug race.
  1074. */
  1075. pm_notifier(cpu_hotplug_pm_callback, 0);
  1076. return 0;
  1077. }
  1078. core_initcall(cpu_hotplug_pm_sync_init);
  1079. #endif /* CONFIG_PM_SLEEP_SMP */
  1080. #endif /* CONFIG_SMP */
  1081. /* Boot processor state steps */
  1082. static struct cpuhp_step cpuhp_bp_states[] = {
  1083. [CPUHP_OFFLINE] = {
  1084. .name = "offline",
  1085. .startup.single = NULL,
  1086. .teardown.single = NULL,
  1087. },
  1088. #ifdef CONFIG_SMP
  1089. [CPUHP_CREATE_THREADS]= {
  1090. .name = "threads:prepare",
  1091. .startup.single = smpboot_create_threads,
  1092. .teardown.single = NULL,
  1093. .cant_stop = true,
  1094. },
  1095. [CPUHP_PERF_PREPARE] = {
  1096. .name = "perf:prepare",
  1097. .startup.single = perf_event_init_cpu,
  1098. .teardown.single = perf_event_exit_cpu,
  1099. },
  1100. [CPUHP_WORKQUEUE_PREP] = {
  1101. .name = "workqueue:prepare",
  1102. .startup.single = workqueue_prepare_cpu,
  1103. .teardown.single = NULL,
  1104. },
  1105. [CPUHP_HRTIMERS_PREPARE] = {
  1106. .name = "hrtimers:prepare",
  1107. .startup.single = hrtimers_prepare_cpu,
  1108. .teardown.single = hrtimers_dead_cpu,
  1109. },
  1110. [CPUHP_SMPCFD_PREPARE] = {
  1111. .name = "smpcfd:prepare",
  1112. .startup.single = smpcfd_prepare_cpu,
  1113. .teardown.single = smpcfd_dead_cpu,
  1114. },
  1115. [CPUHP_RELAY_PREPARE] = {
  1116. .name = "relay:prepare",
  1117. .startup.single = relay_prepare_cpu,
  1118. .teardown.single = NULL,
  1119. },
  1120. [CPUHP_SLAB_PREPARE] = {
  1121. .name = "slab:prepare",
  1122. .startup.single = slab_prepare_cpu,
  1123. .teardown.single = slab_dead_cpu,
  1124. },
  1125. [CPUHP_RCUTREE_PREP] = {
  1126. .name = "RCU/tree:prepare",
  1127. .startup.single = rcutree_prepare_cpu,
  1128. .teardown.single = rcutree_dead_cpu,
  1129. },
  1130. /*
  1131. * Preparatory and dead notifiers. Will be replaced once the notifiers
  1132. * are converted to states.
  1133. */
  1134. [CPUHP_NOTIFY_PREPARE] = {
  1135. .name = "notify:prepare",
  1136. .startup.single = notify_prepare,
  1137. .teardown.single = notify_dead,
  1138. .skip_onerr = true,
  1139. .cant_stop = true,
  1140. },
  1141. /*
  1142. * On the tear-down path, timers_dead_cpu() must be invoked
  1143. * before blk_mq_queue_reinit_notify() from notify_dead(),
  1144. * otherwise a RCU stall occurs.
  1145. */
  1146. [CPUHP_TIMERS_PREPARE] = {
  1147. .name = "timers:dead",
  1148. .startup.single = timers_prepare_cpu,
  1149. .teardown.single = timers_dead_cpu,
  1150. },
  1151. /* Kicks the plugged cpu into life */
  1152. [CPUHP_BRINGUP_CPU] = {
  1153. .name = "cpu:bringup",
  1154. .startup.single = bringup_cpu,
  1155. .teardown.single = NULL,
  1156. .cant_stop = true,
  1157. },
  1158. /*
  1159. * Handled on controll processor until the plugged processor manages
  1160. * this itself.
  1161. */
  1162. [CPUHP_TEARDOWN_CPU] = {
  1163. .name = "cpu:teardown",
  1164. .startup.single = NULL,
  1165. .teardown.single = takedown_cpu,
  1166. .cant_stop = true,
  1167. },
  1168. #else
  1169. [CPUHP_BRINGUP_CPU] = { },
  1170. #endif
  1171. };
  1172. /* Application processor state steps */
  1173. static struct cpuhp_step cpuhp_ap_states[] = {
  1174. #ifdef CONFIG_SMP
  1175. /* Final state before CPU kills itself */
  1176. [CPUHP_AP_IDLE_DEAD] = {
  1177. .name = "idle:dead",
  1178. },
  1179. /*
  1180. * Last state before CPU enters the idle loop to die. Transient state
  1181. * for synchronization.
  1182. */
  1183. [CPUHP_AP_OFFLINE] = {
  1184. .name = "ap:offline",
  1185. .cant_stop = true,
  1186. },
  1187. /* First state is scheduler control. Interrupts are disabled */
  1188. [CPUHP_AP_SCHED_STARTING] = {
  1189. .name = "sched:starting",
  1190. .startup.single = sched_cpu_starting,
  1191. .teardown.single = sched_cpu_dying,
  1192. },
  1193. [CPUHP_AP_RCUTREE_DYING] = {
  1194. .name = "RCU/tree:dying",
  1195. .startup.single = NULL,
  1196. .teardown.single = rcutree_dying_cpu,
  1197. },
  1198. [CPUHP_AP_SMPCFD_DYING] = {
  1199. .name = "smpcfd:dying",
  1200. .startup.single = NULL,
  1201. .teardown.single = smpcfd_dying_cpu,
  1202. },
  1203. /* Entry state on starting. Interrupts enabled from here on. Transient
  1204. * state for synchronsization */
  1205. [CPUHP_AP_ONLINE] = {
  1206. .name = "ap:online",
  1207. },
  1208. /* Handle smpboot threads park/unpark */
  1209. [CPUHP_AP_SMPBOOT_THREADS] = {
  1210. .name = "smpboot/threads:online",
  1211. .startup.single = smpboot_unpark_threads,
  1212. .teardown.single = NULL,
  1213. },
  1214. [CPUHP_AP_PERF_ONLINE] = {
  1215. .name = "perf:online",
  1216. .startup.single = perf_event_init_cpu,
  1217. .teardown.single = perf_event_exit_cpu,
  1218. },
  1219. [CPUHP_AP_WORKQUEUE_ONLINE] = {
  1220. .name = "workqueue:online",
  1221. .startup.single = workqueue_online_cpu,
  1222. .teardown.single = workqueue_offline_cpu,
  1223. },
  1224. [CPUHP_AP_RCUTREE_ONLINE] = {
  1225. .name = "RCU/tree:online",
  1226. .startup.single = rcutree_online_cpu,
  1227. .teardown.single = rcutree_offline_cpu,
  1228. },
  1229. /*
  1230. * Online/down_prepare notifiers. Will be removed once the notifiers
  1231. * are converted to states.
  1232. */
  1233. [CPUHP_AP_NOTIFY_ONLINE] = {
  1234. .name = "notify:online",
  1235. .startup.single = notify_online,
  1236. .teardown.single = notify_down_prepare,
  1237. .skip_onerr = true,
  1238. },
  1239. #endif
  1240. /*
  1241. * The dynamically registered state space is here
  1242. */
  1243. #ifdef CONFIG_SMP
  1244. /* Last state is scheduler control setting the cpu active */
  1245. [CPUHP_AP_ACTIVE] = {
  1246. .name = "sched:active",
  1247. .startup.single = sched_cpu_activate,
  1248. .teardown.single = sched_cpu_deactivate,
  1249. },
  1250. #endif
  1251. /* CPU is fully up and running. */
  1252. [CPUHP_ONLINE] = {
  1253. .name = "online",
  1254. .startup.single = NULL,
  1255. .teardown.single = NULL,
  1256. },
  1257. };
  1258. /* Sanity check for callbacks */
  1259. static int cpuhp_cb_check(enum cpuhp_state state)
  1260. {
  1261. if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
  1262. return -EINVAL;
  1263. return 0;
  1264. }
  1265. static void cpuhp_store_callbacks(enum cpuhp_state state,
  1266. const char *name,
  1267. int (*startup)(unsigned int cpu),
  1268. int (*teardown)(unsigned int cpu),
  1269. bool multi_instance)
  1270. {
  1271. /* (Un)Install the callbacks for further cpu hotplug operations */
  1272. struct cpuhp_step *sp;
  1273. sp = cpuhp_get_step(state);
  1274. sp->startup.single = startup;
  1275. sp->teardown.single = teardown;
  1276. sp->name = name;
  1277. sp->multi_instance = multi_instance;
  1278. INIT_HLIST_HEAD(&sp->list);
  1279. }
  1280. static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
  1281. {
  1282. return cpuhp_get_step(state)->teardown.single;
  1283. }
  1284. /*
  1285. * Call the startup/teardown function for a step either on the AP or
  1286. * on the current CPU.
  1287. */
  1288. static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
  1289. struct hlist_node *node)
  1290. {
  1291. struct cpuhp_step *sp = cpuhp_get_step(state);
  1292. int ret;
  1293. if ((bringup && !sp->startup.single) ||
  1294. (!bringup && !sp->teardown.single))
  1295. return 0;
  1296. /*
  1297. * The non AP bound callbacks can fail on bringup. On teardown
  1298. * e.g. module removal we crash for now.
  1299. */
  1300. #ifdef CONFIG_SMP
  1301. if (cpuhp_is_ap_state(state))
  1302. ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
  1303. else
  1304. ret = cpuhp_invoke_callback(cpu, state, bringup, node);
  1305. #else
  1306. ret = cpuhp_invoke_callback(cpu, state, bringup, node);
  1307. #endif
  1308. BUG_ON(ret && !bringup);
  1309. return ret;
  1310. }
  1311. /*
  1312. * Called from __cpuhp_setup_state on a recoverable failure.
  1313. *
  1314. * Note: The teardown callbacks for rollback are not allowed to fail!
  1315. */
  1316. static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
  1317. struct hlist_node *node)
  1318. {
  1319. int cpu;
  1320. /* Roll back the already executed steps on the other cpus */
  1321. for_each_present_cpu(cpu) {
  1322. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1323. int cpustate = st->state;
  1324. if (cpu >= failedcpu)
  1325. break;
  1326. /* Did we invoke the startup call on that cpu ? */
  1327. if (cpustate >= state)
  1328. cpuhp_issue_call(cpu, state, false, node);
  1329. }
  1330. }
  1331. /*
  1332. * Returns a free for dynamic slot assignment of the Online state. The states
  1333. * are protected by the cpuhp_slot_states mutex and an empty slot is identified
  1334. * by having no name assigned.
  1335. */
  1336. static int cpuhp_reserve_state(enum cpuhp_state state)
  1337. {
  1338. enum cpuhp_state i;
  1339. for (i = CPUHP_AP_ONLINE_DYN; i <= CPUHP_AP_ONLINE_DYN_END; i++) {
  1340. if (cpuhp_ap_states[i].name)
  1341. continue;
  1342. cpuhp_ap_states[i].name = "Reserved";
  1343. return i;
  1344. }
  1345. WARN(1, "No more dynamic states available for CPU hotplug\n");
  1346. return -ENOSPC;
  1347. }
  1348. int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
  1349. bool invoke)
  1350. {
  1351. struct cpuhp_step *sp;
  1352. int cpu;
  1353. int ret;
  1354. sp = cpuhp_get_step(state);
  1355. if (sp->multi_instance == false)
  1356. return -EINVAL;
  1357. get_online_cpus();
  1358. mutex_lock(&cpuhp_state_mutex);
  1359. if (!invoke || !sp->startup.multi)
  1360. goto add_node;
  1361. /*
  1362. * Try to call the startup callback for each present cpu
  1363. * depending on the hotplug state of the cpu.
  1364. */
  1365. for_each_present_cpu(cpu) {
  1366. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1367. int cpustate = st->state;
  1368. if (cpustate < state)
  1369. continue;
  1370. ret = cpuhp_issue_call(cpu, state, true, node);
  1371. if (ret) {
  1372. if (sp->teardown.multi)
  1373. cpuhp_rollback_install(cpu, state, node);
  1374. goto err;
  1375. }
  1376. }
  1377. add_node:
  1378. ret = 0;
  1379. hlist_add_head(node, &sp->list);
  1380. err:
  1381. mutex_unlock(&cpuhp_state_mutex);
  1382. put_online_cpus();
  1383. return ret;
  1384. }
  1385. EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
  1386. /**
  1387. * __cpuhp_setup_state - Setup the callbacks for an hotplug machine state
  1388. * @state: The state to setup
  1389. * @invoke: If true, the startup function is invoked for cpus where
  1390. * cpu state >= @state
  1391. * @startup: startup callback function
  1392. * @teardown: teardown callback function
  1393. *
  1394. * Returns 0 if successful, otherwise a proper error code
  1395. */
  1396. int __cpuhp_setup_state(enum cpuhp_state state,
  1397. const char *name, bool invoke,
  1398. int (*startup)(unsigned int cpu),
  1399. int (*teardown)(unsigned int cpu),
  1400. bool multi_instance)
  1401. {
  1402. int cpu, ret = 0;
  1403. int dyn_state = 0;
  1404. if (cpuhp_cb_check(state) || !name)
  1405. return -EINVAL;
  1406. get_online_cpus();
  1407. mutex_lock(&cpuhp_state_mutex);
  1408. /* currently assignments for the ONLINE state are possible */
  1409. if (state == CPUHP_AP_ONLINE_DYN) {
  1410. dyn_state = 1;
  1411. ret = cpuhp_reserve_state(state);
  1412. if (ret < 0)
  1413. goto out;
  1414. state = ret;
  1415. }
  1416. cpuhp_store_callbacks(state, name, startup, teardown, multi_instance);
  1417. if (!invoke || !startup)
  1418. goto out;
  1419. /*
  1420. * Try to call the startup callback for each present cpu
  1421. * depending on the hotplug state of the cpu.
  1422. */
  1423. for_each_present_cpu(cpu) {
  1424. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1425. int cpustate = st->state;
  1426. if (cpustate < state)
  1427. continue;
  1428. ret = cpuhp_issue_call(cpu, state, true, NULL);
  1429. if (ret) {
  1430. if (teardown)
  1431. cpuhp_rollback_install(cpu, state, NULL);
  1432. cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
  1433. goto out;
  1434. }
  1435. }
  1436. out:
  1437. mutex_unlock(&cpuhp_state_mutex);
  1438. put_online_cpus();
  1439. if (!ret && dyn_state)
  1440. return state;
  1441. return ret;
  1442. }
  1443. EXPORT_SYMBOL(__cpuhp_setup_state);
  1444. int __cpuhp_state_remove_instance(enum cpuhp_state state,
  1445. struct hlist_node *node, bool invoke)
  1446. {
  1447. struct cpuhp_step *sp = cpuhp_get_step(state);
  1448. int cpu;
  1449. BUG_ON(cpuhp_cb_check(state));
  1450. if (!sp->multi_instance)
  1451. return -EINVAL;
  1452. get_online_cpus();
  1453. mutex_lock(&cpuhp_state_mutex);
  1454. if (!invoke || !cpuhp_get_teardown_cb(state))
  1455. goto remove;
  1456. /*
  1457. * Call the teardown callback for each present cpu depending
  1458. * on the hotplug state of the cpu. This function is not
  1459. * allowed to fail currently!
  1460. */
  1461. for_each_present_cpu(cpu) {
  1462. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1463. int cpustate = st->state;
  1464. if (cpustate >= state)
  1465. cpuhp_issue_call(cpu, state, false, node);
  1466. }
  1467. remove:
  1468. hlist_del(node);
  1469. mutex_unlock(&cpuhp_state_mutex);
  1470. put_online_cpus();
  1471. return 0;
  1472. }
  1473. EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
  1474. /**
  1475. * __cpuhp_remove_state - Remove the callbacks for an hotplug machine state
  1476. * @state: The state to remove
  1477. * @invoke: If true, the teardown function is invoked for cpus where
  1478. * cpu state >= @state
  1479. *
  1480. * The teardown callback is currently not allowed to fail. Think
  1481. * about module removal!
  1482. */
  1483. void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
  1484. {
  1485. struct cpuhp_step *sp = cpuhp_get_step(state);
  1486. int cpu;
  1487. BUG_ON(cpuhp_cb_check(state));
  1488. get_online_cpus();
  1489. mutex_lock(&cpuhp_state_mutex);
  1490. if (sp->multi_instance) {
  1491. WARN(!hlist_empty(&sp->list),
  1492. "Error: Removing state %d which has instances left.\n",
  1493. state);
  1494. goto remove;
  1495. }
  1496. if (!invoke || !cpuhp_get_teardown_cb(state))
  1497. goto remove;
  1498. /*
  1499. * Call the teardown callback for each present cpu depending
  1500. * on the hotplug state of the cpu. This function is not
  1501. * allowed to fail currently!
  1502. */
  1503. for_each_present_cpu(cpu) {
  1504. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1505. int cpustate = st->state;
  1506. if (cpustate >= state)
  1507. cpuhp_issue_call(cpu, state, false, NULL);
  1508. }
  1509. remove:
  1510. cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
  1511. mutex_unlock(&cpuhp_state_mutex);
  1512. put_online_cpus();
  1513. }
  1514. EXPORT_SYMBOL(__cpuhp_remove_state);
  1515. #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
  1516. static ssize_t show_cpuhp_state(struct device *dev,
  1517. struct device_attribute *attr, char *buf)
  1518. {
  1519. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1520. return sprintf(buf, "%d\n", st->state);
  1521. }
  1522. static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
  1523. static ssize_t write_cpuhp_target(struct device *dev,
  1524. struct device_attribute *attr,
  1525. const char *buf, size_t count)
  1526. {
  1527. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1528. struct cpuhp_step *sp;
  1529. int target, ret;
  1530. ret = kstrtoint(buf, 10, &target);
  1531. if (ret)
  1532. return ret;
  1533. #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
  1534. if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
  1535. return -EINVAL;
  1536. #else
  1537. if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
  1538. return -EINVAL;
  1539. #endif
  1540. ret = lock_device_hotplug_sysfs();
  1541. if (ret)
  1542. return ret;
  1543. mutex_lock(&cpuhp_state_mutex);
  1544. sp = cpuhp_get_step(target);
  1545. ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
  1546. mutex_unlock(&cpuhp_state_mutex);
  1547. if (ret)
  1548. goto out;
  1549. if (st->state < target)
  1550. ret = do_cpu_up(dev->id, target);
  1551. else
  1552. ret = do_cpu_down(dev->id, target);
  1553. out:
  1554. unlock_device_hotplug();
  1555. return ret ? ret : count;
  1556. }
  1557. static ssize_t show_cpuhp_target(struct device *dev,
  1558. struct device_attribute *attr, char *buf)
  1559. {
  1560. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1561. return sprintf(buf, "%d\n", st->target);
  1562. }
  1563. static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
  1564. static struct attribute *cpuhp_cpu_attrs[] = {
  1565. &dev_attr_state.attr,
  1566. &dev_attr_target.attr,
  1567. NULL
  1568. };
  1569. static struct attribute_group cpuhp_cpu_attr_group = {
  1570. .attrs = cpuhp_cpu_attrs,
  1571. .name = "hotplug",
  1572. NULL
  1573. };
  1574. static ssize_t show_cpuhp_states(struct device *dev,
  1575. struct device_attribute *attr, char *buf)
  1576. {
  1577. ssize_t cur, res = 0;
  1578. int i;
  1579. mutex_lock(&cpuhp_state_mutex);
  1580. for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
  1581. struct cpuhp_step *sp = cpuhp_get_step(i);
  1582. if (sp->name) {
  1583. cur = sprintf(buf, "%3d: %s\n", i, sp->name);
  1584. buf += cur;
  1585. res += cur;
  1586. }
  1587. }
  1588. mutex_unlock(&cpuhp_state_mutex);
  1589. return res;
  1590. }
  1591. static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
  1592. static struct attribute *cpuhp_cpu_root_attrs[] = {
  1593. &dev_attr_states.attr,
  1594. NULL
  1595. };
  1596. static struct attribute_group cpuhp_cpu_root_attr_group = {
  1597. .attrs = cpuhp_cpu_root_attrs,
  1598. .name = "hotplug",
  1599. NULL
  1600. };
  1601. static int __init cpuhp_sysfs_init(void)
  1602. {
  1603. int cpu, ret;
  1604. ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
  1605. &cpuhp_cpu_root_attr_group);
  1606. if (ret)
  1607. return ret;
  1608. for_each_possible_cpu(cpu) {
  1609. struct device *dev = get_cpu_device(cpu);
  1610. if (!dev)
  1611. continue;
  1612. ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
  1613. if (ret)
  1614. return ret;
  1615. }
  1616. return 0;
  1617. }
  1618. device_initcall(cpuhp_sysfs_init);
  1619. #endif
  1620. /*
  1621. * cpu_bit_bitmap[] is a special, "compressed" data structure that
  1622. * represents all NR_CPUS bits binary values of 1<<nr.
  1623. *
  1624. * It is used by cpumask_of() to get a constant address to a CPU
  1625. * mask value that has a single bit set only.
  1626. */
  1627. /* cpu_bit_bitmap[0] is empty - so we can back into it */
  1628. #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
  1629. #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
  1630. #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
  1631. #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
  1632. const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
  1633. MASK_DECLARE_8(0), MASK_DECLARE_8(8),
  1634. MASK_DECLARE_8(16), MASK_DECLARE_8(24),
  1635. #if BITS_PER_LONG > 32
  1636. MASK_DECLARE_8(32), MASK_DECLARE_8(40),
  1637. MASK_DECLARE_8(48), MASK_DECLARE_8(56),
  1638. #endif
  1639. };
  1640. EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
  1641. const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
  1642. EXPORT_SYMBOL(cpu_all_bits);
  1643. #ifdef CONFIG_INIT_ALL_POSSIBLE
  1644. struct cpumask __cpu_possible_mask __read_mostly
  1645. = {CPU_BITS_ALL};
  1646. #else
  1647. struct cpumask __cpu_possible_mask __read_mostly;
  1648. #endif
  1649. EXPORT_SYMBOL(__cpu_possible_mask);
  1650. struct cpumask __cpu_online_mask __read_mostly;
  1651. EXPORT_SYMBOL(__cpu_online_mask);
  1652. struct cpumask __cpu_present_mask __read_mostly;
  1653. EXPORT_SYMBOL(__cpu_present_mask);
  1654. struct cpumask __cpu_active_mask __read_mostly;
  1655. EXPORT_SYMBOL(__cpu_active_mask);
  1656. void init_cpu_present(const struct cpumask *src)
  1657. {
  1658. cpumask_copy(&__cpu_present_mask, src);
  1659. }
  1660. void init_cpu_possible(const struct cpumask *src)
  1661. {
  1662. cpumask_copy(&__cpu_possible_mask, src);
  1663. }
  1664. void init_cpu_online(const struct cpumask *src)
  1665. {
  1666. cpumask_copy(&__cpu_online_mask, src);
  1667. }
  1668. /*
  1669. * Activate the first processor.
  1670. */
  1671. void __init boot_cpu_init(void)
  1672. {
  1673. int cpu = smp_processor_id();
  1674. /* Mark the boot cpu "present", "online" etc for SMP and UP case */
  1675. set_cpu_online(cpu, true);
  1676. set_cpu_active(cpu, true);
  1677. set_cpu_present(cpu, true);
  1678. set_cpu_possible(cpu, true);
  1679. }
  1680. /*
  1681. * Must be called _AFTER_ setting up the per_cpu areas
  1682. */
  1683. void __init boot_cpu_state_init(void)
  1684. {
  1685. per_cpu_ptr(&cpuhp_state, smp_processor_id())->state = CPUHP_ONLINE;
  1686. }