cpu.c 44 KB

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