cpu.c 47 KB

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