cpu.c 43 KB

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