cpu.c 41 KB

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