cpu.c 41 KB

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