cpu.c 44 KB

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