cpu.c 43 KB

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