cpu.c 48 KB

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