cpu.c 47 KB

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