cpu.c 48 KB

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