cpu.c 45 KB

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