cpu.c 47 KB

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