cpu.c 43 KB

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