cpu.c 43 KB

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