cpu.c 48 KB

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